/* * Check for new and old mail. * Brian Katzung 15 July 1986 */ #include #include char *getenv(); char *spool[] = { "/usr/spool/mail", "/usr/mail" }; #define NSPOOL (sizeof(spool) / sizeof(spool[0])) main (ac, av) char **av; { struct passwd *pwp, *getpwuid(); char file[256]; char line[1024]; int num; register char *cp; short hasmail; short newmail; short unread; short sawfrom; FILE *mfd; /* * Look for a mail spool directory. */ *file = '\0'; for (num = 0; num < NSPOOL && access(spool[num], 5); ++num) ; if (num < NSPOOL) sprintf(file, "%s/", spool[num]); /* * Default is current user's mail file. */ if (ac < 2) { /* Use $MAIL if set */ if ((cp = getenv("MAIL")) && (*cp != '\0')) strcpy(file, cp); /* Otherwise, use spool directory and login name */ else { if ((pwp = getpwuid(getuid())) == NULL) { fputs("mailchk: Can't get passwd entry.\n", stderr); exit(1); } strcat(file, pwp->pw_name); } } /* * If argument present, try a user's mail file or a file name. */ else if (strchr(av[1], '/')) strcpy(file, av[1]); else strcat(file, av[1]); if (access(file, 0)) exit(0); if ((mfd = fopen(file, "r")) == NULL) { perror(file); exit(1); } hasmail = 0; newmail = 0; unread = 0; sawfrom = 0; while (fgets(line, sizeof(line), mfd) != NULL) { /* * Look for Unix From lines. */ if (strncmp(line, "From ", 5) == 0) { ++hasmail; ++sawfrom; } /* * See if Mail has ever noticed message. */ if (sawfrom && strncmp(line, "Status: ", 8) == 0) { for (cp = &line[8]; *cp && *cp != 'R'; ++cp); if (*cp != 'R') ++unread; sawfrom = 0; } /* * Check for new messages. */ if (sawfrom && *line == '\n') { ++newmail; sawfrom = 0; } } if (hasmail) { if (ac < 2) printf("You have"); else printf("%s has", av[1]); printf(" mail (%d total, %d new, %d unread).\n", hasmail, newmail, newmail + unread); } }