Commit | Line | Data |
---|---|---|
07a70c1c KB |
1 | /* |
2 | * Copyright (c) 1986 Regents of the University of California. | |
3 | * All rights reserved. The Berkeley software License Agreement | |
4 | * specifies the terms and conditions for redistribution. | |
5 | */ | |
6 | ||
7 | #ifndef lint | |
d8336336 | 8 | static char sccsid[] = "@(#)gethead.c 5.2 (Berkeley) 87/04/11"; |
07a70c1c KB |
9 | #endif not lint |
10 | ||
11 | #include <bug.h> | |
12 | #include <sys/stat.h> | |
07a70c1c KB |
13 | #include <stdio.h> |
14 | ||
15 | static int chk1(); | |
16 | ||
17 | #define ENT(X) sizeof(X) - 1, X | |
18 | HEADER mailhead[] = { /* mail headers */ | |
19 | { NO, YES, NULL, ENT("Date:"), }, | |
20 | { NO, NO, NULL, ENT("From "), }, | |
21 | { NO, YES, NULL, ENT("From:"), }, | |
22 | { NO, NO, chk1, ENT("Index:"), }, | |
23 | { NO, YES, NULL, ENT("Message-Id:"), }, | |
24 | { NO, NO, NULL, ENT("Reply-To:"), }, | |
25 | { NO, NO, NULL, ENT("Return-Path:"), }, | |
26 | { NO, NO, NULL, ENT("Subject:"), }, | |
27 | { NO, NO, NULL, ENT("To:"), }, | |
28 | { ERR, } | |
29 | }; | |
30 | ||
d8336336 KB |
31 | FILE *dfp; /* distf file pointer */ |
32 | char dir[MAXNAMLEN], /* subject and folder */ | |
07a70c1c KB |
33 | folder[MAXNAMLEN]; |
34 | ||
35 | /* | |
36 | * gethead -- | |
37 | * read mail and bug headers from bug report, construct redist headers | |
38 | */ | |
d8336336 KB |
39 | gethead(redist) |
40 | int redist; | |
07a70c1c KB |
41 | { |
42 | register HEADER *hp; /* mail header pointer */ | |
d8336336 KB |
43 | char *strcpy(), *malloc(); |
44 | ||
45 | if (redist) { | |
46 | int fd; | |
47 | char *distf; | |
07a70c1c | 48 | |
d8336336 KB |
49 | distf = "/tmp/BUG_XXXXXX"; |
50 | if (!(fd = mkstemp(distf)) || !(dfp = fdopen(fd, "w+"))) | |
51 | error("can't create redistribution file %s.", distf); | |
52 | /* disappear after last reference is closed */ | |
53 | (void)unlink(distf); | |
54 | } | |
55 | if (!freopen(tmpname, "r", stdin)) | |
56 | error("can't read temporary bug file %s.", tmpname); | |
07a70c1c | 57 | |
d8336336 KB |
58 | while (fgets(bfr, sizeof(bfr), stdin)) { |
59 | for (hp = mailhead; hp->found != ERR; ++hp) | |
07a70c1c | 60 | if (!hp->found) |
d8336336 | 61 | if (!strncmp(hp->tag, bfr, hp->len)) { |
07a70c1c KB |
62 | if (hp->valid && !((*(hp->valid))(bfr))) |
63 | break; | |
64 | if (!(hp->line = malloc((u_int)(strlen(bfr) + 1)))) | |
d8336336 KB |
65 | error("malloc failed.", CHN); |
66 | (void)strcpy(hp->line, bfr); | |
07a70c1c KB |
67 | hp->found = YES; |
68 | break; | |
69 | } | |
d8336336 KB |
70 | if ((hp->found == ERR || hp->redist) && redist) |
71 | fputs(bfr, dfp); | |
07a70c1c KB |
72 | } |
73 | ||
74 | if (!mailhead[INDX_TAG].found) | |
d8336336 | 75 | error("no readable \"Index:\" header in bug report.", CHN); |
07a70c1c KB |
76 | } |
77 | ||
78 | /* | |
79 | * chk1 -- | |
80 | * parse the "Index:" line into folder and directory | |
81 | */ | |
82 | static | |
83 | chk1(line) | |
d8336336 | 84 | char *line; |
07a70c1c KB |
85 | { |
86 | register char *C; /* tmp pointer */ | |
87 | struct stat sbuf; /* existence check */ | |
88 | char *index(); | |
89 | ||
d8336336 | 90 | if (sscanf(line, " Index: %s %s ", folder, dir) != 2) |
07a70c1c | 91 | return(NO); |
d8336336 | 92 | if (C = index(folder, '/')) { /* deal with "bin/from.c" */ |
07a70c1c KB |
93 | if (C == folder) |
94 | return(NO); | |
95 | *C = EOS; | |
96 | } | |
d8336336 | 97 | if (stat(dir, &sbuf) || (sbuf.st_mode & S_IFMT) != S_IFDIR) |
07a70c1c KB |
98 | return(NO); |
99 | return(YES); | |
100 | } |