1 /*
2 * Copyright (c) 1983 Eric P. Allman
3 * Copyright (c) 1988, 1993
4 * The Regents of the University of California. All rights reserved.
5 *
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.
33 */
34
35 #ifndef lint
36 static char copyright[] =
37 "@(#) Copyright (c) 1988, 1993\n\
38 The Regents of the University of California. All rights reserved.\n";
39 #endif /* not lint */
40
41 #ifndef lint
42 static char sccsid[] = "@(#)praliases.c 8.3 (Berkeley) 3/6/94";
43 #endif /* not lint */
44
45 #include <ndbm.h>
46 #include <sendmail.h>
47 #ifdef NEWDB
48 #include <db.h>
49 #endif
50
51 int
main(argc,argv)52 main(argc, argv)
53 int argc;
54 char **argv;
55 {
56 extern char *optarg;
57 extern int optind;
58 DBM *dbp;
59 datum content, key;
60 char *filename;
61 int ch;
62 #ifdef NEWDB
63 const DB *db;
64 DBT newdbkey, newdbcontent;
65 char buf[MAXNAME];
66 #endif
67
68 filename = "/etc/aliases";
69 while ((ch = getopt(argc, argv, "f:")) != EOF)
70 switch((char)ch) {
71 case 'f':
72 filename = optarg;
73 break;
74 case '?':
75 default:
76 (void)fprintf(stderr, "usage: praliases [-f file]\n");
77 exit(EX_USAGE);
78 }
79 argc -= optind;
80 argv += optind;
81
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",
89 newdbkey.size, newdbkey.data,
90 newdbcontent.size, newdbcontent.data);
91 }
92 else for (; *argv; ++argv) {
93 newdbkey.data = *argv;
94 newdbkey.size = strlen(*argv) + 1;
95 if (!db->get(db, &newdbkey, &newdbcontent, 0))
96 printf("%s:%.*s\n", newdbkey.data,
97 newdbcontent.size, newdbcontent.data);
98 else
99 printf("%s: No such key\n",
100 newdbkey.data);
101 }
102 }
103 else {
104 #endif
105 if ((dbp = dbm_open(filename, O_RDONLY, 0)) == NULL) {
106 (void)fprintf(stderr,
107 "praliases: %s: %s\n", filename, strerror(errno));
108 exit(EX_OSFILE);
109 }
110 if (!argc)
111 for (key = dbm_firstkey(dbp);
112 key.dptr != NULL; key = dbm_nextkey(dbp)) {
113 content = dbm_fetch(dbp, key);
114 (void)printf("%.*s:%.*s\n",
115 key.dsize, key.dptr,
116 content.dsize, content.dptr);
117 }
118 else for (; *argv; ++argv) {
119 key.dptr = *argv;
120 key.dsize = strlen(*argv) + 1;
121 content = dbm_fetch(dbp, key);
122 if (!content.dptr)
123 (void)printf("%s: No such key\n", key.dptr);
124 else
125 (void)printf("%s:%.*s\n", key.dptr,
126 content.dsize, content.dptr);
127 }
128 #ifdef NEWDB
129 }
130 #endif
131 exit(EX_OK);
132 }
133