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