xref: /original-bsd/libexec/bugfiler/process.c (revision 6472dbc6)
1b38e0f18Sbostic /*
237c8c8e2Sbostic  * Copyright (c) 1986, 1987 Regents of the University of California.
337c8c8e2Sbostic  * All rights reserved.
437c8c8e2Sbostic  *
5*6472dbc6Sbostic  * %sccs.include.redist.c%
6b38e0f18Sbostic  */
7b38e0f18Sbostic 
8b38e0f18Sbostic #ifndef lint
9*6472dbc6Sbostic static char sccsid[] = "@(#)process.c	5.8 (Berkeley) 06/01/90";
1037c8c8e2Sbostic #endif /* not lint */
11b38e0f18Sbostic 
12b38e0f18Sbostic #include <bug.h>
13b38e0f18Sbostic #include <sys/file.h>
144803d908Sbostic #include <sys/time.h>
15b38e0f18Sbostic #include <stdio.h>
163212f6deSbostic #include <ctype.h>
17b38e0f18Sbostic 
18b38e0f18Sbostic char	pfile[MAXPATHLEN];			/* permanent file name */
19b38e0f18Sbostic 
20b38e0f18Sbostic /*
21b38e0f18Sbostic  * process --
223212f6deSbostic  *	copy report to permanent file,
233212f6deSbostic  *	update summary file.
24b38e0f18Sbostic  */
25b38e0f18Sbostic process()
26b38e0f18Sbostic {
27b38e0f18Sbostic 	register int	rval;			/* read return value */
284803d908Sbostic 	struct timeval	tp;			/* time of day */
293212f6deSbostic 	int	lfd;				/* lock file descriptor */
304803d908Sbostic 	char	*ctime();
31b38e0f18Sbostic 
323212f6deSbostic 	if (access(LOCK_FILE, R_OK) || (lfd = open(LOCK_FILE, O_RDONLY, 0)) < 0)
333212f6deSbostic 		error("can't find lock file %s.", LOCK_FILE);
343212f6deSbostic 	if (flock(lfd, LOCK_EX))
353212f6deSbostic 		error("can't get lock.", CHN);
36b38e0f18Sbostic 	sprintf(pfile, "%s/%s/%d", dir, folder, getnext());
37b38e0f18Sbostic 	fprintf(stderr, "\t%s\n", pfile);
38b38e0f18Sbostic 	if (!(freopen(pfile, "w", stdout)))
393212f6deSbostic 		error("can't create %s.", pfile);
40b38e0f18Sbostic 	rewind(stdin);
41b38e0f18Sbostic 	while ((rval = read(fileno(stdin), bfr, sizeof(bfr))) != ERR && rval)
423212f6deSbostic 		if (write(fileno(stdout), bfr, rval) != rval)
433212f6deSbostic 			error("write to %s failed.", pfile);
44b38e0f18Sbostic 
45b38e0f18Sbostic 	/* append information to the summary file */
46b38e0f18Sbostic 	sprintf(bfr, "%s/%s", dir, SUMMARY_FILE);
47b38e0f18Sbostic 	if (!(freopen(bfr, "a", stdout)))
483212f6deSbostic 		error("can't append to summary file %s.", bfr);
493212f6deSbostic 	if (gettimeofday(&tp, (struct timezone *)NULL))
503212f6deSbostic 		error("can't get time of day.", CHN);
51224b47f9Sbostic 	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");
523212f6deSbostic 	(void)flock(lfd, LOCK_UN);
533212f6deSbostic 	(void)fclose(stdout);
54b38e0f18Sbostic }
55b38e0f18Sbostic 
56b38e0f18Sbostic /*
57b38e0f18Sbostic  * getnext --
58b38e0f18Sbostic  *	get next file name (number)
59b38e0f18Sbostic  */
60b38e0f18Sbostic static
61b38e0f18Sbostic getnext()
62b38e0f18Sbostic {
63b38e0f18Sbostic 	register struct direct *d;		/* directory structure */
64b38e0f18Sbostic 	register DIR *dirp;			/* directory pointer */
6591ce9d53Sbostic 	register int highval, newval;
6691ce9d53Sbostic 	register char *p;
67b38e0f18Sbostic 
6891ce9d53Sbostic 	(void)sprintf(bfr, "%s/%s", dir, folder);
693212f6deSbostic 	if (!(dirp = opendir(bfr)))
703212f6deSbostic 		error("can't read folder directory %s.", bfr);
7191ce9d53Sbostic 	for (highval = -1; d = readdir(dirp);) {
7291ce9d53Sbostic 		for (p = d->d_name; *p && isdigit(*p); ++p);
7391ce9d53Sbostic 		if (!*p && (newval = atoi(d->d_name)) > highval)
743212f6deSbostic 			highval = newval;
75b38e0f18Sbostic 	}
76b38e0f18Sbostic 	closedir(dirp);
773212f6deSbostic 	return(++highval);
78b38e0f18Sbostic }
79