from.c revision 1.14 1 /* $NetBSD: from.c,v 1.14 2001/11/15 14:16:11 kleink 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.14 2001/11/15 14:16:11 kleink 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 (int, char **);
60 int match (const char *, const char *);
61
62 int
63 main(int argc, char **argv)
64 {
65 struct passwd *pwd;
66 int ch, newline;
67 char *file, *sender, *p;
68 #if MAXPATHLEN > BUFSIZ
69 char buf[MAXPATHLEN];
70 #else
71 char buf[BUFSIZ];
72 #endif
73
74 file = sender = NULL;
75 while ((ch = getopt(argc, argv, "f:s:")) != -1)
76 switch((char)ch) {
77 case 'f':
78 file = optarg;
79 break;
80 case 's':
81 sender = optarg;
82 for (p = sender; *p; ++p)
83 if (isupper((unsigned char)*p))
84 *p = tolower(*p);
85 break;
86 default:
87 fprintf(stderr, "usage: from [-f file] [-s sender] [user]\n");
88 exit(1);
89 }
90 argv += optind;
91
92 /*
93 * We find the mailbox by:
94 * 1 -f flag
95 * 2 user
96 * 2 MAIL environment variable
97 * 3 _PATH_MAILDIR/file
98 */
99 if (!file) {
100 if (!(file = *argv)) {
101 if (!(file = getenv("MAIL"))) {
102 if (!(pwd = getpwuid(getuid())))
103 errx(1, "no password file entry for you");
104 if ((file = getenv("LOGNAME")) != NULL ||
105 (file = getenv("USER")) != NULL) {
106 (void)snprintf(buf, sizeof(buf),
107 "%s/%s", _PATH_MAILDIR, file);
108 file = buf;
109 } else
110 (void)snprintf(file = buf, sizeof(buf),
111 "%s/%s",
112 _PATH_MAILDIR, pwd->pw_name);
113 }
114 } else {
115 (void)snprintf(buf, sizeof(buf), "%s/%s",
116 _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(const char *line, const char *sender)
138 {
139 char ch, pch, first;
140 const char *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