add man page, cleanup
[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
92be3e5b
KB
25static char sccsid[] = "@(#)from.c 5.3 (Berkeley) %G%";
26#endif /* not lint */
a0249ec2 27
124dd274
BJ
28#include <ctype.h>
29#include <pwd.h>
92be3e5b 30#include <stdio.h>
124dd274
BJ
31
32main(argc, argv)
a0249ec2 33 int argc;
92be3e5b 34 char **argv;
124dd274 35{
92be3e5b
KB
36 extern char *optarg;
37 extern int optind;
38 struct passwd *pwd, *getpwuid();
39 int ch, newline;
40 char *file, *sender, *p;
41#if MAXPATHLEN > BUFSIZ
42 char buf[MAXPATHLEN];
43#else
44 char buf[BUFSIZ];
45#endif
124dd274 46
92be3e5b
KB
47 file = sender = NULL;
48 while ((ch = getopt(argc, argv, "f:s:")) != EOF)
49 switch((char)ch) {
50 case 'f':
51 file = optarg;
52 break;
53 case 's':
54 sender = optarg;
55 for (p = sender; *p; ++p)
56 if (isupper(*p))
57 *p = tolower(*p);
58 break;
59 case '?':
60 default:
61 fprintf(stderr, "usage: from [-f file] [-s sender] [user]\n");
62 exit(1);
124dd274 63 }
92be3e5b 64 argv += optind;
124dd274 65
92be3e5b
KB
66 if (!file) {
67 if (!(file = *argv)) {
68 if (!(pwd = getpwuid(getuid()))) {
69 fprintf(stderr,
70 "from: no password file entry for you.\n");
124dd274
BJ
71 exit(1);
72 }
92be3e5b 73 file = pwd->pw_name;
124dd274 74 }
92be3e5b
KB
75 (void)sprintf(buf, "/usr/spool/mail/%s", file);
76 file = buf;
124dd274 77 }
92be3e5b
KB
78 if (!freopen(file, "r", stdin)) {
79 fprintf(stderr, "from: can't read %s.\n", file);
80 exit(1);
a4abdc18 81 }
92be3e5b
KB
82 for (newline = 1; fgets(buf, sizeof(buf), stdin);) {
83 if (*buf == '\n') {
84 newline = 1;
85 continue;
124dd274 86 }
92be3e5b
KB
87 if (newline && !strncmp(buf, "From ", 5) &&
88 (!sender || match(buf + 5, sender)))
89 printf("%s", buf);
90 newline = 0;
91 }
124dd274
BJ
92 exit(0);
93}
94
92be3e5b
KB
95match(line, sender)
96 register char *line, *sender;
124dd274 97{
92be3e5b 98 register char ch, pch, first, *p, *t;
124dd274 99
92be3e5b
KB
100 for (first = *sender++;;) {
101 if (isspace(ch = *line))
102 return(0);
124dd274 103 ++line;
92be3e5b
KB
104 if (isupper(ch))
105 ch = tolower(ch);
106 if (ch != first)
107 continue;
108 for (p = sender, t = line;;) {
109 if (!(pch = *p++))
110 return(1);
111 if (isupper(ch = *t++))
112 ch = tolower(ch);
113 if (ch != pch)
114 break;
115 }
124dd274 116 }
92be3e5b 117 /* NOTREACHED */
124dd274 118}