xref: /csrg-svn/libexec/bugfiler/process.c (revision 30890)
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*30890Sbostic static char sccsid[] = "@(#)process.c	5.3 (Berkeley) 87/04/11";
930163Sbostic #endif not lint
1030163Sbostic 
1130163Sbostic #include <bug.h>
1230163Sbostic #include <sys/file.h>
1330425Sbostic #include <sys/time.h>
1430163Sbostic #include <stdio.h>
15*30890Sbostic #include <ctype.h>
1630163Sbostic 
1730163Sbostic char	pfile[MAXPATHLEN];			/* permanent file name */
1830163Sbostic 
1930163Sbostic /*
2030163Sbostic  * process --
21*30890Sbostic  *	copy report to permanent file,
22*30890Sbostic  *	update summary file.
2330163Sbostic  */
2430163Sbostic process()
2530163Sbostic {
2630163Sbostic 	register int	rval;			/* read return value */
2730425Sbostic 	struct timeval	tp;			/* time of day */
28*30890Sbostic 	int	lfd;				/* lock file descriptor */
2930425Sbostic 	char	*ctime();
3030163Sbostic 
31*30890Sbostic 	if (access(LOCK_FILE, R_OK) || (lfd = open(LOCK_FILE, O_RDONLY, 0)) < 0)
32*30890Sbostic 		error("can't find lock file %s.", LOCK_FILE);
33*30890Sbostic 	if (flock(lfd, LOCK_EX))
34*30890Sbostic 		error("can't get lock.", CHN);
35*30890Sbostic 	sprintf(pfile, "%s/%s/%d", dir, folder, getnext());
36*30890Sbostic 	fprintf(stderr, "\t%s\n", pfile);
37*30890Sbostic 	if (!(freopen(pfile, "w", stdout)))
38*30890Sbostic 		error("can't create %s.", pfile);
3930163Sbostic 	rewind(stdin);
40*30890Sbostic 	while ((rval = read(fileno(stdin), bfr, sizeof(bfr))) != ERR && rval)
41*30890Sbostic 		if (write(fileno(stdout), bfr, rval) != rval)
42*30890Sbostic 			error("write to %s failed.", pfile);
4330163Sbostic 
4430163Sbostic 	/* append information to the summary file */
45*30890Sbostic 	sprintf(bfr, "%s/%s", dir, SUMMARY_FILE);
46*30890Sbostic 	if (!(freopen(bfr, "a", stdout)))
47*30890Sbostic 		error("can't append to summary file %s.", bfr);
48*30890Sbostic 	if (gettimeofday(&tp, (struct timezone *)NULL))
49*30890Sbostic 		error("can't get time of day.", CHN);
50*30890Sbostic 	printf("\n%s\t\t%s\t%s\t%s\tOwner: Bugs Bunny\n\tComment: Received\n", pfile, ctime(&tp.tv_sec), mailhead[INDX_TAG].line, mailhead[SUBJ_TAG].found ? mailhead[SUBJ_TAG].line : "Subject:\n");
51*30890Sbostic 	(void)flock(lfd, LOCK_UN);
52*30890Sbostic 	(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 */
64*30890Sbostic 	register int	highval,
65*30890Sbostic 			newval;
66*30890Sbostic 	register char	*C;
6730163Sbostic 
68*30890Sbostic 	sprintf(bfr, "%s/%s", dir, folder);
69*30890Sbostic 	if (!(dirp = opendir(bfr)))
70*30890Sbostic 		error("can't read folder directory %s.", bfr);
71*30890Sbostic 	for (highval = 0;d = readdir(dirp);)
72*30890Sbostic 		for (C = d->d_name;;++C)
73*30890Sbostic 			if (!*C) {
74*30890Sbostic 				if ((newval = atoi(d->d_name)) > highval)
75*30890Sbostic 					highval = newval;
76*30890Sbostic 				break;
77*30890Sbostic 			}
78*30890Sbostic 			else if (!isdigit(*C))
79*30890Sbostic 				break;
8030163Sbostic 	closedir(dirp);
81*30890Sbostic 	return(++highval);
8230163Sbostic }
83