from.c revision 1590
11590Srgrimes/*
21590Srgrimes * Copyright (c) 1980, 1988, 1993
31590Srgrimes *	The Regents of the University of California.  All rights reserved.
41590Srgrimes *
51590Srgrimes * Redistribution and use in source and binary forms, with or without
61590Srgrimes * modification, are permitted provided that the following conditions
71590Srgrimes * are met:
81590Srgrimes * 1. Redistributions of source code must retain the above copyright
91590Srgrimes *    notice, this list of conditions and the following disclaimer.
101590Srgrimes * 2. Redistributions in binary form must reproduce the above copyright
111590Srgrimes *    notice, this list of conditions and the following disclaimer in the
121590Srgrimes *    documentation and/or other materials provided with the distribution.
131590Srgrimes * 3. All advertising materials mentioning features or use of this software
141590Srgrimes *    must display the following acknowledgement:
151590Srgrimes *	This product includes software developed by the University of
161590Srgrimes *	California, Berkeley and its contributors.
171590Srgrimes * 4. Neither the name of the University nor the names of its contributors
181590Srgrimes *    may be used to endorse or promote products derived from this software
191590Srgrimes *    without specific prior written permission.
201590Srgrimes *
211590Srgrimes * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
221590Srgrimes * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
231590Srgrimes * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
241590Srgrimes * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
251590Srgrimes * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
261590Srgrimes * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
271590Srgrimes * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
281590Srgrimes * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
291590Srgrimes * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
301590Srgrimes * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
311590Srgrimes * SUCH DAMAGE.
321590Srgrimes */
331590Srgrimes
341590Srgrimes#ifndef lint
351590Srgrimesstatic char copyright[] =
361590Srgrimes"@(#) Copyright (c) 1980, 1988, 1993\n\
371590Srgrimes	The Regents of the University of California.  All rights reserved.\n";
381590Srgrimes#endif /* not lint */
391590Srgrimes
401590Srgrimes#ifndef lint
411590Srgrimesstatic char sccsid[] = "@(#)from.c	8.1 (Berkeley) 6/6/93";
421590Srgrimes#endif /* not lint */
431590Srgrimes
441590Srgrimes#include <sys/types.h>
451590Srgrimes#include <ctype.h>
461590Srgrimes#include <pwd.h>
471590Srgrimes#include <stdio.h>
481590Srgrimes#include <paths.h>
491590Srgrimes
501590Srgrimesmain(argc, argv)
511590Srgrimes	int argc;
521590Srgrimes	char **argv;
531590Srgrimes{
541590Srgrimes	extern char *optarg;
551590Srgrimes	extern int optind;
561590Srgrimes	struct passwd *pwd;
571590Srgrimes	int ch, newline;
581590Srgrimes	char *file, *sender, *p;
591590Srgrimes#if MAXPATHLEN > BUFSIZ
601590Srgrimes	char buf[MAXPATHLEN];
611590Srgrimes#else
621590Srgrimes	char buf[BUFSIZ];
631590Srgrimes#endif
641590Srgrimes
651590Srgrimes	file = sender = NULL;
661590Srgrimes	while ((ch = getopt(argc, argv, "f:s:")) != EOF)
671590Srgrimes		switch((char)ch) {
681590Srgrimes		case 'f':
691590Srgrimes			file = optarg;
701590Srgrimes			break;
711590Srgrimes		case 's':
721590Srgrimes			sender = optarg;
731590Srgrimes			for (p = sender; *p; ++p)
741590Srgrimes				if (isupper(*p))
751590Srgrimes					*p = tolower(*p);
761590Srgrimes			break;
771590Srgrimes		case '?':
781590Srgrimes		default:
791590Srgrimes			fprintf(stderr, "usage: from [-f file] [-s sender] [user]\n");
801590Srgrimes			exit(1);
811590Srgrimes		}
821590Srgrimes	argv += optind;
831590Srgrimes
841590Srgrimes	if (!file) {
851590Srgrimes		if (!(file = *argv)) {
861590Srgrimes			if (!(pwd = getpwuid(getuid()))) {
871590Srgrimes				fprintf(stderr,
881590Srgrimes				    "from: no password file entry for you.\n");
891590Srgrimes				exit(1);
901590Srgrimes			}
911590Srgrimes			file = pwd->pw_name;
921590Srgrimes		}
931590Srgrimes		(void)sprintf(buf, "%s/%s", _PATH_MAILDIR, file);
941590Srgrimes		file = buf;
951590Srgrimes	}
961590Srgrimes	if (!freopen(file, "r", stdin)) {
971590Srgrimes		fprintf(stderr, "from: can't read %s.\n", file);
981590Srgrimes		exit(1);
991590Srgrimes	}
1001590Srgrimes	for (newline = 1; fgets(buf, sizeof(buf), stdin);) {
1011590Srgrimes		if (*buf == '\n') {
1021590Srgrimes			newline = 1;
1031590Srgrimes			continue;
1041590Srgrimes		}
1051590Srgrimes		if (newline && !strncmp(buf, "From ", 5) &&
1061590Srgrimes		    (!sender || match(buf + 5, sender)))
1071590Srgrimes			printf("%s", buf);
1081590Srgrimes		newline = 0;
1091590Srgrimes	}
1101590Srgrimes	exit(0);
1111590Srgrimes}
1121590Srgrimes
1131590Srgrimesmatch(line, sender)
1141590Srgrimes	register char *line, *sender;
1151590Srgrimes{
1161590Srgrimes	register char ch, pch, first, *p, *t;
1171590Srgrimes
1181590Srgrimes	for (first = *sender++;;) {
1191590Srgrimes		if (isspace(ch = *line))
1201590Srgrimes			return(0);
1211590Srgrimes		++line;
1221590Srgrimes		if (isupper(ch))
1231590Srgrimes			ch = tolower(ch);
1241590Srgrimes		if (ch != first)
1251590Srgrimes			continue;
1261590Srgrimes		for (p = sender, t = line;;) {
1271590Srgrimes			if (!(pch = *p++))
1281590Srgrimes				return(1);
1291590Srgrimes			if (isupper(ch = *t++))
1301590Srgrimes				ch = tolower(ch);
1311590Srgrimes			if (ch != pch)
1321590Srgrimes				break;
1331590Srgrimes		}
1341590Srgrimes	}
1351590Srgrimes	/* NOTREACHED */
1361590Srgrimes}
137