from.c revision 1.10 1 /* $NetBSD: from.c,v 1.10 2000/09/08 13:06:13 mjl Exp $ */
2
3 /*
4 * Copyright (c) 1980, 1988, 1993
5 * The Regents of the University of California. All rights reserved.
6 *
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
9 * are met:
10 * 1. Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
12 * 2. Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in the
14 * documentation and/or other materials provided with the distribution.
15 * 3. All advertising materials mentioning features or use of this software
16 * must display the following acknowledgement:
17 * This product includes software developed by the University of
18 * California, Berkeley and its contributors.
19 * 4. Neither the name of the University nor the names of its contributors
20 * may be used to endorse or promote products derived from this software
21 * without specific prior written permission.
22 *
23 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
24 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
25 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
26 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
27 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
28 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
29 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
30 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
31 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
32 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
33 * SUCH DAMAGE.
34 */
35
36 #include <sys/cdefs.h>
37 #ifndef lint
38 __COPYRIGHT("@(#) Copyright (c) 1980, 1988, 1993\n\
39 The Regents of the University of California. All rights reserved.\n");
40 #endif /* not lint */
41
42 #ifndef lint
43 #if 0
44 static char sccsid[] = "@(#)from.c 8.1 (Berkeley) 6/6/93";
45 #endif
46 __RCSID("$NetBSD: from.c,v 1.10 2000/09/08 13:06:13 mjl Exp $");
47 #endif /* not lint */
48
49 #include <sys/types.h>
50 #include <ctype.h>
51 #include <err.h>
52 #include <paths.h>
53 #include <pwd.h>
54 #include <stdio.h>
55 #include <stdlib.h>
56 #include <string.h>
57 #include <unistd.h>
58
59 int main __P((int, char **));
60 int match __P((char *, char *));
61
62 int
63 main(argc, argv)
64 int argc;
65 char **argv;
66 {
67 struct passwd *pwd;
68 int ch, newline;
69 char *file, *sender, *p;
70 #if MAXPATHLEN > BUFSIZ
71 char buf[MAXPATHLEN];
72 #else
73 char buf[BUFSIZ];
74 #endif
75
76 file = sender = NULL;
77 while ((ch = getopt(argc, argv, "f:s:")) != -1)
78 switch((char)ch) {
79 case 'f':
80 file = optarg;
81 break;
82 case 's':
83 sender = optarg;
84 for (p = sender; *p; ++p)
85 if (isupper((unsigned char)*p))
86 *p = tolower(*p);
87 break;
88 case '?':
89 default:
90 fprintf(stderr, "usage: from [-f file] [-s sender] [user]\n");
91 exit(1);
92 }
93 argv += optind;
94
95 /*
96 * We find the mailbox by:
97 * 1 -f flag
98 * 2 user
99 * 2 MAIL environment variable
100 * 3 _PATH_MAILDIR/file
101 */
102 if (!file) {
103 if (!(file = *argv)) {
104 if (!(file = getenv("MAIL"))) {
105 if (!(pwd = getpwuid(getuid())))
106 err(1, "no password file entry for you");
107 if ((file = getenv("USER")) != NULL) {
108 (void)sprintf(buf, "%s/%s",
109 _PATH_MAILDIR, file);
110 file = buf;
111 } else
112 (void)sprintf(file = buf, "%s/%s",
113 _PATH_MAILDIR, pwd->pw_name);
114 }
115 } else {
116 (void)sprintf(buf, "%s/%s", _PATH_MAILDIR, file);
117 file = buf;
118 }
119 }
120 if (!freopen(file, "r", stdin))
121 err(1, "can't read %s", file);
122
123 for (newline = 1; fgets(buf, sizeof(buf), stdin);) {
124 if (*buf == '\n') {
125 newline = 1;
126 continue;
127 }
128 if (newline && !strncmp(buf, "From ", 5) &&
129 (!sender || match(buf + 5, sender)))
130 printf("%s", buf);
131 newline = 0;
132 }
133 exit(0);
134 }
135
136 int
137 match(line, sender)
138 char *line, *sender;
139 {
140 char ch, pch, first, *p, *t;
141
142 for (first = *sender++;;) {
143 if (isspace((unsigned char)(ch = *line)))
144 return(0);
145 ++line;
146 if (isupper((unsigned char)ch))
147 ch = tolower(ch);
148 if (ch != first)
149 continue;
150 for (p = sender, t = line;;) {
151 if (!(pch = *p++))
152 return(1);
153 if (isupper((unsigned char)(ch = *t++)))
154 ch = tolower(ch);
155 if (ch != pch)
156 break;
157 }
158 }
159 /* NOTREACHED */
160 }
161