xref: /csrg-svn/libexec/bugfiler/process.c (revision 34910)
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
6*34910Sbostic  * provided that the above copyright notice and this paragraph are
7*34910Sbostic  * duplicated in all such forms and that any documentation,
8*34910Sbostic  * advertising materials, and other materials related to such
9*34910Sbostic  * distribution and use acknowledge that the software was developed
10*34910Sbostic  * by the University of California, Berkeley.  The name of the
11*34910Sbostic  * University may not be used to endorse or promote products derived
12*34910Sbostic  * from this software without specific prior written permission.
13*34910Sbostic  * THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR
14*34910Sbostic  * IMPLIED WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED
15*34910Sbostic  * WARRANTIES OF MERCHANTIBILITY AND FITNESS FOR A PARTICULAR PURPOSE.
1630163Sbostic  */
1730163Sbostic 
1830163Sbostic #ifndef lint
19*34910Sbostic static char sccsid[] = "@(#)process.c	5.6 (Berkeley) 06/29/88";
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 {
7330163Sbostic 	register struct direct	*d;		/* directory structure */
7430163Sbostic 	register DIR	*dirp;			/* directory pointer */
7530890Sbostic 	register int	highval,
7630890Sbostic 			newval;
7730890Sbostic 	register char	*C;
7830163Sbostic 
7930890Sbostic 	sprintf(bfr, "%s/%s", dir, folder);
8030890Sbostic 	if (!(dirp = opendir(bfr)))
8130890Sbostic 		error("can't read folder directory %s.", bfr);
8230890Sbostic 	for (highval = 0;d = readdir(dirp);)
8330890Sbostic 		for (C = d->d_name;;++C)
8430890Sbostic 			if (!*C) {
8530890Sbostic 				if ((newval = atoi(d->d_name)) > highval)
8630890Sbostic 					highval = newval;
8730890Sbostic 				break;
8830890Sbostic 			}
8930890Sbostic 			else if (!isdigit(*C))
9030890Sbostic 				break;
9130163Sbostic 	closedir(dirp);
9230890Sbostic 	return(++highval);
9330163Sbostic }
94