[net.sources] A different

buhrt@pur-ee.UUCP (Jeffery A Buhrt) (08/10/86)

Checkmail is a program we wrote a few years ago to
print out mail headers in a cute way.
----
checkmail
	Mail checking program

Checkmail (and linked names) will look for and print mail
or just newmail found in /usr/spool/mail/loginname.

The messages will be printed as:
Name set in From: line............Message from Subject: line

Written by Jeff Buhrt && Curtis Smith

						-Jeff Buhrt
						buhrt@ee.purdue.edu
						pur-ee!buhrt


# This is a shell archive.  Save this into a file, edit it
# and delete all lines above this comment.  Then give this
# file to sh by executing the command "sh file".  The files
# will be extracted into the current directory owned by
# you with default permissions.
#
# The files contained herein are:
#     checkmail.c       Makefile
#
echo 'x - checkmail.c'
sed 's/^X//' <<'________This_Is_The_END________' >>checkmail.c
X/* checkmail
X *	Mail checking program
X *
X * Checkmail (and linked names) will look for and print mail
X * or just newmail found in /usr/spool/mail/loginname.
X *
X * The messages will be printed as:
X *	Name set in From: line............Message from Subject: line
X *
X * Written by Jeff Buhrt (buhrt@ee.purdue.edu) && Curtis Smith
X */
X
X#include <stdio.h>
X#include <pwd.h>
X#include <strings.h>
X#include <errno.h>
X
Xtypedef enum { false = 0, true = 1 } boolean;
X
Xstruct mstr {
X	boolean	hasread;
X	char	from[64];
X	char	subject[64];
X};
X
Xstatic char	idstring[]="Checkmail/checkmail.c Jeff Buhrt/Curtis Smith 8/9/86";
X
Xchar		hostname[16];
Xchar		mailname[] = "/usr/spool/mail/\000xxxxxxxx";
Xchar *		username;
Xint		uid;
Xint		mailcount = 0;
Xint		mailunread = 0;
Xboolean		newmailonly = false;
Xstruct mstr	maillist[256];
Xstruct mstr *	m = maillist;
Xextern char *	sys_errlist[];
Xextern int	errno;
X
Xgetmailname()
X{
X	struct passwd * passent;
X
X	passent = getpwuid(uid);
X	if (passent == (struct passwd *) 0) {
X		fprintf(stderr, "User-id %d is not defined on machine %s\n",
X			uid, hostname);
X		exit(1);
X	}
X	strcat(mailname, passent->pw_name);
X	username = passent->pw_name;
X}
X
Xinit()
X{
X	uid = getuid();
X	gethostname(hostname, sizeof(hostname));
X	hostname[sizeof(hostname)-1] = '\0';
X}
X
Xcutepr(head, tail, dot)
Xchar * head, * tail, dot;
X{
X	int dots, headlen, spaceleft, taillen;
X
X	headlen = strlen(head);
X	taillen = strlen(tail);
X	if (headlen > 38)
X		headlen = 38;
X	spaceleft = 79 - headlen - 2;
X	if (taillen > spaceleft)
X		taillen = spaceleft;
X	dots = 2 + spaceleft - taillen;
X	printf("%*.*s", headlen, headlen, head);
X	while (dots-- > 0)
X		fputc(dot, stdout);
X	printf("%*.*s\n", taillen, taillen, tail);
X}
X
Xmain(argc,argv)
Xint	argc;
Xchar	**argv;
X{
X	register char	*name;
X
X	if ((name = rindex(*argv, '/')) != NULL)
X		name++;
X	else
X		name = *argv;
X
X	if (((strcmp(name, "newmail") == 0) || strcmp(name, "NM") == 0))
X		newmailonly = true;
X
X	init();
X	getmailname();
X	readmail();
X	header();
X}
X
Xreadmail()
X{
X	FILE * ms;
X	boolean control, hasread;
X	char * ep, * sp, inputline[512];
X
X	ms = fopen(mailname, "r");
X	if (ms == (FILE *) 0) {
X		if (errno == ENOENT) {
X			printf("No mail for %s on %s\n", username, hostname);
X			exit(0);
X		}
X		fprintf(stderr, "%s on %s\n", sys_errlist[errno], mailname);
X		exit(1);
X	}
X	control = false;
X	while (fgets(inputline, sizeof(inputline), ms) != (char *) 0) {
X		sp = index(inputline, '\n');
X		if (sp != (char *) 0)
X			*sp = '\0';
X		if (strncmp(inputline, "From ", 5) == 0) {
X			control = true;
X			hasread = false;
X			m->hasread = false;
X			mailcount++;
X			continue;
X		}
X		if ((control) && (*inputline == '\0')) {
X			control = false;
X			m++;
X			if (hasread == false)
X				mailunread++;
X			continue;
X		}
X		if ((control) && (strncmp(inputline, "Subject: ", 9) == 0)) {
X			strncpy(m->subject, inputline+9, sizeof(m->subject));
X			continue;
X		}
X		if ((control) && (strncmp(inputline, "From: ", 6) == 0)) {
X			sp = index(inputline, '(');
X			ep = rindex(inputline, ')');
X			if ((sp == (char *) 0) || (ep == (char *) 0)) {
X				strncpy(m->from, inputline+6, sizeof(m->from));
X				continue;
X			}
X			*ep = '\0';
X			strncpy(m->from, sp+1, sizeof(m->from));
X		}
X		if ((control) && (strncmp(inputline, "Status: ", 8) == 0)) {
X			hasread = true;
X			m->hasread = true;
X			continue;
X		}
X	}
X	fclose(ms);
X}
X
Xheader()
X{
X	char head[128], tail[128];
X	int count;
X	struct mstr * mp;
X
X	if (mailcount == 0) {
X		printf("No messages for %s on %s\n", username, hostname);
X		exit(0);
X	}
X
X	sprintf(head, "Mail (%s on %s)", username, hostname);
X	if (mailunread != mailcount)
X		sprintf(tail, "%d Letter%s (%d unread)",
X			mailcount, mailcount == 1 ? "" : "s", mailunread);
X	else
X		sprintf(tail, "%d Letter%s", mailcount, mailcount == 1 ?
X			"" : "s");
X	cutepr(head, tail, ' ');
X	fputc('\n', stdout);
X	count = 1;
X	for (mp = maillist; mp < m; mp++)
X	{
X		sprintf(head, "%c %2d. %s",
X			mp->hasread == false ? '*' : ' ', count++, mp->from);
X
X		if ((newmailonly == false) || (mp->hasread == false))
X			cutepr(head, mp->subject, '.');
X	}
X}
________This_Is_The_END________
echo 'x - Makefile'
sed 's/^X//' <<'________This_Is_The_END________' >>Makefile
XCC= cc
XCFLAGS=-s -O
XFILE= checkmail.c
XOBJ= checkmail.o
XLIBS=
XBINDIR=.
X
Xtabkill:
X	cc ${FILE} ${CFLAGS} ${LIBS} -o ${BINDIR}/checkmail
X	-rm ${BINDIR}/newmail ${BINDIR}/M ${BINDIR}/NM
X	ln ${BINDIR}/checkmail ${BINDIR}/newmail
X	ln ${BINDIR}/checkmail ${BINDIR}/M
X	ln ${BINDIR}/checkmail ${BINDIR}/NM
________This_Is_The_END________
exit