134046Sbostic /*
234914Sbostic  * Copyright (c) 1983 Eric P. Allman
362516Sbostic  * Copyright (c) 1988, 1993
462516Sbostic  *	The Regents of the University of California.  All rights reserved.
534046Sbostic  *
642823Sbostic  * %sccs.include.redist.c%
734046Sbostic  */
830811Sbostic 
934046Sbostic #ifndef lint
1062516Sbostic static char copyright[] =
1162516Sbostic "@(#) Copyright (c) 1988, 1993\n\
1262516Sbostic 	The Regents of the University of California.  All rights reserved.\n";
1334046Sbostic #endif /* not lint */
1430811Sbostic 
1534046Sbostic #ifndef lint
16*66315Seric static char sccsid[] = "@(#)praliases.c	8.3 (Berkeley) 03/06/94";
1734046Sbostic #endif /* not lint */
1830811Sbostic 
1960161Sbostic #include <ndbm.h>
2034046Sbostic #include <sendmail.h>
2160174Seric #ifdef NEWDB
2260174Seric #include <db.h>
2360174Seric #endif
2434046Sbostic 
2560161Sbostic int
main(argc,argv)2630811Sbostic main(argc, argv)
2760161Sbostic 	int argc;
2830811Sbostic 	char **argv;
2930811Sbostic {
3034046Sbostic 	extern char *optarg;
3134046Sbostic 	extern int optind;
3260161Sbostic 	DBM *dbp;
3360161Sbostic 	datum content, key;
3460161Sbostic 	char *filename;
3534046Sbostic 	int ch;
3660174Seric #ifdef NEWDB
3760174Seric 	const DB *db;
3860174Seric 	DBT newdbkey, newdbcontent;
3960174Seric 	char buf[MAXNAME];
4060174Seric #endif
4130811Sbostic 
4260161Sbostic 	filename = "/etc/aliases";
4334046Sbostic 	while ((ch = getopt(argc, argv, "f:")) != EOF)
4434046Sbostic 		switch((char)ch) {
4534046Sbostic 		case 'f':
4634046Sbostic 			filename = optarg;
4734046Sbostic 			break;
4834046Sbostic 		case '?':
4934046Sbostic 		default:
5060161Sbostic 			(void)fprintf(stderr, "usage: praliases [-f file]\n");
5134046Sbostic 			exit(EX_USAGE);
5234046Sbostic 		}
5334046Sbostic 	argc -= optind;
5434046Sbostic 	argv += optind;
5530811Sbostic 
5660174Seric #ifdef NEWDB
5760174Seric 	(void) strcpy(buf, filename);
5860174Seric 	(void) strcat(buf, ".db");
5960174Seric 	if (db = dbopen(buf, O_RDONLY, 0444 , DB_HASH, NULL)) {
6060174Seric 		if (!argc) {
6160174Seric 			while(!db->seq(db, &newdbkey, &newdbcontent, R_NEXT))
62*66315Seric 				printf("%.*s:%.*s\n",
63*66315Seric 					newdbkey.size, newdbkey.data,
64*66315Seric 					newdbcontent.size, newdbcontent.data);
6560174Seric 		}
6660174Seric 		else for (; *argv; ++argv) {
6760174Seric 			newdbkey.data = *argv;
6860174Seric 			newdbkey.size = strlen(*argv) + 1;
69*66315Seric 			if (!db->get(db, &newdbkey, &newdbcontent, 0))
70*66315Seric 				printf("%s:%.*s\n", newdbkey.data,
71*66315Seric 					newdbcontent.size, newdbcontent.data);
7260174Seric 			else
7360174Seric 				printf("%s: No such key\n",
7460174Seric 					newdbkey.data);
7560174Seric 		}
7660161Sbostic 	}
7760174Seric 	else {
7860174Seric #endif
7960174Seric 		if ((dbp = dbm_open(filename, O_RDONLY, 0)) == NULL) {
8060174Seric 			(void)fprintf(stderr,
8160174Seric 			    "praliases: %s: %s\n", filename, strerror(errno));
8260174Seric 			exit(EX_OSFILE);
8360174Seric 		}
8460174Seric 		if (!argc)
8564565Seric 			for (key = dbm_firstkey(dbp);
8660174Seric 			    key.dptr != NULL; key = dbm_nextkey(dbp)) {
8760174Seric 				content = dbm_fetch(dbp, key);
88*66315Seric 				(void)printf("%.*s:%.*s\n",
89*66315Seric 					key.dsize, key.dptr,
90*66315Seric 					content.dsize, content.dptr);
9160174Seric 			}
9260174Seric 		else for (; *argv; ++argv) {
9360174Seric 			key.dptr = *argv;
9460174Seric 			key.dsize = strlen(*argv) + 1;
9560161Sbostic 			content = dbm_fetch(dbp, key);
9660174Seric 			if (!content.dptr)
9760174Seric 				(void)printf("%s: No such key\n", key.dptr);
9860174Seric 			else
99*66315Seric 				(void)printf("%s:%.*s\n", key.dptr,
100*66315Seric 					content.dsize, content.dptr);
10130811Sbostic 		}
10260174Seric #ifdef NEWDB
10330811Sbostic 	}
10460174Seric #endif
10530811Sbostic 	exit(EX_OK);
10630811Sbostic }
107