xref: /original-bsd/libexec/bugfiler/process.c (revision ad93c43e)
1 /*
2  * Copyright (c) 1986, 1987 Regents of the University of California.
3  * All rights reserved.
4  *
5  * Redistribution and use in source and binary forms are permitted
6  * provided that this notice is preserved and that due credit is given
7  * to the University of California at Berkeley. The name of the University
8  * may not be used to endorse or promote products derived from this
9  * software without specific prior written permission. This software
10  * is provided ``as is'' without express or implied warranty.
11  */
12 
13 #ifndef lint
14 static char sccsid[] = "@(#)process.c	5.5 (Berkeley) 02/01/88";
15 #endif /* not lint */
16 
17 #include <bug.h>
18 #include <sys/file.h>
19 #include <sys/time.h>
20 #include <stdio.h>
21 #include <ctype.h>
22 
23 char	pfile[MAXPATHLEN];			/* permanent file name */
24 
25 /*
26  * process --
27  *	copy report to permanent file,
28  *	update summary file.
29  */
30 process()
31 {
32 	register int	rval;			/* read return value */
33 	struct timeval	tp;			/* time of day */
34 	int	lfd;				/* lock file descriptor */
35 	char	*ctime();
36 
37 	if (access(LOCK_FILE, R_OK) || (lfd = open(LOCK_FILE, O_RDONLY, 0)) < 0)
38 		error("can't find lock file %s.", LOCK_FILE);
39 	if (flock(lfd, LOCK_EX))
40 		error("can't get lock.", CHN);
41 	sprintf(pfile, "%s/%s/%d", dir, folder, getnext());
42 	fprintf(stderr, "\t%s\n", pfile);
43 	if (!(freopen(pfile, "w", stdout)))
44 		error("can't create %s.", pfile);
45 	rewind(stdin);
46 	while ((rval = read(fileno(stdin), bfr, sizeof(bfr))) != ERR && rval)
47 		if (write(fileno(stdout), bfr, rval) != rval)
48 			error("write to %s failed.", pfile);
49 
50 	/* append information to the summary file */
51 	sprintf(bfr, "%s/%s", dir, SUMMARY_FILE);
52 	if (!(freopen(bfr, "a", stdout)))
53 		error("can't append to summary file %s.", bfr);
54 	if (gettimeofday(&tp, (struct timezone *)NULL))
55 		error("can't get time of day.", CHN);
56 	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");
57 	(void)flock(lfd, LOCK_UN);
58 	(void)fclose(stdout);
59 }
60 
61 /*
62  * getnext --
63  *	get next file name (number)
64  */
65 static
66 getnext()
67 {
68 	register struct direct	*d;		/* directory structure */
69 	register DIR	*dirp;			/* directory pointer */
70 	register int	highval,
71 			newval;
72 	register char	*C;
73 
74 	sprintf(bfr, "%s/%s", dir, folder);
75 	if (!(dirp = opendir(bfr)))
76 		error("can't read folder directory %s.", bfr);
77 	for (highval = 0;d = readdir(dirp);)
78 		for (C = d->d_name;;++C)
79 			if (!*C) {
80 				if ((newval = atoi(d->d_name)) > highval)
81 					highval = newval;
82 				break;
83 			}
84 			else if (!isdigit(*C))
85 				break;
86 	closedir(dirp);
87 	return(++highval);
88 }
89