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