xref: /original-bsd/libexec/bugfiler/process.c (revision 37c8c8e2)
1b38e0f18Sbostic /*
2*37c8c8e2Sbostic  * Copyright (c) 1986, 1987 Regents of the University of California.
3*37c8c8e2Sbostic  * All rights reserved.
4*37c8c8e2Sbostic  *
5*37c8c8e2Sbostic  * Redistribution and use in source and binary forms are permitted
6*37c8c8e2Sbostic  * provided that this notice is preserved and that due credit is given
7*37c8c8e2Sbostic  * to the University of California at Berkeley. The name of the University
8*37c8c8e2Sbostic  * may not be used to endorse or promote products derived from this
9*37c8c8e2Sbostic  * software without specific prior written permission. This software
10*37c8c8e2Sbostic  * is provided ``as is'' without express or implied warranty.
11b38e0f18Sbostic  */
12b38e0f18Sbostic 
13b38e0f18Sbostic #ifndef lint
14*37c8c8e2Sbostic static char sccsid[] = "@(#)process.c	5.5 (Berkeley) 02/01/88";
15*37c8c8e2Sbostic #endif /* not lint */
16b38e0f18Sbostic 
17b38e0f18Sbostic #include <bug.h>
18b38e0f18Sbostic #include <sys/file.h>
194803d908Sbostic #include <sys/time.h>
20b38e0f18Sbostic #include <stdio.h>
213212f6deSbostic #include <ctype.h>
22b38e0f18Sbostic 
23b38e0f18Sbostic char	pfile[MAXPATHLEN];			/* permanent file name */
24b38e0f18Sbostic 
25b38e0f18Sbostic /*
26b38e0f18Sbostic  * process --
273212f6deSbostic  *	copy report to permanent file,
283212f6deSbostic  *	update summary file.
29b38e0f18Sbostic  */
30b38e0f18Sbostic process()
31b38e0f18Sbostic {
32b38e0f18Sbostic 	register int	rval;			/* read return value */
334803d908Sbostic 	struct timeval	tp;			/* time of day */
343212f6deSbostic 	int	lfd;				/* lock file descriptor */
354803d908Sbostic 	char	*ctime();
36b38e0f18Sbostic 
373212f6deSbostic 	if (access(LOCK_FILE, R_OK) || (lfd = open(LOCK_FILE, O_RDONLY, 0)) < 0)
383212f6deSbostic 		error("can't find lock file %s.", LOCK_FILE);
393212f6deSbostic 	if (flock(lfd, LOCK_EX))
403212f6deSbostic 		error("can't get lock.", CHN);
41b38e0f18Sbostic 	sprintf(pfile, "%s/%s/%d", dir, folder, getnext());
42b38e0f18Sbostic 	fprintf(stderr, "\t%s\n", pfile);
43b38e0f18Sbostic 	if (!(freopen(pfile, "w", stdout)))
443212f6deSbostic 		error("can't create %s.", pfile);
45b38e0f18Sbostic 	rewind(stdin);
46b38e0f18Sbostic 	while ((rval = read(fileno(stdin), bfr, sizeof(bfr))) != ERR && rval)
473212f6deSbostic 		if (write(fileno(stdout), bfr, rval) != rval)
483212f6deSbostic 			error("write to %s failed.", pfile);
49b38e0f18Sbostic 
50b38e0f18Sbostic 	/* append information to the summary file */
51b38e0f18Sbostic 	sprintf(bfr, "%s/%s", dir, SUMMARY_FILE);
52b38e0f18Sbostic 	if (!(freopen(bfr, "a", stdout)))
533212f6deSbostic 		error("can't append to summary file %s.", bfr);
543212f6deSbostic 	if (gettimeofday(&tp, (struct timezone *)NULL))
553212f6deSbostic 		error("can't get time of day.", CHN);
56224b47f9Sbostic 	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");
573212f6deSbostic 	(void)flock(lfd, LOCK_UN);
583212f6deSbostic 	(void)fclose(stdout);
59b38e0f18Sbostic }
60b38e0f18Sbostic 
61b38e0f18Sbostic /*
62b38e0f18Sbostic  * getnext --
63b38e0f18Sbostic  *	get next file name (number)
64b38e0f18Sbostic  */
65b38e0f18Sbostic static
66b38e0f18Sbostic getnext()
67b38e0f18Sbostic {
68b38e0f18Sbostic 	register struct direct	*d;		/* directory structure */
69b38e0f18Sbostic 	register DIR	*dirp;			/* directory pointer */
703212f6deSbostic 	register int	highval,
713212f6deSbostic 			newval;
723212f6deSbostic 	register char	*C;
73b38e0f18Sbostic 
74b38e0f18Sbostic 	sprintf(bfr, "%s/%s", dir, folder);
753212f6deSbostic 	if (!(dirp = opendir(bfr)))
763212f6deSbostic 		error("can't read folder directory %s.", bfr);
773212f6deSbostic 	for (highval = 0;d = readdir(dirp);)
783212f6deSbostic 		for (C = d->d_name;;++C)
793212f6deSbostic 			if (!*C) {
803212f6deSbostic 				if ((newval = atoi(d->d_name)) > highval)
813212f6deSbostic 					highval = newval;
823212f6deSbostic 				break;
83b38e0f18Sbostic 			}
843212f6deSbostic 			else if (!isdigit(*C))
853212f6deSbostic 				break;
86b38e0f18Sbostic 	closedir(dirp);
873212f6deSbostic 	return(++highval);
88b38e0f18Sbostic }
89