xref: /original-bsd/libexec/bugfiler/process.c (revision 92e05a12)
1b38e0f18Sbostic /*
2*92e05a12Sbostic  * Copyright (c) 1986, 1987, 1993
3*92e05a12Sbostic  *	The Regents of the University of California.  All rights reserved.
437c8c8e2Sbostic  *
56472dbc6Sbostic  * %sccs.include.redist.c%
6b38e0f18Sbostic  */
7b38e0f18Sbostic 
8b38e0f18Sbostic #ifndef lint
9*92e05a12Sbostic static char sccsid[] = "@(#)process.c	8.1 (Berkeley) 06/04/93";
1037c8c8e2Sbostic #endif /* not lint */
11b38e0f18Sbostic 
12054717d4Sbostic #include <sys/param.h>
134803d908Sbostic #include <sys/time.h>
14c48498dbSbostic 
153212f6deSbostic #include <ctype.h>
16c48498dbSbostic #include <dirent.h>
17c48498dbSbostic #include <fcntl.h>
18c48498dbSbostic #include <stdio.h>
19054717d4Sbostic #include <stdlib.h>
20c48498dbSbostic #include <unistd.h>
21c48498dbSbostic 
22054717d4Sbostic #include "bug.h"
23c48498dbSbostic #include "extern.h"
24b38e0f18Sbostic 
25b38e0f18Sbostic char	pfile[MAXPATHLEN];			/* permanent file name */
26b38e0f18Sbostic 
27c48498dbSbostic static int getnext __P((void));
28c48498dbSbostic 
29b38e0f18Sbostic /*
30b38e0f18Sbostic  * process --
313212f6deSbostic  *	copy report to permanent file,
323212f6deSbostic  *	update summary file.
33b38e0f18Sbostic  */
34c48498dbSbostic int
process()35b38e0f18Sbostic process()
36b38e0f18Sbostic {
37b38e0f18Sbostic 	register int	rval;			/* read return value */
384803d908Sbostic 	struct timeval	tp;			/* time of day */
393212f6deSbostic 	int	lfd;				/* lock file descriptor */
40b38e0f18Sbostic 
413212f6deSbostic 	if (access(LOCK_FILE, R_OK) || (lfd = open(LOCK_FILE, O_RDONLY, 0)) < 0)
423212f6deSbostic 		error("can't find lock file %s.", LOCK_FILE);
433212f6deSbostic 	if (flock(lfd, LOCK_EX))
443212f6deSbostic 		error("can't get lock.", CHN);
45b38e0f18Sbostic 	sprintf(pfile, "%s/%s/%d", dir, folder, getnext());
46b38e0f18Sbostic 	fprintf(stderr, "\t%s\n", pfile);
47b38e0f18Sbostic 	if (!(freopen(pfile, "w", stdout)))
483212f6deSbostic 		error("can't create %s.", pfile);
49b38e0f18Sbostic 	rewind(stdin);
50b38e0f18Sbostic 	while ((rval = read(fileno(stdin), bfr, sizeof(bfr))) != ERR && rval)
513212f6deSbostic 		if (write(fileno(stdout), bfr, rval) != rval)
523212f6deSbostic 			error("write to %s failed.", pfile);
53b38e0f18Sbostic 
54b38e0f18Sbostic 	/* append information to the summary file */
55b38e0f18Sbostic 	sprintf(bfr, "%s/%s", dir, SUMMARY_FILE);
56b38e0f18Sbostic 	if (!(freopen(bfr, "a", stdout)))
573212f6deSbostic 		error("can't append to summary file %s.", bfr);
583212f6deSbostic 	if (gettimeofday(&tp, (struct timezone *)NULL))
593212f6deSbostic 		error("can't get time of day.", CHN);
60c48498dbSbostic 	printf("\n%s\t\t%s\t%s\t%s\tOwner: Bugs Bunny\n\tStatus: Received\n",
61c48498dbSbostic 	    pfile, ctime(&tp.tv_sec), mailhead[INDX_TAG].line,
62c48498dbSbostic 	    mailhead[SUBJ_TAG].found ? mailhead[SUBJ_TAG].line : "Subject:\n");
633212f6deSbostic 	(void)flock(lfd, LOCK_UN);
643212f6deSbostic 	(void)fclose(stdout);
65b38e0f18Sbostic }
66b38e0f18Sbostic 
67b38e0f18Sbostic /*
68b38e0f18Sbostic  * getnext --
69b38e0f18Sbostic  *	get next file name (number)
70b38e0f18Sbostic  */
71054717d4Sbostic static int
getnext()72b38e0f18Sbostic getnext()
73b38e0f18Sbostic {
74054717d4Sbostic 	register struct dirent *d;		/* directory structure */
75b38e0f18Sbostic 	register DIR *dirp;			/* directory pointer */
7691ce9d53Sbostic 	register int highval, newval;
7791ce9d53Sbostic 	register char *p;
78b38e0f18Sbostic 
7991ce9d53Sbostic 	(void)sprintf(bfr, "%s/%s", dir, folder);
803212f6deSbostic 	if (!(dirp = opendir(bfr)))
813212f6deSbostic 		error("can't read folder directory %s.", bfr);
8291ce9d53Sbostic 	for (highval = -1; d = readdir(dirp);) {
8391ce9d53Sbostic 		for (p = d->d_name; *p && isdigit(*p); ++p);
8491ce9d53Sbostic 		if (!*p && (newval = atoi(d->d_name)) > highval)
853212f6deSbostic 			highval = newval;
86b38e0f18Sbostic 	}
87b38e0f18Sbostic 	closedir(dirp);
883212f6deSbostic 	return(++highval);
89b38e0f18Sbostic }
90