NetBSD/libexec/bugfiler/process.c

110 lines
4.0 KiB
C

/*
* Copyright (c) 1986, 1987 Regents of the University of California.
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
* 3. All advertising materials mentioning features or use of this software
* must display the following acknowledgement:
* This product includes software developed by the University of
* California, Berkeley and its contributors.
* 4. Neither the name of the University nor the names of its contributors
* may be used to endorse or promote products derived from this software
* without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
* ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
* SUCH DAMAGE.
*/
#ifndef lint
/*static char sccsid[] = "from: @(#)process.c 5.9 (Berkeley) 2/25/91";*/
static char rcsid[] = "$Id: process.c,v 1.2 1993/08/01 18:31:23 mycroft Exp $";
#endif /* not lint */
#include <sys/param.h>
#include <sys/time.h>
#include <fcntl.h>
#include <dirent.h>
#include <unistd.h>
#include <stdio.h>
#include <ctype.h>
#include <stdlib.h>
#include "bug.h"
char pfile[MAXPATHLEN]; /* permanent file name */
/*
* process --
* copy report to permanent file,
* update summary file.
*/
process()
{
register int rval; /* read return value */
struct timeval tp; /* time of day */
int lfd; /* lock file descriptor */
static int getnext();
if (access(LOCK_FILE, R_OK) || (lfd = open(LOCK_FILE, O_RDONLY, 0)) < 0)
error("can't find lock file %s.", LOCK_FILE);
if (flock(lfd, LOCK_EX))
error("can't get lock.", CHN);
sprintf(pfile, "%s/%s/%d", dir, folder, getnext());
fprintf(stderr, "\t%s\n", pfile);
if (!(freopen(pfile, "w", stdout)))
error("can't create %s.", pfile);
rewind(stdin);
while ((rval = read(fileno(stdin), bfr, sizeof(bfr))) != ERR && rval)
if (write(fileno(stdout), bfr, rval) != rval)
error("write to %s failed.", pfile);
/* append information to the summary file */
sprintf(bfr, "%s/%s", dir, SUMMARY_FILE);
if (!(freopen(bfr, "a", stdout)))
error("can't append to summary file %s.", bfr);
if (gettimeofday(&tp, (struct timezone *)NULL))
error("can't get time of day.", CHN);
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");
(void)flock(lfd, LOCK_UN);
(void)fclose(stdout);
}
/*
* getnext --
* get next file name (number)
*/
static int
getnext()
{
register struct dirent *d; /* directory structure */
register DIR *dirp; /* directory pointer */
register int highval, newval;
register char *p;
(void)sprintf(bfr, "%s/%s", dir, folder);
if (!(dirp = opendir(bfr)))
error("can't read folder directory %s.", bfr);
for (highval = -1; d = readdir(dirp);) {
for (p = d->d_name; *p && isdigit(*p); ++p);
if (!*p && (newval = atoi(d->d_name)) > highval)
highval = newval;
}
closedir(dirp);
return(++highval);
}