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