1 /* 2 * Copyright 1994 Sun Microsystems, Inc. All rights reserved. 3 * Use is subject to license terms. 4 */ 5 6 /* 7 * Copyright (c) 1980 Regents of the University of California. 8 * All rights reserved. The Berkeley software License Agreement 9 * specifies the terms and conditions for redistribution. 10 */ 11 12 #pragma ident "%Z%%M% %I% %E% SMI" 13 14 #include <stdio.h> 15 #include <sys/types.h> 16 #include <unistd.h> 17 #include <stdlib.h> 18 #include <ctype.h> 19 #include <pwd.h> 20 #include <sys/param.h> 21 #include <string.h> 22 23 static int match(char *, char *); 24 25 main(argc, argv) 26 int argc; 27 register char **argv; 28 { 29 char lbuf[BUFSIZ]; 30 char lbuf2[BUFSIZ]; 31 register struct passwd *pp; 32 int stashed = 0; 33 register char *name; 34 char *sender = NULL; 35 char mailbox[MAXPATHLEN]; 36 char *tmp_mailbox; 37 extern char *optarg; 38 extern int optind; 39 extern int opterr; 40 int c; 41 int errflg = 0; 42 43 opterr = 0; 44 while ((c = getopt(argc, argv, "s:")) != EOF) 45 switch (c) { 46 case 's': 47 sender = optarg; 48 for (name = sender; *name; name++) 49 if (isupper(*name)) 50 *name = tolower(*name); 51 break; 52 case '?': 53 errflg++; 54 break; 55 } 56 if (errflg) { 57 (void) fprintf(stderr, 58 "Usage: from [-s sender] [user]\n"); 59 exit(1); 60 } 61 62 if (optind < argc) { 63 (void) sprintf(mailbox, "/var/mail/%s", argv[optind]); 64 } else { 65 if (tmp_mailbox = getenv("MAIL")) { 66 (void) strcpy(mailbox, tmp_mailbox); 67 } else { 68 name = getlogin(); 69 if (name == NULL || strlen(name) == 0) { 70 pp = getpwuid(getuid()); 71 if (pp == NULL) { 72 (void) fprintf(stderr, 73 "Who are you?\n"); 74 exit(1); 75 } 76 name = pp->pw_name; 77 } 78 (void) sprintf(mailbox, "/var/mail/%s", name); 79 } 80 } 81 if (freopen(mailbox, "r", stdin) == NULL) { 82 (void) fprintf(stderr, "Can't open %s\n", mailbox); 83 exit(0); 84 } 85 while (fgets(lbuf, sizeof (lbuf), stdin) != NULL) 86 if (lbuf[0] == '\n' && stashed) { 87 stashed = 0; 88 (void) printf("%s", lbuf2); 89 } else if (strncmp(lbuf, "From ", 5) == 0 && 90 (sender == NULL || match(&lbuf[4], sender))) { 91 (void) strcpy(lbuf2, lbuf); 92 stashed = 1; 93 } 94 if (stashed) 95 (void) printf("%s", lbuf2); 96 return(0); 97 } 98 99 static int 100 match(line, str) 101 register char *line, *str; 102 { 103 register char ch; 104 105 while (*line == ' ' || *line == '\t') 106 ++line; 107 if (*line == '\n') 108 return (0); 109 while (*str && *line != ' ' && *line != '\t' && *line != '\n') { 110 ch = isupper(*line) ? tolower(*line) : *line; 111 if (ch != *str++) 112 return (0); 113 line++; 114 } 115 return (*str == '\0'); 116 } 117