xref: /csrg-svn/libexec/bugfiler/process.c (revision 42663)
130163Sbostic /*
233416Sbostic  * Copyright (c) 1986, 1987 Regents of the University of California.
333416Sbostic  * All rights reserved.
433416Sbostic  *
5*42663Sbostic  * %sccs.include.redist.c%
630163Sbostic  */
730163Sbostic 
830163Sbostic #ifndef lint
9*42663Sbostic static char sccsid[] = "@(#)process.c	5.8 (Berkeley) 06/01/90";
1033416Sbostic #endif /* not lint */
1130163Sbostic 
1230163Sbostic #include <bug.h>
1330163Sbostic #include <sys/file.h>
1430425Sbostic #include <sys/time.h>
1530163Sbostic #include <stdio.h>
1630890Sbostic #include <ctype.h>
1730163Sbostic 
1830163Sbostic char	pfile[MAXPATHLEN];			/* permanent file name */
1930163Sbostic 
2030163Sbostic /*
2130163Sbostic  * process --
2230890Sbostic  *	copy report to permanent file,
2330890Sbostic  *	update summary file.
2430163Sbostic  */
2530163Sbostic process()
2630163Sbostic {
2730163Sbostic 	register int	rval;			/* read return value */
2830425Sbostic 	struct timeval	tp;			/* time of day */
2930890Sbostic 	int	lfd;				/* lock file descriptor */
3030425Sbostic 	char	*ctime();
3130163Sbostic 
3230890Sbostic 	if (access(LOCK_FILE, R_OK) || (lfd = open(LOCK_FILE, O_RDONLY, 0)) < 0)
3330890Sbostic 		error("can't find lock file %s.", LOCK_FILE);
3430890Sbostic 	if (flock(lfd, LOCK_EX))
3530890Sbostic 		error("can't get lock.", CHN);
3630890Sbostic 	sprintf(pfile, "%s/%s/%d", dir, folder, getnext());
3730890Sbostic 	fprintf(stderr, "\t%s\n", pfile);
3830890Sbostic 	if (!(freopen(pfile, "w", stdout)))
3930890Sbostic 		error("can't create %s.", pfile);
4030163Sbostic 	rewind(stdin);
4130890Sbostic 	while ((rval = read(fileno(stdin), bfr, sizeof(bfr))) != ERR && rval)
4230890Sbostic 		if (write(fileno(stdout), bfr, rval) != rval)
4330890Sbostic 			error("write to %s failed.", pfile);
4430163Sbostic 
4530163Sbostic 	/* append information to the summary file */
4630890Sbostic 	sprintf(bfr, "%s/%s", dir, SUMMARY_FILE);
4730890Sbostic 	if (!(freopen(bfr, "a", stdout)))
4830890Sbostic 		error("can't append to summary file %s.", bfr);
4930890Sbostic 	if (gettimeofday(&tp, (struct timezone *)NULL))
5030890Sbostic 		error("can't get time of day.", CHN);
5130947Sbostic 	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");
5230890Sbostic 	(void)flock(lfd, LOCK_UN);
5330890Sbostic 	(void)fclose(stdout);
5430163Sbostic }
5530163Sbostic 
5630163Sbostic /*
5730163Sbostic  * getnext --
5830163Sbostic  *	get next file name (number)
5930163Sbostic  */
6030163Sbostic static
6130163Sbostic getnext()
6230163Sbostic {
6338449Sbostic 	register struct direct *d;		/* directory structure */
6438449Sbostic 	register DIR *dirp;			/* directory pointer */
6538449Sbostic 	register int highval, newval;
6638449Sbostic 	register char *p;
6730163Sbostic 
6838449Sbostic 	(void)sprintf(bfr, "%s/%s", dir, folder);
6930890Sbostic 	if (!(dirp = opendir(bfr)))
7030890Sbostic 		error("can't read folder directory %s.", bfr);
7138449Sbostic 	for (highval = -1; d = readdir(dirp);) {
7238449Sbostic 		for (p = d->d_name; *p && isdigit(*p); ++p);
7338449Sbostic 		if (!*p && (newval = atoi(d->d_name)) > highval)
7438449Sbostic 			highval = newval;
7538449Sbostic 	}
7630163Sbostic 	closedir(dirp);
7730890Sbostic 	return(++highval);
7830163Sbostic }
79