xref: /original-bsd/libexec/bugfiler/process.c (revision 3212f6de)
1b38e0f18Sbostic /*
2b38e0f18Sbostic  * Copyright (c) 1986 Regents of the University of California.
3b38e0f18Sbostic  * All rights reserved.  The Berkeley software License Agreement
4b38e0f18Sbostic  * specifies the terms and conditions for redistribution.
5b38e0f18Sbostic  */
6b38e0f18Sbostic 
7b38e0f18Sbostic #ifndef lint
8*3212f6deSbostic static char sccsid[] = "@(#)process.c	5.3 (Berkeley) 87/04/11";
9b38e0f18Sbostic #endif not lint
10b38e0f18Sbostic 
11b38e0f18Sbostic #include <bug.h>
12b38e0f18Sbostic #include <sys/file.h>
134803d908Sbostic #include <sys/time.h>
14b38e0f18Sbostic #include <stdio.h>
15*3212f6deSbostic #include <ctype.h>
16b38e0f18Sbostic 
17b38e0f18Sbostic char	pfile[MAXPATHLEN];			/* permanent file name */
18b38e0f18Sbostic 
19b38e0f18Sbostic /*
20b38e0f18Sbostic  * process --
21*3212f6deSbostic  *	copy report to permanent file,
22*3212f6deSbostic  *	update summary file.
23b38e0f18Sbostic  */
24b38e0f18Sbostic process()
25b38e0f18Sbostic {
26b38e0f18Sbostic 	register int	rval;			/* read return value */
274803d908Sbostic 	struct timeval	tp;			/* time of day */
28*3212f6deSbostic 	int	lfd;				/* lock file descriptor */
294803d908Sbostic 	char	*ctime();
30b38e0f18Sbostic 
31*3212f6deSbostic 	if (access(LOCK_FILE, R_OK) || (lfd = open(LOCK_FILE, O_RDONLY, 0)) < 0)
32*3212f6deSbostic 		error("can't find lock file %s.", LOCK_FILE);
33*3212f6deSbostic 	if (flock(lfd, LOCK_EX))
34*3212f6deSbostic 		error("can't get lock.", CHN);
35b38e0f18Sbostic 	sprintf(pfile, "%s/%s/%d", dir, folder, getnext());
36b38e0f18Sbostic 	fprintf(stderr, "\t%s\n", pfile);
37b38e0f18Sbostic 	if (!(freopen(pfile, "w", stdout)))
38*3212f6deSbostic 		error("can't create %s.", pfile);
39b38e0f18Sbostic 	rewind(stdin);
40b38e0f18Sbostic 	while ((rval = read(fileno(stdin), bfr, sizeof(bfr))) != ERR && rval)
41*3212f6deSbostic 		if (write(fileno(stdout), bfr, rval) != rval)
42*3212f6deSbostic 			error("write to %s failed.", pfile);
43b38e0f18Sbostic 
44b38e0f18Sbostic 	/* append information to the summary file */
45b38e0f18Sbostic 	sprintf(bfr, "%s/%s", dir, SUMMARY_FILE);
46b38e0f18Sbostic 	if (!(freopen(bfr, "a", stdout)))
47*3212f6deSbostic 		error("can't append to summary file %s.", bfr);
48*3212f6deSbostic 	if (gettimeofday(&tp, (struct timezone *)NULL))
49*3212f6deSbostic 		error("can't get time of day.", CHN);
504803d908Sbostic 	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*3212f6deSbostic 	(void)flock(lfd, LOCK_UN);
52*3212f6deSbostic 	(void)fclose(stdout);
53b38e0f18Sbostic }
54b38e0f18Sbostic 
55b38e0f18Sbostic /*
56b38e0f18Sbostic  * getnext --
57b38e0f18Sbostic  *	get next file name (number)
58b38e0f18Sbostic  */
59b38e0f18Sbostic static
60b38e0f18Sbostic getnext()
61b38e0f18Sbostic {
62b38e0f18Sbostic 	register struct direct	*d;		/* directory structure */
63b38e0f18Sbostic 	register DIR	*dirp;			/* directory pointer */
64*3212f6deSbostic 	register int	highval,
65*3212f6deSbostic 			newval;
66*3212f6deSbostic 	register char	*C;
67b38e0f18Sbostic 
68b38e0f18Sbostic 	sprintf(bfr, "%s/%s", dir, folder);
69*3212f6deSbostic 	if (!(dirp = opendir(bfr)))
70*3212f6deSbostic 		error("can't read folder directory %s.", bfr);
71*3212f6deSbostic 	for (highval = 0;d = readdir(dirp);)
72*3212f6deSbostic 		for (C = d->d_name;;++C)
73*3212f6deSbostic 			if (!*C) {
74*3212f6deSbostic 				if ((newval = atoi(d->d_name)) > highval)
75*3212f6deSbostic 					highval = newval;
76*3212f6deSbostic 				break;
77b38e0f18Sbostic 			}
78*3212f6deSbostic 			else if (!isdigit(*C))
79*3212f6deSbostic 				break;
80b38e0f18Sbostic 	closedir(dirp);
81*3212f6deSbostic 	return(++highval);
82b38e0f18Sbostic }
83