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