from.c revision 1.13 1 /* $NetBSD: from.c,v 1.13 2001/07/01 00:09:46 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.13 2001/07/01 00:09:46 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 (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("USER")) != NULL) {
105 (void)snprintf(buf, sizeof(buf),
106 "%s/%s", _PATH_MAILDIR, file);
107 file = buf;
108 } else
109 (void)snprintf(file = buf, sizeof(buf),
110 "%s/%s",
111 _PATH_MAILDIR, pwd->pw_name);
112 }
113 } else {
114 (void)snprintf(buf, sizeof(buf), "%s/%s",
115 _PATH_MAILDIR, file);
116 file = buf;
117 }
118 }
119 if (!freopen(file, "r", stdin))
120 err(1, "can't read %s", file);
121
122 for (newline = 1; fgets(buf, sizeof(buf), stdin);) {
123 if (*buf == '\n') {
124 newline = 1;
125 continue;
126 }
127 if (newline && !strncmp(buf, "From ", 5) &&
128 (!sender || match(buf + 5, sender)))
129 printf("%s", buf);
130 newline = 0;
131 }
132 exit(0);
133 }
134
135 int
136 match(const char *line, const char *sender)
137 {
138 char ch, pch, first;
139 const char *p, *t;
140
141 for (first = *sender++;;) {
142 if (isspace((unsigned char)(ch = *line)))
143 return(0);
144 ++line;
145 if (isupper((unsigned char)ch))
146 ch = tolower(ch);
147 if (ch != first)
148 continue;
149 for (p = sender, t = line;;) {
150 if (!(pch = *p++))
151 return(1);
152 if (isupper((unsigned char)(ch = *t++)))
153 ch = tolower(ch);
154 if (ch != pch)
155 break;
156 }
157 }
158 /* NOTREACHED */
159 }
160