130163Sbostic /*
2*61420Sbostic * Copyright (c) 1986, 1987, 1993
3*61420Sbostic * The Regents of the University of California. All rights reserved.
433416Sbostic *
542663Sbostic * %sccs.include.redist.c%
630163Sbostic */
730163Sbostic
830163Sbostic #ifndef lint
9*61420Sbostic static char sccsid[] = "@(#)process.c 8.1 (Berkeley) 06/04/93";
1033416Sbostic #endif /* not lint */
1130163Sbostic
1246667Sbostic #include <sys/param.h>
1330425Sbostic #include <sys/time.h>
1460083Sbostic
1560083Sbostic #include <ctype.h>
1660083Sbostic #include <dirent.h>
1746667Sbostic #include <fcntl.h>
1830163Sbostic #include <stdio.h>
1946667Sbostic #include <stdlib.h>
2060083Sbostic #include <unistd.h>
2160083Sbostic
2246667Sbostic #include "bug.h"
2360083Sbostic #include "extern.h"
2430163Sbostic
2530163Sbostic char pfile[MAXPATHLEN]; /* permanent file name */
2630163Sbostic
2760083Sbostic static int getnext __P((void));
2860083Sbostic
2930163Sbostic /*
3030163Sbostic * process --
3130890Sbostic * copy report to permanent file,
3230890Sbostic * update summary file.
3330163Sbostic */
3460083Sbostic int
process()3530163Sbostic process()
3630163Sbostic {
3730163Sbostic register int rval; /* read return value */
3830425Sbostic struct timeval tp; /* time of day */
3930890Sbostic int lfd; /* lock file descriptor */
4030163Sbostic
4130890Sbostic if (access(LOCK_FILE, R_OK) || (lfd = open(LOCK_FILE, O_RDONLY, 0)) < 0)
4230890Sbostic error("can't find lock file %s.", LOCK_FILE);
4330890Sbostic if (flock(lfd, LOCK_EX))
4430890Sbostic error("can't get lock.", CHN);
4530890Sbostic sprintf(pfile, "%s/%s/%d", dir, folder, getnext());
4630890Sbostic fprintf(stderr, "\t%s\n", pfile);
4730890Sbostic if (!(freopen(pfile, "w", stdout)))
4830890Sbostic error("can't create %s.", pfile);
4930163Sbostic rewind(stdin);
5030890Sbostic while ((rval = read(fileno(stdin), bfr, sizeof(bfr))) != ERR && rval)
5130890Sbostic if (write(fileno(stdout), bfr, rval) != rval)
5230890Sbostic error("write to %s failed.", pfile);
5330163Sbostic
5430163Sbostic /* append information to the summary file */
5530890Sbostic sprintf(bfr, "%s/%s", dir, SUMMARY_FILE);
5630890Sbostic if (!(freopen(bfr, "a", stdout)))
5730890Sbostic error("can't append to summary file %s.", bfr);
5830890Sbostic if (gettimeofday(&tp, (struct timezone *)NULL))
5930890Sbostic error("can't get time of day.", CHN);
6060083Sbostic printf("\n%s\t\t%s\t%s\t%s\tOwner: Bugs Bunny\n\tStatus: Received\n",
6160083Sbostic pfile, ctime(&tp.tv_sec), mailhead[INDX_TAG].line,
6260083Sbostic mailhead[SUBJ_TAG].found ? mailhead[SUBJ_TAG].line : "Subject:\n");
6330890Sbostic (void)flock(lfd, LOCK_UN);
6430890Sbostic (void)fclose(stdout);
6530163Sbostic }
6630163Sbostic
6730163Sbostic /*
6830163Sbostic * getnext --
6930163Sbostic * get next file name (number)
7030163Sbostic */
7146667Sbostic static int
getnext()7230163Sbostic getnext()
7330163Sbostic {
7446667Sbostic register struct dirent *d; /* directory structure */
7538449Sbostic register DIR *dirp; /* directory pointer */
7638449Sbostic register int highval, newval;
7738449Sbostic register char *p;
7830163Sbostic
7938449Sbostic (void)sprintf(bfr, "%s/%s", dir, folder);
8030890Sbostic if (!(dirp = opendir(bfr)))
8130890Sbostic error("can't read folder directory %s.", bfr);
8238449Sbostic for (highval = -1; d = readdir(dirp);) {
8338449Sbostic for (p = d->d_name; *p && isdigit(*p); ++p);
8438449Sbostic if (!*p && (newval = atoi(d->d_name)) > highval)
8538449Sbostic highval = newval;
8638449Sbostic }
8730163Sbostic closedir(dirp);
8830890Sbostic return(++highval);
8930163Sbostic }
90