rheader(&dbtype, &pflag, &dbinfo);
rheader(&dbtype, &checkprint, &dbinfo);
main(argc, argv)	int argc;	char *argv[];{	extern char *optarg;	extern int optind;	DB *dbp;	DBT key, data;	DBTYPE argtype, dbtype;	DB_ENV *dbenv;	DB_INFO dbinfo;	db_recno_t recno;	int ch, no_header, pflag;	char **clist, **clp, *home;	/* Allocate enough room for configuration arguments. */	if ((clp = clist = (char **)calloc(argc + 1, sizeof(char *))) == NULL)		err(1, NULL);	home = NULL;	no_header = 0;	argtype = dbtype = DB_UNKNOWN;	while ((ch = getopt(argc, argv, "c:f:h:Tt:")) != EOF)		switch (ch) {		case 'c':			*clp++ = optarg;			break;		case 'f':			if (freopen(optarg, "r", stdin) == NULL)				err(1, "%s", optarg);			break;		case 'h':			home = optarg;			break;		case 'T':			no_header = pflag = 1;			break;		case 't':			if (strcmp(optarg, "btree") == 0) {				argtype = DB_BTREE;				break;			}			if (strcmp(optarg, "hash") == 0) {				argtype = DB_HASH;				break;			}			if (strcmp(optarg, "recno") == 0) {				argtype = DB_RECNO;				break;			}			usage();			/* NOTREACHED */		case '?':		default:			usage();		}	argc -= optind;	argv += optind;	if (argc != 1)		usage();	/* Initialize the environment. */	dbenv = db_init(home);	memset(&dbinfo, 0, sizeof(DB_INFO));	/*	 * Read the header.  If there isn't any header, we're expecting flat	 * text, set the pflag appropriately.	 */	if (no_header)		dbtype = argtype;	else {		rheader(&dbtype, &pflag, &dbinfo);		if (argtype != DB_UNKNOWN) {			/* Conversion to/from recno is prohibited. */			if ((dbtype == DB_RECNO && argtype != DB_RECNO) ||			    (argtype == DB_RECNO && dbtype != DB_RECNO))				errx(1,			    "databases of type recno may not be converted");			dbtype = argtype;		}	}	if (dbtype == DB_UNKNOWN)		errx(1, "no database type specified");	/* Apply command-line configuration changes. */	configure(&dbinfo, clist);	/* Open the DB file. */	if ((errno = db_open(argv[0], dbtype, DB_CREATE | DB_TRUNCATE,	    S_IRUSR | S_IWUSR | S_IRGRP | S_IWGRP | S_IROTH | S_IWOTH,	    dbenv, &dbinfo, &dbp)) != 0)		err(1, "%s", argv[0]);	/* Initialize the key/data pair. */	memset(&key, 0, sizeof(DBT));	if ((key.data = (void *)malloc(key.ulen = 1024)) == NULL) {		errno = ENOMEM;		err(1, NULL);	}	memset(&data, 0, sizeof(DBT));	if ((data.data = (void *)malloc(data.ulen = 1024)) == NULL) {		errno = ENOMEM;		err(1, NULL);	}	/* Get each key/data pair and add them to the database. */	if (dbtype == DB_RECNO) {		key.data = &recno;		key.size = sizeof(recno);		for (recno = 1;; ++recno) {			if (pflag) {				if (dbt_rprint(&data))					break;			} else				if (dbt_rdump(&data))					break;			if ((errno = dbp->put(dbp, NULL, &key, &data, 0)) != 0)				err(1, "%s", argv[0]);		}	} else		for (;;) {			if (pflag) {				if (dbt_rprint(&key))					break;				if (dbt_rprint(&data))					goto fmt;			} else {				if (dbt_rdump(&key))					break;				if (dbt_rdump(&data))fmt:					err(1, "odd number of key/data pairs");			}			if ((errno = dbp->put(dbp, NULL, &key, &data, 0)) != 0)				err(1, "%s", argv[0]);		}	if ((errno = dbp->close(dbp, 0)) != 0)		err(1, "%s", argv[0]);	return (0);}