130163Sbostic /* 230163Sbostic * Copyright (c) 1986 Regents of the University of California. 330163Sbostic * All rights reserved. The Berkeley software License Agreement 430163Sbostic * specifies the terms and conditions for redistribution. 530163Sbostic */ 630163Sbostic 730163Sbostic #ifndef lint 8*30947Sbostic static char sccsid[] = "@(#)process.c 5.4 (Berkeley) 87/04/23"; 930163Sbostic #endif not lint 1030163Sbostic 1130163Sbostic #include <bug.h> 1230163Sbostic #include <sys/file.h> 1330425Sbostic #include <sys/time.h> 1430163Sbostic #include <stdio.h> 1530890Sbostic #include <ctype.h> 1630163Sbostic 1730163Sbostic char pfile[MAXPATHLEN]; /* permanent file name */ 1830163Sbostic 1930163Sbostic /* 2030163Sbostic * process -- 2130890Sbostic * copy report to permanent file, 2230890Sbostic * update summary file. 2330163Sbostic */ 2430163Sbostic process() 2530163Sbostic { 2630163Sbostic register int rval; /* read return value */ 2730425Sbostic struct timeval tp; /* time of day */ 2830890Sbostic int lfd; /* lock file descriptor */ 2930425Sbostic char *ctime(); 3030163Sbostic 3130890Sbostic if (access(LOCK_FILE, R_OK) || (lfd = open(LOCK_FILE, O_RDONLY, 0)) < 0) 3230890Sbostic error("can't find lock file %s.", LOCK_FILE); 3330890Sbostic if (flock(lfd, LOCK_EX)) 3430890Sbostic error("can't get lock.", CHN); 3530890Sbostic sprintf(pfile, "%s/%s/%d", dir, folder, getnext()); 3630890Sbostic fprintf(stderr, "\t%s\n", pfile); 3730890Sbostic if (!(freopen(pfile, "w", stdout))) 3830890Sbostic error("can't create %s.", pfile); 3930163Sbostic rewind(stdin); 4030890Sbostic while ((rval = read(fileno(stdin), bfr, sizeof(bfr))) != ERR && rval) 4130890Sbostic if (write(fileno(stdout), bfr, rval) != rval) 4230890Sbostic error("write to %s failed.", pfile); 4330163Sbostic 4430163Sbostic /* append information to the summary file */ 4530890Sbostic sprintf(bfr, "%s/%s", dir, SUMMARY_FILE); 4630890Sbostic if (!(freopen(bfr, "a", stdout))) 4730890Sbostic error("can't append to summary file %s.", bfr); 4830890Sbostic if (gettimeofday(&tp, (struct timezone *)NULL)) 4930890Sbostic error("can't get time of day.", CHN); 50*30947Sbostic printf("\n%s\t\t%s\t%s\t%s\tOwner: Bugs Bunny\n\tStatus: Received\n", pfile, ctime(&tp.tv_sec), mailhead[INDX_TAG].line, mailhead[SUBJ_TAG].found ? mailhead[SUBJ_TAG].line : "Subject:\n"); 5130890Sbostic (void)flock(lfd, LOCK_UN); 5230890Sbostic (void)fclose(stdout); 5330163Sbostic } 5430163Sbostic 5530163Sbostic /* 5630163Sbostic * getnext -- 5730163Sbostic * get next file name (number) 5830163Sbostic */ 5930163Sbostic static 6030163Sbostic getnext() 6130163Sbostic { 6230163Sbostic register struct direct *d; /* directory structure */ 6330163Sbostic register DIR *dirp; /* directory pointer */ 6430890Sbostic register int highval, 6530890Sbostic newval; 6630890Sbostic register char *C; 6730163Sbostic 6830890Sbostic sprintf(bfr, "%s/%s", dir, folder); 6930890Sbostic if (!(dirp = opendir(bfr))) 7030890Sbostic error("can't read folder directory %s.", bfr); 7130890Sbostic for (highval = 0;d = readdir(dirp);) 7230890Sbostic for (C = d->d_name;;++C) 7330890Sbostic if (!*C) { 7430890Sbostic if ((newval = atoi(d->d_name)) > highval) 7530890Sbostic highval = newval; 7630890Sbostic break; 7730890Sbostic } 7830890Sbostic else if (!isdigit(*C)) 7930890Sbostic break; 8030163Sbostic closedir(dirp); 8130890Sbostic return(++highval); 8230163Sbostic } 83