xref: /csrg-svn/usr.bin/from/from.c (revision 17015)
1*17015Ssam static char *sccsid = "@(#)from.c	4.2 (Berkeley) 08/25/84";
2*17015Ssam 
31020Sbill #include <stdio.h>
41020Sbill #include <ctype.h>
51020Sbill #include <pwd.h>
61020Sbill 
71020Sbill struct	passwd *getpwuid();
81020Sbill 
91020Sbill main(argc, argv)
10*17015Ssam 	int argc;
11*17015Ssam 	register char **argv;
121020Sbill {
131020Sbill 	char lbuf[BUFSIZ];
141020Sbill 	char lbuf2[BUFSIZ];
151020Sbill 	register struct passwd *pp;
161020Sbill 	int stashed = 0;
171020Sbill 	register char *name;
181020Sbill 	char *sender;
191020Sbill 	char *getlogin();
201020Sbill 
211020Sbill 	if (argc > 1 && *(argv[1]) == '-' && (*++argv)[1] == 's') {
221020Sbill 		if (--argc <= 1) {
231020Sbill 			fprintf (stderr, "Usage: from [-s sender] [user]\n");
241020Sbill 			exit (1);
251020Sbill 		}
261020Sbill 		--argc;
271020Sbill 		sender = *++argv;
281020Sbill 		for (name = sender; *name; name++)
291020Sbill 			if (isupper(*name))
301020Sbill 				*name = tolower(*name);
311020Sbill 
32*17015Ssam 	} else
331020Sbill 		sender = NULL;
341020Sbill 	if (chdir("/usr/spool/mail") < 0)
351020Sbill 		exit(1);
361020Sbill 	if (argc > 1)
371020Sbill 		name = argv[1];
381020Sbill 	else {
391020Sbill 		name = getlogin ();
401020Sbill 		if (name == NULL || strlen(name) == 0) {
411020Sbill 			pp = getpwuid(getuid());
421020Sbill 			if (pp == NULL) {
431020Sbill 				fprintf(stderr, "Who are you?\n");
441020Sbill 				exit(1);
451020Sbill 			}
461020Sbill 			name = pp->pw_name;
471020Sbill 		}
481020Sbill 	}
491020Sbill 	if (freopen(name, "r", stdin) == NULL)
501020Sbill 		exit(0);
51*17015Ssam 	while (fgets(lbuf, sizeof lbuf, stdin) != NULL)
521020Sbill 		if (lbuf[0] == '\n' && stashed) {
531020Sbill 			stashed = 0;
541020Sbill 			printf("%s", lbuf2);
55*17015Ssam 		} else if (strncmp(lbuf, "From ", 5) == 0 &&
561020Sbill 		    (sender == NULL || match(&lbuf[4], sender))) {
571020Sbill 			strcpy(lbuf2, lbuf);
581020Sbill 			stashed = 1;
591020Sbill 		}
601020Sbill 	if (stashed)
611020Sbill 		printf("%s", lbuf2);
621020Sbill 	exit(0);
631020Sbill }
641020Sbill 
651020Sbill match (line, str)
66*17015Ssam 	register char *line, *str;
671020Sbill {
681020Sbill 	register char ch;
691020Sbill 
701020Sbill 	while (*line == ' ' || *line == '\t')
711020Sbill 		++line;
721020Sbill 	if (*line == '\n')
731020Sbill 		return (0);
741020Sbill 	while (*str && *line != ' ' && *line != '\t' && *line != '\n') {
751020Sbill 		ch = isupper(*line) ? tolower(*line) : *line;
761020Sbill 		if (ch != *str++)
771020Sbill 			return (0);
781020Sbill 		line++;
791020Sbill 	}
801020Sbill 	return (*str == '\0');
811020Sbill }
82