man page in the right palce
[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 *
5 * Redistribution and use in source and binary forms are permitted
6 * provided that the above copyright notice and this paragraph are
7 * duplicated in all such forms and that any documentation,
8 * advertising materials, and other materials related to such
9 * distribution and use acknowledge that the software was developed
10 * by the University of California, Berkeley. The name of the
11 * University may not be used to endorse or promote products derived
12 * from this software without specific prior written permission.
13 * THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR
14 * IMPLIED WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED
15 * WARRANTIES OF MERCHANTIBILITY AND FITNESS FOR A PARTICULAR PURPOSE.
22e155fc
DF
16 */
17
18#ifndef lint
19char copyright[] =
92be3e5b 20"@(#) Copyright (c) 1980, 1988 The Regents of the University of California.\n\
22e155fc 21 All rights reserved.\n";
92be3e5b 22#endif /* not lint */
22e155fc
DF
23
24#ifndef lint
fc74dff1 25static char sccsid[] = "@(#)from.c 5.4 (Berkeley) %G%";
92be3e5b 26#endif /* not lint */
a0249ec2 27
fc74dff1 28#include <sys/types.h>
124dd274
BJ
29#include <ctype.h>
30#include <pwd.h>
92be3e5b 31#include <stdio.h>
124dd274
BJ
32
33main(argc, argv)
a0249ec2 34 int argc;
92be3e5b 35 char **argv;
124dd274 36{
92be3e5b
KB
37 extern char *optarg;
38 extern int optind;
39 struct passwd *pwd, *getpwuid();
40 int ch, newline;
41 char *file, *sender, *p;
42#if MAXPATHLEN > BUFSIZ
43 char buf[MAXPATHLEN];
44#else
45 char buf[BUFSIZ];
46#endif
124dd274 47
92be3e5b
KB
48 file = sender = NULL;
49 while ((ch = getopt(argc, argv, "f:s:")) != EOF)
50 switch((char)ch) {
51 case 'f':
52 file = optarg;
53 break;
54 case 's':
55 sender = optarg;
56 for (p = sender; *p; ++p)
57 if (isupper(*p))
58 *p = tolower(*p);
59 break;
60 case '?':
61 default:
62 fprintf(stderr, "usage: from [-f file] [-s sender] [user]\n");
63 exit(1);
124dd274 64 }
92be3e5b 65 argv += optind;
124dd274 66
92be3e5b
KB
67 if (!file) {
68 if (!(file = *argv)) {
69 if (!(pwd = getpwuid(getuid()))) {
70 fprintf(stderr,
71 "from: no password file entry for you.\n");
124dd274
BJ
72 exit(1);
73 }
92be3e5b 74 file = pwd->pw_name;
124dd274 75 }
92be3e5b
KB
76 (void)sprintf(buf, "/usr/spool/mail/%s", file);
77 file = buf;
124dd274 78 }
92be3e5b
KB
79 if (!freopen(file, "r", stdin)) {
80 fprintf(stderr, "from: can't read %s.\n", file);
81 exit(1);
a4abdc18 82 }
92be3e5b
KB
83 for (newline = 1; fgets(buf, sizeof(buf), stdin);) {
84 if (*buf == '\n') {
85 newline = 1;
86 continue;
124dd274 87 }
92be3e5b
KB
88 if (newline && !strncmp(buf, "From ", 5) &&
89 (!sender || match(buf + 5, sender)))
90 printf("%s", buf);
91 newline = 0;
92 }
124dd274
BJ
93 exit(0);
94}
95
92be3e5b
KB
96match(line, sender)
97 register char *line, *sender;
124dd274 98{
92be3e5b 99 register char ch, pch, first, *p, *t;
124dd274 100
92be3e5b
KB
101 for (first = *sender++;;) {
102 if (isspace(ch = *line))
103 return(0);
124dd274 104 ++line;
92be3e5b
KB
105 if (isupper(ch))
106 ch = tolower(ch);
107 if (ch != first)
108 continue;
109 for (p = sender, t = line;;) {
110 if (!(pch = *p++))
111 return(1);
112 if (isupper(ch = *t++))
113 ch = tolower(ch);
114 if (ch != pch)
115 break;
116 }
124dd274 117 }
92be3e5b 118 /* NOTREACHED */
124dd274 119}