Commit | Line | Data |
---|---|---|
07a70c1c | 1 | /* |
aec2eff2 KB |
2 | * Copyright (c) 1986, 1987 Regents of the University of California. |
3 | * All rights reserved. | |
4 | * | |
1343342a | 5 | * %sccs.include.redist.c% |
07a70c1c KB |
6 | */ |
7 | ||
8 | #ifndef lint | |
6dfa0163 | 9 | static char sccsid[] = "@(#)gethead.c 5.11 (Berkeley) %G%"; |
aec2eff2 | 10 | #endif /* not lint */ |
07a70c1c | 11 | |
d0ef4a0d | 12 | #include <sys/param.h> |
07a70c1c | 13 | #include <sys/stat.h> |
6dfa0163 | 14 | |
d0ef4a0d | 15 | #include <dirent.h> |
07a70c1c | 16 | #include <stdio.h> |
d0ef4a0d KB |
17 | #include <stdlib.h> |
18 | #include <string.h> | |
6dfa0163 KB |
19 | #include <unistd.h> |
20 | ||
435e8dff | 21 | #include "pathnames.h" |
d0ef4a0d | 22 | #include "bug.h" |
6dfa0163 | 23 | #include "extern.h" |
07a70c1c | 24 | |
6dfa0163 KB |
25 | static int chk1 __P((char *)); |
26 | static int pbuf __P((char *)); | |
07a70c1c KB |
27 | |
28 | #define ENT(X) sizeof(X) - 1, X | |
29 | HEADER mailhead[] = { /* mail headers */ | |
30 | { NO, YES, NULL, ENT("Date:"), }, | |
31 | { NO, NO, NULL, ENT("From "), }, | |
32 | { NO, YES, NULL, ENT("From:"), }, | |
33 | { NO, NO, chk1, ENT("Index:"), }, | |
34 | { NO, YES, NULL, ENT("Message-Id:"), }, | |
24213599 MK |
35 | { NO, YES, NULL, ENT("Reply-To:"), }, |
36 | { NO, YES, NULL, ENT("Return-Path:"), }, | |
5ea1bf0e | 37 | { NO, NO, pbuf, ENT("Subject:"), }, |
24213599 MK |
38 | { NO, YES, NULL, ENT("To:"), }, |
39 | { NO, NO, NULL, ENT("Apparently-To:"), }, | |
07a70c1c KB |
40 | { ERR, } |
41 | }; | |
42 | ||
d8336336 KB |
43 | FILE *dfp; /* distf file pointer */ |
44 | char dir[MAXNAMLEN], /* subject and folder */ | |
07a70c1c KB |
45 | folder[MAXNAMLEN]; |
46 | ||
47 | /* | |
48 | * gethead -- | |
49 | * read mail and bug headers from bug report, construct redist headers | |
50 | */ | |
6dfa0163 | 51 | void |
d8336336 KB |
52 | gethead(redist) |
53 | int redist; | |
07a70c1c KB |
54 | { |
55 | register HEADER *hp; /* mail header pointer */ | |
d8336336 KB |
56 | |
57 | if (redist) { | |
58 | int fd; | |
59 | char *distf; | |
07a70c1c | 60 | |
6e1d805a | 61 | distf = strdup(_PATH_TMP); |
d8336336 KB |
62 | if (!(fd = mkstemp(distf)) || !(dfp = fdopen(fd, "w+"))) |
63 | error("can't create redistribution file %s.", distf); | |
64 | /* disappear after last reference is closed */ | |
65 | (void)unlink(distf); | |
6e1d805a | 66 | free(distf); |
d8336336 KB |
67 | } |
68 | if (!freopen(tmpname, "r", stdin)) | |
69 | error("can't read temporary bug file %s.", tmpname); | |
07a70c1c | 70 | |
d8336336 KB |
71 | while (fgets(bfr, sizeof(bfr), stdin)) { |
72 | for (hp = mailhead; hp->found != ERR; ++hp) | |
07a70c1c | 73 | if (!hp->found) |
d8336336 | 74 | if (!strncmp(hp->tag, bfr, hp->len)) { |
07a70c1c KB |
75 | if (hp->valid && !((*(hp->valid))(bfr))) |
76 | break; | |
6dfa0163 KB |
77 | if (!(hp->line = |
78 | malloc((u_int)(strlen(bfr) + 1)))) | |
d8336336 KB |
79 | error("malloc failed.", CHN); |
80 | (void)strcpy(hp->line, bfr); | |
07a70c1c KB |
81 | hp->found = YES; |
82 | break; | |
83 | } | |
d8336336 KB |
84 | if ((hp->found == ERR || hp->redist) && redist) |
85 | fputs(bfr, dfp); | |
07a70c1c KB |
86 | } |
87 | ||
88 | if (!mailhead[INDX_TAG].found) | |
d8336336 | 89 | error("no readable \"Index:\" header in bug report.", CHN); |
07a70c1c KB |
90 | } |
91 | ||
92 | /* | |
93 | * chk1 -- | |
94 | * parse the "Index:" line into folder and directory | |
95 | */ | |
6dfa0163 | 96 | static int |
07a70c1c | 97 | chk1(line) |
d8336336 | 98 | char *line; |
07a70c1c KB |
99 | { |
100 | register char *C; /* tmp pointer */ | |
101 | struct stat sbuf; /* existence check */ | |
07a70c1c | 102 | |
d8336336 | 103 | if (sscanf(line, " Index: %s %s ", folder, dir) != 2) |
07a70c1c | 104 | return(NO); |
6dfa0163 | 105 | if (C = strchr(folder, '/')) { /* deal with "bin/from.c" */ |
07a70c1c KB |
106 | if (C == folder) |
107 | return(NO); | |
108 | *C = EOS; | |
109 | } | |
d8336336 | 110 | if (stat(dir, &sbuf) || (sbuf.st_mode & S_IFMT) != S_IFDIR) |
07a70c1c | 111 | return(NO); |
5ea1bf0e KB |
112 | (void)pbuf(line); |
113 | return(YES); | |
114 | } | |
115 | ||
116 | /* | |
117 | * pbuf -- | |
118 | * kludge so that summary file looks pretty | |
119 | */ | |
6dfa0163 | 120 | static int |
5ea1bf0e KB |
121 | pbuf(line) |
122 | char *line; | |
123 | { | |
124 | register char *rp, /* tmp pointers */ | |
125 | *wp; | |
126 | ||
127 | for (rp = line; *rp == ' ' || *rp == '\t'; ++rp); | |
128 | for (wp = line; *rp; ++wp) { | |
129 | if ((*wp = *rp++) != ' ' && *wp != '\t') | |
130 | continue; | |
131 | *wp = ' '; | |
132 | while (*rp == ' ' || *rp == '\t') | |
133 | ++rp; | |
134 | } | |
135 | if (wp[-1] == ' ') /* wp can't == line */ | |
136 | --wp; | |
137 | *wp = EOS; | |
07a70c1c KB |
138 | return(YES); |
139 | } |