xref: /original-bsd/libexec/bugfiler/process.c (revision c3e32dec)
1 /*
2  * Copyright (c) 1986, 1987, 1993
3  *	The Regents of the University of California.  All rights reserved.
4  *
5  * %sccs.include.redist.c%
6  */
7 
8 #ifndef lint
9 static char sccsid[] = "@(#)process.c	8.1 (Berkeley) 06/04/93";
10 #endif /* not lint */
11 
12 #include <sys/param.h>
13 #include <sys/time.h>
14 
15 #include <ctype.h>
16 #include <dirent.h>
17 #include <fcntl.h>
18 #include <stdio.h>
19 #include <stdlib.h>
20 #include <unistd.h>
21 
22 #include "bug.h"
23 #include "extern.h"
24 
25 char	pfile[MAXPATHLEN];			/* permanent file name */
26 
27 static int getnext __P((void));
28 
29 /*
30  * process --
31  *	copy report to permanent file,
32  *	update summary file.
33  */
34 int
35 process()
36 {
37 	register int	rval;			/* read return value */
38 	struct timeval	tp;			/* time of day */
39 	int	lfd;				/* lock file descriptor */
40 
41 	if (access(LOCK_FILE, R_OK) || (lfd = open(LOCK_FILE, O_RDONLY, 0)) < 0)
42 		error("can't find lock file %s.", LOCK_FILE);
43 	if (flock(lfd, LOCK_EX))
44 		error("can't get lock.", CHN);
45 	sprintf(pfile, "%s/%s/%d", dir, folder, getnext());
46 	fprintf(stderr, "\t%s\n", pfile);
47 	if (!(freopen(pfile, "w", stdout)))
48 		error("can't create %s.", pfile);
49 	rewind(stdin);
50 	while ((rval = read(fileno(stdin), bfr, sizeof(bfr))) != ERR && rval)
51 		if (write(fileno(stdout), bfr, rval) != rval)
52 			error("write to %s failed.", pfile);
53 
54 	/* append information to the summary file */
55 	sprintf(bfr, "%s/%s", dir, SUMMARY_FILE);
56 	if (!(freopen(bfr, "a", stdout)))
57 		error("can't append to summary file %s.", bfr);
58 	if (gettimeofday(&tp, (struct timezone *)NULL))
59 		error("can't get time of day.", CHN);
60 	printf("\n%s\t\t%s\t%s\t%s\tOwner: Bugs Bunny\n\tStatus: Received\n",
61 	    pfile, ctime(&tp.tv_sec), mailhead[INDX_TAG].line,
62 	    mailhead[SUBJ_TAG].found ? mailhead[SUBJ_TAG].line : "Subject:\n");
63 	(void)flock(lfd, LOCK_UN);
64 	(void)fclose(stdout);
65 }
66 
67 /*
68  * getnext --
69  *	get next file name (number)
70  */
71 static int
72 getnext()
73 {
74 	register struct dirent *d;		/* directory structure */
75 	register DIR *dirp;			/* directory pointer */
76 	register int highval, newval;
77 	register char *p;
78 
79 	(void)sprintf(bfr, "%s/%s", dir, folder);
80 	if (!(dirp = opendir(bfr)))
81 		error("can't read folder directory %s.", bfr);
82 	for (highval = -1; d = readdir(dirp);) {
83 		for (p = d->d_name; *p && isdigit(*p); ++p);
84 		if (!*p && (newval = atoi(d->d_name)) > highval)
85 			highval = newval;
86 	}
87 	closedir(dirp);
88 	return(++highval);
89 }
90