xref: /csrg-svn/libexec/bugfiler/process.c (revision 33416)
130163Sbostic /*
2*33416Sbostic  * Copyright (c) 1986, 1987 Regents of the University of California.
3*33416Sbostic  * All rights reserved.
4*33416Sbostic  *
5*33416Sbostic  * Redistribution and use in source and binary forms are permitted
6*33416Sbostic  * provided that this notice is preserved and that due credit is given
7*33416Sbostic  * to the University of California at Berkeley. The name of the University
8*33416Sbostic  * may not be used to endorse or promote products derived from this
9*33416Sbostic  * software without specific prior written permission. This software
10*33416Sbostic  * is provided ``as is'' without express or implied warranty.
1130163Sbostic  */
1230163Sbostic 
1330163Sbostic #ifndef lint
14*33416Sbostic static char sccsid[] = "@(#)process.c	5.5 (Berkeley) 02/01/88";
15*33416Sbostic #endif /* not lint */
1630163Sbostic 
1730163Sbostic #include <bug.h>
1830163Sbostic #include <sys/file.h>
1930425Sbostic #include <sys/time.h>
2030163Sbostic #include <stdio.h>
2130890Sbostic #include <ctype.h>
2230163Sbostic 
2330163Sbostic char	pfile[MAXPATHLEN];			/* permanent file name */
2430163Sbostic 
2530163Sbostic /*
2630163Sbostic  * process --
2730890Sbostic  *	copy report to permanent file,
2830890Sbostic  *	update summary file.
2930163Sbostic  */
3030163Sbostic process()
3130163Sbostic {
3230163Sbostic 	register int	rval;			/* read return value */
3330425Sbostic 	struct timeval	tp;			/* time of day */
3430890Sbostic 	int	lfd;				/* lock file descriptor */
3530425Sbostic 	char	*ctime();
3630163Sbostic 
3730890Sbostic 	if (access(LOCK_FILE, R_OK) || (lfd = open(LOCK_FILE, O_RDONLY, 0)) < 0)
3830890Sbostic 		error("can't find lock file %s.", LOCK_FILE);
3930890Sbostic 	if (flock(lfd, LOCK_EX))
4030890Sbostic 		error("can't get lock.", CHN);
4130890Sbostic 	sprintf(pfile, "%s/%s/%d", dir, folder, getnext());
4230890Sbostic 	fprintf(stderr, "\t%s\n", pfile);
4330890Sbostic 	if (!(freopen(pfile, "w", stdout)))
4430890Sbostic 		error("can't create %s.", pfile);
4530163Sbostic 	rewind(stdin);
4630890Sbostic 	while ((rval = read(fileno(stdin), bfr, sizeof(bfr))) != ERR && rval)
4730890Sbostic 		if (write(fileno(stdout), bfr, rval) != rval)
4830890Sbostic 			error("write to %s failed.", pfile);
4930163Sbostic 
5030163Sbostic 	/* append information to the summary file */
5130890Sbostic 	sprintf(bfr, "%s/%s", dir, SUMMARY_FILE);
5230890Sbostic 	if (!(freopen(bfr, "a", stdout)))
5330890Sbostic 		error("can't append to summary file %s.", bfr);
5430890Sbostic 	if (gettimeofday(&tp, (struct timezone *)NULL))
5530890Sbostic 		error("can't get time of day.", CHN);
5630947Sbostic 	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");
5730890Sbostic 	(void)flock(lfd, LOCK_UN);
5830890Sbostic 	(void)fclose(stdout);
5930163Sbostic }
6030163Sbostic 
6130163Sbostic /*
6230163Sbostic  * getnext --
6330163Sbostic  *	get next file name (number)
6430163Sbostic  */
6530163Sbostic static
6630163Sbostic getnext()
6730163Sbostic {
6830163Sbostic 	register struct direct	*d;		/* directory structure */
6930163Sbostic 	register DIR	*dirp;			/* directory pointer */
7030890Sbostic 	register int	highval,
7130890Sbostic 			newval;
7230890Sbostic 	register char	*C;
7330163Sbostic 
7430890Sbostic 	sprintf(bfr, "%s/%s", dir, folder);
7530890Sbostic 	if (!(dirp = opendir(bfr)))
7630890Sbostic 		error("can't read folder directory %s.", bfr);
7730890Sbostic 	for (highval = 0;d = readdir(dirp);)
7830890Sbostic 		for (C = d->d_name;;++C)
7930890Sbostic 			if (!*C) {
8030890Sbostic 				if ((newval = atoi(d->d_name)) > highval)
8130890Sbostic 					highval = newval;
8230890Sbostic 				break;
8330890Sbostic 			}
8430890Sbostic 			else if (!isdigit(*C))
8530890Sbostic 				break;
8630163Sbostic 	closedir(dirp);
8730890Sbostic 	return(++highval);
8830163Sbostic }
89