File: zfile.c

package info (click to toggle)
uae 0.7.6-4
  • links: PTS
  • area: contrib
  • in suites: potato
  • size: 3,352 kB
  • ctags: 6,537
  • sloc: ansic: 54,019; asm: 1,981; cpp: 713; objc: 549; makefile: 234; perl: 202; sh: 21
file content (247 lines) | stat: -rw-r--r-- 5,060 bytes parent folder | download | duplicates (2)
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
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
 /*
  * UAE - The Un*x Amiga Emulator
  *
  * routines to handle compressed file automatically
  *
  * (c) 1996 Samuel Devulder, Tim Gunn
  */

#include "sysconfig.h"
#include "sysdeps.h"

#include "config.h"
#include "options.h"
#include "zfile.h"

#ifdef USE_ZFILE

#ifdef AMIGA
extern char *amiga_dev_path;   /* dev: */
extern char *ixemul_dev_path;  /* /dev/ */
extern int readdevice (const char *, char *);
#endif

static struct zfile
{
    struct zfile *next;
    FILE *f;
    char name[L_tmpnam];
} *zlist;

/*
 * called on exit()
 */
void zfile_exit(void)
{
    struct zfile *l;
    
    while ((l = zlist)) {
	zlist = l->next;
	fclose(l->f);
	unlink(l->name); /* sam: in case unlink() after fopen() fails */
	free(l);
    }
}

/*
 * fclose() but for a compressed file
 */
int zfile_close(FILE *f)
{
    struct zfile *pl = NULL;
    struct zfile *l  = zlist;
    int ret;

    while(l && l->f!=f) {
	pl = l;
	l = l->next;
    }
    if (!l)
	return fclose(f);
    ret = fclose(l->f);
    
    if(!pl)
	zlist = l->next;
    else
	pl->next = l->next;
    free(l);

    return ret;
}

/*
 * gzip decompression
 */
static int gunzip (const char *decompress, const char *src, const char *dst)
{
    char cmd[1024];
    if (!dst) 
	return 1;
    sprintf (cmd, "%s -c -d %s >%s", decompress, src, dst);
    return !system (cmd);
}

/*
 * lha decompression
 */
static int lha (const char *src, const char *dst)
{
    char cmd[1024];
    if (!dst)
	return 1;
#if defined(AMIGA)
    sprintf (cmd, "lha -q -N p %s >%s", src, dst);
#else
    sprintf (cmd, "lha pq %s >%s", src, dst);
#endif
    return !system (cmd);
}

/*
 * (pk)unzip decompression
 */
static int unzip(const char *src, const char *dst)
{
    char cmd[1024];
    if (!dst)
	return 1;
#if defined AMIGA || defined __unix
    sprintf (cmd, "unzip -p %s '*.adf' >%s", src, dst);
    return !system (cmd);
#endif
}

/*
 * decompresses the file (or check if dest is null)
 */
static int uncompress(const char *name, char *dest)
{
    char *ext = strrchr (name, '.');
    char nam[1024];

    if (ext != NULL && access (name, 0) >= 0) {
	ext++;
	if (strcasecmp (ext, "z") == 0
	    || strcasecmp (ext, "gz") == 0
	    || strcasecmp (ext, "adz") == 0
	    || strcasecmp (ext, "roz") == 0)
	    return gunzip ("gzip", name, dest);
	if (strcasecmp (ext, "bz") == 0)
	    return gunzip ("bzip", name, dest);
	if (strcasecmp (ext, "bz2") == 0)
	    return gunzip ("bzip2", name, dest);

#ifndef __DOS__
	if (strcasecmp (ext, "lha") == 0
	    || strcasecmp (ext, "lzh") == 0)
	    return lha (name, dest);
	if (strcasecmp (ext, "zip") == 0)
	     return unzip (name, dest);
#endif
    }

    if (access (strcat (strcpy (nam, name), ".z"), 0) >= 0
	|| access (strcat (strcpy (nam, name), ".Z"), 0) >= 0
	|| access (strcat (strcpy (nam, name), ".gz"), 0) >= 0
	|| access (strcat (strcpy (nam, name), ".GZ"), 0) >= 0
	|| access (strcat (strcpy (nam, name), ".adz"), 0) >= 0
	|| access (strcat (strcpy (nam, name), ".roz"), 0) >= 0)
	return gunzip ("gzip", nam, dest);

    if (access (strcat (strcpy (nam, name), ".bz"), 0) >= 0
	|| access (strcat (strcpy (nam, name), ".BZ"), 0) >= 0)
	return gunzip ("bzip", nam, dest);

    if (access (strcat (strcpy (nam, name), ".bz2"), 0) >= 0
	|| access (strcat (strcpy (nam, name), ".BZ2"), 0) >= 0)
	return gunzip ("bzip2", nam, dest);

#ifndef __DOS__
    if (access (strcat (strcpy (nam, name), ".lha"), 0) >= 0
	|| access (strcat (strcpy (nam, name), ".LHA"), 0) >= 0
	|| access (strcat (strcpy (nam, name), ".lzh"), 0) >= 0
	|| access (strcat (strcpy (nam, name), ".LZH"), 0) >= 0)
	return lha (nam, dest);

    if (access (strcat (strcpy (nam, name),".zip"),0) >= 0
	|| access (strcat (strcpy (nam, name),".ZIP"),0) >= 0)
       return unzip (nam, dest);
#endif

#if defined(AMIGA)
    /* sam: must be before real access to work */
    if (!strnicmp (name, ixemul_dev_path, strlen (ixemul_dev_path)))
	return readdevice (name + strlen (ixemul_dev_path), dest);
    if (!strnicmp (name, amiga_dev_path, strlen (amiga_dev_path)))
	return readdevice (name + strlen (amiga_dev_path), dest);
#endif

    return 0;
}

/*
 * fopen() for a compressed file
 */
FILE *zfile_open(const char *name, const char *mode)
{
    struct zfile *l;
    int fd = 0;

    if(! uncompress (name, NULL))
	return fopen (name, mode);

    l = malloc (sizeof *l);
    if (!l)
	return NULL;

    tmpnam(l->name);

    /* On the amiga this would make ixemul loose the break handler */
    /* ==> fixed in exmul v4.6 */
    fd = creat(l->name, 0666);
    if (fd < 0)
	return NULL;

    if (!uncompress (name, l->name)) {
	free(l);
	close (fd);
	unlink(l->name);
	return NULL;
    }

    l->f = uae_fopen_del (l->name, mode);

    close (fd);

    if (l->f == NULL) {
	free(l);
	return NULL;
    }

    l->next = zlist;
    zlist   = l;

    return l->f;
}

#else

/*
 * Stubs for machines that this doesn't work on.
 */

void zfile_exit(void)
{
}

int zfile_close(FILE *f)
{
    return fclose(f);
}

FILE *zfile_open(const char *name, const char *mode)
{
    return fopen(name, mode);
}

#endif