1*98e5374cSlukem /* $NetBSD: from.c,v 1.17 2008/07/21 14:19:22 lukem Exp $ */
27642736cSjtc
361f28255Scgd /*
47642736cSjtc * Copyright (c) 1980, 1988, 1993
57642736cSjtc * The Regents of the University of California. All rights reserved.
661f28255Scgd *
761f28255Scgd * Redistribution and use in source and binary forms, with or without
861f28255Scgd * modification, are permitted provided that the following conditions
961f28255Scgd * are met:
1061f28255Scgd * 1. Redistributions of source code must retain the above copyright
1161f28255Scgd * notice, this list of conditions and the following disclaimer.
1261f28255Scgd * 2. Redistributions in binary form must reproduce the above copyright
1361f28255Scgd * notice, this list of conditions and the following disclaimer in the
1461f28255Scgd * documentation and/or other materials provided with the distribution.
1589aaa1bbSagc * 3. Neither the name of the University nor the names of its contributors
1661f28255Scgd * may be used to endorse or promote products derived from this software
1761f28255Scgd * without specific prior written permission.
1861f28255Scgd *
1961f28255Scgd * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
2061f28255Scgd * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
2161f28255Scgd * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
2261f28255Scgd * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
2361f28255Scgd * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
2461f28255Scgd * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
2561f28255Scgd * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
2661f28255Scgd * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
2761f28255Scgd * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
2861f28255Scgd * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
2961f28255Scgd * SUCH DAMAGE.
3061f28255Scgd */
3161f28255Scgd
32e207462dSlukem #include <sys/cdefs.h>
3361f28255Scgd #ifndef lint
34*98e5374cSlukem __COPYRIGHT("@(#) Copyright (c) 1980, 1988, 1993\
35*98e5374cSlukem The Regents of the University of California. All rights reserved.");
3661f28255Scgd #endif /* not lint */
3761f28255Scgd
3861f28255Scgd #ifndef lint
397642736cSjtc #if 0
407642736cSjtc static char sccsid[] = "@(#)from.c 8.1 (Berkeley) 6/6/93";
417642736cSjtc #endif
42*98e5374cSlukem __RCSID("$NetBSD: from.c,v 1.17 2008/07/21 14:19:22 lukem Exp $");
4361f28255Scgd #endif /* not lint */
4461f28255Scgd
4561f28255Scgd #include <sys/types.h>
4661f28255Scgd #include <ctype.h>
47f9e3c909Smjl #include <err.h>
48e207462dSlukem #include <paths.h>
4961f28255Scgd #include <pwd.h>
5061f28255Scgd #include <stdio.h>
510479ea62Sderaadt #include <stdlib.h>
52e207462dSlukem #include <string.h>
530479ea62Sderaadt #include <unistd.h>
5461f28255Scgd
55680cd1fdSmjl int main (int, char **);
56680cd1fdSmjl int match (const char *, const char *);
57e207462dSlukem
58e207462dSlukem int
main(int argc,char ** argv)59680cd1fdSmjl main(int argc, char **argv)
6061f28255Scgd {
6161f28255Scgd struct passwd *pwd;
6261f28255Scgd int ch, newline;
6361f28255Scgd char *file, *sender, *p;
6461f28255Scgd #if MAXPATHLEN > BUFSIZ
6561f28255Scgd char buf[MAXPATHLEN];
6661f28255Scgd #else
6761f28255Scgd char buf[BUFSIZ];
6861f28255Scgd #endif
6961f28255Scgd
7061f28255Scgd file = sender = NULL;
71906b60f5Slukem while ((ch = getopt(argc, argv, "f:s:")) != -1)
7261f28255Scgd switch((char)ch) {
7361f28255Scgd case 'f':
7461f28255Scgd file = optarg;
7561f28255Scgd break;
7661f28255Scgd case 's':
7761f28255Scgd sender = optarg;
7861f28255Scgd for (p = sender; *p; ++p)
7925b3737cSdsl *p = tolower((unsigned char)*p);
8061f28255Scgd break;
8161f28255Scgd default:
8261f28255Scgd fprintf(stderr, "usage: from [-f file] [-s sender] [user]\n");
8361f28255Scgd exit(1);
8461f28255Scgd }
8561f28255Scgd argv += optind;
8661f28255Scgd
870479ea62Sderaadt /*
880479ea62Sderaadt * We find the mailbox by:
890479ea62Sderaadt * 1 -f flag
900479ea62Sderaadt * 2 user
910479ea62Sderaadt * 2 MAIL environment variable
920479ea62Sderaadt * 3 _PATH_MAILDIR/file
930479ea62Sderaadt */
9461f28255Scgd if (!file) {
9561f28255Scgd if (!(file = *argv)) {
960479ea62Sderaadt if (!(file = getenv("MAIL"))) {
97f9e3c909Smjl if (!(pwd = getpwuid(getuid())))
985f98a040Smjl errx(1, "no password file entry for you");
996f7054a5Skleink if ((file = getenv("LOGNAME")) != NULL ||
1006f7054a5Skleink (file = getenv("USER")) != NULL) {
1015f98a040Smjl (void)snprintf(buf, sizeof(buf),
1025f98a040Smjl "%s/%s", _PATH_MAILDIR, file);
1039e809275Sderaadt file = buf;
1049e809275Sderaadt } else
1055f98a040Smjl (void)snprintf(file = buf, sizeof(buf),
1065f98a040Smjl "%s/%s",
1070479ea62Sderaadt _PATH_MAILDIR, pwd->pw_name);
10861f28255Scgd }
109b175f116Sderaadt } else {
1105f98a040Smjl (void)snprintf(buf, sizeof(buf), "%s/%s",
1115f98a040Smjl _PATH_MAILDIR, file);
112b175f116Sderaadt file = buf;
1130479ea62Sderaadt }
11461f28255Scgd }
115f9e3c909Smjl if (!freopen(file, "r", stdin))
1165f98a040Smjl err(1, "can't read %s", file);
117f9e3c909Smjl
11861f28255Scgd for (newline = 1; fgets(buf, sizeof(buf), stdin);) {
11961f28255Scgd if (*buf == '\n') {
12061f28255Scgd newline = 1;
12161f28255Scgd continue;
12261f28255Scgd }
12361f28255Scgd if (newline && !strncmp(buf, "From ", 5) &&
12461f28255Scgd (!sender || match(buf + 5, sender)))
12561f28255Scgd printf("%s", buf);
12661f28255Scgd newline = 0;
12761f28255Scgd }
12861f28255Scgd exit(0);
12961f28255Scgd }
13061f28255Scgd
131e207462dSlukem int
match(const char * line,const char * sender)132680cd1fdSmjl match(const char *line, const char *sender)
13361f28255Scgd {
134680cd1fdSmjl char ch, pch, first;
135680cd1fdSmjl const char *p, *t;
13661f28255Scgd
13761f28255Scgd for (first = *sender++;;) {
1389794a7e0Schristos if (isspace((unsigned char)(ch = *line)))
13961f28255Scgd return(0);
14061f28255Scgd ++line;
14125b3737cSdsl ch = tolower((unsigned char)ch);
14261f28255Scgd if (ch != first)
14361f28255Scgd continue;
14461f28255Scgd for (p = sender, t = line;;) {
14561f28255Scgd if (!(pch = *p++))
14661f28255Scgd return(1);
14725b3737cSdsl ch = tolower((unsigned char)*t++);
14861f28255Scgd if (ch != pch)
14961f28255Scgd break;
15061f28255Scgd }
15161f28255Scgd }
15261f28255Scgd /* NOTREACHED */
15361f28255Scgd }
154