BSD 4_4 release
[unix-history] / usr / src / usr.sbin / sendmail / praliases / praliases.c
CommitLineData
d8a9c561 1/*
5e8b0e60 2 * Copyright (c) 1983 Eric P. Allman
ad787160
C
3 * Copyright (c) 1988, 1993
4 * The Regents of the University of California. All rights reserved.
d8a9c561 5 *
ad787160
C
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions
8 * are met:
9 * 1. Redistributions of source code must retain the above copyright
10 * notice, this list of conditions and the following disclaimer.
11 * 2. Redistributions in binary form must reproduce the above copyright
12 * notice, this list of conditions and the following disclaimer in the
13 * documentation and/or other materials provided with the distribution.
14 * 3. All advertising materials mentioning features or use of this software
15 * must display the following acknowledgement:
16 * This product includes software developed by the University of
17 * California, Berkeley and its contributors.
18 * 4. Neither the name of the University nor the names of its contributors
19 * may be used to endorse or promote products derived from this software
20 * without specific prior written permission.
21 *
22 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
23 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
24 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
25 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
26 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
27 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
28 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
29 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
30 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
31 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
32 * SUCH DAMAGE.
d8a9c561 33 */
43937410 34
d8a9c561 35#ifndef lint
ad787160
C
36static char copyright[] =
37"@(#) Copyright (c) 1988, 1993\n\
38 The Regents of the University of California. All rights reserved.\n";
d8a9c561
KB
39#endif /* not lint */
40
41#ifndef lint
ad787160 42static char sccsid[] = "@(#)praliases.c 8.1 (Berkeley) 6/7/93";
d8a9c561
KB
43#endif /* not lint */
44
e4328fb1 45#include <ndbm.h>
d8a9c561 46#include <sendmail.h>
75be036c
EA
47#ifdef NEWDB
48#include <db.h>
49#endif
d8a9c561 50
e4328fb1 51int
43937410 52main(argc, argv)
e4328fb1 53 int argc;
43937410
KB
54 char **argv;
55{
d8a9c561
KB
56 extern char *optarg;
57 extern int optind;
e4328fb1
KB
58 DBM *dbp;
59 datum content, key;
60 char *filename;
d8a9c561 61 int ch;
75be036c
EA
62#ifdef NEWDB
63 const DB *db;
64 DBT newdbkey, newdbcontent;
65 char buf[MAXNAME];
66#endif
43937410 67
e4328fb1 68 filename = "/etc/aliases";
d8a9c561
KB
69 while ((ch = getopt(argc, argv, "f:")) != EOF)
70 switch((char)ch) {
71 case 'f':
72 filename = optarg;
73 break;
74 case '?':
75 default:
e4328fb1 76 (void)fprintf(stderr, "usage: praliases [-f file]\n");
d8a9c561
KB
77 exit(EX_USAGE);
78 }
79 argc -= optind;
80 argv += optind;
43937410 81
75be036c
EA
82#ifdef NEWDB
83 (void) strcpy(buf, filename);
84 (void) strcat(buf, ".db");
85 if (db = dbopen(buf, O_RDONLY, 0444 , DB_HASH, NULL)) {
86 if (!argc) {
87 while(!db->seq(db, &newdbkey, &newdbcontent, R_NEXT))
88 printf("%s:%s\n", newdbkey.data,
89 newdbcontent.data);
90 }
91 else for (; *argv; ++argv) {
92 newdbkey.data = *argv;
93 newdbkey.size = strlen(*argv) + 1;
94 if ( !db->get(db, &newdbkey, &newdbcontent, 0) )
95 printf("%s:%s\n", newdbkey.data,
96 newdbcontent.data);
97 else
98 printf("%s: No such key\n",
99 newdbkey.data);
100 }
e4328fb1 101 }
75be036c
EA
102 else {
103#endif
104 if ((dbp = dbm_open(filename, O_RDONLY, 0)) == NULL) {
105 (void)fprintf(stderr,
106 "praliases: %s: %s\n", filename, strerror(errno));
107 exit(EX_OSFILE);
108 }
109 if (!argc)
110 for (key = dbm_nextkey(dbp);
111 key.dptr != NULL; key = dbm_nextkey(dbp)) {
112 content = dbm_fetch(dbp, key);
113 (void)printf("%s:%s\n", key.dptr, content.dptr);
114 }
115 else for (; *argv; ++argv) {
116 key.dptr = *argv;
117 key.dsize = strlen(*argv) + 1;
e4328fb1 118 content = dbm_fetch(dbp, key);
75be036c
EA
119 if (!content.dptr)
120 (void)printf("%s: No such key\n", key.dptr);
121 else
122 (void)printf("%s:%s\n", key.dptr, content.dptr);
43937410 123 }
75be036c 124#ifdef NEWDB
43937410 125 }
75be036c 126#endif
43937410
KB
127 exit(EX_OK);
128}