]> CyberLeo.Net >> Repos - FreeBSD/releng/9.2.git/blob - usr.bin/from/from.c
- Copy stable/9 to releng/9.2 as part of the 9.2-RELEASE cycle.
[FreeBSD/releng/9.2.git] / usr.bin / from / from.c
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  * 4. 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
30 #ifndef lint
31 static const char copyright[] =
32 "@(#) Copyright (c) 1980, 1988, 1993\n\
33         The Regents of the University of California.  All rights reserved.\n";
34 #endif /* not lint */
35
36 #ifndef lint
37 #if 0
38 static char sccsid[] = "@(#)from.c      8.1 (Berkeley) 6/6/93";
39 #endif
40 #endif /* not lint */
41 #include <sys/cdefs.h>
42 __FBSDID("$FreeBSD$");
43
44 #include <sys/types.h>
45 #include <ctype.h>
46 #include <err.h>
47 #include <pwd.h>
48 #include <stdio.h>
49 #include <stdlib.h>
50 #include <paths.h>
51 #include <string.h>
52 #include <unistd.h>
53
54 int match(const char *, const char *);
55 static void usage(void);
56
57 int
58 main(int argc, char **argv)
59 {
60         FILE *mbox;
61         struct passwd *pwd;
62         int ch, count, newline;
63         const char *file;
64         char *sender, *p;
65 #if MAXPATHLEN > BUFSIZ
66         char buf[MAXPATHLEN];
67 #else
68         char buf[BUFSIZ];
69 #endif
70
71         file = sender = NULL;
72         count = -1;
73         while ((ch = getopt(argc, argv, "cf:s:")) != -1)
74                 switch (ch) {
75                 case 'c':
76                         count = 0;
77                         break;
78                 case 'f':
79                         file = optarg;
80                         break;
81                 case 's':
82                         sender = optarg;
83                         for (p = sender; *p; ++p)
84                                 if (isupper(*p))
85                                         *p = tolower(*p);
86                         break;
87                 case '?':
88                 default:
89                         usage();
90                 }
91         argc -= optind;
92         argv += optind;
93
94         if (file == NULL) {
95                 if (argc) {
96                         (void)snprintf(buf, sizeof(buf), "%s/%s", _PATH_MAILDIR, *argv);
97                         file  = buf;
98                 } else {
99                         if (!(file = getenv("MAIL"))) {
100                                 if (!(pwd = getpwuid(getuid())))
101                                         errx(1, "no password file entry for you");
102                                 file = pwd->pw_name;
103                                 (void)snprintf(buf, sizeof(buf),
104                                     "%s/%s", _PATH_MAILDIR, file);
105                                 file = buf;
106                         }
107                 }
108         }
109
110         /* read from stdin */
111         if (strcmp(file, "-") == 0) {
112                 mbox = stdin;
113         } 
114         else if ((mbox = fopen(file, "r")) == NULL) {
115                 errx(1, "can't read %s", file);
116         }
117         for (newline = 1; fgets(buf, sizeof(buf), mbox);) {
118                 if (*buf == '\n') {
119                         newline = 1;
120                         continue;
121                 }
122                 if (newline && !strncmp(buf, "From ", 5) &&
123                     (!sender || match(buf + 5, sender))) {
124                         if (count != -1)
125                                 count++;
126                         else
127                                 printf("%s", buf);
128                 }
129                 newline = 0;
130         }
131         if (count != -1)
132                 printf("There %s %d message%s in your incoming mailbox.\n",
133                     count == 1 ? "is" : "are", count, count == 1 ? "" : "s"); 
134         fclose(mbox);
135         exit(0);
136 }
137
138 static void
139 usage(void)
140 {
141         fprintf(stderr, "usage: from [-c] [-f file] [-s sender] [user]\n");
142         exit(1);
143 }
144
145 int
146 match(const char *line, const char *sender)
147 {
148         char ch, pch, first;
149         const char *p, *t;
150
151         for (first = *sender++;;) {
152                 if (isspace(ch = *line))
153                         return(0);
154                 ++line;
155                 if (isupper(ch))
156                         ch = tolower(ch);
157                 if (ch != first)
158                         continue;
159                 for (p = sender, t = line;;) {
160                         if (!(pch = *p++))
161                                 return(1);
162                         if (isupper(ch = *t++))
163                                 ch = tolower(ch);
164                         if (ch != pch)
165                                 break;
166                 }
167         }
168         /* NOTREACHED */
169 }