10Sstevel@tonic-gate /*
2*523Sbasabi * Copyright 2005 Sun Microsystems, Inc. All rights reserved.
30Sstevel@tonic-gate * Use is subject to license terms.
40Sstevel@tonic-gate */
50Sstevel@tonic-gate
60Sstevel@tonic-gate /*
70Sstevel@tonic-gate * Copyright (c) 1980 Regents of the University of California.
80Sstevel@tonic-gate * All rights reserved. The Berkeley software License Agreement
90Sstevel@tonic-gate * specifies the terms and conditions for redistribution.
100Sstevel@tonic-gate */
110Sstevel@tonic-gate
120Sstevel@tonic-gate #pragma ident "%Z%%M% %I% %E% SMI"
130Sstevel@tonic-gate
140Sstevel@tonic-gate #include <stdio.h>
150Sstevel@tonic-gate #include <sys/types.h>
160Sstevel@tonic-gate #include <unistd.h>
170Sstevel@tonic-gate #include <stdlib.h>
180Sstevel@tonic-gate #include <ctype.h>
190Sstevel@tonic-gate #include <pwd.h>
200Sstevel@tonic-gate #include <sys/param.h>
210Sstevel@tonic-gate #include <string.h>
220Sstevel@tonic-gate
230Sstevel@tonic-gate static int match(char *, char *);
240Sstevel@tonic-gate
25*523Sbasabi int
main(int argc,char ** argv)26*523Sbasabi main(int argc, char **argv)
270Sstevel@tonic-gate {
280Sstevel@tonic-gate char lbuf[BUFSIZ];
290Sstevel@tonic-gate char lbuf2[BUFSIZ];
30*523Sbasabi struct passwd *pp;
310Sstevel@tonic-gate int stashed = 0;
32*523Sbasabi char *name;
330Sstevel@tonic-gate char *sender = NULL;
340Sstevel@tonic-gate char mailbox[MAXPATHLEN];
350Sstevel@tonic-gate char *tmp_mailbox;
360Sstevel@tonic-gate extern char *optarg;
370Sstevel@tonic-gate extern int optind;
380Sstevel@tonic-gate extern int opterr;
390Sstevel@tonic-gate int c;
400Sstevel@tonic-gate int errflg = 0;
410Sstevel@tonic-gate
420Sstevel@tonic-gate opterr = 0;
430Sstevel@tonic-gate while ((c = getopt(argc, argv, "s:")) != EOF)
440Sstevel@tonic-gate switch (c) {
450Sstevel@tonic-gate case 's':
460Sstevel@tonic-gate sender = optarg;
470Sstevel@tonic-gate for (name = sender; *name; name++)
480Sstevel@tonic-gate if (isupper(*name))
490Sstevel@tonic-gate *name = tolower(*name);
500Sstevel@tonic-gate break;
510Sstevel@tonic-gate case '?':
520Sstevel@tonic-gate errflg++;
530Sstevel@tonic-gate break;
540Sstevel@tonic-gate }
550Sstevel@tonic-gate if (errflg) {
560Sstevel@tonic-gate (void) fprintf(stderr,
570Sstevel@tonic-gate "Usage: from [-s sender] [user]\n");
580Sstevel@tonic-gate exit(1);
590Sstevel@tonic-gate }
600Sstevel@tonic-gate
610Sstevel@tonic-gate if (optind < argc) {
620Sstevel@tonic-gate (void) sprintf(mailbox, "/var/mail/%s", argv[optind]);
630Sstevel@tonic-gate } else {
640Sstevel@tonic-gate if (tmp_mailbox = getenv("MAIL")) {
650Sstevel@tonic-gate (void) strcpy(mailbox, tmp_mailbox);
660Sstevel@tonic-gate } else {
670Sstevel@tonic-gate name = getlogin();
680Sstevel@tonic-gate if (name == NULL || strlen(name) == 0) {
690Sstevel@tonic-gate pp = getpwuid(getuid());
700Sstevel@tonic-gate if (pp == NULL) {
710Sstevel@tonic-gate (void) fprintf(stderr,
720Sstevel@tonic-gate "Who are you?\n");
730Sstevel@tonic-gate exit(1);
740Sstevel@tonic-gate }
750Sstevel@tonic-gate name = pp->pw_name;
760Sstevel@tonic-gate }
770Sstevel@tonic-gate (void) sprintf(mailbox, "/var/mail/%s", name);
780Sstevel@tonic-gate }
790Sstevel@tonic-gate }
800Sstevel@tonic-gate if (freopen(mailbox, "r", stdin) == NULL) {
810Sstevel@tonic-gate (void) fprintf(stderr, "Can't open %s\n", mailbox);
820Sstevel@tonic-gate exit(0);
830Sstevel@tonic-gate }
840Sstevel@tonic-gate while (fgets(lbuf, sizeof (lbuf), stdin) != NULL)
850Sstevel@tonic-gate if (lbuf[0] == '\n' && stashed) {
860Sstevel@tonic-gate stashed = 0;
870Sstevel@tonic-gate (void) printf("%s", lbuf2);
880Sstevel@tonic-gate } else if (strncmp(lbuf, "From ", 5) == 0 &&
890Sstevel@tonic-gate (sender == NULL || match(&lbuf[4], sender))) {
900Sstevel@tonic-gate (void) strcpy(lbuf2, lbuf);
910Sstevel@tonic-gate stashed = 1;
920Sstevel@tonic-gate }
930Sstevel@tonic-gate if (stashed)
940Sstevel@tonic-gate (void) printf("%s", lbuf2);
95*523Sbasabi return (0);
960Sstevel@tonic-gate }
970Sstevel@tonic-gate
980Sstevel@tonic-gate static int
match(char * line,char * str)99*523Sbasabi match(char *line, char *str)
1000Sstevel@tonic-gate {
101*523Sbasabi char ch;
1020Sstevel@tonic-gate
1030Sstevel@tonic-gate while (*line == ' ' || *line == '\t')
1040Sstevel@tonic-gate ++line;
1050Sstevel@tonic-gate if (*line == '\n')
1060Sstevel@tonic-gate return (0);
1070Sstevel@tonic-gate while (*str && *line != ' ' && *line != '\t' && *line != '\n') {
1080Sstevel@tonic-gate ch = isupper(*line) ? tolower(*line) : *line;
1090Sstevel@tonic-gate if (ch != *str++)
1100Sstevel@tonic-gate return (0);
1110Sstevel@tonic-gate line++;
1120Sstevel@tonic-gate }
1130Sstevel@tonic-gate return (*str == '\0');
1140Sstevel@tonic-gate }
115