xref: /original-bsd/libexec/bugfiler/redist.c (revision bafc759a)
1 /*
2  * Copyright (c) 1986, 1987 Regents of the University of California.
3  * All rights reserved.
4  *
5  * %sccs.include.redist.c%
6  */
7 
8 #ifndef lint
9 static char sccsid[] = "@(#)redist.c	5.12 (Berkeley) 04/01/91";
10 #endif /* not lint */
11 
12 #include <sys/param.h>
13 #include <dirent.h>
14 #include <stdio.h>
15 #include <ctype.h>
16 #include <string.h>
17 #include "bug.h"
18 #include "pathnames.h"
19 
20 /*
21  * redist --
22  *	Redistribute a bug report to those people indicated in the
23  *	redistribution list file.
24  */
25 redist()
26 {
27 	extern FILE	*dfp;		/* dist file fp */
28 	extern char	pfile[];	/* permanent bug file */
29 	register char	*C1, *C2;
30 	FILE	*pf, *popen();
31 	int	group;
32 	char	*p, *index();
33 
34 	(void)sprintf(bfr, "%s/%s", dir, DIST_FILE);
35 	if (!freopen(bfr, "r", stdin))
36 		return;
37 	for (pf = NULL, group = 0; fgets(bfr, sizeof(bfr), stdin);) {
38 		if (C1 = index(bfr, '\n'))
39 			*C1 = '\0';
40 nextline:	if (*bfr == COMMENT || isspace(*bfr) || !(C1 = index(bfr, ':')))
41 			continue;
42 		*C1 = EOS;
43 		if (!strcmp(bfr, folder) || !strcmp(bfr, "all")) {
44 			for (++C1; *C1 && (*C1 == ' ' || *C1 == '\t'); ++C1);
45 			if (!*C1)			/* if empty list */
46 				continue;
47 			if (!pf) {
48 				if (!(pf = popen(MAIL_CMD, "w")))
49 					error("sendmail pipe failed.", CHN);
50 				if (mailhead[SUBJ_TAG].found)
51 					fprintf(pf,
52 					    "%s", mailhead[SUBJ_TAG].line);
53 				else
54 					fprintf(pf,
55 					    "Subject: Untitled Bug Report\n");
56 				if (!mailhead[TO_TAG].line) {
57 					if (mailhead[APPAR_TO_TAG].line)
58 					    fprintf(pf, "To%s",
59 					      index(mailhead[APPAR_TO_TAG].line,
60 					      ':'));
61 					else
62 					    fprintf(pf, "To: %s\n",  BUGS_ID);
63 				}
64 				fputs("Resent-To: ", pf);
65 			}
66 			/*
67 			 * write out first entry, then succeeding entries
68 			 * backward compatible, handles back slashes at end
69 			 * of line
70 			 */
71 			if (group++)
72 				fputs(", ", pf);
73 			for (;;) {
74 				if (C2 = index(C1, '\\'))
75 					*C2 = EOS;
76 				fputs(C1, pf);
77 				if (!fgets(bfr, sizeof(bfr), stdin))
78 					break;
79 				if (C1 = index(bfr, '\n'))
80 					*C1 = '\0';
81 				if (*bfr != ' ' && *bfr != '\t')
82 					goto nextline;
83 				for (C1 = bfr;
84 				    *C1 && (*C1 == ' ' || *C1 == '\t'); ++C1);
85 			}
86 		}
87 	}
88 	if (!pf)
89 		return;
90 
91 	putc('\n', pf);
92 
93 	rewind(dfp);
94 	/* add Reference header and copy bug report out */
95 	while (fgets(bfr, sizeof(bfr), dfp) && *bfr != '\n')
96 		fputs(bfr, pf);
97 	fprintf(pf, "\n%sReference: %s\n\n", mailhead[INDX_TAG].line, pfile);
98 	while (fgets(bfr, sizeof(bfr), dfp))
99 		fputs(bfr, pf);
100 	(void)pclose(pf);
101 }
102