Commit | Line | Data |
---|---|---|
15637ed4 RG |
1 | /* |
2 | * Copyright (c) 1983 Eric P. Allman | |
6f14531a RG |
3 | * Copyright (c) 1988, 1993 |
4 | * The Regents of the University of California. All rights reserved. | |
15637ed4 RG |
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 | |
6f14531a RG |
36 | static char copyright[] = |
37 | "@(#) Copyright (c) 1988, 1993\n\ | |
38 | The Regents of the University of California. All rights reserved.\n"; | |
15637ed4 RG |
39 | #endif /* not lint */ |
40 | ||
41 | #ifndef lint | |
6f14531a | 42 | static char sccsid[] = "@(#)praliases.c 8.1 (Berkeley) 6/7/93"; |
15637ed4 RG |
43 | #endif /* not lint */ |
44 | ||
6f14531a | 45 | #include <ndbm.h> |
15637ed4 | 46 | #include <sendmail.h> |
6f14531a RG |
47 | #ifdef NEWDB |
48 | #include <db.h> | |
49 | #endif | |
15637ed4 | 50 | |
6f14531a | 51 | int |
15637ed4 | 52 | main(argc, argv) |
6f14531a | 53 | int argc; |
15637ed4 RG |
54 | char **argv; |
55 | { | |
56 | extern char *optarg; | |
57 | extern int optind; | |
6f14531a RG |
58 | DBM *dbp; |
59 | datum content, key; | |
60 | char *filename; | |
15637ed4 | 61 | int ch; |
6f14531a RG |
62 | #ifdef NEWDB |
63 | const DB *db; | |
64 | DBT newdbkey, newdbcontent; | |
65 | char buf[MAXNAME]; | |
66 | #endif | |
15637ed4 | 67 | |
6f14531a | 68 | filename = "/etc/aliases"; |
15637ed4 RG |
69 | while ((ch = getopt(argc, argv, "f:")) != EOF) |
70 | switch((char)ch) { | |
71 | case 'f': | |
72 | filename = optarg; | |
73 | break; | |
74 | case '?': | |
75 | default: | |
6f14531a | 76 | (void)fprintf(stderr, "usage: praliases [-f file]\n"); |
15637ed4 RG |
77 | exit(EX_USAGE); |
78 | } | |
79 | argc -= optind; | |
80 | argv += optind; | |
81 | ||
6f14531a RG |
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 | } | |
101 | } | |
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; | |
118 | content = dbm_fetch(dbp, key); | |
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); | |
15637ed4 | 123 | } |
6f14531a | 124 | #ifdef NEWDB |
15637ed4 | 125 | } |
6f14531a | 126 | #endif |
15637ed4 RG |
127 | exit(EX_OK); |
128 | } |