1 /*-
2 * SPDX-License-Identifier: BSD-3-Clause
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. Neither the name of the University nor the names of its contributors
16 * may be used to endorse or promote products derived from this software
17 * without specific prior written permission.
18 *
19 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
20 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
21 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
22 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
23 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
24 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
25 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
26 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
27 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
28 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
29 * SUCH DAMAGE.
30 */
31
32 #ifndef lint
33 static const char copyright[] =
34 "@(#) Copyright (c) 1980, 1988, 1993\n\
35 The Regents of the University of California. All rights reserved.\n";
36 #endif /* not lint */
37
38 #ifndef lint
39 #if 0
40 static char sccsid[] = "@(#)from.c 8.1 (Berkeley) 6/6/93";
41 #endif
42 #endif /* not lint */
43 #include <sys/cdefs.h>
44 #include <sys/types.h>
45 #include <ctype.h>
46 #include <err.h>
47 #include <pwd.h>
48 #include <stdio.h>
49 #include <stdlib.h>
50 #include <paths.h>
51 #include <string.h>
52 #include <unistd.h>
53
54 static int match(const char *, const char *);
55 static void usage(void) __dead2;
56
57 int
main(int argc,char ** argv)58 main(int argc, char **argv)
59 {
60 FILE *mbox;
61 struct passwd *pwd;
62 int ch, count, newline;
63 const char *file;
64 char *sender, *p;
65 #if MAXPATHLEN > BUFSIZ
66 char buf[MAXPATHLEN];
67 #else
68 char buf[BUFSIZ];
69 #endif
70
71 file = sender = NULL;
72 count = -1;
73 while ((ch = getopt(argc, argv, "cf:s:")) != -1)
74 switch (ch) {
75 case 'c':
76 count = 0;
77 break;
78 case 'f':
79 file = optarg;
80 break;
81 case 's':
82 sender = optarg;
83 for (p = sender; *p; ++p)
84 *p = tolower(*p);
85 break;
86 case '?':
87 default:
88 usage();
89 }
90 argc -= optind;
91 argv += optind;
92
93 if (file == NULL) {
94 if (argc) {
95 (void)snprintf(buf, sizeof(buf), "%s/%s", _PATH_MAILDIR, *argv);
96 file = buf;
97 } else {
98 if (!(file = getenv("MAIL"))) {
99 if (!(pwd = getpwuid(getuid())))
100 errx(1, "no password file entry for you");
101 file = pwd->pw_name;
102 (void)snprintf(buf, sizeof(buf),
103 "%s/%s", _PATH_MAILDIR, file);
104 file = buf;
105 }
106 }
107 }
108
109 /* read from stdin */
110 if (strcmp(file, "-") == 0) {
111 mbox = stdin;
112 }
113 else if ((mbox = fopen(file, "r")) == NULL) {
114 errx(1, "can't read %s", file);
115 }
116 for (newline = 1; fgets(buf, sizeof(buf), mbox);) {
117 if (*buf == '\n') {
118 newline = 1;
119 continue;
120 }
121 if (newline && !strncmp(buf, "From ", 5) &&
122 (!sender || match(buf + 5, sender))) {
123 if (count != -1)
124 count++;
125 else
126 printf("%s", buf);
127 }
128 newline = 0;
129 }
130 if (count != -1)
131 printf("There %s %d message%s in your incoming mailbox.\n",
132 count == 1 ? "is" : "are", count, count == 1 ? "" : "s");
133 fclose(mbox);
134 exit(0);
135 }
136
137 static void
usage(void)138 usage(void)
139 {
140 fprintf(stderr, "usage: from [-c] [-f file] [-s sender] [user]\n");
141 exit(1);
142 }
143
144 static int
match(const char * line,const char * sender)145 match(const char *line, const char *sender)
146 {
147 char ch, pch, first;
148 const char *p, *t;
149
150 for (first = *sender++;;) {
151 if (isspace(ch = *line))
152 return(0);
153 ++line;
154 ch = tolower(ch);
155 if (ch != first)
156 continue;
157 for (p = sender, t = line;;) {
158 if (!(pch = *p++))
159 return(1);
160 ch = tolower(*t);
161 t++;
162 if (ch != pch)
163 break;
164 }
165 }
166 /* NOTREACHED */
167 }
168