DataMuseum.dk

Presents historical artifacts from the history of:

DKUUG/EUUG Conference tapes

This is an automatic "excavation" of a thematic subset of
artifacts from Datamuseum.dk's BitArchive.

See our Wiki for more about DKUUG/EUUG Conference tapes

Excavated with: AutoArchaeologist - Free & Open Source Software.


top - metrics - download
Index: T m

⟦0821ad869⟧ TextFile

    Length: 3526 (0xdc6)
    Types: TextFile
    Names: »msgs.c«

Derivation

└─⟦9ae75bfbd⟧ Bits:30007242 EUUGD3: Starter Kit
    └─⟦e84e043a4⟧ »EurOpenD3/mail/pop/trout-popd.tar.Z« 
        └─⟦632bfcc3f⟧ 
            └─⟦this⟧ »msgs.c« 

TextFile

/*
 * This code shamelessly stolen from msgs.c and hacked up for use with popd. 
 * Credit to Berkeley or whoever originally wrote this thing. 
 */
#include <stdio.h>
#include <sys/param.h>
#include <pwd.h>
#include <sys/file.h>
#include <sys/types.h>
#include <sys/stat.h>

#define USRMSGS "/usr/msgs"	/* was in msgs.h */

#define	NO	0
#define	YES	1
#define MSGSRC	".msgsrc"	/* user's rc file */
#define BOUNDS	"bounds"	/* message bounds file */

typedef char    bool;

FILE           *newmsg;
char            inbuf[BUFSIZ];
char            fname[128];
int             msg;

/*
 * Copy msgs messages to the specified file.  If the file is NULL, just
 * return how many messages are waiting. 
 */
int
msgs(dest)
	char           *dest;	/* destination: file where the messages
				 * should go */
{
	FILE           *f;
	bool            newrc;
	int             rcfirst = 0;	/* first message to print (from .rc) */
	int             firstmsg, nextmsg, lastmsg = 0;
	long            t;
	FILE           *bounds, *msgsrc;
	char            msgsrcpath[MAXPATHLEN];
	struct		passwd  *pw;
	struct		stat sbuf;
	int		firstline;

	/*
	 * determine current message bounds 
	 */
	sprintf(fname, "%s/%s", USRMSGS, BOUNDS);
	if ((bounds = fopen(fname, "r")) == NULL)
		return 0;

	fscanf(bounds, "%d %d\n", &firstmsg, &lastmsg);
	fclose(bounds);

	/*
	 * Get the user's .msgsrc file 
	 */
	if ((pw = getpwuid(getuid())) == NULL)
		return 0;
	strcpy(msgsrcpath, pw->pw_dir);
	strcat(msgsrcpath, "/");
	strcat(msgsrcpath, MSGSRC);
	if ((msgsrc = fopen(msgsrcpath, "r")) != NULL) {
		newrc = NO;
		fscanf(msgsrc, "%d\n", &nextmsg);
		fclose(msgsrc);
		if (nextmsg > lastmsg + 1) {
			/* bounds were reset */
			ftruncate(fileno(msgsrc), 0);
			newrc = YES;
		} else
			rcfirst = nextmsg;
		fclose(msgsrc);
	} else
		newrc = YES;

	msgsrc = fopen(msgsrcpath, "a");
	if (msgsrc == NULL)
		return 0;

	if (rcfirst) {
		if (rcfirst > lastmsg + 1)
			rcfirst = nextmsg;
		if (rcfirst > firstmsg)
			firstmsg = rcfirst;	/* don't set below first msg */
	}
	if (newrc) {
		nextmsg = firstmsg;
		fseek(msgsrc, 0L, 0);
		fprintf(msgsrc, "%d\n", nextmsg);
		fflush(msgsrc);
	}
	if (dest == NULL || strlen(dest) == 0) {
		/* just checking.  no copy required */
		fclose(msgsrc);
		return (firstmsg <= lastmsg);
	}
	if ((f = fopen(dest, "a")) == NULL) {
		fclose(msgsrc);
		return 0;
	}
	flock(fileno(f), LOCK_EX);

	for (msg = firstmsg; msg <= lastmsg; msg++) {

		sprintf(fname, "%s/%d", USRMSGS, msg);
		newmsg = fopen(fname, "r");
		if (newmsg == NULL)
			continue;

		/*
		 * Sanity check.
		 * If we get a small or zero length message,
		 * just ignore it.  64 bytes is barely enough
		 * to hold a From:, Date:, and Subject: line.
		 */
		fstat(fileno(newmsg), &sbuf);
		if (sbuf.st_size < 64) {
			fclose(newmsg);
			continue;
		}

		firstline = 1;
		while (fgets(inbuf, sizeof inbuf, newmsg) != NULL) {
			if (firstline) {
				if (strncmp(inbuf, "From ", 5) == 0) {
					fputs(inbuf, f);
				} else {
					time(&t);
					fprintf(f, "From msgs %s", ctime(&t));
				}
				firstline = 0;
			} else {
				if (strncmp(inbuf, "From ", 5) == 0)
					fputs(">", f);
				fputs(inbuf, f);
			}
		}
		/* make sure there is a blank line separator. */
		fprintf(f, "\n");

		fclose(newmsg);
	}

	flock(fileno(f), LOCK_UN);
	fclose(f);

	/*
	 * Make sure .rc file gets updated 
	 */
	if (--msg >= nextmsg) {
		nextmsg = msg + 1;
		fseek(msgsrc, 0L, 0);
		fprintf(msgsrc, "%d\n", nextmsg);
		fflush(msgsrc);
		fclose(msgsrc);
	}
	return ((lastmsg + 1) - firstmsg);
}