h08711
s 00289/00000/00000
d D 1.1 95/07/04 20:12:48 tim 1 0
c 
e
u
U
f e 0
t
T
I 1
#include <stdio.h>
#include <string.h>
#include "project.h"
/* start of meta file operations code */
/* the meta file consists of line terminated data in the following format */
/* the full path component, E.g. LOC::M\MAIL\ */
/* mu current email adress, e.g. tim.graves@uk.sun.com the following repeated */
/* datafile name, E.g. in1.msg or out1.msg */
/* to, a list of recieved addresses, e.g. tim.graves@uk.sun.com MUST contain at least one address */
/* from, the origionator of the email */
/* subject, obvious */
/* cc, the cc list */
/* bcc, the bcc list (empty on reciept )*/
/* read, N if the email is unread, R if read */
/*at the end of the repeats */
/* END */
/* No other items */
/* */
/* read in the data descriptions */
int readmeta(fname, itemlist)
char * fname ;
struct ll * itemlist;
{
	FILE * fd ;
	int ret ;
	if (mtadbg == TRUE)
		printf("CALL: readmeta (fname = %s, itemlist = OMITTED)", fname) ;
	if ((fd = fopen(fname, "r")) == NULL)
	{
		printf("FILE ERROR:  readmeta, error in opening %s", fname) ;
		return (FILEERR) ;
	}
	/* load the one off data */
	ret = getf(fd, itemlist->mydata->mypath) ;
	if ( ret == FALSE)
	{
		printf("FILE ERROR: readmeta, error in reading my path from %s", fname) ;
		return(FILEERR) ;
	}
	ret = getf(fd, itemlist->mydata->myaddr) ;
	if (ret == FALSE)
	{
		printf("FILE ERROR: readmeta, error in reading my address from %s", fname) ;
		return(FILEERR) ;
	}
	while((ret = readhdr(fd, itemlist))== TRUE)
		;
	fclose(fd) ;
	if (ret == FILEERR)
	{
		printf("FILE ERROR: readmeta, error in reading meta record from %s", fname) ;
		return(FILEERR) ;
	}
	return(TRUE) ;
}

/* write the data descriptions out */
wrtmeta(fname, itemlist)
char * fname;
struct ll * itemlist ;
{
	FILE * fd ;
	int ret ;
		
	if (mtadbg == TRUE)
		printf("CALL: wrtmeta (fname = %s, itemlist = OMITTED)", fname) ;
	ret = unlink(fname) ; /* delete the origional file */
	if (ret < 0)
	{
		printf("FILE ERROR: wrtmeta, error in deleting %s prior to output", fname) ;
		return(FILEERR) ;
	}
	if ((fd = fopen(fname, "w")) == NULL)
	{
		printf("FILE ERROR: wrtmeta, error in opening %s for output", fname) ;
		return(FILEERR) ;
	}
	/* write the one off data */
	ret = putf(fd, itemlist->mydata->mypath) ;
	if (ret == FILEERR)
	{
		printf("FILE ERROR: wrtmeta, error in writting my path to %s", fname) ;
		fclose(fd) ;
		return(FILEERR) ;
	}
	ret = putf(fd, itemlist->mydata->myaddr) ;
	if (ret == FILEERR)
	{
		printf("FILE ERROR: wrtmeta, error in writting my address to %s", fname) ;
		fclose(fd) ;
		return(FILEERR) ;
	}
	while(itemlist != NULL)
	{
		ret = wrthdr(fd, itemlist) ;
		if (ret == FILEERR)
			break ;
		itemlist = itemlist->next ;
	}
	if (ret == FILEERR)
	{
		printf("FILE ERROR: wrtmeta, error in writting record to %s", fname) ;
		fclose(fd) ;
		return(FILEERR) ;
	}
	ret = putf(fd, "END") ;
	if (ret == FILEERR)
	{
		printf("FILE ERROR: wrtmeta, error in writting terminator (END) to %s", fname) ;
		fclose(fd) ;
		return(FILEERR) ;
	}
	fclose(fd) ;
	return(TRUE) ;
}

/* read the from, subject and return path etc from the data file */
readhdr(fd, itemlist)
FILE * fd;
struct ll * itemlist;
{
	struct ll * newll ;
	char datafile[20] ;
	char to[100] ;
	char from[100] ;
	char subject[100];
	char cc[100];
	char bcc[100];
	char cread[100] ;
	int read, ret ;
	if (mtadbg == TRUE)
		printf("CALL: readhdr(fd = OMITTED, itemlist = OMITTED)");
	/* read in an entry */
	ret = getf(fd, datafile) ;
	if (ret == FILEERR)
	{
		printf("FILE ERROR: readhdr, error in reading datafile") ;
		return(FILEERR) ;
	}
	if (strcmp(datafile, "END") == 0)
		return(FALSE) ;
	ret = getf(fd, to) ;
	if (ret == FILEERR)
	{
		printf("FILE ERROR: readhdr, error in reading to") ;
		return(FILEERR) ;
	}
	ret = getf(fd, from) ;
	if (ret == FILEERR)
	{
		printf("FILE ERROR: readhdr, error in reading from") ;
		return(FILEERR) ;
	}
	ret = getf(fd, subject) ;
	if (ret == FILEERR)
	{
		printf("FILE ERROR: readhdr, error in reading subject") ;
		return(FILEERR) ;
	}
	ret = getf(fd, cc) ;
	if (ret == FILEERR)
	{
		printf("FILE ERROR: readhdr, error in reading cc") ;
		return(FILEERR) ;
	}
	ret = getf(fd, bcc) ;
	if (ret == FILEERR)
	{
		printf("FILE ERROR: readhdr, error in reading bcc") ;
		return(FILEERR) ;
	}
	ret = getf(fd, cread) ;
	if (ret == FILEERR)
	{
		printf("FILE ERROR: readhdr, error in reading read") ;
		return(FILEERR) ;
	}	
	if (cread[0] == 'R')
		read = RD ;
	else
		read = NEW ;
	
	/* get a new ll structure */
	newll = llnew ();
	/* fill in the structure */
	llfill(newll, datafile, to, from, subject, cc, bcc, ACTIVE, read, itemlist->count + 1) ;
	/* add the new item into the list */
	lladd(newll, itemlist) ;
	return(TRUE) ;
}
/* write a subject and destination etc to the data file */
wrthdr(fd,item)
FILE * fd;
struct ll * item;
{
	int ret ;
	if (mtadbg == TRUE)
		printf("CALL: wrthdr(fd = OMITTED, item = OMITTED)");
	/* only outut the record if its active */
	if (item->status != ACTIVE)
		return(TRUE) ;
	ret = putf(fd, item->datafile);
	if ( ret == FILEERR)
	{
		printf("FILE ERROR: wrthdr, error in writting datafile (%s)", item->datafile) ;
		return(FILEERR) ;
	}
	ret = putf(fd, item->to) ;
	if ( ret == FILEERR)
	{
		printf("FILE ERROR: wrthdr, error in writting to (%s)", item->to) ;
		return(FILEERR) ;
	}
	ret = putf(fd, item->from) ;
	if ( ret == FILEERR)
	{
		printf("FILE ERROR: wrthdr, error in writting from (%s)", item->from) ;
		return(FILEERR) ;
	}
	ret = putf(fd, item->subject) ;
	if ( ret == FILEERR)
	{
		printf("FILE ERROR: wrthdr, error in writting subject (%s)", item->subject) ;
		return(FILEERR) ;
	}
	ret = putf(fd, item->cc) ;
	if ( ret == FILEERR)
	{
		printf("FILE ERROR: wrthdr, error in writting cc (%s)", item->cc) ;
		return(FILEERR) ;
	}
	ret = putf(fd, item->bcc) ;
	if ( ret == FILEERR)
	{
		printf("FILE ERROR: wrthdr, error in writting bcc (%s)", item->bcc) ;
		return(FILEERR) ;
	}
	if (item->read == RD)
		ret = putf(fd, "R") ;
	else
		ret = putf(fd, "N") ;
	if ( ret == FILEERR)
	{
		printf("FILE ERROR: wrthdr, error in writting read (%s)", item->read == RD ? "R" : "N") ;
		return(FILEERR) ;
	}
	return(TRUE) ;
}
/* end of meta file operations code */

/* start of ancillary file and keyboard IO code */
/* read a line of text from file handle */
getf(fd, line)
FILE * fd;
char * line;
{
	char *ret ;
	if (ancdbg == TRUE)
		printf("CALL: getf(fd = OMITTED, line = OMITTED)");
	if (fgets(line, 1024, fd) == NULL)
		return(FILEERR) ;
	/* remove the newline */
	if ((int) strlen(line) > 0)
		if (line[strlen(line)-1] == '\n')
			line[strlen(line) -1] = '\0' ;
	if (ancdbg == TRUE)
	{
		printf("line is :%s:", line) ;
	}

	return(TRUE) ;
}
/* write a line of text to file handle */
putf(fd, line)
FILE * fd;
char * line;
{
	int ret ;
	if (ancdbg == TRUE)
		printf("CALL: putf (fd = OMITTED, line = %s)", line) ;
	ret = fprintf(fd, "%s\n", line) ;
	if (ret < 0)
		return(FILEERR) ;
	if (ancdbg == TRUE)
	{
		printf("ret is %d, line is :%s:", ret, line) ;
	}
	return(TRUE) ;
}
E 1
