4.3BSD-UWisc/src/ucb/from/from.c

Compare this file to the similar file:
Show the results in this format:

/*
 * Copyright (c) 1980 Regents of the University of California.
 * All rights reserved.  The Berkeley software License Agreement
 * specifies the terms and conditions for redistribution.
 */

#ifndef lint
char copyright[] =
"@(#) Copyright (c) 1980 Regents of the University of California.\n\
 All rights reserved.\n";
#endif not lint

#ifndef lint
static char sccsid[] = "@(#)from.c	5.2 (Berkeley) 11/4/85";
#endif not lint

#include <stdio.h>
#include <ctype.h>
#include <pwd.h>

struct	passwd *getpwuid();

main(argc, argv)
	int argc;
	register char **argv;
{
	char lbuf[BUFSIZ];
	char lbuf2[BUFSIZ];
#ifdef UW
	char dbuf[BUFSIZ], sbuf[BUFSIZ];
#endif UW
	register struct passwd *pp;
	int stashed = 0;
	register char *name;
	char *sender;
#ifndef UW
    char *getlogin();
#endif  UW

	if (argc > 1 && *(argv[1]) == '-' && (*++argv)[1] == 's') {
		if (--argc <= 1) {
			fprintf (stderr, "Usage: from [-s sender] [user]\n");
			exit (1);
		}
		--argc;
		sender = *++argv;
		for (name = sender; *name; name++)
			if (isupper(*name))
				*name = tolower(*name);

	} else
		sender = NULL;
	if (chdir("/usr/spool/mail") < 0)
		exit(1);
	if (argc > 1)
		name = argv[1];
	else {
#ifndef UW
		name = getlogin ();
		if (name == NULL || strlen(name) == 0) {
#endif UW
			pp = getpwuid(getuid());
			if (pp == NULL) {
				fprintf(stderr, "Who are you?\n");
				exit(1);
			}
			name = pp->pw_name;
#ifndef UW
		}
#endif UW
	}
	if (freopen(name, "r", stdin) == NULL) {
		fprintf(stderr, "Can't open /usr/spool/mail/%s\n", name);
		exit(0);
	}
#ifdef  UW
    *sbuf = 0;
	while(fgets(lbuf, sizeof lbuf, stdin) != NULL) {
        lbuf[strlen(lbuf)-1] = 0;
        if (lbuf[0] == 0 && stashed) {
            if(*sbuf == 0)
                printf("> %s\n", lbuf2);
            else
				printf("> %-47.47s [%s]\n", lbuf2, sbuf);
            *sbuf = stashed = 0;
        } else if ((strncmp(lbuf, "From ", 5) == 0) &&
            (sender == NULL || match(&lbuf[4], sender))) {
            strcpy(lbuf2, lbuf+5);
            strcpy(dbuf, lbuf+strlen(lbuf)-25); /* save the date */
			stashed = 1;
        } else if ((strncmp(lbuf, "From:", 5) == 0) && stashed &&
            (sender == NULL || match(&lbuf[4], sender))) {
            strcpy(lbuf2, lbuf+6);
			strcpy(lbuf2+strlen(lbuf2), dbuf);
        } else if ((strncmp(lbuf, "Subject:", 8) == 0) && stashed) {
            strcpy(sbuf, lbuf+9);
            sbuf[27] = 0;
        }
	}
	if (stashed)
        if(*sbuf == 0)
            printf("> %s\n", lbuf2);
        else
            printf("> %-47.47s [%s]\n", lbuf2, sbuf);
#else
	while (fgets(lbuf, sizeof lbuf, stdin) != NULL)
		if (lbuf[0] == '\n' && stashed) {
			stashed = 0;
			printf("%s", lbuf2);
		} else if (strncmp(lbuf, "From ", 5) == 0 &&
		    (sender == NULL || match(&lbuf[4], sender))) {
			strcpy(lbuf2, lbuf);
			stashed = 1;
		}
	if (stashed)
		printf("%s", lbuf2);
#endif UW
	exit(0);
}

match (line, str)
	register char *line, *str;
{
	register char ch;

	while (*line == ' ' || *line == '\t')
		++line;
	if (*line == '\n')
		return (0);
	while (*str && *line != ' ' && *line != '\t' && *line != '\n') {
		ch = isupper(*line) ? tolower(*line) : *line;
		if (ch != *str++)
			return (0);
		line++;
	}
	return (*str == '\0');
}