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