add Berkeley specific copyright
[unix-history] / usr / src / libexec / bugfiler / process.c
CommitLineData
10236a65 1/*
aec2eff2
KB
2 * Copyright (c) 1986, 1987 Regents of the University of California.
3 * All rights reserved.
4 *
5 * Redistribution and use in source and binary forms are permitted
6 * provided that this notice is preserved and that due credit is given
7 * to the University of California at Berkeley. The name of the University
8 * may not be used to endorse or promote products derived from this
9 * software without specific prior written permission. This software
10 * is provided ``as is'' without express or implied warranty.
10236a65
KB
11 */
12
13#ifndef lint
aec2eff2
KB
14static char sccsid[] = "@(#)process.c 5.5 (Berkeley) %G%";
15#endif /* not lint */
10236a65
KB
16
17#include <bug.h>
18#include <sys/file.h>
f36776a7 19#include <sys/time.h>
10236a65 20#include <stdio.h>
d8336336 21#include <ctype.h>
10236a65
KB
22
23char pfile[MAXPATHLEN]; /* permanent file name */
24
25/*
26 * process --
d8336336
KB
27 * copy report to permanent file,
28 * update summary file.
10236a65
KB
29 */
30process()
31{
32 register int rval; /* read return value */
f36776a7 33 struct timeval tp; /* time of day */
d8336336 34 int lfd; /* lock file descriptor */
f36776a7 35 char *ctime();
10236a65 36
d8336336
KB
37 if (access(LOCK_FILE, R_OK) || (lfd = open(LOCK_FILE, O_RDONLY, 0)) < 0)
38 error("can't find lock file %s.", LOCK_FILE);
39 if (flock(lfd, LOCK_EX))
40 error("can't get lock.", CHN);
41 sprintf(pfile, "%s/%s/%d", dir, folder, getnext());
42 fprintf(stderr, "\t%s\n", pfile);
43 if (!(freopen(pfile, "w", stdout)))
44 error("can't create %s.", pfile);
10236a65 45 rewind(stdin);
d8336336
KB
46 while ((rval = read(fileno(stdin), bfr, sizeof(bfr))) != ERR && rval)
47 if (write(fileno(stdout), bfr, rval) != rval)
48 error("write to %s failed.", pfile);
10236a65
KB
49
50 /* append information to the summary file */
d8336336
KB
51 sprintf(bfr, "%s/%s", dir, SUMMARY_FILE);
52 if (!(freopen(bfr, "a", stdout)))
53 error("can't append to summary file %s.", bfr);
54 if (gettimeofday(&tp, (struct timezone *)NULL))
55 error("can't get time of day.", CHN);
3de934b4 56 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");
d8336336
KB
57 (void)flock(lfd, LOCK_UN);
58 (void)fclose(stdout);
10236a65
KB
59}
60
61/*
62 * getnext --
63 * get next file name (number)
64 */
65static
66getnext()
67{
68 register struct direct *d; /* directory structure */
69 register DIR *dirp; /* directory pointer */
d8336336
KB
70 register int highval,
71 newval;
72 register char *C;
10236a65 73
d8336336
KB
74 sprintf(bfr, "%s/%s", dir, folder);
75 if (!(dirp = opendir(bfr)))
76 error("can't read folder directory %s.", bfr);
77 for (highval = 0;d = readdir(dirp);)
78 for (C = d->d_name;;++C)
79 if (!*C) {
80 if ((newval = atoi(d->d_name)) > highval)
81 highval = newval;
82 break;
83 }
84 else if (!isdigit(*C))
85 break;
10236a65 86 closedir(dirp);
d8336336 87 return(++highval);
10236a65 88}