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