1 /*- 2 * SPDX-License-Identifier: BSD-3-Clause 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. Neither the name of the University nor the names of its contributors 16 * may be used to endorse or promote products derived from this software 17 * without specific prior written permission. 18 * 19 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 20 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 21 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 22 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 23 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 24 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 25 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 26 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 27 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 28 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 29 * SUCH DAMAGE. 30 */ 31 32 #ifndef lint 33 static const char copyright[] = 34 "@(#) Copyright (c) 1980, 1988, 1993\n\ 35 The Regents of the University of California. All rights reserved.\n"; 36 #endif /* not lint */ 37 38 #ifndef lint 39 #endif /* not lint */ 40 #include <sys/cdefs.h> 41 #include <sys/types.h> 42 #include <ctype.h> 43 #include <err.h> 44 #include <pwd.h> 45 #include <stdio.h> 46 #include <stdlib.h> 47 #include <paths.h> 48 #include <string.h> 49 #include <unistd.h> 50 51 static int match(const char *, const char *); 52 static void usage(void) __dead2; 53 54 int 55 main(int argc, char **argv) 56 { 57 FILE *mbox; 58 struct passwd *pwd; 59 int ch, count, newline; 60 const char *file; 61 char *sender, *p; 62 #if MAXPATHLEN > BUFSIZ 63 char buf[MAXPATHLEN]; 64 #else 65 char buf[BUFSIZ]; 66 #endif 67 68 file = sender = NULL; 69 count = -1; 70 while ((ch = getopt(argc, argv, "cf:s:")) != -1) 71 switch (ch) { 72 case 'c': 73 count = 0; 74 break; 75 case 'f': 76 file = optarg; 77 break; 78 case 's': 79 sender = optarg; 80 for (p = sender; *p; ++p) 81 *p = tolower(*p); 82 break; 83 case '?': 84 default: 85 usage(); 86 } 87 argc -= optind; 88 argv += optind; 89 90 if (file == NULL) { 91 if (argc) { 92 (void)snprintf(buf, sizeof(buf), "%s/%s", _PATH_MAILDIR, *argv); 93 file = buf; 94 } else { 95 if (!(file = getenv("MAIL"))) { 96 if (!(pwd = getpwuid(getuid()))) 97 errx(1, "no password file entry for you"); 98 file = pwd->pw_name; 99 (void)snprintf(buf, sizeof(buf), 100 "%s/%s", _PATH_MAILDIR, file); 101 file = buf; 102 } 103 } 104 } 105 106 /* read from stdin */ 107 if (strcmp(file, "-") == 0) { 108 mbox = stdin; 109 } 110 else if ((mbox = fopen(file, "r")) == NULL) { 111 errx(1, "can't read %s", file); 112 } 113 for (newline = 1; fgets(buf, sizeof(buf), mbox);) { 114 if (*buf == '\n') { 115 newline = 1; 116 continue; 117 } 118 if (newline && !strncmp(buf, "From ", 5) && 119 (!sender || match(buf + 5, sender))) { 120 if (count != -1) 121 count++; 122 else 123 printf("%s", buf); 124 } 125 newline = 0; 126 } 127 if (count != -1) 128 printf("There %s %d message%s in your incoming mailbox.\n", 129 count == 1 ? "is" : "are", count, count == 1 ? "" : "s"); 130 fclose(mbox); 131 exit(0); 132 } 133 134 static void 135 usage(void) 136 { 137 fprintf(stderr, "usage: from [-c] [-f file] [-s sender] [user]\n"); 138 exit(1); 139 } 140 141 static int 142 match(const char *line, const char *sender) 143 { 144 char ch, pch, first; 145 const char *p, *t; 146 147 for (first = *sender++;;) { 148 if (isspace(ch = *line)) 149 return(0); 150 ++line; 151 ch = tolower(ch); 152 if (ch != first) 153 continue; 154 for (p = sender, t = line;;) { 155 if (!(pch = *p++)) 156 return(1); 157 ch = tolower(*t); 158 t++; 159 if (ch != pch) 160 break; 161 } 162 } 163 /* NOTREACHED */ 164 } 165