if no home directory, log into '/'; delete old #ifdef/notdef and
[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
5e8b0e60
KB
6 * provided that the above copyright notice and this paragraph are
7 * duplicated in all such forms and that any documentation,
8 * advertising materials, and other materials related to such
9 * distribution and use acknowledge that the software was developed
10 * by the University of California, Berkeley. The name of the
11 * University may not be used to endorse or promote products derived
12 * from this software without specific prior written permission.
13 * THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR
14 * IMPLIED WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED
15 * WARRANTIES OF MERCHANTIBILITY AND FITNESS FOR A PARTICULAR PURPOSE.
10236a65
KB
16 */
17
18#ifndef lint
5e8b0e60 19static char sccsid[] = "@(#)process.c 5.6 (Berkeley) %G%";
aec2eff2 20#endif /* not lint */
10236a65
KB
21
22#include <bug.h>
23#include <sys/file.h>
f36776a7 24#include <sys/time.h>
10236a65 25#include <stdio.h>
d8336336 26#include <ctype.h>
10236a65
KB
27
28char pfile[MAXPATHLEN]; /* permanent file name */
29
30/*
31 * process --
d8336336
KB
32 * copy report to permanent file,
33 * update summary file.
10236a65
KB
34 */
35process()
36{
37 register int rval; /* read return value */
f36776a7 38 struct timeval tp; /* time of day */
d8336336 39 int lfd; /* lock file descriptor */
f36776a7 40 char *ctime();
10236a65 41
d8336336
KB
42 if (access(LOCK_FILE, R_OK) || (lfd = open(LOCK_FILE, O_RDONLY, 0)) < 0)
43 error("can't find lock file %s.", LOCK_FILE);
44 if (flock(lfd, LOCK_EX))
45 error("can't get lock.", CHN);
46 sprintf(pfile, "%s/%s/%d", dir, folder, getnext());
47 fprintf(stderr, "\t%s\n", pfile);
48 if (!(freopen(pfile, "w", stdout)))
49 error("can't create %s.", pfile);
10236a65 50 rewind(stdin);
d8336336
KB
51 while ((rval = read(fileno(stdin), bfr, sizeof(bfr))) != ERR && rval)
52 if (write(fileno(stdout), bfr, rval) != rval)
53 error("write to %s failed.", pfile);
10236a65
KB
54
55 /* append information to the summary file */
d8336336
KB
56 sprintf(bfr, "%s/%s", dir, SUMMARY_FILE);
57 if (!(freopen(bfr, "a", stdout)))
58 error("can't append to summary file %s.", bfr);
59 if (gettimeofday(&tp, (struct timezone *)NULL))
60 error("can't get time of day.", CHN);
3de934b4 61 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
62 (void)flock(lfd, LOCK_UN);
63 (void)fclose(stdout);
10236a65
KB
64}
65
66/*
67 * getnext --
68 * get next file name (number)
69 */
70static
71getnext()
72{
73 register struct direct *d; /* directory structure */
74 register DIR *dirp; /* directory pointer */
d8336336
KB
75 register int highval,
76 newval;
77 register char *C;
10236a65 78
d8336336
KB
79 sprintf(bfr, "%s/%s", dir, folder);
80 if (!(dirp = opendir(bfr)))
81 error("can't read folder directory %s.", bfr);
82 for (highval = 0;d = readdir(dirp);)
83 for (C = d->d_name;;++C)
84 if (!*C) {
85 if ((newval = atoi(d->d_name)) > highval)
86 highval = newval;
87 break;
88 }
89 else if (!isdigit(*C))
90 break;
10236a65 91 closedir(dirp);
d8336336 92 return(++highval);
10236a65 93}