xref: /freebsd/usr.bin/from/from.c (revision fbbd9655e5107c68e4e0146ff22b73d7350475bc)
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.
13*fbbd9655SWarner Losh  * 3. Neither the name of the University nor the names of its contributors
149b50d902SRodney W. Grimes  *    may be used to endorse or promote products derived from this software
159b50d902SRodney W. Grimes  *    without specific prior written permission.
169b50d902SRodney W. Grimes  *
179b50d902SRodney W. Grimes  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
189b50d902SRodney W. Grimes  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
199b50d902SRodney W. Grimes  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
209b50d902SRodney W. Grimes  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
219b50d902SRodney W. Grimes  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
229b50d902SRodney W. Grimes  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
239b50d902SRodney W. Grimes  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
249b50d902SRodney W. Grimes  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
259b50d902SRodney W. Grimes  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
269b50d902SRodney W. Grimes  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
279b50d902SRodney W. Grimes  * SUCH DAMAGE.
289b50d902SRodney W. Grimes  */
299b50d902SRodney W. Grimes 
309b50d902SRodney W. Grimes #ifndef lint
31f589c9aaSPhilippe Charnier static const char copyright[] =
329b50d902SRodney W. Grimes "@(#) Copyright (c) 1980, 1988, 1993\n\
339b50d902SRodney W. Grimes 	The Regents of the University of California.  All rights reserved.\n";
349b50d902SRodney W. Grimes #endif /* not lint */
359b50d902SRodney W. Grimes 
369b50d902SRodney W. Grimes #ifndef lint
37f589c9aaSPhilippe Charnier #if 0
389b50d902SRodney W. Grimes static char sccsid[] = "@(#)from.c	8.1 (Berkeley) 6/6/93";
39f589c9aaSPhilippe Charnier #endif
409b50d902SRodney W. Grimes #endif /* not lint */
41e026a48cSDavid E. O'Brien #include <sys/cdefs.h>
42e026a48cSDavid E. O'Brien __FBSDID("$FreeBSD$");
439b50d902SRodney W. Grimes 
449b50d902SRodney W. Grimes #include <sys/types.h>
459b50d902SRodney W. Grimes #include <ctype.h>
46f589c9aaSPhilippe Charnier #include <err.h>
479b50d902SRodney W. Grimes #include <pwd.h>
489b50d902SRodney W. Grimes #include <stdio.h>
49338fc8ebSJordan K. Hubbard #include <stdlib.h>
509b50d902SRodney W. Grimes #include <paths.h>
51f589c9aaSPhilippe Charnier #include <string.h>
52f589c9aaSPhilippe Charnier #include <unistd.h>
539b50d902SRodney W. Grimes 
541e7652ccSEitan Adler static int match(const char *, const char *);
55f1bb2cd2SWarner Losh static void usage(void);
56f589c9aaSPhilippe Charnier 
57f589c9aaSPhilippe Charnier int
58c948c058SJuli Mallett main(int argc, char **argv)
599b50d902SRodney W. Grimes {
60c948c058SJuli Mallett 	FILE *mbox;
619b50d902SRodney W. Grimes 	struct passwd *pwd;
6282753326SBrian Feldman 	int ch, count, newline;
63f4ac32deSDavid Malone 	const char *file;
64f4ac32deSDavid Malone 	char *sender, *p;
659b50d902SRodney W. Grimes #if MAXPATHLEN > BUFSIZ
669b50d902SRodney W. Grimes 	char buf[MAXPATHLEN];
679b50d902SRodney W. Grimes #else
689b50d902SRodney W. Grimes 	char buf[BUFSIZ];
699b50d902SRodney W. Grimes #endif
709b50d902SRodney W. Grimes 
719b50d902SRodney W. Grimes 	file = sender = NULL;
7282753326SBrian Feldman 	count = -1;
7382753326SBrian Feldman 	while ((ch = getopt(argc, argv, "cf:s:")) != -1)
7482753326SBrian Feldman 		switch (ch) {
7582753326SBrian Feldman 		case 'c':
7682753326SBrian Feldman 			count = 0;
7782753326SBrian Feldman 			break;
789b50d902SRodney W. Grimes 		case 'f':
799b50d902SRodney W. Grimes 			file = optarg;
809b50d902SRodney W. Grimes 			break;
819b50d902SRodney W. Grimes 		case 's':
829b50d902SRodney W. Grimes 			sender = optarg;
839b50d902SRodney W. Grimes 			for (p = sender; *p; ++p)
849b50d902SRodney W. Grimes 				*p = tolower(*p);
859b50d902SRodney W. Grimes 			break;
869b50d902SRodney W. Grimes 		case '?':
879b50d902SRodney W. Grimes 		default:
88f589c9aaSPhilippe Charnier 			usage();
899b50d902SRodney W. Grimes 		}
90c948c058SJuli Mallett 	argc -= optind;
919b50d902SRodney W. Grimes 	argv += optind;
929b50d902SRodney W. Grimes 
93c948c058SJuli Mallett 	if (file == NULL) {
94c948c058SJuli Mallett 		if (argc) {
95d2ba10a9SRuslan Ermilov 			(void)snprintf(buf, sizeof(buf), "%s/%s", _PATH_MAILDIR, *argv);
96338fc8ebSJordan K. Hubbard 			file  = buf;
97338fc8ebSJordan K. Hubbard 		} else {
98338fc8ebSJordan K. Hubbard 			if (!(file = getenv("MAIL"))) {
99f589c9aaSPhilippe Charnier 				if (!(pwd = getpwuid(getuid())))
100f589c9aaSPhilippe Charnier 					errx(1, "no password file entry for you");
1019b50d902SRodney W. Grimes 				file = pwd->pw_name;
102d2ba10a9SRuslan Ermilov 				(void)snprintf(buf, sizeof(buf),
103338fc8ebSJordan K. Hubbard 				    "%s/%s", _PATH_MAILDIR, file);
1049b50d902SRodney W. Grimes 				file = buf;
1059b50d902SRodney W. Grimes 			}
106338fc8ebSJordan K. Hubbard 		}
107338fc8ebSJordan K. Hubbard 	}
10898d04b7cSWolfram Schneider 
10998d04b7cSWolfram Schneider 	/* read from stdin */
11098d04b7cSWolfram Schneider 	if (strcmp(file, "-") == 0) {
111c948c058SJuli Mallett 		mbox = stdin;
11298d04b7cSWolfram Schneider 	}
113c948c058SJuli Mallett 	else if ((mbox = fopen(file, "r")) == NULL) {
114f589c9aaSPhilippe Charnier 		errx(1, "can't read %s", file);
1159b50d902SRodney W. Grimes 	}
116c948c058SJuli Mallett 	for (newline = 1; fgets(buf, sizeof(buf), mbox);) {
1179b50d902SRodney W. Grimes 		if (*buf == '\n') {
1189b50d902SRodney W. Grimes 			newline = 1;
1199b50d902SRodney W. Grimes 			continue;
1209b50d902SRodney W. Grimes 		}
1219b50d902SRodney W. Grimes 		if (newline && !strncmp(buf, "From ", 5) &&
12282753326SBrian Feldman 		    (!sender || match(buf + 5, sender))) {
12382753326SBrian Feldman 			if (count != -1)
12482753326SBrian Feldman 				count++;
12582753326SBrian Feldman 			else
1269b50d902SRodney W. Grimes 				printf("%s", buf);
12782753326SBrian Feldman 		}
1289b50d902SRodney W. Grimes 		newline = 0;
1299b50d902SRodney W. Grimes 	}
13082753326SBrian Feldman 	if (count != -1)
13182753326SBrian Feldman 		printf("There %s %d message%s in your incoming mailbox.\n",
13282753326SBrian Feldman 		    count == 1 ? "is" : "are", count, count == 1 ? "" : "s");
133c948c058SJuli Mallett 	fclose(mbox);
1349b50d902SRodney W. Grimes 	exit(0);
1359b50d902SRodney W. Grimes }
1369b50d902SRodney W. Grimes 
137f589c9aaSPhilippe Charnier static void
138c948c058SJuli Mallett usage(void)
139f589c9aaSPhilippe Charnier {
14082753326SBrian Feldman 	fprintf(stderr, "usage: from [-c] [-f file] [-s sender] [user]\n");
141f589c9aaSPhilippe Charnier 	exit(1);
142f589c9aaSPhilippe Charnier }
143f589c9aaSPhilippe Charnier 
1441e7652ccSEitan Adler static int
145f4ac32deSDavid Malone match(const char *line, const char *sender)
1469b50d902SRodney W. Grimes {
147f4ac32deSDavid Malone 	char ch, pch, first;
148f4ac32deSDavid Malone 	const char *p, *t;
1499b50d902SRodney W. Grimes 
1509b50d902SRodney W. Grimes 	for (first = *sender++;;) {
1519b50d902SRodney W. Grimes 		if (isspace(ch = *line))
1529b50d902SRodney W. Grimes 			return(0);
1539b50d902SRodney W. Grimes 		++line;
1549b50d902SRodney W. Grimes 		ch = tolower(ch);
1559b50d902SRodney W. Grimes 		if (ch != first)
1569b50d902SRodney W. Grimes 			continue;
1579b50d902SRodney W. Grimes 		for (p = sender, t = line;;) {
1589b50d902SRodney W. Grimes 			if (!(pch = *p++))
1599b50d902SRodney W. Grimes 				return(1);
1601e7652ccSEitan Adler 			ch = tolower(*t);
1611e7652ccSEitan Adler 			t++;
1629b50d902SRodney W. Grimes 			if (ch != pch)
1639b50d902SRodney W. Grimes 				break;
1649b50d902SRodney W. Grimes 		}
1659b50d902SRodney W. Grimes 	}
1669b50d902SRodney W. Grimes 	/* NOTREACHED */
1679b50d902SRodney W. Grimes }
168