xref: /openbsd-src/usr.bin/from/from.c (revision 43003dfe3ad45d1698bed8a37f2b0f5b14f20d4f)
1 /*	$OpenBSD: from.c,v 1.13 2007/02/20 13:52:22 jmc 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.13 2007/02/20 13:52:22 jmc 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 	struct passwd *pwd;
62 	int ch, newline;
63 	char *file, *sender, *p;
64 #if MAXPATHLEN > BUFSIZ
65 	char buf[MAXPATHLEN];
66 #else
67 	char buf[BUFSIZ];
68 #endif
69 
70 	file = sender = NULL;
71 	while ((ch = getopt(argc, argv, "f:s:")) != -1)
72 		switch((char)ch) {
73 		case 'f':
74 			file = optarg;
75 			break;
76 		case 's':
77 			sender = optarg;
78 			for (p = sender; *p; ++p)
79 				if (isupper(*p))
80 					*p = tolower(*p);
81 			break;
82 		case '?':
83 		default:
84 			fprintf(stderr,
85 			    "usage: from [-f file] [-s sender] [user]\n");
86 			exit(1);
87 		}
88 	argv += optind;
89 
90 	/*
91 	 * We find the mailbox by:
92 	 *	1 -f flag
93 	 *	2 user
94 	 *	2 MAIL environment variable
95 	 *	3 _PATH_MAILDIR/file
96 	 */
97 	if (!file) {
98 		if (!(file = *argv)) {
99 			if (!(file = getenv("MAIL"))) {
100 				if (!(pwd = getpwuid(getuid())))
101 					errx(1, "no password file entry for you");
102 				if ((file = getenv("USER"))) {
103 					(void)snprintf(buf, sizeof(buf),
104 					    "%s/%s", _PATH_MAILDIR, file);
105 					file = buf;
106 				} else
107 					(void)snprintf(file = buf, sizeof(buf),
108 					    "%s/%s", _PATH_MAILDIR,
109 					    pwd->pw_name);
110 			}
111 		} else {
112 			(void)snprintf(buf, sizeof(buf), "%s/%s",
113 			    _PATH_MAILDIR, file);
114 			file = buf;
115 		}
116 	}
117 	if (!freopen(file, "r", stdin))
118 		err(1, "%s", file);
119 	for (newline = 1; fgets(buf, sizeof(buf), stdin);) {
120 		if (*buf == '\n') {
121 			newline = 1;
122 			continue;
123 		}
124 		if (newline && !strncmp(buf, "From ", 5) &&
125 		    (!sender || match(buf + 5, sender)))
126 			printf("%s", buf);
127 		newline = 0;
128 	}
129 	exit(0);
130 }
131 
132 int
133 match(char *line, char *sender)
134 {
135 	char ch, pch, first, *p, *t;
136 
137 	for (first = *sender++;;) {
138 		if (isspace(ch = *line))
139 			return(0);
140 		++line;
141 		if (isupper(ch))
142 			ch = tolower(ch);
143 		if (ch != first)
144 			continue;
145 		for (p = sender, t = line;;) {
146 			if (!(pch = *p++))
147 				return(1);
148 			if (isupper(ch = *t++))
149 				ch = tolower(ch);
150 			if (ch != pch)
151 				break;
152 		}
153 	}
154 	/* NOTREACHED */
155 }
156