add Berkeley copyright notice
[unix-history] / usr / src / usr.bin / from / from.c
CommitLineData
22e155fc
DF
1/*
2 * Copyright (c) 1980 Regents of the University of California.
3 * All rights reserved. The Berkeley software License Agreement
4 * specifies the terms and conditions for redistribution.
5 */
6
7#ifndef lint
8char copyright[] =
9"@(#) Copyright (c) 1980 Regents of the University of California.\n\
10 All rights reserved.\n";
11#endif not lint
12
13#ifndef lint
a4abdc18 14static char sccsid[] = "@(#)from.c 5.2 (Berkeley) %G%";
22e155fc 15#endif not lint
a0249ec2 16
124dd274
BJ
17#include <stdio.h>
18#include <ctype.h>
19#include <pwd.h>
20
21struct passwd *getpwuid();
22
23main(argc, argv)
a0249ec2
SL
24 int argc;
25 register char **argv;
124dd274
BJ
26{
27 char lbuf[BUFSIZ];
28 char lbuf2[BUFSIZ];
29 register struct passwd *pp;
30 int stashed = 0;
31 register char *name;
32 char *sender;
33 char *getlogin();
34
35 if (argc > 1 && *(argv[1]) == '-' && (*++argv)[1] == 's') {
36 if (--argc <= 1) {
37 fprintf (stderr, "Usage: from [-s sender] [user]\n");
38 exit (1);
39 }
40 --argc;
41 sender = *++argv;
42 for (name = sender; *name; name++)
43 if (isupper(*name))
44 *name = tolower(*name);
45
a0249ec2 46 } else
124dd274
BJ
47 sender = NULL;
48 if (chdir("/usr/spool/mail") < 0)
49 exit(1);
50 if (argc > 1)
51 name = argv[1];
52 else {
53 name = getlogin ();
54 if (name == NULL || strlen(name) == 0) {
55 pp = getpwuid(getuid());
56 if (pp == NULL) {
57 fprintf(stderr, "Who are you?\n");
58 exit(1);
59 }
60 name = pp->pw_name;
61 }
62 }
a4abdc18
JB
63 if (freopen(name, "r", stdin) == NULL) {
64 fprintf(stderr, "Can't open /usr/spool/mail/%s\n", name);
124dd274 65 exit(0);
a4abdc18 66 }
a0249ec2 67 while (fgets(lbuf, sizeof lbuf, stdin) != NULL)
124dd274
BJ
68 if (lbuf[0] == '\n' && stashed) {
69 stashed = 0;
70 printf("%s", lbuf2);
a0249ec2 71 } else if (strncmp(lbuf, "From ", 5) == 0 &&
124dd274
BJ
72 (sender == NULL || match(&lbuf[4], sender))) {
73 strcpy(lbuf2, lbuf);
74 stashed = 1;
75 }
76 if (stashed)
77 printf("%s", lbuf2);
78 exit(0);
79}
80
124dd274 81match (line, str)
a0249ec2 82 register char *line, *str;
124dd274
BJ
83{
84 register char ch;
85
86 while (*line == ' ' || *line == '\t')
87 ++line;
88 if (*line == '\n')
89 return (0);
90 while (*str && *line != ' ' && *line != '\t' && *line != '\n') {
91 ch = isupper(*line) ? tolower(*line) : *line;
92 if (ch != *str++)
93 return (0);
94 line++;
95 }
96 return (*str == '\0');
97}