File: embed.c

package info (click to toggle)
mgp 1.13a+upstream20090219-11
  • links: PTS
  • area: main
  • in suites: bullseye, buster, sid
  • size: 4,128 kB
  • sloc: ansic: 32,387; sh: 4,295; lisp: 1,405; yacc: 945; lex: 264; makefile: 158; perl: 117; awk: 9
file content (146 lines) | stat: -rw-r--r-- 4,061 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
/*
 * Copyright (C) 1997 and 1998 WIDE Project.  All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions
 * are met:
 * 1. Redistributions of source code must retain the above copyright
 *    notice, this list of conditions and the following disclaimer.
 * 2. Redistributions in binary form must reproduce the above copyright
 *    notice, this list of conditions and the following disclaimer in the
 *    documentation and/or other materials provided with the distribution.
 * 3. Neither the name of the project nor the names of its contributors
 *    may be used to endorse or promote products derived from this software
 *    without specific prior written permission.
 *
 * THIS SOFTWARE IS PROVIDED BY THE PROJECT AND CONTRIBUTORS ``AS IS'' AND
 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
 * ARE DISCLAIMED.  IN NO EVENT SHALL THE PROJECT OR CONTRIBUTORS BE LIABLE
 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
 * SUCH DAMAGE.
 */
/*
 * $Id: embed.c,v 1.7 1999/01/05 03:37:31 itojun Exp $
 */

#include "mgp.h"
#include <dirent.h>

char *mgpwdir = DEFAULT_MGPWDIR;
char mgpwdirname[BUFSIZ] = "";

char *
allochk(char *q)
{
	if (q == NULL) {
		fprintf(stderr, "malloc: %s\n", strerror(errno));
		cleanup(-1);
	}
	return (q);
}

char *
embed_fname(fname)
	char *fname;
{
	char *bf = NULL;

	if (strncmp(fname, EMBEDDIR, strlen(EMBEDDIR)) != 0)
		return fname;
	fname += strlen(EMBEDDIR);
	if (*mgpwdirname == '\0') {	/* not initialized yet */
		snprintf(mgpwdirname, sizeof(mgpwdirname), "%s/mgp.%d", mgpwdir, getpid());
		if (mkdir(mgpwdirname, 0700) < 0) {
			fprintf(stderr, "%s: %s\n", mgpwdirname,
				strerror(errno));
			cleanup(-1);
		}
	}
	asprintf(&bf, "%s/%s", mgpwdirname, fname);
	return allochk(bf);
}

void
embed_file(fp, p, lineno)
	FILE *fp;	/* stream to get data */
	struct ctrl *p;
	int *lineno;
{
	char *bf = NULL;
	char buf[BUFSIZ];
	FILE *pp;
	struct stat st;
	int len;

	if (stat(mgpwdirname, &st) < 0) {
		fprintf(stderr, "no EMBED directory found\n");
		cleanup(-1);
	}

#ifdef UUDECODEOPT
# define UUDECODE_ALL	UUDECODE " " UUDECODEOPT
#else
#define UUDECODE_ALL	UUDECODE
#endif
	if ((pp = popen(UUDECODE_ALL, "w")) == NULL) {
		fprintf(stderr, "popen: %s\n", strerror(errno));
		cleanup(-1);
	}
	asprintf(&bf, "%s/%s", mgpwdirname, p->ctc_value);
	if (access(bf, F_OK) == 0) {
		fprintf(stderr, "embedded filename duplicated: %s\n",
			p->ctc_value);
		cleanup(-1);
	}
	free(bf);
	fprintf(pp, "begin 600 %s/%s\n", mgpwdirname, p->ctc_value);
	while (fgets(buf, sizeof(buf), fp)) {
		(*lineno)++;
		if (strncasecmp(buf, "%endembed", 9) == 0)
			break;
		fputs(buf, pp);
	}
	fprintf(pp, "end\n");
	pclose(pp);
	len = strlen(p->ctc_value);
	if (len > 3 && strncmp(p->ctc_value + len - 3, ".gz", 3) == 0) {
		asprintf(&bf, "%s %s/%s", GUNZIP, mgpwdirname, p->ctc_value);
		system(bf);
		free(bf);
	}
	return;
}

void
cleandir()		/* called by signal and quitting the mgp */ 
{
	DIR *dp;
	struct dirent *dep;
	char *fname = NULL;

	if (*mgpwdirname == '\0')
		return;
	if ((dp = opendir(mgpwdirname)) == NULL) {
		fprintf(stderr, "%s: %s\n", mgpwdirname, strerror(errno));
		return;
	}
	while ((dep = readdir(dp))) {
		if (strcmp(dep->d_name, ".") == 0 ||
		    strcmp(dep->d_name, "..") == 0)
			continue;
		asprintf(&fname, "%s/%s", mgpwdirname, dep->d_name);
		if (unlink(fname)) {
			fprintf(stderr, "unlink of %s: %s", fname,
				strerror(errno));
		}
		free(fname);
	}
	closedir(dp);
	rmdir(mgpwdirname);
}