1: /*
   2:  * Copyright (c) 1980 Regents of the University of California.
   3:  * All rights reserved.  The Berkeley software License Agreement
   4:  * specifies the terms and conditions for redistribution.
   5:  */
   6: 
   7: #if !defined(lint) && defined(DOSCCS)
   8: char copyright[] =
   9: "@(#) Copyright (c) 1980 Regents of the University of California.\n\
  10:  All rights reserved.\n";
  11: 
  12: static char sccsid[] = "@(#)from.c	5.2.1 (2.11BSD) 1997/10/2";
  13: #endif
  14: 
  15: #include <stdio.h>
  16: #include <ctype.h>
  17: #include <pwd.h>
  18: #include <unistd.h>
  19: 
  20: main(argc, argv)
  21:     int argc;
  22:     register char **argv;
  23: {
  24:     char lbuf[BUFSIZ];
  25:     char lbuf2[BUFSIZ];
  26:     register struct passwd *pp;
  27:     int stashed = 0;
  28:     register char *name;
  29:     char *sender;
  30: 
  31:     if (argc > 1 && *(argv[1]) == '-' && (*++argv)[1] == 's') {
  32:         if (--argc <= 1) {
  33:             fprintf (stderr, "Usage: from [-s sender] [user]\n");
  34:             exit (1);
  35:         }
  36:         --argc;
  37:         sender = *++argv;
  38:         for (name = sender; *name; name++)
  39:             if (isupper(*name))
  40:                 *name = tolower(*name);
  41: 
  42:     } else
  43:         sender = NULL;
  44:     if (chdir("/usr/spool/mail") < 0)
  45:         exit(1);
  46:     if (argc > 1)
  47:         name = argv[1];
  48:     else {
  49:         name = getlogin();
  50:         if (name == NULL || strlen(name) == 0) {
  51:             pp = getpwuid(getuid());
  52:             if (pp == NULL) {
  53:                 fprintf(stderr, "Who are you?\n");
  54:                 exit(1);
  55:             }
  56:             name = pp->pw_name;
  57:         }
  58:     }
  59:     if (freopen(name, "r", stdin) == NULL) {
  60:         fprintf(stderr, "Can't open /usr/spool/mail/%s\n", name);
  61:         exit(0);
  62:     }
  63:     while (fgets(lbuf, sizeof lbuf, stdin) != NULL)
  64:         if (lbuf[0] == '\n' && stashed) {
  65:             stashed = 0;
  66:             printf("%s", lbuf2);
  67:         } else if (strncmp(lbuf, "From ", 5) == 0 &&
  68:             (sender == NULL || match(&lbuf[4], sender))) {
  69:             strcpy(lbuf2, lbuf);
  70:             stashed = 1;
  71:         }
  72:     if (stashed)
  73:         printf("%s", lbuf2);
  74:     exit(0);
  75: }
  76: 
  77: match (line, str)
  78:     register char *line, *str;
  79: {
  80:     register char ch;
  81: 
  82:     while (*line == ' ' || *line == '\t')
  83:         ++line;
  84:     if (*line == '\n')
  85:         return (0);
  86:     while (*str && *line != ' ' && *line != '\t' && *line != '\n') {
  87:         ch = isupper(*line) ? tolower(*line) : *line;
  88:         if (ch != *str++)
  89:             return (0);
  90:         line++;
  91:     }
  92:     return (*str == '\0');
  93: }

Defined functions

main defined in line 20; never used
match defined in line 77; used 1 times
  • in line 68

Defined variables

copyright defined in line 8; never used
sccsid defined in line 12; never used
Last modified: 1997-10-03
Generated: 2016-12-26
Generated by src2html V0.67
page hit count: 589
Valid CSS Valid XHTML 1.0 Strict