xref: /freebsd/usr.bin/from/from.c (revision 397e83df75e0fcd0d3fcb95ae4d794cb7600fc89)
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 #include <sys/types.h>
33 #include <ctype.h>
34 #include <err.h>
35 #include <pwd.h>
36 #include <stdio.h>
37 #include <stdlib.h>
38 #include <paths.h>
39 #include <string.h>
40 #include <unistd.h>
41 
42 static int match(const char *, const char *);
43 static void usage(void) __dead2;
44 
45 int
46 main(int argc, char **argv)
47 {
48 	FILE *mbox;
49 	struct passwd *pwd;
50 	int ch, count, newline;
51 	const char *file;
52 	char *sender, *p;
53 #if MAXPATHLEN > BUFSIZ
54 	char buf[MAXPATHLEN];
55 #else
56 	char buf[BUFSIZ];
57 #endif
58 
59 	file = sender = NULL;
60 	count = -1;
61 	while ((ch = getopt(argc, argv, "cf:s:")) != -1)
62 		switch (ch) {
63 		case 'c':
64 			count = 0;
65 			break;
66 		case 'f':
67 			file = optarg;
68 			break;
69 		case 's':
70 			sender = optarg;
71 			for (p = sender; *p; ++p)
72 				*p = tolower(*p);
73 			break;
74 		case '?':
75 		default:
76 			usage();
77 		}
78 	argc -= optind;
79 	argv += optind;
80 
81 	if (file == NULL) {
82 		if (argc) {
83 			(void)snprintf(buf, sizeof(buf), "%s/%s", _PATH_MAILDIR, *argv);
84 			file  = buf;
85 		} else {
86 			if (!(file = getenv("MAIL"))) {
87 				if (!(pwd = getpwuid(getuid())))
88 					errx(1, "no password file entry for you");
89 				file = pwd->pw_name;
90 				(void)snprintf(buf, sizeof(buf),
91 				    "%s/%s", _PATH_MAILDIR, file);
92 				file = buf;
93 			}
94 		}
95 	}
96 
97 	/* read from stdin */
98 	if (strcmp(file, "-") == 0) {
99 		mbox = stdin;
100 	}
101 	else if ((mbox = fopen(file, "r")) == NULL) {
102 		errx(1, "can't read %s", file);
103 	}
104 	for (newline = 1; fgets(buf, sizeof(buf), mbox);) {
105 		if (*buf == '\n') {
106 			newline = 1;
107 			continue;
108 		}
109 		if (newline && !strncmp(buf, "From ", 5) &&
110 		    (!sender || match(buf + 5, sender))) {
111 			if (count != -1)
112 				count++;
113 			else
114 				printf("%s", buf);
115 		}
116 		newline = 0;
117 	}
118 	if (count != -1)
119 		printf("There %s %d message%s in your incoming mailbox.\n",
120 		    count == 1 ? "is" : "are", count, count == 1 ? "" : "s");
121 	fclose(mbox);
122 	exit(0);
123 }
124 
125 static void
126 usage(void)
127 {
128 	fprintf(stderr, "usage: from [-c] [-f file] [-s sender] [user]\n");
129 	exit(1);
130 }
131 
132 static int
133 match(const char *line, const char *sender)
134 {
135 	char ch, pch, first;
136 	const char *p, *t;
137 
138 	for (first = *sender++;;) {
139 		if (isspace(ch = *line))
140 			return(0);
141 		++line;
142 		ch = tolower(ch);
143 		if (ch != first)
144 			continue;
145 		for (p = sender, t = line;;) {
146 			if (!(pch = *p++))
147 				return(1);
148 			ch = tolower(*t);
149 			t++;
150 			if (ch != pch)
151 				break;
152 		}
153 	}
154 	/* NOTREACHED */
155 }
156