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