xref: /titanic_53/usr/src/ucbcmd/from/from.c (revision 7c478bd95313f5f23a4c958a745db2134aa03244)
1*7c478bd9Sstevel@tonic-gate /*
2*7c478bd9Sstevel@tonic-gate  * Copyright 1994 Sun Microsystems, Inc.  All rights reserved.
3*7c478bd9Sstevel@tonic-gate  * Use is subject to license terms.
4*7c478bd9Sstevel@tonic-gate  */
5*7c478bd9Sstevel@tonic-gate 
6*7c478bd9Sstevel@tonic-gate /*
7*7c478bd9Sstevel@tonic-gate  * Copyright (c) 1980 Regents of the University of California.
8*7c478bd9Sstevel@tonic-gate  * All rights reserved.  The Berkeley software License Agreement
9*7c478bd9Sstevel@tonic-gate  * specifies the terms and conditions for redistribution.
10*7c478bd9Sstevel@tonic-gate  */
11*7c478bd9Sstevel@tonic-gate 
12*7c478bd9Sstevel@tonic-gate #pragma ident	"%Z%%M%	%I%	%E% SMI"
13*7c478bd9Sstevel@tonic-gate 
14*7c478bd9Sstevel@tonic-gate #include <stdio.h>
15*7c478bd9Sstevel@tonic-gate #include <sys/types.h>
16*7c478bd9Sstevel@tonic-gate #include <unistd.h>
17*7c478bd9Sstevel@tonic-gate #include <stdlib.h>
18*7c478bd9Sstevel@tonic-gate #include <ctype.h>
19*7c478bd9Sstevel@tonic-gate #include <pwd.h>
20*7c478bd9Sstevel@tonic-gate #include <sys/param.h>
21*7c478bd9Sstevel@tonic-gate #include <string.h>
22*7c478bd9Sstevel@tonic-gate 
23*7c478bd9Sstevel@tonic-gate static int match(char *, char *);
24*7c478bd9Sstevel@tonic-gate 
25*7c478bd9Sstevel@tonic-gate main(argc, argv)
26*7c478bd9Sstevel@tonic-gate 	int argc;
27*7c478bd9Sstevel@tonic-gate 	register char **argv;
28*7c478bd9Sstevel@tonic-gate {
29*7c478bd9Sstevel@tonic-gate 	char lbuf[BUFSIZ];
30*7c478bd9Sstevel@tonic-gate 	char lbuf2[BUFSIZ];
31*7c478bd9Sstevel@tonic-gate 	register struct passwd *pp;
32*7c478bd9Sstevel@tonic-gate 	int stashed = 0;
33*7c478bd9Sstevel@tonic-gate 	register char *name;
34*7c478bd9Sstevel@tonic-gate 	char *sender = NULL;
35*7c478bd9Sstevel@tonic-gate 	char mailbox[MAXPATHLEN];
36*7c478bd9Sstevel@tonic-gate 	char *tmp_mailbox;
37*7c478bd9Sstevel@tonic-gate 	extern char *optarg;
38*7c478bd9Sstevel@tonic-gate 	extern int optind;
39*7c478bd9Sstevel@tonic-gate 	extern int opterr;
40*7c478bd9Sstevel@tonic-gate 	int c;
41*7c478bd9Sstevel@tonic-gate 	int errflg = 0;
42*7c478bd9Sstevel@tonic-gate 
43*7c478bd9Sstevel@tonic-gate 	opterr = 0;
44*7c478bd9Sstevel@tonic-gate 	while ((c = getopt(argc, argv, "s:")) != EOF)
45*7c478bd9Sstevel@tonic-gate 		switch (c) {
46*7c478bd9Sstevel@tonic-gate 		case 's':
47*7c478bd9Sstevel@tonic-gate 			sender = optarg;
48*7c478bd9Sstevel@tonic-gate 			for (name = sender; *name; name++)
49*7c478bd9Sstevel@tonic-gate 				if (isupper(*name))
50*7c478bd9Sstevel@tonic-gate 					*name = tolower(*name);
51*7c478bd9Sstevel@tonic-gate 			break;
52*7c478bd9Sstevel@tonic-gate 		case '?':
53*7c478bd9Sstevel@tonic-gate 			errflg++;
54*7c478bd9Sstevel@tonic-gate 			break;
55*7c478bd9Sstevel@tonic-gate 		}
56*7c478bd9Sstevel@tonic-gate 	if (errflg) {
57*7c478bd9Sstevel@tonic-gate 		(void) fprintf(stderr,
58*7c478bd9Sstevel@tonic-gate 			    "Usage: from [-s sender] [user]\n");
59*7c478bd9Sstevel@tonic-gate 		exit(1);
60*7c478bd9Sstevel@tonic-gate 	}
61*7c478bd9Sstevel@tonic-gate 
62*7c478bd9Sstevel@tonic-gate 	if (optind < argc) {
63*7c478bd9Sstevel@tonic-gate 		(void) sprintf(mailbox, "/var/mail/%s", argv[optind]);
64*7c478bd9Sstevel@tonic-gate 	} else {
65*7c478bd9Sstevel@tonic-gate 		if (tmp_mailbox = getenv("MAIL")) {
66*7c478bd9Sstevel@tonic-gate 			(void) strcpy(mailbox, tmp_mailbox);
67*7c478bd9Sstevel@tonic-gate 		} else {
68*7c478bd9Sstevel@tonic-gate 			name = getlogin();
69*7c478bd9Sstevel@tonic-gate 			if (name == NULL || strlen(name) == 0) {
70*7c478bd9Sstevel@tonic-gate 				pp = getpwuid(getuid());
71*7c478bd9Sstevel@tonic-gate 				if (pp == NULL) {
72*7c478bd9Sstevel@tonic-gate 					(void) fprintf(stderr,
73*7c478bd9Sstevel@tonic-gate 					    "Who are you?\n");
74*7c478bd9Sstevel@tonic-gate 					exit(1);
75*7c478bd9Sstevel@tonic-gate 				}
76*7c478bd9Sstevel@tonic-gate 				name = pp->pw_name;
77*7c478bd9Sstevel@tonic-gate 			}
78*7c478bd9Sstevel@tonic-gate 			(void) sprintf(mailbox, "/var/mail/%s", name);
79*7c478bd9Sstevel@tonic-gate 		}
80*7c478bd9Sstevel@tonic-gate 	}
81*7c478bd9Sstevel@tonic-gate 	if (freopen(mailbox, "r", stdin) == NULL) {
82*7c478bd9Sstevel@tonic-gate 		(void) fprintf(stderr, "Can't open %s\n", mailbox);
83*7c478bd9Sstevel@tonic-gate 		exit(0);
84*7c478bd9Sstevel@tonic-gate 	}
85*7c478bd9Sstevel@tonic-gate 	while (fgets(lbuf, sizeof (lbuf), stdin) != NULL)
86*7c478bd9Sstevel@tonic-gate 		if (lbuf[0] == '\n' && stashed) {
87*7c478bd9Sstevel@tonic-gate 			stashed = 0;
88*7c478bd9Sstevel@tonic-gate 			(void) printf("%s", lbuf2);
89*7c478bd9Sstevel@tonic-gate 		} else if (strncmp(lbuf, "From ", 5) == 0 &&
90*7c478bd9Sstevel@tonic-gate 		    (sender == NULL || match(&lbuf[4], sender))) {
91*7c478bd9Sstevel@tonic-gate 			(void) strcpy(lbuf2, lbuf);
92*7c478bd9Sstevel@tonic-gate 			stashed = 1;
93*7c478bd9Sstevel@tonic-gate 		}
94*7c478bd9Sstevel@tonic-gate 	if (stashed)
95*7c478bd9Sstevel@tonic-gate 		(void) printf("%s", lbuf2);
96*7c478bd9Sstevel@tonic-gate 	return(0);
97*7c478bd9Sstevel@tonic-gate }
98*7c478bd9Sstevel@tonic-gate 
99*7c478bd9Sstevel@tonic-gate static int
100*7c478bd9Sstevel@tonic-gate match(line, str)
101*7c478bd9Sstevel@tonic-gate 	register char *line, *str;
102*7c478bd9Sstevel@tonic-gate {
103*7c478bd9Sstevel@tonic-gate 	register char ch;
104*7c478bd9Sstevel@tonic-gate 
105*7c478bd9Sstevel@tonic-gate 	while (*line == ' ' || *line == '\t')
106*7c478bd9Sstevel@tonic-gate 		++line;
107*7c478bd9Sstevel@tonic-gate 	if (*line == '\n')
108*7c478bd9Sstevel@tonic-gate 		return (0);
109*7c478bd9Sstevel@tonic-gate 	while (*str && *line != ' ' && *line != '\t' && *line != '\n') {
110*7c478bd9Sstevel@tonic-gate 		ch = isupper(*line) ? tolower(*line) : *line;
111*7c478bd9Sstevel@tonic-gate 		if (ch != *str++)
112*7c478bd9Sstevel@tonic-gate 			return (0);
113*7c478bd9Sstevel@tonic-gate 		line++;
114*7c478bd9Sstevel@tonic-gate 	}
115*7c478bd9Sstevel@tonic-gate 	return (*str == '\0');
116*7c478bd9Sstevel@tonic-gate }
117