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