17c478bd9Sstevel@tonic-gate /*
2*032624d5Sbasabi * Copyright 2005 Sun Microsystems, Inc. All rights reserved.
37c478bd9Sstevel@tonic-gate * Use is subject to license terms.
47c478bd9Sstevel@tonic-gate */
57c478bd9Sstevel@tonic-gate
67c478bd9Sstevel@tonic-gate /*
77c478bd9Sstevel@tonic-gate * Copyright (c) 1980 Regents of the University of California.
87c478bd9Sstevel@tonic-gate * All rights reserved. The Berkeley software License Agreement
97c478bd9Sstevel@tonic-gate * specifies the terms and conditions for redistribution.
107c478bd9Sstevel@tonic-gate */
117c478bd9Sstevel@tonic-gate
127c478bd9Sstevel@tonic-gate #pragma ident "%Z%%M% %I% %E% SMI"
137c478bd9Sstevel@tonic-gate
147c478bd9Sstevel@tonic-gate #include <stdio.h>
157c478bd9Sstevel@tonic-gate #include <sys/types.h>
167c478bd9Sstevel@tonic-gate #include <unistd.h>
177c478bd9Sstevel@tonic-gate #include <stdlib.h>
187c478bd9Sstevel@tonic-gate #include <ctype.h>
197c478bd9Sstevel@tonic-gate #include <pwd.h>
207c478bd9Sstevel@tonic-gate #include <sys/param.h>
217c478bd9Sstevel@tonic-gate #include <string.h>
227c478bd9Sstevel@tonic-gate
237c478bd9Sstevel@tonic-gate static int match(char *, char *);
247c478bd9Sstevel@tonic-gate
25*032624d5Sbasabi int
main(int argc,char ** argv)26*032624d5Sbasabi main(int argc, char **argv)
277c478bd9Sstevel@tonic-gate {
287c478bd9Sstevel@tonic-gate char lbuf[BUFSIZ];
297c478bd9Sstevel@tonic-gate char lbuf2[BUFSIZ];
30*032624d5Sbasabi struct passwd *pp;
317c478bd9Sstevel@tonic-gate int stashed = 0;
32*032624d5Sbasabi char *name;
337c478bd9Sstevel@tonic-gate char *sender = NULL;
347c478bd9Sstevel@tonic-gate char mailbox[MAXPATHLEN];
357c478bd9Sstevel@tonic-gate char *tmp_mailbox;
367c478bd9Sstevel@tonic-gate extern char *optarg;
377c478bd9Sstevel@tonic-gate extern int optind;
387c478bd9Sstevel@tonic-gate extern int opterr;
397c478bd9Sstevel@tonic-gate int c;
407c478bd9Sstevel@tonic-gate int errflg = 0;
417c478bd9Sstevel@tonic-gate
427c478bd9Sstevel@tonic-gate opterr = 0;
437c478bd9Sstevel@tonic-gate while ((c = getopt(argc, argv, "s:")) != EOF)
447c478bd9Sstevel@tonic-gate switch (c) {
457c478bd9Sstevel@tonic-gate case 's':
467c478bd9Sstevel@tonic-gate sender = optarg;
477c478bd9Sstevel@tonic-gate for (name = sender; *name; name++)
487c478bd9Sstevel@tonic-gate if (isupper(*name))
497c478bd9Sstevel@tonic-gate *name = tolower(*name);
507c478bd9Sstevel@tonic-gate break;
517c478bd9Sstevel@tonic-gate case '?':
527c478bd9Sstevel@tonic-gate errflg++;
537c478bd9Sstevel@tonic-gate break;
547c478bd9Sstevel@tonic-gate }
557c478bd9Sstevel@tonic-gate if (errflg) {
567c478bd9Sstevel@tonic-gate (void) fprintf(stderr,
577c478bd9Sstevel@tonic-gate "Usage: from [-s sender] [user]\n");
587c478bd9Sstevel@tonic-gate exit(1);
597c478bd9Sstevel@tonic-gate }
607c478bd9Sstevel@tonic-gate
617c478bd9Sstevel@tonic-gate if (optind < argc) {
627c478bd9Sstevel@tonic-gate (void) sprintf(mailbox, "/var/mail/%s", argv[optind]);
637c478bd9Sstevel@tonic-gate } else {
647c478bd9Sstevel@tonic-gate if (tmp_mailbox = getenv("MAIL")) {
657c478bd9Sstevel@tonic-gate (void) strcpy(mailbox, tmp_mailbox);
667c478bd9Sstevel@tonic-gate } else {
677c478bd9Sstevel@tonic-gate name = getlogin();
687c478bd9Sstevel@tonic-gate if (name == NULL || strlen(name) == 0) {
697c478bd9Sstevel@tonic-gate pp = getpwuid(getuid());
707c478bd9Sstevel@tonic-gate if (pp == NULL) {
717c478bd9Sstevel@tonic-gate (void) fprintf(stderr,
727c478bd9Sstevel@tonic-gate "Who are you?\n");
737c478bd9Sstevel@tonic-gate exit(1);
747c478bd9Sstevel@tonic-gate }
757c478bd9Sstevel@tonic-gate name = pp->pw_name;
767c478bd9Sstevel@tonic-gate }
777c478bd9Sstevel@tonic-gate (void) sprintf(mailbox, "/var/mail/%s", name);
787c478bd9Sstevel@tonic-gate }
797c478bd9Sstevel@tonic-gate }
807c478bd9Sstevel@tonic-gate if (freopen(mailbox, "r", stdin) == NULL) {
817c478bd9Sstevel@tonic-gate (void) fprintf(stderr, "Can't open %s\n", mailbox);
827c478bd9Sstevel@tonic-gate exit(0);
837c478bd9Sstevel@tonic-gate }
847c478bd9Sstevel@tonic-gate while (fgets(lbuf, sizeof (lbuf), stdin) != NULL)
857c478bd9Sstevel@tonic-gate if (lbuf[0] == '\n' && stashed) {
867c478bd9Sstevel@tonic-gate stashed = 0;
877c478bd9Sstevel@tonic-gate (void) printf("%s", lbuf2);
887c478bd9Sstevel@tonic-gate } else if (strncmp(lbuf, "From ", 5) == 0 &&
897c478bd9Sstevel@tonic-gate (sender == NULL || match(&lbuf[4], sender))) {
907c478bd9Sstevel@tonic-gate (void) strcpy(lbuf2, lbuf);
917c478bd9Sstevel@tonic-gate stashed = 1;
927c478bd9Sstevel@tonic-gate }
937c478bd9Sstevel@tonic-gate if (stashed)
947c478bd9Sstevel@tonic-gate (void) printf("%s", lbuf2);
957c478bd9Sstevel@tonic-gate return (0);
967c478bd9Sstevel@tonic-gate }
977c478bd9Sstevel@tonic-gate
987c478bd9Sstevel@tonic-gate static int
match(char * line,char * str)99*032624d5Sbasabi match(char *line, char *str)
1007c478bd9Sstevel@tonic-gate {
101*032624d5Sbasabi char ch;
1027c478bd9Sstevel@tonic-gate
1037c478bd9Sstevel@tonic-gate while (*line == ' ' || *line == '\t')
1047c478bd9Sstevel@tonic-gate ++line;
1057c478bd9Sstevel@tonic-gate if (*line == '\n')
1067c478bd9Sstevel@tonic-gate return (0);
1077c478bd9Sstevel@tonic-gate while (*str && *line != ' ' && *line != '\t' && *line != '\n') {
1087c478bd9Sstevel@tonic-gate ch = isupper(*line) ? tolower(*line) : *line;
1097c478bd9Sstevel@tonic-gate if (ch != *str++)
1107c478bd9Sstevel@tonic-gate return (0);
1117c478bd9Sstevel@tonic-gate line++;
1127c478bd9Sstevel@tonic-gate }
1137c478bd9Sstevel@tonic-gate return (*str == '\0');
1147c478bd9Sstevel@tonic-gate }
115