xref: /csrg-svn/libexec/bugfiler/gethead.c (revision 30890)
130162Sbostic /*
230162Sbostic  * Copyright (c) 1986 Regents of the University of California.
330162Sbostic  * All rights reserved.  The Berkeley software License Agreement
430162Sbostic  * specifies the terms and conditions for redistribution.
530162Sbostic  */
630162Sbostic 
730162Sbostic #ifndef lint
8*30890Sbostic static char sccsid[] = "@(#)gethead.c	5.2 (Berkeley) 87/04/11";
930162Sbostic #endif not lint
1030162Sbostic 
1130162Sbostic #include <bug.h>
1230162Sbostic #include <sys/stat.h>
1330162Sbostic #include <stdio.h>
1430162Sbostic 
1530162Sbostic static int	chk1();
1630162Sbostic 
1730162Sbostic #define ENT(X)	sizeof(X) - 1, X
1830162Sbostic HEADER	mailhead[] = {				/* mail headers */
1930162Sbostic 	{ NO, YES,  NULL, ENT("Date:"), },
2030162Sbostic 	{ NO,  NO,  NULL, ENT("From "), },
2130162Sbostic 	{ NO, YES,  NULL, ENT("From:"), },
2230162Sbostic 	{ NO,  NO,  chk1, ENT("Index:"), },
2330162Sbostic 	{ NO, YES,  NULL, ENT("Message-Id:"), },
2430162Sbostic 	{ NO,  NO,  NULL, ENT("Reply-To:"), },
2530162Sbostic 	{ NO,  NO,  NULL, ENT("Return-Path:"), },
2630162Sbostic 	{ NO,  NO,  NULL, ENT("Subject:"), },
2730162Sbostic 	{ NO,  NO,  NULL, ENT("To:"), },
2830162Sbostic 	{ ERR, }
2930162Sbostic };
3030162Sbostic 
31*30890Sbostic FILE	*dfp;				/* distf file pointer */
32*30890Sbostic char	dir[MAXNAMLEN],			/* subject and folder */
3330162Sbostic 	folder[MAXNAMLEN];
3430162Sbostic 
3530162Sbostic /*
3630162Sbostic  * gethead --
3730162Sbostic  *	read mail and bug headers from bug report, construct redist headers
3830162Sbostic  */
39*30890Sbostic gethead(redist)
40*30890Sbostic 	int	redist;
4130162Sbostic {
4230162Sbostic 	register HEADER	*hp;		/* mail header pointer */
43*30890Sbostic 	char	*strcpy(), *malloc();
4430162Sbostic 
45*30890Sbostic 	if (redist) {
46*30890Sbostic 		int	fd;
47*30890Sbostic 		char	*distf;
4830162Sbostic 
49*30890Sbostic 		distf = "/tmp/BUG_XXXXXX";
50*30890Sbostic 		if (!(fd = mkstemp(distf)) || !(dfp = fdopen(fd, "w+")))
51*30890Sbostic 			error("can't create redistribution file %s.", distf);
52*30890Sbostic 		/* disappear after last reference is closed */
53*30890Sbostic 		(void)unlink(distf);
54*30890Sbostic 	}
55*30890Sbostic 	if (!freopen(tmpname, "r", stdin))
56*30890Sbostic 		error("can't read temporary bug file %s.", tmpname);
57*30890Sbostic 
58*30890Sbostic 	while (fgets(bfr, sizeof(bfr), stdin)) {
59*30890Sbostic 		for (hp = mailhead; hp->found != ERR; ++hp)
6030162Sbostic 			if (!hp->found)
61*30890Sbostic 				if (!strncmp(hp->tag, bfr, hp->len)) {
6230162Sbostic 					if (hp->valid && !((*(hp->valid))(bfr)))
6330162Sbostic 						break;
6430162Sbostic 					if (!(hp->line = malloc((u_int)(strlen(bfr) + 1))))
65*30890Sbostic 						error("malloc failed.", CHN);
66*30890Sbostic 					(void)strcpy(hp->line, bfr);
6730162Sbostic 					hp->found = YES;
6830162Sbostic 					break;
6930162Sbostic 				}
70*30890Sbostic 		if ((hp->found == ERR || hp->redist) && redist)
71*30890Sbostic 			fputs(bfr, dfp);
7230162Sbostic 	}
7330162Sbostic 
7430162Sbostic 	if (!mailhead[INDX_TAG].found)
75*30890Sbostic 		error("no readable \"Index:\" header in bug report.", CHN);
7630162Sbostic }
7730162Sbostic 
7830162Sbostic /*
7930162Sbostic  * chk1 --
8030162Sbostic  *	parse the "Index:" line into folder and directory
8130162Sbostic  */
8230162Sbostic static
8330162Sbostic chk1(line)
84*30890Sbostic 	char	*line;
8530162Sbostic {
8630162Sbostic 	register char	*C;		/* tmp pointer */
8730162Sbostic 	struct stat	sbuf;		/* existence check */
8830162Sbostic 	char	*index();
8930162Sbostic 
90*30890Sbostic 	if (sscanf(line, " Index: %s %s ", folder, dir) != 2)
9130162Sbostic 		return(NO);
92*30890Sbostic 	if (C = index(folder, '/')) {	/* deal with "bin/from.c" */
9330162Sbostic 		if (C == folder)
9430162Sbostic 			return(NO);
9530162Sbostic 		*C = EOS;
9630162Sbostic 	}
97*30890Sbostic 	if (stat(dir, &sbuf) || (sbuf.st_mode & S_IFMT) != S_IFDIR)
9830162Sbostic 		return(NO);
9930162Sbostic 	return(YES);
10030162Sbostic }
101