134046Sbostic /*
234914Sbostic  * Copyright (c) 1983 Eric P. Allman
3*62516Sbostic  * Copyright (c) 1988, 1993
4*62516Sbostic  *	The Regents of the University of California.  All rights reserved.
534046Sbostic  *
642823Sbostic  * %sccs.include.redist.c%
734046Sbostic  */
830811Sbostic 
934046Sbostic #ifndef lint
10*62516Sbostic static char copyright[] =
11*62516Sbostic "@(#) Copyright (c) 1988, 1993\n\
12*62516Sbostic 	The Regents of the University of California.  All rights reserved.\n";
1334046Sbostic #endif /* not lint */
1430811Sbostic 
1534046Sbostic #ifndef lint
16*62516Sbostic static char sccsid[] = "@(#)praliases.c	8.1 (Berkeley) 06/07/93";
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
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))
6260174Seric 				printf("%s:%s\n", newdbkey.data,
6360174Seric 						newdbcontent.data);
6460174Seric 		}
6560174Seric 		else for (; *argv; ++argv) {
6660174Seric 			newdbkey.data = *argv;
6760174Seric 			newdbkey.size = strlen(*argv) + 1;
6860174Seric 			if ( !db->get(db, &newdbkey, &newdbcontent, 0) )
6960174Seric 				printf("%s:%s\n", newdbkey.data,
7060174Seric 					newdbcontent.data);
7160174Seric 			else
7260174Seric 				printf("%s: No such key\n",
7360174Seric 					newdbkey.data);
7460174Seric 		}
7560161Sbostic 	}
7660174Seric 	else {
7760174Seric #endif
7860174Seric 		if ((dbp = dbm_open(filename, O_RDONLY, 0)) == NULL) {
7960174Seric 			(void)fprintf(stderr,
8060174Seric 			    "praliases: %s: %s\n", filename, strerror(errno));
8160174Seric 			exit(EX_OSFILE);
8260174Seric 		}
8360174Seric 		if (!argc)
8460174Seric 			for (key = dbm_nextkey(dbp);
8560174Seric 			    key.dptr != NULL; key = dbm_nextkey(dbp)) {
8660174Seric 				content = dbm_fetch(dbp, key);
8760174Seric 				(void)printf("%s:%s\n", key.dptr, content.dptr);
8860174Seric 			}
8960174Seric 		else for (; *argv; ++argv) {
9060174Seric 			key.dptr = *argv;
9160174Seric 			key.dsize = strlen(*argv) + 1;
9260161Sbostic 			content = dbm_fetch(dbp, key);
9360174Seric 			if (!content.dptr)
9460174Seric 				(void)printf("%s: No such key\n", key.dptr);
9560174Seric 			else
9660174Seric 				(void)printf("%s:%s\n", key.dptr, content.dptr);
9730811Sbostic 		}
9860174Seric #ifdef NEWDB
9930811Sbostic 	}
10060174Seric #endif
10130811Sbostic 	exit(EX_OK);
10230811Sbostic }
103