Annotation of src/usr.bin/from/from.c, Revision 1.17
1.17 ! deraadt 1: /* $OpenBSD: from.c,v 1.16 2013/11/27 13:32:02 okan 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: #include <sys/types.h>
34: #include <ctype.h>
35: #include <pwd.h>
36: #include <stdio.h>
37: #include <stdlib.h>
38: #include <unistd.h>
39: #include <paths.h>
1.7 deraadt 40: #include <string.h>
1.4 mickey 41: #include <err.h>
1.1 deraadt 42:
1.7 deraadt 43: int match(char *, char *);
44:
45: int
1.10 deraadt 46: main(int argc, char *argv[])
1.1 deraadt 47: {
48: struct passwd *pwd;
49: int ch, newline;
50: char *file, *sender, *p;
1.17 ! deraadt 51: #if PATH_MAX > BUFSIZ
! 52: char buf[PATH_MAX];
1.1 deraadt 53: #else
54: char buf[BUFSIZ];
55: #endif
56:
57: file = sender = NULL;
1.3 millert 58: while ((ch = getopt(argc, argv, "f:s:")) != -1)
1.16 okan 59: switch(ch) {
1.1 deraadt 60: case 'f':
61: file = optarg;
62: break;
63: case 's':
64: sender = optarg;
65: for (p = sender; *p; ++p)
1.15 deraadt 66: if (isupper((unsigned char)*p))
67: *p = tolower((unsigned char)*p);
1.1 deraadt 68: break;
69: case '?':
70: default:
1.13 jmc 71: fprintf(stderr,
72: "usage: from [-f file] [-s sender] [user]\n");
1.1 deraadt 73: exit(1);
74: }
75: argv += optind;
76:
77: /*
78: * We find the mailbox by:
79: * 1 -f flag
80: * 2 user
81: * 2 MAIL environment variable
82: * 3 _PATH_MAILDIR/file
83: */
84: if (!file) {
85: if (!(file = *argv)) {
86: if (!(file = getenv("MAIL"))) {
1.4 mickey 87: if (!(pwd = getpwuid(getuid())))
88: errx(1, "no password file entry for you");
1.7 deraadt 89: if ((file = getenv("USER"))) {
1.5 aaron 90: (void)snprintf(buf, sizeof(buf),
1.11 deraadt 91: "%s/%s", _PATH_MAILDIR, file);
1.1 deraadt 92: file = buf;
93: } else
1.5 aaron 94: (void)snprintf(file = buf, sizeof(buf),
1.11 deraadt 95: "%s/%s", _PATH_MAILDIR,
96: pwd->pw_name);
1.1 deraadt 97: }
98: } else {
1.5 aaron 99: (void)snprintf(buf, sizeof(buf), "%s/%s",
1.11 deraadt 100: _PATH_MAILDIR, file);
1.1 deraadt 101: file = buf;
102: }
103: }
1.4 mickey 104: if (!freopen(file, "r", stdin))
1.6 millert 105: err(1, "%s", file);
1.1 deraadt 106: for (newline = 1; fgets(buf, sizeof(buf), stdin);) {
107: if (*buf == '\n') {
108: newline = 1;
109: continue;
110: }
111: if (newline && !strncmp(buf, "From ", 5) &&
112: (!sender || match(buf + 5, sender)))
113: printf("%s", buf);
114: newline = 0;
115: }
116: exit(0);
117: }
118:
1.7 deraadt 119: int
1.10 deraadt 120: match(char *line, char *sender)
1.1 deraadt 121: {
1.8 mpech 122: char ch, pch, first, *p, *t;
1.1 deraadt 123:
124: for (first = *sender++;;) {
1.15 deraadt 125: if (isspace((unsigned char)(ch = *line)))
1.1 deraadt 126: return(0);
127: ++line;
1.15 deraadt 128: if (isupper((unsigned char)ch))
129: ch = tolower((unsigned char)ch);
1.1 deraadt 130: if (ch != first)
131: continue;
132: for (p = sender, t = line;;) {
133: if (!(pch = *p++))
134: return(1);
1.15 deraadt 135: if (isupper((unsigned char)(ch = *t++)))
136: ch = tolower((unsigned char)ch);
1.1 deraadt 137: if (ch != pch)
138: break;
139: }
140: }
141: /* NOTREACHED */
142: }