xref: /illumos-gate/usr/src/ucbcmd/from/from.c (revision 8c0b080c)
17c478bd9Sstevel@tonic-gate /*
2*032624d5Sbasabi  * Copyright 2005 Sun Microsystems, Inc.  All rights reserved.
37c478bd9Sstevel@tonic-gate  * Use is subject to license terms.
47c478bd9Sstevel@tonic-gate  */
57c478bd9Sstevel@tonic-gate 
67c478bd9Sstevel@tonic-gate /*
77c478bd9Sstevel@tonic-gate  * Copyright (c) 1980 Regents of the University of California.
87c478bd9Sstevel@tonic-gate  * All rights reserved.  The Berkeley software License Agreement
97c478bd9Sstevel@tonic-gate  * specifies the terms and conditions for redistribution.
107c478bd9Sstevel@tonic-gate  */
117c478bd9Sstevel@tonic-gate 
127c478bd9Sstevel@tonic-gate #include <stdio.h>
137c478bd9Sstevel@tonic-gate #include <sys/types.h>
147c478bd9Sstevel@tonic-gate #include <unistd.h>
157c478bd9Sstevel@tonic-gate #include <stdlib.h>
167c478bd9Sstevel@tonic-gate #include <ctype.h>
177c478bd9Sstevel@tonic-gate #include <pwd.h>
187c478bd9Sstevel@tonic-gate #include <sys/param.h>
197c478bd9Sstevel@tonic-gate #include <string.h>
207c478bd9Sstevel@tonic-gate 
217c478bd9Sstevel@tonic-gate static int match(char *, char *);
227c478bd9Sstevel@tonic-gate 
23*032624d5Sbasabi int
main(int argc,char ** argv)24*032624d5Sbasabi main(int argc, char **argv)
257c478bd9Sstevel@tonic-gate {
267c478bd9Sstevel@tonic-gate 	char lbuf[BUFSIZ];
277c478bd9Sstevel@tonic-gate 	char lbuf2[BUFSIZ];
28*032624d5Sbasabi 	struct passwd *pp;
297c478bd9Sstevel@tonic-gate 	int stashed = 0;
30*032624d5Sbasabi 	char *name;
317c478bd9Sstevel@tonic-gate 	char *sender = NULL;
327c478bd9Sstevel@tonic-gate 	char mailbox[MAXPATHLEN];
337c478bd9Sstevel@tonic-gate 	char *tmp_mailbox;
347c478bd9Sstevel@tonic-gate 	extern char *optarg;
357c478bd9Sstevel@tonic-gate 	extern int optind;
367c478bd9Sstevel@tonic-gate 	extern int opterr;
377c478bd9Sstevel@tonic-gate 	int c;
387c478bd9Sstevel@tonic-gate 	int errflg = 0;
397c478bd9Sstevel@tonic-gate 
407c478bd9Sstevel@tonic-gate 	opterr = 0;
417c478bd9Sstevel@tonic-gate 	while ((c = getopt(argc, argv, "s:")) != EOF)
427c478bd9Sstevel@tonic-gate 		switch (c) {
437c478bd9Sstevel@tonic-gate 		case 's':
447c478bd9Sstevel@tonic-gate 			sender = optarg;
457c478bd9Sstevel@tonic-gate 			for (name = sender; *name; name++)
467c478bd9Sstevel@tonic-gate 				if (isupper(*name))
477c478bd9Sstevel@tonic-gate 					*name = tolower(*name);
487c478bd9Sstevel@tonic-gate 			break;
497c478bd9Sstevel@tonic-gate 		case '?':
507c478bd9Sstevel@tonic-gate 			errflg++;
517c478bd9Sstevel@tonic-gate 			break;
527c478bd9Sstevel@tonic-gate 		}
537c478bd9Sstevel@tonic-gate 	if (errflg) {
547c478bd9Sstevel@tonic-gate 		(void) fprintf(stderr,
557c478bd9Sstevel@tonic-gate 			    "Usage: from [-s sender] [user]\n");
567c478bd9Sstevel@tonic-gate 		exit(1);
577c478bd9Sstevel@tonic-gate 	}
587c478bd9Sstevel@tonic-gate 
597c478bd9Sstevel@tonic-gate 	if (optind < argc) {
607c478bd9Sstevel@tonic-gate 		(void) sprintf(mailbox, "/var/mail/%s", argv[optind]);
617c478bd9Sstevel@tonic-gate 	} else {
627c478bd9Sstevel@tonic-gate 		if (tmp_mailbox = getenv("MAIL")) {
637c478bd9Sstevel@tonic-gate 			(void) strcpy(mailbox, tmp_mailbox);
647c478bd9Sstevel@tonic-gate 		} else {
657c478bd9Sstevel@tonic-gate 			name = getlogin();
667c478bd9Sstevel@tonic-gate 			if (name == NULL || strlen(name) == 0) {
677c478bd9Sstevel@tonic-gate 				pp = getpwuid(getuid());
687c478bd9Sstevel@tonic-gate 				if (pp == NULL) {
697c478bd9Sstevel@tonic-gate 					(void) fprintf(stderr,
707c478bd9Sstevel@tonic-gate 					    "Who are you?\n");
717c478bd9Sstevel@tonic-gate 					exit(1);
727c478bd9Sstevel@tonic-gate 				}
737c478bd9Sstevel@tonic-gate 				name = pp->pw_name;
747c478bd9Sstevel@tonic-gate 			}
757c478bd9Sstevel@tonic-gate 			(void) sprintf(mailbox, "/var/mail/%s", name);
767c478bd9Sstevel@tonic-gate 		}
777c478bd9Sstevel@tonic-gate 	}
787c478bd9Sstevel@tonic-gate 	if (freopen(mailbox, "r", stdin) == NULL) {
797c478bd9Sstevel@tonic-gate 		(void) fprintf(stderr, "Can't open %s\n", mailbox);
807c478bd9Sstevel@tonic-gate 		exit(0);
817c478bd9Sstevel@tonic-gate 	}
827c478bd9Sstevel@tonic-gate 	while (fgets(lbuf, sizeof (lbuf), stdin) != NULL)
837c478bd9Sstevel@tonic-gate 		if (lbuf[0] == '\n' && stashed) {
847c478bd9Sstevel@tonic-gate 			stashed = 0;
857c478bd9Sstevel@tonic-gate 			(void) printf("%s", lbuf2);
867c478bd9Sstevel@tonic-gate 		} else if (strncmp(lbuf, "From ", 5) == 0 &&
877c478bd9Sstevel@tonic-gate 		    (sender == NULL || match(&lbuf[4], sender))) {
887c478bd9Sstevel@tonic-gate 			(void) strcpy(lbuf2, lbuf);
897c478bd9Sstevel@tonic-gate 			stashed = 1;
907c478bd9Sstevel@tonic-gate 		}
917c478bd9Sstevel@tonic-gate 	if (stashed)
927c478bd9Sstevel@tonic-gate 		(void) printf("%s", lbuf2);
93*032624d5Sbasabi 	return (0);
947c478bd9Sstevel@tonic-gate }
957c478bd9Sstevel@tonic-gate 
967c478bd9Sstevel@tonic-gate static int
match(char * line,char * str)97*032624d5Sbasabi match(char *line, char *str)
987c478bd9Sstevel@tonic-gate {
99*032624d5Sbasabi 	char ch;
1007c478bd9Sstevel@tonic-gate 
1017c478bd9Sstevel@tonic-gate 	while (*line == ' ' || *line == '\t')
1027c478bd9Sstevel@tonic-gate 		++line;
1037c478bd9Sstevel@tonic-gate 	if (*line == '\n')
1047c478bd9Sstevel@tonic-gate 		return (0);
1057c478bd9Sstevel@tonic-gate 	while (*str && *line != ' ' && *line != '\t' && *line != '\n') {
1067c478bd9Sstevel@tonic-gate 		ch = isupper(*line) ? tolower(*line) : *line;
1077c478bd9Sstevel@tonic-gate 		if (ch != *str++)
1087c478bd9Sstevel@tonic-gate 			return (0);
1097c478bd9Sstevel@tonic-gate 		line++;
1107c478bd9Sstevel@tonic-gate 	}
1117c478bd9Sstevel@tonic-gate 	return (*str == '\0');
1127c478bd9Sstevel@tonic-gate }
113