Annotation of src/usr.bin/from/from.c, Revision 1.11
1.11 ! deraadt 1: /* $OpenBSD: from.c,v 1.10 2003/06/10 22:20:46 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.11 ! deraadt 43: static char rcsid[] = "$OpenBSD: from.c,v 1.10 2003/06/10 22:20:46 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: extern char *optarg;
62: extern int optind;
63: struct passwd *pwd;
64: int ch, newline;
65: char *file, *sender, *p;
66: #if MAXPATHLEN > BUFSIZ
67: char buf[MAXPATHLEN];
68: #else
69: char buf[BUFSIZ];
70: #endif
71:
72: file = sender = NULL;
1.3 millert 73: while ((ch = getopt(argc, argv, "f:s:")) != -1)
1.1 deraadt 74: switch((char)ch) {
75: case 'f':
76: file = optarg;
77: break;
78: case 's':
79: sender = optarg;
80: for (p = sender; *p; ++p)
81: if (isupper(*p))
82: *p = tolower(*p);
83: break;
84: case '?':
85: default:
86: fprintf(stderr, "usage: from [-f file] [-s sender] [user]\n");
87: exit(1);
88: }
89: argv += optind;
90:
91: /*
92: * We find the mailbox by:
93: * 1 -f flag
94: * 2 user
95: * 2 MAIL environment variable
96: * 3 _PATH_MAILDIR/file
97: */
98: if (!file) {
99: if (!(file = *argv)) {
100: if (!(file = getenv("MAIL"))) {
1.4 mickey 101: if (!(pwd = getpwuid(getuid())))
102: errx(1, "no password file entry for you");
1.7 deraadt 103: if ((file = getenv("USER"))) {
1.5 aaron 104: (void)snprintf(buf, sizeof(buf),
1.11 ! deraadt 105: "%s/%s", _PATH_MAILDIR, file);
1.1 deraadt 106: file = buf;
107: } else
1.5 aaron 108: (void)snprintf(file = buf, sizeof(buf),
1.11 ! deraadt 109: "%s/%s", _PATH_MAILDIR,
! 110: pwd->pw_name);
1.1 deraadt 111: }
112: } else {
1.5 aaron 113: (void)snprintf(buf, sizeof(buf), "%s/%s",
1.11 ! deraadt 114: _PATH_MAILDIR, file);
1.1 deraadt 115: file = buf;
116: }
117: }
1.4 mickey 118: if (!freopen(file, "r", stdin))
1.6 millert 119: err(1, "%s", file);
1.1 deraadt 120: for (newline = 1; fgets(buf, sizeof(buf), stdin);) {
121: if (*buf == '\n') {
122: newline = 1;
123: continue;
124: }
125: if (newline && !strncmp(buf, "From ", 5) &&
126: (!sender || match(buf + 5, sender)))
127: printf("%s", buf);
128: newline = 0;
129: }
130: exit(0);
131: }
132:
1.7 deraadt 133: int
1.10 deraadt 134: match(char *line, char *sender)
1.1 deraadt 135: {
1.8 mpech 136: char ch, pch, first, *p, *t;
1.1 deraadt 137:
138: for (first = *sender++;;) {
139: if (isspace(ch = *line))
140: return(0);
141: ++line;
142: if (isupper(ch))
143: ch = tolower(ch);
144: if (ch != first)
145: continue;
146: for (p = sender, t = line;;) {
147: if (!(pch = *p++))
148: return(1);
149: if (isupper(ch = *t++))
150: ch = tolower(ch);
151: if (ch != pch)
152: break;
153: }
154: }
155: /* NOTREACHED */
156: }