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