1 /* $NetBSD: from.c,v 1.6 1995/09/01 01:39:10 jtc Exp $ */ 2 3 /* 4 * Copyright (c) 1980, 1988, 1993 5 * The Regents of the University of California. All rights reserved. 6 * 7 * Redistribution and use in source and binary forms, with or without 8 * modification, are permitted provided that the following conditions 9 * are met: 10 * 1. Redistributions of source code must retain the above copyright 11 * notice, this list of conditions and the following disclaimer. 12 * 2. Redistributions in binary form must reproduce the above copyright 13 * notice, this list of conditions and the following disclaimer in the 14 * documentation and/or other materials provided with the distribution. 15 * 3. All advertising materials mentioning features or use of this software 16 * must display the following acknowledgement: 17 * This product includes software developed by the University of 18 * California, Berkeley and its contributors. 19 * 4. Neither the name of the University nor the names of its contributors 20 * may be used to endorse or promote products derived from this software 21 * without specific prior written permission. 22 * 23 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 24 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 25 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 26 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 27 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 28 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 29 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 30 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 31 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 32 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 33 * SUCH DAMAGE. 34 */ 35 36 #ifndef lint 37 static char copyright[] = 38 "@(#) Copyright (c) 1980, 1988, 1993\n\ 39 The Regents of the University of California. All rights reserved.\n"; 40 #endif /* not lint */ 41 42 #ifndef lint 43 #if 0 44 static char sccsid[] = "@(#)from.c 8.1 (Berkeley) 6/6/93"; 45 #endif 46 static char rcsid[] = "$NetBSD: from.c,v 1.6 1995/09/01 01:39:10 jtc Exp $"; 47 #endif /* not lint */ 48 49 #include <sys/types.h> 50 #include <ctype.h> 51 #include <pwd.h> 52 #include <stdio.h> 53 #include <stdlib.h> 54 #include <unistd.h> 55 #include <paths.h> 56 57 main(argc, argv) 58 int argc; 59 char **argv; 60 { 61 extern char *optarg; 62 extern int optind; 63 struct passwd *pwd; 64 int ch, newline; 65 char *file, *sender, *p; 66 #if MAXPATHLEN > BUFSIZ 67 char buf[MAXPATHLEN]; 68 #else 69 char buf[BUFSIZ]; 70 #endif 71 72 file = sender = NULL; 73 while ((ch = getopt(argc, argv, "f:s:")) != EOF) 74 switch((char)ch) { 75 case 'f': 76 file = optarg; 77 break; 78 case 's': 79 sender = optarg; 80 for (p = sender; *p; ++p) 81 if (isupper(*p)) 82 *p = tolower(*p); 83 break; 84 case '?': 85 default: 86 fprintf(stderr, "usage: from [-f file] [-s sender] [user]\n"); 87 exit(1); 88 } 89 argv += optind; 90 91 /* 92 * We find the mailbox by: 93 * 1 -f flag 94 * 2 user 95 * 2 MAIL environment variable 96 * 3 _PATH_MAILDIR/file 97 */ 98 if (!file) { 99 if (!(file = *argv)) { 100 if (!(file = getenv("MAIL"))) { 101 if (!(pwd = getpwuid(getuid()))) { 102 (void)fprintf(stderr, 103 "from: no password file entry for you.\n"); 104 exit(1); 105 } 106 if (file = getenv("USER")) { 107 (void)sprintf(buf, "%s/%s", 108 _PATH_MAILDIR, file); 109 file = buf; 110 } else 111 (void)sprintf(file = buf, "%s/%s", 112 _PATH_MAILDIR, pwd->pw_name); 113 } 114 } else { 115 (void)sprintf(buf, "%s/%s", _PATH_MAILDIR, file); 116 file = buf; 117 } 118 } 119 if (!freopen(file, "r", stdin)) { 120 (void)fprintf(stderr, "from: can't read %s.\n", file); 121 exit(1); 122 } 123 for (newline = 1; fgets(buf, sizeof(buf), stdin);) { 124 if (*buf == '\n') { 125 newline = 1; 126 continue; 127 } 128 if (newline && !strncmp(buf, "From ", 5) && 129 (!sender || match(buf + 5, sender))) 130 printf("%s", buf); 131 newline = 0; 132 } 133 exit(0); 134 } 135 136 match(line, sender) 137 register char *line, *sender; 138 { 139 register char ch, pch, first, *p, *t; 140 141 for (first = *sender++;;) { 142 if (isspace(ch = *line)) 143 return(0); 144 ++line; 145 if (isupper(ch)) 146 ch = tolower(ch); 147 if (ch != first) 148 continue; 149 for (p = sender, t = line;;) { 150 if (!(pch = *p++)) 151 return(1); 152 if (isupper(ch = *t++)) 153 ch = tolower(ch); 154 if (ch != pch) 155 break; 156 } 157 } 158 /* NOTREACHED */ 159 } 160