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