2 * Copyright (c) 1980, 1988, 1993
3 * The Regents of the University of California. All rights reserved.
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
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.
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
31 static const char copyright[] =
32 "@(#) Copyright (c) 1980, 1988, 1993\n\
33 The Regents of the University of California. All rights reserved.\n";
38 static char sccsid[] = "@(#)from.c 8.1 (Berkeley) 6/6/93";
41 #include <sys/cdefs.h>
42 __FBSDID("$FreeBSD$");
44 #include <sys/types.h>
54 static int match(const char *, const char *);
55 static void usage(void);
58 main(int argc, char **argv)
62 int ch, count, newline;
65 #if MAXPATHLEN > BUFSIZ
73 while ((ch = getopt(argc, argv, "cf:s:")) != -1)
83 for (p = sender; *p; ++p)
95 (void)snprintf(buf, sizeof(buf), "%s/%s", _PATH_MAILDIR, *argv);
98 if (!(file = getenv("MAIL"))) {
99 if (!(pwd = getpwuid(getuid())))
100 errx(1, "no password file entry for you");
102 (void)snprintf(buf, sizeof(buf),
103 "%s/%s", _PATH_MAILDIR, file);
109 /* read from stdin */
110 if (strcmp(file, "-") == 0) {
113 else if ((mbox = fopen(file, "r")) == NULL) {
114 errx(1, "can't read %s", file);
116 for (newline = 1; fgets(buf, sizeof(buf), mbox);) {
121 if (newline && !strncmp(buf, "From ", 5) &&
122 (!sender || match(buf + 5, sender))) {
131 printf("There %s %d message%s in your incoming mailbox.\n",
132 count == 1 ? "is" : "are", count, count == 1 ? "" : "s");
140 fprintf(stderr, "usage: from [-c] [-f file] [-s sender] [user]\n");
145 match(const char *line, const char *sender)
150 for (first = *sender++;;) {
151 if (isspace(ch = *line))
157 for (p = sender, t = line;;) {