xref: /original-bsd/libexec/bugfiler/process.c (revision 33b18212)
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 the above copyright notice and this paragraph are
7  * duplicated in all such forms and that any documentation,
8  * advertising materials, and other materials related to such
9  * distribution and use acknowledge that the software was developed
10  * by the University of California, Berkeley.  The name of the
11  * University may not be used to endorse or promote products derived
12  * from this software without specific prior written permission.
13  * THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR
14  * IMPLIED WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED
15  * WARRANTIES OF MERCHANTIBILITY AND FITNESS FOR A PARTICULAR PURPOSE.
16  */
17 
18 #ifndef lint
19 static char sccsid[] = "@(#)process.c	5.6 (Berkeley) 06/29/88";
20 #endif /* not lint */
21 
22 #include <bug.h>
23 #include <sys/file.h>
24 #include <sys/time.h>
25 #include <stdio.h>
26 #include <ctype.h>
27 
28 char	pfile[MAXPATHLEN];			/* permanent file name */
29 
30 /*
31  * process --
32  *	copy report to permanent file,
33  *	update summary file.
34  */
35 process()
36 {
37 	register int	rval;			/* read return value */
38 	struct timeval	tp;			/* time of day */
39 	int	lfd;				/* lock file descriptor */
40 	char	*ctime();
41 
42 	if (access(LOCK_FILE, R_OK) || (lfd = open(LOCK_FILE, O_RDONLY, 0)) < 0)
43 		error("can't find lock file %s.", LOCK_FILE);
44 	if (flock(lfd, LOCK_EX))
45 		error("can't get lock.", CHN);
46 	sprintf(pfile, "%s/%s/%d", dir, folder, getnext());
47 	fprintf(stderr, "\t%s\n", pfile);
48 	if (!(freopen(pfile, "w", stdout)))
49 		error("can't create %s.", pfile);
50 	rewind(stdin);
51 	while ((rval = read(fileno(stdin), bfr, sizeof(bfr))) != ERR && rval)
52 		if (write(fileno(stdout), bfr, rval) != rval)
53 			error("write to %s failed.", pfile);
54 
55 	/* append information to the summary file */
56 	sprintf(bfr, "%s/%s", dir, SUMMARY_FILE);
57 	if (!(freopen(bfr, "a", stdout)))
58 		error("can't append to summary file %s.", bfr);
59 	if (gettimeofday(&tp, (struct timezone *)NULL))
60 		error("can't get time of day.", CHN);
61 	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");
62 	(void)flock(lfd, LOCK_UN);
63 	(void)fclose(stdout);
64 }
65 
66 /*
67  * getnext --
68  *	get next file name (number)
69  */
70 static
71 getnext()
72 {
73 	register struct direct	*d;		/* directory structure */
74 	register DIR	*dirp;			/* directory pointer */
75 	register int	highval,
76 			newval;
77 	register char	*C;
78 
79 	sprintf(bfr, "%s/%s", dir, folder);
80 	if (!(dirp = opendir(bfr)))
81 		error("can't read folder directory %s.", bfr);
82 	for (highval = 0;d = readdir(dirp);)
83 		for (C = d->d_name;;++C)
84 			if (!*C) {
85 				if ((newval = atoi(d->d_name)) > highval)
86 					highval = newval;
87 				break;
88 			}
89 			else if (!isdigit(*C))
90 				break;
91 	closedir(dirp);
92 	return(++highval);
93 }
94