Commit | Line | Data |
---|---|---|
22e155fc | 1 | /* |
1492d204 KB |
2 | * Copyright (c) 1980, 1988, 1993 |
3 | * The Regents of the University of California. All rights reserved. | |
92be3e5b | 4 | * |
f15db449 | 5 | * %sccs.include.redist.c% |
22e155fc DF |
6 | */ |
7 | ||
8 | #ifndef lint | |
1492d204 KB |
9 | static char copyright[] = |
10 | "@(#) Copyright (c) 1980, 1988, 1993\n\ | |
11 | The Regents of the University of California. All rights reserved.\n"; | |
92be3e5b | 12 | #endif /* not lint */ |
22e155fc DF |
13 | |
14 | #ifndef lint | |
1492d204 | 15 | static char sccsid[] = "@(#)from.c 8.1 (Berkeley) %G%"; |
92be3e5b | 16 | #endif /* not lint */ |
a0249ec2 | 17 | |
fc74dff1 | 18 | #include <sys/types.h> |
124dd274 BJ |
19 | #include <ctype.h> |
20 | #include <pwd.h> | |
92be3e5b | 21 | #include <stdio.h> |
11949e61 | 22 | #include <paths.h> |
124dd274 BJ |
23 | |
24 | main(argc, argv) | |
a0249ec2 | 25 | int argc; |
92be3e5b | 26 | char **argv; |
124dd274 | 27 | { |
92be3e5b KB |
28 | extern char *optarg; |
29 | extern int optind; | |
b61c5edc | 30 | struct passwd *pwd; |
92be3e5b KB |
31 | int ch, newline; |
32 | char *file, *sender, *p; | |
33 | #if MAXPATHLEN > BUFSIZ | |
34 | char buf[MAXPATHLEN]; | |
35 | #else | |
36 | char buf[BUFSIZ]; | |
37 | #endif | |
124dd274 | 38 | |
92be3e5b KB |
39 | file = sender = NULL; |
40 | while ((ch = getopt(argc, argv, "f:s:")) != EOF) | |
41 | switch((char)ch) { | |
42 | case 'f': | |
43 | file = optarg; | |
44 | break; | |
45 | case 's': | |
46 | sender = optarg; | |
47 | for (p = sender; *p; ++p) | |
48 | if (isupper(*p)) | |
49 | *p = tolower(*p); | |
50 | break; | |
51 | case '?': | |
52 | default: | |
53 | fprintf(stderr, "usage: from [-f file] [-s sender] [user]\n"); | |
54 | exit(1); | |
124dd274 | 55 | } |
92be3e5b | 56 | argv += optind; |
124dd274 | 57 | |
92be3e5b KB |
58 | if (!file) { |
59 | if (!(file = *argv)) { | |
60 | if (!(pwd = getpwuid(getuid()))) { | |
61 | fprintf(stderr, | |
62 | "from: no password file entry for you.\n"); | |
124dd274 BJ |
63 | exit(1); |
64 | } | |
92be3e5b | 65 | file = pwd->pw_name; |
124dd274 | 66 | } |
11949e61 | 67 | (void)sprintf(buf, "%s/%s", _PATH_MAILDIR, file); |
92be3e5b | 68 | file = buf; |
124dd274 | 69 | } |
92be3e5b KB |
70 | if (!freopen(file, "r", stdin)) { |
71 | fprintf(stderr, "from: can't read %s.\n", file); | |
72 | exit(1); | |
a4abdc18 | 73 | } |
92be3e5b KB |
74 | for (newline = 1; fgets(buf, sizeof(buf), stdin);) { |
75 | if (*buf == '\n') { | |
76 | newline = 1; | |
77 | continue; | |
124dd274 | 78 | } |
92be3e5b KB |
79 | if (newline && !strncmp(buf, "From ", 5) && |
80 | (!sender || match(buf + 5, sender))) | |
81 | printf("%s", buf); | |
82 | newline = 0; | |
83 | } | |
124dd274 BJ |
84 | exit(0); |
85 | } | |
86 | ||
92be3e5b KB |
87 | match(line, sender) |
88 | register char *line, *sender; | |
124dd274 | 89 | { |
92be3e5b | 90 | register char ch, pch, first, *p, *t; |
124dd274 | 91 | |
92be3e5b KB |
92 | for (first = *sender++;;) { |
93 | if (isspace(ch = *line)) | |
94 | return(0); | |
124dd274 | 95 | ++line; |
92be3e5b KB |
96 | if (isupper(ch)) |
97 | ch = tolower(ch); | |
98 | if (ch != first) | |
99 | continue; | |
100 | for (p = sender, t = line;;) { | |
101 | if (!(pch = *p++)) | |
102 | return(1); | |
103 | if (isupper(ch = *t++)) | |
104 | ch = tolower(ch); | |
105 | if (ch != pch) | |
106 | break; | |
107 | } | |
124dd274 | 108 | } |
92be3e5b | 109 | /* NOTREACHED */ |
124dd274 | 110 | } |