xref: /freebsd/usr.bin/from/from.c (revision 338fc8eb62853c5e4ff1d06d333eea97927af9dd)
19b50d902SRodney W. Grimes /*
29b50d902SRodney W. Grimes  * Copyright (c) 1980, 1988, 1993
39b50d902SRodney W. Grimes  *	The Regents of the University of California.  All rights reserved.
49b50d902SRodney W. Grimes  *
59b50d902SRodney W. Grimes  * Redistribution and use in source and binary forms, with or without
69b50d902SRodney W. Grimes  * modification, are permitted provided that the following conditions
79b50d902SRodney W. Grimes  * are met:
89b50d902SRodney W. Grimes  * 1. Redistributions of source code must retain the above copyright
99b50d902SRodney W. Grimes  *    notice, this list of conditions and the following disclaimer.
109b50d902SRodney W. Grimes  * 2. Redistributions in binary form must reproduce the above copyright
119b50d902SRodney W. Grimes  *    notice, this list of conditions and the following disclaimer in the
129b50d902SRodney W. Grimes  *    documentation and/or other materials provided with the distribution.
139b50d902SRodney W. Grimes  * 3. All advertising materials mentioning features or use of this software
149b50d902SRodney W. Grimes  *    must display the following acknowledgement:
159b50d902SRodney W. Grimes  *	This product includes software developed by the University of
169b50d902SRodney W. Grimes  *	California, Berkeley and its contributors.
179b50d902SRodney W. Grimes  * 4. Neither the name of the University nor the names of its contributors
189b50d902SRodney W. Grimes  *    may be used to endorse or promote products derived from this software
199b50d902SRodney W. Grimes  *    without specific prior written permission.
209b50d902SRodney W. Grimes  *
219b50d902SRodney W. Grimes  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
229b50d902SRodney W. Grimes  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
239b50d902SRodney W. Grimes  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
249b50d902SRodney W. Grimes  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
259b50d902SRodney W. Grimes  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
269b50d902SRodney W. Grimes  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
279b50d902SRodney W. Grimes  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
289b50d902SRodney W. Grimes  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
299b50d902SRodney W. Grimes  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
309b50d902SRodney W. Grimes  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
319b50d902SRodney W. Grimes  * SUCH DAMAGE.
329b50d902SRodney W. Grimes  */
339b50d902SRodney W. Grimes 
349b50d902SRodney W. Grimes #ifndef lint
359b50d902SRodney W. Grimes static char copyright[] =
369b50d902SRodney W. Grimes "@(#) Copyright (c) 1980, 1988, 1993\n\
379b50d902SRodney W. Grimes 	The Regents of the University of California.  All rights reserved.\n";
389b50d902SRodney W. Grimes #endif /* not lint */
399b50d902SRodney W. Grimes 
409b50d902SRodney W. Grimes #ifndef lint
419b50d902SRodney W. Grimes static char sccsid[] = "@(#)from.c	8.1 (Berkeley) 6/6/93";
429b50d902SRodney W. Grimes #endif /* not lint */
439b50d902SRodney W. Grimes 
449b50d902SRodney W. Grimes #include <sys/types.h>
45338fc8ebSJordan K. Hubbard 
469b50d902SRodney W. Grimes #include <ctype.h>
479b50d902SRodney W. Grimes #include <pwd.h>
489b50d902SRodney W. Grimes #include <stdio.h>
49338fc8ebSJordan K. Hubbard #include <stdlib.h>
5098d04b7cSWolfram Schneider #include <string.h>
519b50d902SRodney W. Grimes #include <paths.h>
529b50d902SRodney W. Grimes 
539b50d902SRodney W. Grimes main(argc, argv)
549b50d902SRodney W. Grimes 	int argc;
559b50d902SRodney W. Grimes 	char **argv;
569b50d902SRodney W. Grimes {
579b50d902SRodney W. Grimes 	extern char *optarg;
589b50d902SRodney W. Grimes 	extern int optind;
599b50d902SRodney W. Grimes 	struct passwd *pwd;
609b50d902SRodney W. Grimes 	int ch, newline;
619b50d902SRodney W. Grimes 	char *file, *sender, *p;
629b50d902SRodney W. Grimes #if MAXPATHLEN > BUFSIZ
639b50d902SRodney W. Grimes 	char buf[MAXPATHLEN];
649b50d902SRodney W. Grimes #else
659b50d902SRodney W. Grimes 	char buf[BUFSIZ];
669b50d902SRodney W. Grimes #endif
679b50d902SRodney W. Grimes 
689b50d902SRodney W. Grimes 	file = sender = NULL;
6998d04b7cSWolfram Schneider 	while ((ch = getopt(argc, argv, "f:s:?")) != EOF)
709b50d902SRodney W. Grimes 		switch((char)ch) {
719b50d902SRodney W. Grimes 		case 'f':
729b50d902SRodney W. Grimes 			file = optarg;
739b50d902SRodney W. Grimes 			break;
749b50d902SRodney W. Grimes 		case 's':
759b50d902SRodney W. Grimes 			sender = optarg;
769b50d902SRodney W. Grimes 			for (p = sender; *p; ++p)
779b50d902SRodney W. Grimes 				if (isupper(*p))
789b50d902SRodney W. Grimes 					*p = tolower(*p);
799b50d902SRodney W. Grimes 			break;
809b50d902SRodney W. Grimes 		case '?':
819b50d902SRodney W. Grimes 		default:
829b50d902SRodney W. Grimes 			fprintf(stderr, "usage: from [-f file] [-s sender] [user]\n");
839b50d902SRodney W. Grimes 			exit(1);
849b50d902SRodney W. Grimes 		}
859b50d902SRodney W. Grimes 	argv += optind;
869b50d902SRodney W. Grimes 
879b50d902SRodney W. Grimes 	if (!file) {
88338fc8ebSJordan K. Hubbard 		if (*argv) {
89338fc8ebSJordan K. Hubbard 			(void)sprintf(buf, "%s/%s", _PATH_MAILDIR, *argv);
90338fc8ebSJordan K. Hubbard 			file  = buf;
91338fc8ebSJordan K. Hubbard 		} else {
92338fc8ebSJordan K. Hubbard 			if (!(file = getenv("MAIL"))) {
939b50d902SRodney W. Grimes 				if (!(pwd = getpwuid(getuid()))) {
94338fc8ebSJordan K. Hubbard 					(void)fprintf(stderr,
959b50d902SRodney W. Grimes 				    "from: no password file entry for you.\n");
969b50d902SRodney W. Grimes 					exit(1);
979b50d902SRodney W. Grimes 				}
989b50d902SRodney W. Grimes 				file = pwd->pw_name;
99338fc8ebSJordan K. Hubbard 				(void)sprintf(buf,
100338fc8ebSJordan K. Hubbard 				    "%s/%s", _PATH_MAILDIR, file);
1019b50d902SRodney W. Grimes 				file = buf;
1029b50d902SRodney W. Grimes 			}
103338fc8ebSJordan K. Hubbard 		}
104338fc8ebSJordan K. Hubbard 	}
10598d04b7cSWolfram Schneider 
10698d04b7cSWolfram Schneider 	/* read from stdin */
10798d04b7cSWolfram Schneider 	if (strcmp(file, "-") == 0) {
10898d04b7cSWolfram Schneider 	}
10998d04b7cSWolfram Schneider 	else if (!freopen(file, "r", stdin)) {
1109b50d902SRodney W. Grimes 		fprintf(stderr, "from: can't read %s.\n", file);
1119b50d902SRodney W. Grimes 		exit(1);
1129b50d902SRodney W. Grimes 	}
1139b50d902SRodney W. Grimes 	for (newline = 1; fgets(buf, sizeof(buf), stdin);) {
1149b50d902SRodney W. Grimes 		if (*buf == '\n') {
1159b50d902SRodney W. Grimes 			newline = 1;
1169b50d902SRodney W. Grimes 			continue;
1179b50d902SRodney W. Grimes 		}
1189b50d902SRodney W. Grimes 		if (newline && !strncmp(buf, "From ", 5) &&
1199b50d902SRodney W. Grimes 		    (!sender || match(buf + 5, sender)))
1209b50d902SRodney W. Grimes 			printf("%s", buf);
1219b50d902SRodney W. Grimes 		newline = 0;
1229b50d902SRodney W. Grimes 	}
1239b50d902SRodney W. Grimes 	exit(0);
1249b50d902SRodney W. Grimes }
1259b50d902SRodney W. Grimes 
1269b50d902SRodney W. Grimes match(line, sender)
1279b50d902SRodney W. Grimes 	register char *line, *sender;
1289b50d902SRodney W. Grimes {
1299b50d902SRodney W. Grimes 	register char ch, pch, first, *p, *t;
1309b50d902SRodney W. Grimes 
1319b50d902SRodney W. Grimes 	for (first = *sender++;;) {
1329b50d902SRodney W. Grimes 		if (isspace(ch = *line))
1339b50d902SRodney W. Grimes 			return(0);
1349b50d902SRodney W. Grimes 		++line;
1359b50d902SRodney W. Grimes 		if (isupper(ch))
1369b50d902SRodney W. Grimes 			ch = tolower(ch);
1379b50d902SRodney W. Grimes 		if (ch != first)
1389b50d902SRodney W. Grimes 			continue;
1399b50d902SRodney W. Grimes 		for (p = sender, t = line;;) {
1409b50d902SRodney W. Grimes 			if (!(pch = *p++))
1419b50d902SRodney W. Grimes 				return(1);
1429b50d902SRodney W. Grimes 			if (isupper(ch = *t++))
1439b50d902SRodney W. Grimes 				ch = tolower(ch);
1449b50d902SRodney W. Grimes 			if (ch != pch)
1459b50d902SRodney W. Grimes 				break;
1469b50d902SRodney W. Grimes 		}
1479b50d902SRodney W. Grimes 	}
1489b50d902SRodney W. Grimes 	/* NOTREACHED */
1499b50d902SRodney W. Grimes }
150