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