1 /*
2  * Copyright (C) 1997 and 1998 WIDE Project.  All rights reserved.
3  *
4  * Redistribution and use in source and binary forms, with or without
5  * modification, are permitted provided that the following conditions
6  * are met:
7  * 1. Redistributions of source code must retain the above copyright
8  *    notice, this list of conditions and the following disclaimer.
9  * 2. Redistributions in binary form must reproduce the above copyright
10  *    notice, this list of conditions and the following disclaimer in the
11  *    documentation and/or other materials provided with the distribution.
12  * 3. Neither the name of the project nor the names of its contributors
13  *    may be used to endorse or promote products derived from this software
14  *    without specific prior written permission.
15  *
16  * THIS SOFTWARE IS PROVIDED BY THE PROJECT AND CONTRIBUTORS ``AS IS'' AND
17  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
19  * ARE DISCLAIMED.  IN NO EVENT SHALL THE PROJECT OR CONTRIBUTORS BE LIABLE
20  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
21  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
22  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
23  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
24  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
25  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
26  * SUCH DAMAGE.
27  */
28 /*
29  * $Id: embed.c,v 1.7 1999/01/05 03:37:31 itojun Exp $
30  */
31 
32 #include "mgp.h"
33 #include <dirent.h>
34 
35 char *mgpwdir = DEFAULT_MGPWDIR;
36 char mgpwdirname[BUFSIZ] = "";
37 
38 char *
allocpy(p)39 allocpy(p)
40 	char *p;
41 {
42 	char *q;
43 
44 	q = (char *)malloc(strlen(p) + 1);
45 	if (q == NULL) {
46 		fprintf(stderr, "malloc: %s\n", strerror(errno));
47 		cleanup(-1);
48 	}
49 	return strcpy(q, p);
50 }
51 
52 char *
embed_fname(fname)53 embed_fname(fname)
54 	char *fname;
55 {
56 	char buf[BUFSIZ];
57 
58 	if (strncmp(fname, EMBEDDIR, strlen(EMBEDDIR)) != 0)
59 		return fname;
60 	fname += strlen(EMBEDDIR);
61 	if (*mgpwdirname == '\0') {	/* not initialized yet */
62 		sprintf(mgpwdirname, "%s/mgp.%d", mgpwdir, getpid());
63 		if (mkdir(mgpwdirname, 0700) < 0) {
64 			fprintf(stderr, "%s: %s\n", mgpwdirname,
65 				strerror(errno));
66 			cleanup(-1);
67 		}
68 	}
69 	sprintf(buf, "%s/%s", mgpwdirname, fname);
70 	return allocpy(buf);
71 }
72 
73 void
embed_file(fp,p,lineno)74 embed_file(fp, p, lineno)
75 	FILE *fp;	/* stream to get data */
76 	struct ctrl *p;
77 	int *lineno;
78 {
79 	char buf[BUFSIZ];
80 	FILE *pp;
81 	struct stat st;
82 	int len;
83 
84 	if (stat(mgpwdirname, &st) < 0) {
85 		fprintf(stderr, "no EMBED directory found\n");
86 		cleanup(-1);
87 	}
88 
89 #ifdef UUDECODEOPT
90 # define UUDECODE_ALL	UUDECODE " " UUDECODEOPT
91 #else
92 #define UUDECODE_ALL	UUDECODE
93 #endif
94 	if ((pp = popen(UUDECODE_ALL, "w")) == NULL) {
95 		fprintf(stderr, "popen: %s\n", strerror(errno));
96 		cleanup(-1);
97 	}
98 	sprintf(buf, "%s/%s", mgpwdirname, p->ctc_value);
99 	if (access(buf, F_OK) == 0) {
100 		fprintf(stderr, "embedded filename duplicated: %s\n",
101 			p->ctc_value);
102 		cleanup(-1);
103 	}
104 	fprintf(pp, "begin 600 %s/%s\n", mgpwdirname, p->ctc_value);
105 	while (fgets(buf, sizeof(buf), fp)) {
106 		(*lineno)++;
107 		if (strncasecmp(buf, "%endembed", 9) == 0)
108 			break;
109 		fputs(buf, pp);
110 	}
111 	fprintf(pp, "end\n");
112 	pclose(pp);
113 	len = strlen(p->ctc_value);
114 	if (len > 3 && strncmp(p->ctc_value + len - 3, ".gz", 3) == 0) {
115 		sprintf(buf, "%s %s/%s", GUNZIP, mgpwdirname, p->ctc_value);
116 		system(buf);
117 	}
118 	return;
119 }
120 
121 void
cleandir()122 cleandir()		/* called by signal and quitting the mgp */
123 {
124 	DIR *dp;
125 	struct dirent *dep;
126 	char fname[BUFSIZ];
127 
128 	if (*mgpwdirname == '\0')
129 		return;
130 	if ((dp = opendir(mgpwdirname)) == NULL) {
131 		fprintf(stderr, "%s: %s\n", mgpwdirname, strerror(errno));
132 		return;
133 	}
134 	while ((dep = readdir(dp))) {
135 		if (strcmp(dep->d_name, ".") == 0 ||
136 		    strcmp(dep->d_name, "..") == 0)
137 			continue;
138 		sprintf(fname, "%s/%s", mgpwdirname, dep->d_name);
139 		if (unlink(fname)) {
140 			fprintf(stderr, "unlink of %s: %s", fname,
141 				strerror(errno));
142 		}
143 	}
144 	closedir(dp);
145 	rmdir(mgpwdirname);
146 }
147