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