err(1, fname);
err(1, "%s", fname);
static DB	*ng_load __P((const char *));static void	 ng_write __P((DB *, DB *, int));static void	 ng_rwrite __P((DB *, DB *, int));static void	 usage __P((void));static void	 cleanup __P((void));#ifdef DEBUG_NGstatic int 	 debug = 0;static void	 ng_dump __P((DB *));static void	 ng_rdump __P((DB *));#endif /* DEBUG_NG */static const char ng_empty[] = "";#define NG_EMPTY(a)	((a) ? (a) : ng_empty)static char    *dbname = _PATH_NETGROUP_DB;intmain(argc, argv)	int		  argc;	char		**argv;{	DB		 *db, *ndb, *hdb, *udb;	int               ch;	char		  buf[MAXPATHLEN];	char		 *fname = _PATH_NETGROUP;	while ((ch = getopt(argc, argv, "do:")) != -1)		switch (ch) {#ifdef DEBUG_NG		case 'd':			debug++;			break;#endif		case 'o':			dbname = optarg;			break;		case '?':		default:			usage();		}	argc -= optind;	argv += optind;	if (argc == 1)		fname = *argv;	else if (argc > 1)		usage();	if (atexit(cleanup))		err(1, "Cannot install exit handler");	/* Read and parse the netgroup file */	ndb = ng_load(fname);#ifdef DEBUG_NG	if (debug) {		(void) fprintf(stderr, "#### Database\n");		ng_dump(ndb);	}#endif	/* Reverse the database by host */	hdb = ng_reverse(ndb, offsetof(struct netgroup, ng_host));#ifdef DEBUG_NG	if (debug) {		(void) fprintf(stderr, "#### Reverse by host\n");		ng_rdump(hdb);	}#endif	/* Reverse the database by user */	udb = ng_reverse(ndb, offsetof(struct netgroup, ng_user));#ifdef DEBUG_NG	if (debug) {		(void) fprintf(stderr, "#### Reverse by user\n");		ng_rdump(udb);	}#endif	(void) snprintf(buf, sizeof(buf), "%s.tmp", dbname);	db = dbopen(buf, O_RDWR | O_CREAT | O_EXCL,		    (S_IRUSR | S_IWUSR | S_IRGRP | S_IROTH), DB_HASH, NULL);	if (!db)		err(1, buf);	ng_write(db, ndb, _NG_KEYBYNAME);	ng_rwrite(db, udb, _NG_KEYBYUSER);	ng_rwrite(db, hdb, _NG_KEYBYHOST);	if ((db->close)(db))		err(1, "Error closing database");	if (rename(buf, dbname) == -1)		err(1, "Cannot rename `%s' to `%s'", buf, dbname);	return 0;}/* * cleanup(): Remove temporary files upon exit */static voidcleanup(){	char buf[MAXPATHLEN];	(void) snprintf(buf, sizeof(buf), "%s.tmp", dbname);	(void) unlink(buf);}/* * ng_load(): Load the netgroup database from a file */static DB *ng_load(fname)	const char     *fname;{	FILE           *fp;	DB             *db;	char           *buf;	size_t          size;	struct nentry  *tail, *head, *e;	char           *p, *name;	struct netgroup *ng;	DBT             data, key;	/* Open the netgroup file */	if ((fp = fopen(fname, "r")) == NULL)		err(1, fname);	db = dbopen(NULL, O_RDWR | O_CREAT | O_EXCL, 0, DB_HASH, NULL);	if (db == NULL)		err(1, "dbopen");	while ((buf = getline(fp, &size)) != NULL) {		tail = head = NULL;		p = buf;		while (p != NULL) {			switch (_ng_parse(&p, &name, &ng)) {			case _NG_NONE:				/* done with this one */				p = NULL;				free(buf);				if (head == NULL)					break;				key.data = (u_char *) head->n_name;				key.size = strlen(head->n_name) + 1;				data.data = (u_char *) & head;				data.size = sizeof(head);				switch ((db->put)(db, &key, &data, 						  R_NOOVERWRITE)) {				case 0:					break;				case 1:					warnx("Duplicate entry netgroup `%s'",					      head->n_name);					break;				case -1:					err(1, "put");					break;				default:					abort();					break;				}				break;			case _NG_NAME:				e = NEW(struct nentry);				e->n_type = _NG_NAME;				e->n_name = name;				e->n_next = NULL;				e->n_size = size;				if (tail == NULL)					head = tail = e;				else {					tail->n_next = e;					tail = e;				}				break;			case _NG_GROUP:				if (tail == NULL) {					char fmt[BUFSIZ];					_ng_print(fmt, sizeof(fmt), ng);					errx(1, "no netgroup key for %s", fmt);				}				else {					e = NEW(struct nentry);					e->n_type = _NG_GROUP;					e->n_group = ng;					e->n_next = NULL;					e->n_size = size;					tail->n_next = e;					tail = e;				}				break;			default:				abort();				break;			}		}	}	(void) fclose(fp);	return db;}