1 /* 2 * Copyright (c) 1980, 1988 The Regents of the University of California. 3 * All rights reserved. 4 * 5 * %sccs.include.redist.c% 6 */ 7 8 #ifndef lint 9 char copyright[] = 10 "@(#) Copyright (c) 1980, 1988 The Regents of the University of California.\n\ 11 All rights reserved.\n"; 12 #endif /* not lint */ 13 14 #ifndef lint 15 static char sccsid[] = "@(#)from.c 5.5 (Berkeley) 06/01/90"; 16 #endif /* not lint */ 17 18 #include <sys/types.h> 19 #include <ctype.h> 20 #include <pwd.h> 21 #include <stdio.h> 22 23 main(argc, argv) 24 int argc; 25 char **argv; 26 { 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 37 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); 54 } 55 argv += optind; 56 57 if (!file) { 58 if (!(file = *argv)) { 59 if (!(pwd = getpwuid(getuid()))) { 60 fprintf(stderr, 61 "from: no password file entry for you.\n"); 62 exit(1); 63 } 64 file = pwd->pw_name; 65 } 66 (void)sprintf(buf, "/usr/spool/mail/%s", file); 67 file = buf; 68 } 69 if (!freopen(file, "r", stdin)) { 70 fprintf(stderr, "from: can't read %s.\n", file); 71 exit(1); 72 } 73 for (newline = 1; fgets(buf, sizeof(buf), stdin);) { 74 if (*buf == '\n') { 75 newline = 1; 76 continue; 77 } 78 if (newline && !strncmp(buf, "From ", 5) && 79 (!sender || match(buf + 5, sender))) 80 printf("%s", buf); 81 newline = 0; 82 } 83 exit(0); 84 } 85 86 match(line, sender) 87 register char *line, *sender; 88 { 89 register char ch, pch, first, *p, *t; 90 91 for (first = *sender++;;) { 92 if (isspace(ch = *line)) 93 return(0); 94 ++line; 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 } 107 } 108 /* NOTREACHED */ 109 } 110