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