xref: /csrg-svn/usr.bin/from/from.c (revision 62003)
121556Sdist /*
2*62003Sbostic  * Copyright (c) 1980, 1988, 1993
3*62003Sbostic  *	The Regents of the University of California.  All rights reserved.
435750Sbostic  *
542731Sbostic  * %sccs.include.redist.c%
621556Sdist  */
717015Ssam 
821556Sdist #ifndef lint
9*62003Sbostic static char copyright[] =
10*62003Sbostic "@(#) Copyright (c) 1980, 1988, 1993\n\
11*62003Sbostic 	The Regents of the University of California.  All rights reserved.\n";
1235750Sbostic #endif /* not lint */
1321556Sdist 
1421556Sdist #ifndef lint
15*62003Sbostic static char sccsid[] = "@(#)from.c	8.1 (Berkeley) 06/06/93";
1635750Sbostic #endif /* not lint */
1721556Sdist 
1837074Sbostic #include <sys/types.h>
191020Sbill #include <ctype.h>
201020Sbill #include <pwd.h>
2135750Sbostic #include <stdio.h>
2244936Sbostic #include <paths.h>
231020Sbill 
main(argc,argv)241020Sbill main(argc, argv)
2517015Ssam 	int argc;
2635750Sbostic 	char **argv;
271020Sbill {
2835750Sbostic 	extern char *optarg;
2935750Sbostic 	extern int optind;
3046827Sbostic 	struct passwd *pwd;
3135750Sbostic 	int ch, newline;
3235750Sbostic 	char *file, *sender, *p;
3335750Sbostic #if MAXPATHLEN > BUFSIZ
3435750Sbostic 	char buf[MAXPATHLEN];
3535750Sbostic #else
3635750Sbostic 	char buf[BUFSIZ];
3735750Sbostic #endif
381020Sbill 
3935750Sbostic 	file = sender = NULL;
4035750Sbostic 	while ((ch = getopt(argc, argv, "f:s:")) != EOF)
4135750Sbostic 		switch((char)ch) {
4235750Sbostic 		case 'f':
4335750Sbostic 			file = optarg;
4435750Sbostic 			break;
4535750Sbostic 		case 's':
4635750Sbostic 			sender = optarg;
4735750Sbostic 			for (p = sender; *p; ++p)
4835750Sbostic 				if (isupper(*p))
4935750Sbostic 					*p = tolower(*p);
5035750Sbostic 			break;
5135750Sbostic 		case '?':
5235750Sbostic 		default:
5335750Sbostic 			fprintf(stderr, "usage: from [-f file] [-s sender] [user]\n");
5435750Sbostic 			exit(1);
551020Sbill 		}
5635750Sbostic 	argv += optind;
571020Sbill 
5835750Sbostic 	if (!file) {
5935750Sbostic 		if (!(file = *argv)) {
6035750Sbostic 			if (!(pwd = getpwuid(getuid()))) {
6135750Sbostic 				fprintf(stderr,
6235750Sbostic 				    "from: no password file entry for you.\n");
631020Sbill 				exit(1);
641020Sbill 			}
6535750Sbostic 			file = pwd->pw_name;
661020Sbill 		}
6744936Sbostic 		(void)sprintf(buf, "%s/%s", _PATH_MAILDIR, file);
6835750Sbostic 		file = buf;
691020Sbill 	}
7035750Sbostic 	if (!freopen(file, "r", stdin)) {
7135750Sbostic 		fprintf(stderr, "from: can't read %s.\n", file);
7235750Sbostic 		exit(1);
7325381Sbloom 	}
7435750Sbostic 	for (newline = 1; fgets(buf, sizeof(buf), stdin);) {
7535750Sbostic 		if (*buf == '\n') {
7635750Sbostic 			newline = 1;
7735750Sbostic 			continue;
781020Sbill 		}
7935750Sbostic 		if (newline && !strncmp(buf, "From ", 5) &&
8035750Sbostic 		    (!sender || match(buf + 5, sender)))
8135750Sbostic 			printf("%s", buf);
8235750Sbostic 		newline = 0;
8335750Sbostic 	}
841020Sbill 	exit(0);
851020Sbill }
861020Sbill 
match(line,sender)8735750Sbostic match(line, sender)
8835750Sbostic 	register char *line, *sender;
891020Sbill {
9035750Sbostic 	register char ch, pch, first, *p, *t;
911020Sbill 
9235750Sbostic 	for (first = *sender++;;) {
9335750Sbostic 		if (isspace(ch = *line))
9435750Sbostic 			return(0);
951020Sbill 		++line;
9635750Sbostic 		if (isupper(ch))
9735750Sbostic 			ch = tolower(ch);
9835750Sbostic 		if (ch != first)
9935750Sbostic 			continue;
10035750Sbostic 		for (p = sender, t = line;;) {
10135750Sbostic 			if (!(pch = *p++))
10235750Sbostic 				return(1);
10335750Sbostic 			if (isupper(ch = *t++))
10435750Sbostic 				ch = tolower(ch);
10535750Sbostic 			if (ch != pch)
10635750Sbostic 				break;
10735750Sbostic 		}
1081020Sbill 	}
10935750Sbostic 	/* NOTREACHED */
1101020Sbill }
111