130163Sbostic /* 233416Sbostic * Copyright (c) 1986, 1987 Regents of the University of California. 333416Sbostic * All rights reserved. 433416Sbostic * 533416Sbostic * Redistribution and use in source and binary forms are permitted 634910Sbostic * provided that the above copyright notice and this paragraph are 734910Sbostic * duplicated in all such forms and that any documentation, 834910Sbostic * advertising materials, and other materials related to such 934910Sbostic * distribution and use acknowledge that the software was developed 1034910Sbostic * by the University of California, Berkeley. The name of the 1134910Sbostic * University may not be used to endorse or promote products derived 1234910Sbostic * from this software without specific prior written permission. 1334910Sbostic * THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR 1434910Sbostic * IMPLIED WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED 1534910Sbostic * WARRANTIES OF MERCHANTIBILITY AND FITNESS FOR A PARTICULAR PURPOSE. 1630163Sbostic */ 1730163Sbostic 1830163Sbostic #ifndef lint 19*38449Sbostic static char sccsid[] = "@(#)process.c 5.7 (Berkeley) 07/16/89"; 2033416Sbostic #endif /* not lint */ 2130163Sbostic 2230163Sbostic #include <bug.h> 2330163Sbostic #include <sys/file.h> 2430425Sbostic #include <sys/time.h> 2530163Sbostic #include <stdio.h> 2630890Sbostic #include <ctype.h> 2730163Sbostic 2830163Sbostic char pfile[MAXPATHLEN]; /* permanent file name */ 2930163Sbostic 3030163Sbostic /* 3130163Sbostic * process -- 3230890Sbostic * copy report to permanent file, 3330890Sbostic * update summary file. 3430163Sbostic */ 3530163Sbostic process() 3630163Sbostic { 3730163Sbostic register int rval; /* read return value */ 3830425Sbostic struct timeval tp; /* time of day */ 3930890Sbostic int lfd; /* lock file descriptor */ 4030425Sbostic char *ctime(); 4130163Sbostic 4230890Sbostic if (access(LOCK_FILE, R_OK) || (lfd = open(LOCK_FILE, O_RDONLY, 0)) < 0) 4330890Sbostic error("can't find lock file %s.", LOCK_FILE); 4430890Sbostic if (flock(lfd, LOCK_EX)) 4530890Sbostic error("can't get lock.", CHN); 4630890Sbostic sprintf(pfile, "%s/%s/%d", dir, folder, getnext()); 4730890Sbostic fprintf(stderr, "\t%s\n", pfile); 4830890Sbostic if (!(freopen(pfile, "w", stdout))) 4930890Sbostic error("can't create %s.", pfile); 5030163Sbostic rewind(stdin); 5130890Sbostic while ((rval = read(fileno(stdin), bfr, sizeof(bfr))) != ERR && rval) 5230890Sbostic if (write(fileno(stdout), bfr, rval) != rval) 5330890Sbostic error("write to %s failed.", pfile); 5430163Sbostic 5530163Sbostic /* append information to the summary file */ 5630890Sbostic sprintf(bfr, "%s/%s", dir, SUMMARY_FILE); 5730890Sbostic if (!(freopen(bfr, "a", stdout))) 5830890Sbostic error("can't append to summary file %s.", bfr); 5930890Sbostic if (gettimeofday(&tp, (struct timezone *)NULL)) 6030890Sbostic error("can't get time of day.", CHN); 6130947Sbostic 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"); 6230890Sbostic (void)flock(lfd, LOCK_UN); 6330890Sbostic (void)fclose(stdout); 6430163Sbostic } 6530163Sbostic 6630163Sbostic /* 6730163Sbostic * getnext -- 6830163Sbostic * get next file name (number) 6930163Sbostic */ 7030163Sbostic static 7130163Sbostic getnext() 7230163Sbostic { 73*38449Sbostic register struct direct *d; /* directory structure */ 74*38449Sbostic register DIR *dirp; /* directory pointer */ 75*38449Sbostic register int highval, newval; 76*38449Sbostic register char *p; 7730163Sbostic 78*38449Sbostic (void)sprintf(bfr, "%s/%s", dir, folder); 7930890Sbostic if (!(dirp = opendir(bfr))) 8030890Sbostic error("can't read folder directory %s.", bfr); 81*38449Sbostic for (highval = -1; d = readdir(dirp);) { 82*38449Sbostic for (p = d->d_name; *p && isdigit(*p); ++p); 83*38449Sbostic if (!*p && (newval = atoi(d->d_name)) > highval) 84*38449Sbostic highval = newval; 85*38449Sbostic } 8630163Sbostic closedir(dirp); 8730890Sbostic return(++highval); 8830163Sbostic } 89