xref: /original-bsd/libexec/bugfiler/process.c (revision 91ce9d53)
1b38e0f18Sbostic /*
237c8c8e2Sbostic  * Copyright (c) 1986, 1987 Regents of the University of California.
337c8c8e2Sbostic  * All rights reserved.
437c8c8e2Sbostic  *
537c8c8e2Sbostic  * Redistribution and use in source and binary forms are permitted
65fa3563aSbostic  * provided that the above copyright notice and this paragraph are
75fa3563aSbostic  * duplicated in all such forms and that any documentation,
85fa3563aSbostic  * advertising materials, and other materials related to such
95fa3563aSbostic  * distribution and use acknowledge that the software was developed
105fa3563aSbostic  * by the University of California, Berkeley.  The name of the
115fa3563aSbostic  * University may not be used to endorse or promote products derived
125fa3563aSbostic  * from this software without specific prior written permission.
135fa3563aSbostic  * THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR
145fa3563aSbostic  * IMPLIED WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED
155fa3563aSbostic  * WARRANTIES OF MERCHANTIBILITY AND FITNESS FOR A PARTICULAR PURPOSE.
16b38e0f18Sbostic  */
17b38e0f18Sbostic 
18b38e0f18Sbostic #ifndef lint
19*91ce9d53Sbostic static char sccsid[] = "@(#)process.c	5.7 (Berkeley) 07/16/89";
2037c8c8e2Sbostic #endif /* not lint */
21b38e0f18Sbostic 
22b38e0f18Sbostic #include <bug.h>
23b38e0f18Sbostic #include <sys/file.h>
244803d908Sbostic #include <sys/time.h>
25b38e0f18Sbostic #include <stdio.h>
263212f6deSbostic #include <ctype.h>
27b38e0f18Sbostic 
28b38e0f18Sbostic char	pfile[MAXPATHLEN];			/* permanent file name */
29b38e0f18Sbostic 
30b38e0f18Sbostic /*
31b38e0f18Sbostic  * process --
323212f6deSbostic  *	copy report to permanent file,
333212f6deSbostic  *	update summary file.
34b38e0f18Sbostic  */
35b38e0f18Sbostic process()
36b38e0f18Sbostic {
37b38e0f18Sbostic 	register int	rval;			/* read return value */
384803d908Sbostic 	struct timeval	tp;			/* time of day */
393212f6deSbostic 	int	lfd;				/* lock file descriptor */
404803d908Sbostic 	char	*ctime();
41b38e0f18Sbostic 
423212f6deSbostic 	if (access(LOCK_FILE, R_OK) || (lfd = open(LOCK_FILE, O_RDONLY, 0)) < 0)
433212f6deSbostic 		error("can't find lock file %s.", LOCK_FILE);
443212f6deSbostic 	if (flock(lfd, LOCK_EX))
453212f6deSbostic 		error("can't get lock.", CHN);
46b38e0f18Sbostic 	sprintf(pfile, "%s/%s/%d", dir, folder, getnext());
47b38e0f18Sbostic 	fprintf(stderr, "\t%s\n", pfile);
48b38e0f18Sbostic 	if (!(freopen(pfile, "w", stdout)))
493212f6deSbostic 		error("can't create %s.", pfile);
50b38e0f18Sbostic 	rewind(stdin);
51b38e0f18Sbostic 	while ((rval = read(fileno(stdin), bfr, sizeof(bfr))) != ERR && rval)
523212f6deSbostic 		if (write(fileno(stdout), bfr, rval) != rval)
533212f6deSbostic 			error("write to %s failed.", pfile);
54b38e0f18Sbostic 
55b38e0f18Sbostic 	/* append information to the summary file */
56b38e0f18Sbostic 	sprintf(bfr, "%s/%s", dir, SUMMARY_FILE);
57b38e0f18Sbostic 	if (!(freopen(bfr, "a", stdout)))
583212f6deSbostic 		error("can't append to summary file %s.", bfr);
593212f6deSbostic 	if (gettimeofday(&tp, (struct timezone *)NULL))
603212f6deSbostic 		error("can't get time of day.", CHN);
61224b47f9Sbostic 	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");
623212f6deSbostic 	(void)flock(lfd, LOCK_UN);
633212f6deSbostic 	(void)fclose(stdout);
64b38e0f18Sbostic }
65b38e0f18Sbostic 
66b38e0f18Sbostic /*
67b38e0f18Sbostic  * getnext --
68b38e0f18Sbostic  *	get next file name (number)
69b38e0f18Sbostic  */
70b38e0f18Sbostic static
71b38e0f18Sbostic getnext()
72b38e0f18Sbostic {
73b38e0f18Sbostic 	register struct direct *d;		/* directory structure */
74b38e0f18Sbostic 	register DIR *dirp;			/* directory pointer */
75*91ce9d53Sbostic 	register int highval, newval;
76*91ce9d53Sbostic 	register char *p;
77b38e0f18Sbostic 
78*91ce9d53Sbostic 	(void)sprintf(bfr, "%s/%s", dir, folder);
793212f6deSbostic 	if (!(dirp = opendir(bfr)))
803212f6deSbostic 		error("can't read folder directory %s.", bfr);
81*91ce9d53Sbostic 	for (highval = -1; d = readdir(dirp);) {
82*91ce9d53Sbostic 		for (p = d->d_name; *p && isdigit(*p); ++p);
83*91ce9d53Sbostic 		if (!*p && (newval = atoi(d->d_name)) > highval)
843212f6deSbostic 			highval = newval;
85b38e0f18Sbostic 	}
86b38e0f18Sbostic 	closedir(dirp);
873212f6deSbostic 	return(++highval);
88b38e0f18Sbostic }
89