134046Sbostic /*
234914Sbostic  * Copyright (c) 1983 Eric P. Allman
334046Sbostic  * Copyright (c) 1988 Regents of the University of California.
434046Sbostic  * All rights reserved.
534046Sbostic  *
642823Sbostic  * %sccs.include.redist.c%
734046Sbostic  */
830811Sbostic 
934046Sbostic #ifndef lint
1034046Sbostic char copyright[] =
1134046Sbostic "@(#) Copyright (c) 1988 Regents of the University of California.\n\
1234046Sbostic  All rights reserved.\n";
1334046Sbostic #endif /* not lint */
1430811Sbostic 
1534046Sbostic #ifndef lint
16*60174Seric static char sccsid[] = "@(#)praliases.c	6.3 (Berkeley) 05/21/93";
1734046Sbostic #endif /* not lint */
1830811Sbostic 
1960161Sbostic #include <ndbm.h>
2034046Sbostic #include <sendmail.h>
21*60174Seric #ifdef NEWDB
22*60174Seric #include <db.h>
23*60174Seric #endif
2434046Sbostic 
2560161Sbostic int
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;
36*60174Seric #ifdef NEWDB
37*60174Seric 	const DB *db;
38*60174Seric 	DBT newdbkey, newdbcontent;
39*60174Seric 	char buf[MAXNAME];
40*60174Seric #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 
56*60174Seric #ifdef NEWDB
57*60174Seric 	(void) strcpy(buf, filename);
58*60174Seric 	(void) strcat(buf, ".db");
59*60174Seric 	if (db = dbopen(buf, O_RDONLY, 0444 , DB_HASH, NULL)) {
60*60174Seric 		if (!argc) {
61*60174Seric 			while(!db->seq(db, &newdbkey, &newdbcontent, R_NEXT))
62*60174Seric 				printf("%s:%s\n", newdbkey.data,
63*60174Seric 						newdbcontent.data);
64*60174Seric 		}
65*60174Seric 		else for (; *argv; ++argv) {
66*60174Seric 			newdbkey.data = *argv;
67*60174Seric 			newdbkey.size = strlen(*argv) + 1;
68*60174Seric 			if ( !db->get(db, &newdbkey, &newdbcontent, 0) )
69*60174Seric 				printf("%s:%s\n", newdbkey.data,
70*60174Seric 					newdbcontent.data);
71*60174Seric 			else
72*60174Seric 				printf("%s: No such key\n",
73*60174Seric 					newdbkey.data);
74*60174Seric 		}
7560161Sbostic 	}
76*60174Seric 	else {
77*60174Seric #endif
78*60174Seric 		if ((dbp = dbm_open(filename, O_RDONLY, 0)) == NULL) {
79*60174Seric 			(void)fprintf(stderr,
80*60174Seric 			    "praliases: %s: %s\n", filename, strerror(errno));
81*60174Seric 			exit(EX_OSFILE);
82*60174Seric 		}
83*60174Seric 		if (!argc)
84*60174Seric 			for (key = dbm_nextkey(dbp);
85*60174Seric 			    key.dptr != NULL; key = dbm_nextkey(dbp)) {
86*60174Seric 				content = dbm_fetch(dbp, key);
87*60174Seric 				(void)printf("%s:%s\n", key.dptr, content.dptr);
88*60174Seric 			}
89*60174Seric 		else for (; *argv; ++argv) {
90*60174Seric 			key.dptr = *argv;
91*60174Seric 			key.dsize = strlen(*argv) + 1;
9260161Sbostic 			content = dbm_fetch(dbp, key);
93*60174Seric 			if (!content.dptr)
94*60174Seric 				(void)printf("%s: No such key\n", key.dptr);
95*60174Seric 			else
96*60174Seric 				(void)printf("%s:%s\n", key.dptr, content.dptr);
9730811Sbostic 		}
98*60174Seric #ifdef NEWDB
9930811Sbostic 	}
100*60174Seric #endif
10130811Sbostic 	exit(EX_OK);
10230811Sbostic }
103