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