xref: /dragonfly/usr.bin/from/from.c (revision 26720ae0)
1 /*
2  * Copyright (c) 1980, 1988, 1993
3  *	The Regents of the University of California.  All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions
7  * are met:
8  * 1. Redistributions of source code must retain the above copyright
9  *    notice, this list of conditions and the following disclaimer.
10  * 2. Redistributions in binary form must reproduce the above copyright
11  *    notice, this list of conditions and the following disclaimer in the
12  *    documentation and/or other materials provided with the distribution.
13  * 3. Neither the name of the University nor the names of its contributors
14  *    may be used to endorse or promote products derived from this software
15  *    without specific prior written permission.
16  *
17  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
18  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
21  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
23  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
24  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
25  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
26  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
27  * SUCH DAMAGE.
28  *
29  * @(#) Copyright (c) 1980, 1988, 1993 The Regents of the University of California.  All rights reserved.
30  * @(#)from.c	8.1 (Berkeley) 6/6/93
31  * $FreeBSD: src/usr.bin/from/from.c,v 1.8.2.1 2000/10/06 08:19:19 ru Exp $
32  */
33 
34 #include <sys/param.h>
35 
36 #include <ctype.h>
37 #include <err.h>
38 #include <pwd.h>
39 #include <stdio.h>
40 #include <stdlib.h>
41 #include <paths.h>
42 #include <string.h>
43 #include <unistd.h>
44 
45 static int	match(char *, char *);
46 static void	usage(void);
47 
48 int
49 main(int argc, char **argv)
50 {
51 	struct passwd *pwd;
52 	int ch, count, newline;
53 	const char *file;
54 	char *sender, *p;
55 #if MAXPATHLEN > BUFSIZ
56 	char buf[MAXPATHLEN];
57 #else
58 	char buf[BUFSIZ];
59 #endif
60 
61 	file = sender = NULL;
62 	count = -1;
63 	while ((ch = getopt(argc, argv, "cf:s:")) != -1)
64 		switch (ch) {
65 		case 'c':
66 			count = 0;
67 			break;
68 		case 'f':
69 			file = optarg;
70 			break;
71 		case 's':
72 			sender = optarg;
73 			for (p = sender; *p; ++p)
74 				*p = tolower(*p);
75 			break;
76 		case '?':
77 		default:
78 			usage();
79 		}
80 	argv += optind;
81 
82 	if (file == NULL) {
83 		if (*argv) {
84 			snprintf(buf, sizeof(buf), "%s/%s", _PATH_MAILDIR, *argv);
85 			file  = buf;
86 		} else {
87 			if ((file = getenv("MAIL")) == NULL) {
88 				if (!(pwd = getpwuid(getuid())))
89 					errx(1, "no password file entry for you");
90 				file = pwd->pw_name;
91 				snprintf(buf, sizeof(buf),
92 				    "%s/%s", _PATH_MAILDIR, file);
93 				file = buf;
94 			}
95 		}
96 	}
97 
98 	/* read from stdin */
99 	if (strcmp(file, "-") == 0) {
100 	}
101 	else if (freopen(file, "r", stdin) == NULL) {
102 		err(1, "can't read %s", file);
103 	}
104 	for (newline = 1; fgets(buf, sizeof(buf), stdin);) {
105 		if (*buf == '\n') {
106 			newline = 1;
107 			continue;
108 		}
109 		if (newline && !strncmp(buf, "From ", 5) &&
110 		    (!sender || match(buf + 5, sender))) {
111 			if (count != -1)
112 				count++;
113 			else
114 				printf("%s", buf);
115 		}
116 		newline = 0;
117 	}
118 	if (count != -1)
119 		printf("There %s %d message%s in your incoming mailbox.\n",
120 		    count == 1 ? "is" : "are", count, count == 1 ? "" : "s");
121 	exit(0);
122 }
123 
124 static void
125 usage(void)
126 {
127 	fprintf(stderr, "usage: from [-c] [-f file] [-s sender] [user]\n");
128 	exit(1);
129 }
130 
131 static int
132 match(char *line, char *sender)
133 {
134 	char ch, pch, first, *p, *t;
135 
136 	for (first = *sender++;;) {
137 		if (isspace(ch = *line))
138 			return(0);
139 		++line;
140 		ch = tolower(ch);
141 		if (ch != first)
142 			continue;
143 		for (p = sender, t = line;;) {
144 			if (!(pch = *p++))
145 				return(1);
146 			ch = tolower(*t++);
147 			if (ch != pch)
148 				break;
149 		}
150 	}
151 	/* NOTREACHED */
152 }
153