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