xref: /freebsd/usr.bin/from/from.c (revision 0b8224d1cc9dc6c9778ba04a75b2c8d47e5d7481)
18a16b7a1SPedro F. Giffuni /*-
28a16b7a1SPedro F. Giffuni  * SPDX-License-Identifier: BSD-3-Clause
38a16b7a1SPedro F. Giffuni  *
49b50d902SRodney W. Grimes  * Copyright (c) 1980, 1988, 1993
59b50d902SRodney W. Grimes  *	The Regents of the University of California.  All rights reserved.
69b50d902SRodney W. Grimes  *
79b50d902SRodney W. Grimes  * Redistribution and use in source and binary forms, with or without
89b50d902SRodney W. Grimes  * modification, are permitted provided that the following conditions
99b50d902SRodney W. Grimes  * are met:
109b50d902SRodney W. Grimes  * 1. Redistributions of source code must retain the above copyright
119b50d902SRodney W. Grimes  *    notice, this list of conditions and the following disclaimer.
129b50d902SRodney W. Grimes  * 2. Redistributions in binary form must reproduce the above copyright
139b50d902SRodney W. Grimes  *    notice, this list of conditions and the following disclaimer in the
149b50d902SRodney W. Grimes  *    documentation and/or other materials provided with the distribution.
15fbbd9655SWarner Losh  * 3. Neither the name of the University nor the names of its contributors
169b50d902SRodney W. Grimes  *    may be used to endorse or promote products derived from this software
179b50d902SRodney W. Grimes  *    without specific prior written permission.
189b50d902SRodney W. Grimes  *
199b50d902SRodney W. Grimes  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
209b50d902SRodney W. Grimes  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
219b50d902SRodney W. Grimes  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
229b50d902SRodney W. Grimes  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
239b50d902SRodney W. Grimes  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
249b50d902SRodney W. Grimes  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
259b50d902SRodney W. Grimes  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
269b50d902SRodney W. Grimes  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
279b50d902SRodney W. Grimes  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
289b50d902SRodney W. Grimes  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
299b50d902SRodney W. Grimes  * SUCH DAMAGE.
309b50d902SRodney W. Grimes  */
319b50d902SRodney W. Grimes 
329b50d902SRodney W. Grimes #include <sys/types.h>
339b50d902SRodney W. Grimes #include <ctype.h>
34f589c9aaSPhilippe Charnier #include <err.h>
359b50d902SRodney W. Grimes #include <pwd.h>
369b50d902SRodney W. Grimes #include <stdio.h>
37338fc8ebSJordan K. Hubbard #include <stdlib.h>
389b50d902SRodney W. Grimes #include <paths.h>
39f589c9aaSPhilippe Charnier #include <string.h>
40f589c9aaSPhilippe Charnier #include <unistd.h>
419b50d902SRodney W. Grimes 
421e7652ccSEitan Adler static int match(const char *, const char *);
43*a1b6427aSAlfonso Gregory static void usage(void) __dead2;
44f589c9aaSPhilippe Charnier 
45f589c9aaSPhilippe Charnier int
main(int argc,char ** argv)46c948c058SJuli Mallett main(int argc, char **argv)
479b50d902SRodney W. Grimes {
48c948c058SJuli Mallett 	FILE *mbox;
499b50d902SRodney W. Grimes 	struct passwd *pwd;
5082753326SBrian Feldman 	int ch, count, newline;
51f4ac32deSDavid Malone 	const char *file;
52f4ac32deSDavid Malone 	char *sender, *p;
539b50d902SRodney W. Grimes #if MAXPATHLEN > BUFSIZ
549b50d902SRodney W. Grimes 	char buf[MAXPATHLEN];
559b50d902SRodney W. Grimes #else
569b50d902SRodney W. Grimes 	char buf[BUFSIZ];
579b50d902SRodney W. Grimes #endif
589b50d902SRodney W. Grimes 
599b50d902SRodney W. Grimes 	file = sender = NULL;
6082753326SBrian Feldman 	count = -1;
6182753326SBrian Feldman 	while ((ch = getopt(argc, argv, "cf:s:")) != -1)
6282753326SBrian Feldman 		switch (ch) {
6382753326SBrian Feldman 		case 'c':
6482753326SBrian Feldman 			count = 0;
6582753326SBrian Feldman 			break;
669b50d902SRodney W. Grimes 		case 'f':
679b50d902SRodney W. Grimes 			file = optarg;
689b50d902SRodney W. Grimes 			break;
699b50d902SRodney W. Grimes 		case 's':
709b50d902SRodney W. Grimes 			sender = optarg;
719b50d902SRodney W. Grimes 			for (p = sender; *p; ++p)
729b50d902SRodney W. Grimes 				*p = tolower(*p);
739b50d902SRodney W. Grimes 			break;
749b50d902SRodney W. Grimes 		case '?':
759b50d902SRodney W. Grimes 		default:
76f589c9aaSPhilippe Charnier 			usage();
779b50d902SRodney W. Grimes 		}
78c948c058SJuli Mallett 	argc -= optind;
799b50d902SRodney W. Grimes 	argv += optind;
809b50d902SRodney W. Grimes 
81c948c058SJuli Mallett 	if (file == NULL) {
82c948c058SJuli Mallett 		if (argc) {
83d2ba10a9SRuslan Ermilov 			(void)snprintf(buf, sizeof(buf), "%s/%s", _PATH_MAILDIR, *argv);
84338fc8ebSJordan K. Hubbard 			file  = buf;
85338fc8ebSJordan K. Hubbard 		} else {
86338fc8ebSJordan K. Hubbard 			if (!(file = getenv("MAIL"))) {
87f589c9aaSPhilippe Charnier 				if (!(pwd = getpwuid(getuid())))
88f589c9aaSPhilippe Charnier 					errx(1, "no password file entry for you");
899b50d902SRodney W. Grimes 				file = pwd->pw_name;
90d2ba10a9SRuslan Ermilov 				(void)snprintf(buf, sizeof(buf),
91338fc8ebSJordan K. Hubbard 				    "%s/%s", _PATH_MAILDIR, file);
929b50d902SRodney W. Grimes 				file = buf;
939b50d902SRodney W. Grimes 			}
94338fc8ebSJordan K. Hubbard 		}
95338fc8ebSJordan K. Hubbard 	}
9698d04b7cSWolfram Schneider 
9798d04b7cSWolfram Schneider 	/* read from stdin */
9898d04b7cSWolfram Schneider 	if (strcmp(file, "-") == 0) {
99c948c058SJuli Mallett 		mbox = stdin;
10098d04b7cSWolfram Schneider 	}
101c948c058SJuli Mallett 	else if ((mbox = fopen(file, "r")) == NULL) {
102f589c9aaSPhilippe Charnier 		errx(1, "can't read %s", file);
1039b50d902SRodney W. Grimes 	}
104c948c058SJuli Mallett 	for (newline = 1; fgets(buf, sizeof(buf), mbox);) {
1059b50d902SRodney W. Grimes 		if (*buf == '\n') {
1069b50d902SRodney W. Grimes 			newline = 1;
1079b50d902SRodney W. Grimes 			continue;
1089b50d902SRodney W. Grimes 		}
1099b50d902SRodney W. Grimes 		if (newline && !strncmp(buf, "From ", 5) &&
11082753326SBrian Feldman 		    (!sender || match(buf + 5, sender))) {
11182753326SBrian Feldman 			if (count != -1)
11282753326SBrian Feldman 				count++;
11382753326SBrian Feldman 			else
1149b50d902SRodney W. Grimes 				printf("%s", buf);
11582753326SBrian Feldman 		}
1169b50d902SRodney W. Grimes 		newline = 0;
1179b50d902SRodney W. Grimes 	}
11882753326SBrian Feldman 	if (count != -1)
11982753326SBrian Feldman 		printf("There %s %d message%s in your incoming mailbox.\n",
12082753326SBrian Feldman 		    count == 1 ? "is" : "are", count, count == 1 ? "" : "s");
121c948c058SJuli Mallett 	fclose(mbox);
1229b50d902SRodney W. Grimes 	exit(0);
1239b50d902SRodney W. Grimes }
1249b50d902SRodney W. Grimes 
125f589c9aaSPhilippe Charnier static void
usage(void)126c948c058SJuli Mallett usage(void)
127f589c9aaSPhilippe Charnier {
12882753326SBrian Feldman 	fprintf(stderr, "usage: from [-c] [-f file] [-s sender] [user]\n");
129f589c9aaSPhilippe Charnier 	exit(1);
130f589c9aaSPhilippe Charnier }
131f589c9aaSPhilippe Charnier 
1321e7652ccSEitan Adler static int
match(const char * line,const char * sender)133f4ac32deSDavid Malone match(const char *line, const char *sender)
1349b50d902SRodney W. Grimes {
135f4ac32deSDavid Malone 	char ch, pch, first;
136f4ac32deSDavid Malone 	const char *p, *t;
1379b50d902SRodney W. Grimes 
1389b50d902SRodney W. Grimes 	for (first = *sender++;;) {
1399b50d902SRodney W. Grimes 		if (isspace(ch = *line))
1409b50d902SRodney W. Grimes 			return(0);
1419b50d902SRodney W. Grimes 		++line;
1429b50d902SRodney W. Grimes 		ch = tolower(ch);
1439b50d902SRodney W. Grimes 		if (ch != first)
1449b50d902SRodney W. Grimes 			continue;
1459b50d902SRodney W. Grimes 		for (p = sender, t = line;;) {
1469b50d902SRodney W. Grimes 			if (!(pch = *p++))
1479b50d902SRodney W. Grimes 				return(1);
1481e7652ccSEitan Adler 			ch = tolower(*t);
1491e7652ccSEitan Adler 			t++;
1509b50d902SRodney W. Grimes 			if (ch != pch)
1519b50d902SRodney W. Grimes 				break;
1529b50d902SRodney W. Grimes 		}
1539b50d902SRodney W. Grimes 	}
1549b50d902SRodney W. Grimes 	/* NOTREACHED */
1559b50d902SRodney W. Grimes }
156