xref: /original-bsd/libexec/bugfiler/process.c (revision 054717d4)
1b38e0f18Sbostic /*
237c8c8e2Sbostic  * Copyright (c) 1986, 1987 Regents of the University of California.
337c8c8e2Sbostic  * All rights reserved.
437c8c8e2Sbostic  *
56472dbc6Sbostic  * %sccs.include.redist.c%
6b38e0f18Sbostic  */
7b38e0f18Sbostic 
8b38e0f18Sbostic #ifndef lint
9*054717d4Sbostic static char sccsid[] = "@(#)process.c	5.9 (Berkeley) 02/25/91";
1037c8c8e2Sbostic #endif /* not lint */
11b38e0f18Sbostic 
12*054717d4Sbostic #include <sys/param.h>
134803d908Sbostic #include <sys/time.h>
14*054717d4Sbostic #include <fcntl.h>
15*054717d4Sbostic #include <dirent.h>
16*054717d4Sbostic #include <unistd.h>
17b38e0f18Sbostic #include <stdio.h>
183212f6deSbostic #include <ctype.h>
19*054717d4Sbostic #include <stdlib.h>
20*054717d4Sbostic #include "bug.h"
21b38e0f18Sbostic 
22b38e0f18Sbostic char	pfile[MAXPATHLEN];			/* permanent file name */
23b38e0f18Sbostic 
24b38e0f18Sbostic /*
25b38e0f18Sbostic  * process --
263212f6deSbostic  *	copy report to permanent file,
273212f6deSbostic  *	update summary file.
28b38e0f18Sbostic  */
29b38e0f18Sbostic process()
30b38e0f18Sbostic {
31b38e0f18Sbostic 	register int	rval;			/* read return value */
324803d908Sbostic 	struct timeval	tp;			/* time of day */
333212f6deSbostic 	int	lfd;				/* lock file descriptor */
34*054717d4Sbostic 	static int getnext();
35b38e0f18Sbostic 
363212f6deSbostic 	if (access(LOCK_FILE, R_OK) || (lfd = open(LOCK_FILE, O_RDONLY, 0)) < 0)
373212f6deSbostic 		error("can't find lock file %s.", LOCK_FILE);
383212f6deSbostic 	if (flock(lfd, LOCK_EX))
393212f6deSbostic 		error("can't get lock.", CHN);
40b38e0f18Sbostic 	sprintf(pfile, "%s/%s/%d", dir, folder, getnext());
41b38e0f18Sbostic 	fprintf(stderr, "\t%s\n", pfile);
42b38e0f18Sbostic 	if (!(freopen(pfile, "w", stdout)))
433212f6deSbostic 		error("can't create %s.", pfile);
44b38e0f18Sbostic 	rewind(stdin);
45b38e0f18Sbostic 	while ((rval = read(fileno(stdin), bfr, sizeof(bfr))) != ERR && rval)
463212f6deSbostic 		if (write(fileno(stdout), bfr, rval) != rval)
473212f6deSbostic 			error("write to %s failed.", pfile);
48b38e0f18Sbostic 
49b38e0f18Sbostic 	/* append information to the summary file */
50b38e0f18Sbostic 	sprintf(bfr, "%s/%s", dir, SUMMARY_FILE);
51b38e0f18Sbostic 	if (!(freopen(bfr, "a", stdout)))
523212f6deSbostic 		error("can't append to summary file %s.", bfr);
533212f6deSbostic 	if (gettimeofday(&tp, (struct timezone *)NULL))
543212f6deSbostic 		error("can't get time of day.", CHN);
55224b47f9Sbostic 	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");
563212f6deSbostic 	(void)flock(lfd, LOCK_UN);
573212f6deSbostic 	(void)fclose(stdout);
58b38e0f18Sbostic }
59b38e0f18Sbostic 
60b38e0f18Sbostic /*
61b38e0f18Sbostic  * getnext --
62b38e0f18Sbostic  *	get next file name (number)
63b38e0f18Sbostic  */
64*054717d4Sbostic static int
65b38e0f18Sbostic getnext()
66b38e0f18Sbostic {
67*054717d4Sbostic 	register struct dirent *d;		/* directory structure */
68b38e0f18Sbostic 	register DIR *dirp;			/* directory pointer */
6991ce9d53Sbostic 	register int highval, newval;
7091ce9d53Sbostic 	register char *p;
71b38e0f18Sbostic 
7291ce9d53Sbostic 	(void)sprintf(bfr, "%s/%s", dir, folder);
733212f6deSbostic 	if (!(dirp = opendir(bfr)))
743212f6deSbostic 		error("can't read folder directory %s.", bfr);
7591ce9d53Sbostic 	for (highval = -1; d = readdir(dirp);) {
7691ce9d53Sbostic 		for (p = d->d_name; *p && isdigit(*p); ++p);
7791ce9d53Sbostic 		if (!*p && (newval = atoi(d->d_name)) > highval)
783212f6deSbostic 			highval = newval;
79b38e0f18Sbostic 	}
80b38e0f18Sbostic 	closedir(dirp);
813212f6deSbostic 	return(++highval);
82b38e0f18Sbostic }
83