|
|
1.1 root 1: /*
2: * UAE - The Un*x Amiga Emulator
3: *
4: * routines to handle compressed file automatically
5: *
1.1.1.2 root 6: * (c) 1996 Samuel Devulder, Tim Gunn
1.1 root 7: */
8:
9: #include "sysconfig.h"
10: #include "sysdeps.h"
11:
12: #include "config.h"
13: #include "options.h"
14: #include "zfile.h"
15:
16: #ifdef USE_ZFILE
17:
1.1.1.2 root 18: #ifdef AMIGA
19: extern char *amiga_dev_path; /* dev: */
20: extern char *ixemul_dev_path; /* /dev/ */
1.1.1.3 root 21: extern int readdevice (const char *, char *);
1.1.1.2 root 22: #endif
23:
1.1.1.4 root 24: struct zfile
1.1 root 25: {
1.1.1.3 root 26: struct zfile *next;
27: FILE *f;
28: char name[L_tmpnam];
1.1.1.4 root 29: };
30:
31: static struct zfile *zlist = 0;
1.1 root 32:
33: /*
34: * called on exit()
35: */
1.1.1.4 root 36: void zfile_exit (void)
1.1 root 37: {
1.1.1.3 root 38: struct zfile *l;
39:
40: while ((l = zlist)) {
41: zlist = l->next;
1.1.1.4 root 42: fclose (l->f);
43: unlink (l->name); /* sam: in case unlink() after fopen() fails */
44: free (l);
1.1 root 45: }
46: }
47:
48: /*
49: * fclose() but for a compressed file
50: */
1.1.1.4 root 51: int zfile_close (FILE *f)
1.1 root 52: {
1.1.1.3 root 53: struct zfile *pl = NULL;
54: struct zfile *l = zlist;
55: int ret;
56:
1.1.1.4 root 57: while (l && l->f!=f) {
1.1.1.3 root 58: pl = l;
59: l = l->next;
60: }
1.1.1.4 root 61: if (l == 0)
62: return fclose (f);
63: ret = fclose (l->f);
64: unlink (l->name);
65:
1.1.1.3 root 66: if(!pl)
67: zlist = l->next;
68: else
69: pl->next = l->next;
1.1.1.4 root 70: free (l);
1.1 root 71:
1.1.1.3 root 72: return ret;
1.1 root 73: }
74:
75: /*
76: * gzip decompression
77: */
1.1.1.3 root 78: static int gunzip (const char *decompress, const char *src, const char *dst)
1.1 root 79: {
1.1.1.3 root 80: char cmd[1024];
1.1.1.7 ! root 81: char *ext = strrchr (src, '.');
! 82: if (!dst)
! 83: return 1;
! 84: #if defined(AMIGA)
! 85: sprintf (cmd, "%s -c -d -S %s \"%s\" > \"%s\"", decompress, ext, src, dst);
! 86: #else
! 87: sprintf (cmd, "%s -c -d \"%s\" > \"%s\"", decompress, src, dst);
! 88: #endif
! 89: return !system (cmd);
! 90: }
! 91:
! 92: /*
! 93: * bzip/bzip2 decompression
! 94: */
! 95: static int bunzip (const char *decompress, const char *src, const char *dst)
! 96: {
! 97: char cmd[1024];
1.1.1.3 root 98: if (!dst)
99: return 1;
1.1.1.6 root 100: sprintf (cmd, "%s -c -d \"%s\" > \"%s\"", decompress, src, dst);
1.1.1.3 root 101: return !system (cmd);
1.1 root 102: }
103:
104: /*
105: * lha decompression
106: */
1.1.1.3 root 107: static int lha (const char *src, const char *dst)
1.1 root 108: {
1.1.1.3 root 109: char cmd[1024];
110: if (!dst)
111: return 1;
1.1.1.2 root 112: #if defined(AMIGA)
1.1.1.3 root 113: sprintf (cmd, "lha -q -N p %s >%s", src, dst);
1.1.1.2 root 114: #else
1.1.1.3 root 115: sprintf (cmd, "lha pq %s >%s", src, dst);
1.1.1.2 root 116: #endif
1.1.1.3 root 117: return !system (cmd);
1.1.1.2 root 118: }
119:
120: /*
121: * (pk)unzip decompression
122: */
1.1.1.4 root 123: static int unzip (const char *src, const char *dst)
1.1.1.2 root 124: {
1.1.1.3 root 125: char cmd[1024];
126: if (!dst)
127: return 1;
128: #if defined AMIGA || defined __unix
129: sprintf (cmd, "unzip -p %s '*.adf' >%s", src, dst);
130: return !system (cmd);
1.1.1.2 root 131: #endif
1.1 root 132: }
133:
134: /*
135: * decompresses the file (or check if dest is null)
136: */
1.1.1.4 root 137: static int uncompress (const char *name, char *dest)
1.1 root 138: {
1.1.1.3 root 139: char *ext = strrchr (name, '.');
1.1 root 140: char nam[1024];
141:
1.1.1.3 root 142: if (ext != NULL && access (name, 0) >= 0) {
1.1 root 143: ext++;
1.1.1.3 root 144: if (strcasecmp (ext, "z") == 0
145: || strcasecmp (ext, "gz") == 0
146: || strcasecmp (ext, "adz") == 0
147: || strcasecmp (ext, "roz") == 0)
148: return gunzip ("gzip", name, dest);
149: if (strcasecmp (ext, "bz") == 0)
1.1.1.7 ! root 150: return bunzip ("bzip", name, dest);
1.1.1.3 root 151: if (strcasecmp (ext, "bz2") == 0)
1.1.1.7 ! root 152: return bunzip ("bzip2", name, dest);
1.1.1.3 root 153:
1.1.1.2 root 154: #ifndef __DOS__
1.1.1.3 root 155: if (strcasecmp (ext, "lha") == 0
156: || strcasecmp (ext, "lzh") == 0)
157: return lha (name, dest);
158: if (strcasecmp (ext, "zip") == 0)
159: return unzip (name, dest);
1.1.1.2 root 160: #endif
1.1 root 161: }
162:
1.1.1.3 root 163: if (access (strcat (strcpy (nam, name), ".z"), 0) >= 0
164: || access (strcat (strcpy (nam, name), ".Z"), 0) >= 0
165: || access (strcat (strcpy (nam, name), ".gz"), 0) >= 0
166: || access (strcat (strcpy (nam, name), ".GZ"), 0) >= 0
167: || access (strcat (strcpy (nam, name), ".adz"), 0) >= 0
168: || access (strcat (strcpy (nam, name), ".roz"), 0) >= 0)
169: return gunzip ("gzip", nam, dest);
170:
171: if (access (strcat (strcpy (nam, name), ".bz"), 0) >= 0
172: || access (strcat (strcpy (nam, name), ".BZ"), 0) >= 0)
1.1.1.7 ! root 173: return bunzip ("bzip", nam, dest);
1.1.1.3 root 174:
175: if (access (strcat (strcpy (nam, name), ".bz2"), 0) >= 0
176: || access (strcat (strcpy (nam, name), ".BZ2"), 0) >= 0)
1.1.1.7 ! root 177: return bunzip ("bzip2", nam, dest);
1.1 root 178:
1.1.1.2 root 179: #ifndef __DOS__
1.1.1.3 root 180: if (access (strcat (strcpy (nam, name), ".lha"), 0) >= 0
181: || access (strcat (strcpy (nam, name), ".LHA"), 0) >= 0
182: || access (strcat (strcpy (nam, name), ".lzh"), 0) >= 0
183: || access (strcat (strcpy (nam, name), ".LZH"), 0) >= 0)
184: return lha (nam, dest);
185:
186: if (access (strcat (strcpy (nam, name),".zip"),0) >= 0
187: || access (strcat (strcpy (nam, name),".ZIP"),0) >= 0)
188: return unzip (nam, dest);
1.1.1.2 root 189: #endif
190:
191: #if defined(AMIGA)
1.1.1.3 root 192: /* sam: must be before real access to work */
193: if (!strnicmp (name, ixemul_dev_path, strlen (ixemul_dev_path)))
194: return readdevice (name + strlen (ixemul_dev_path), dest);
195: if (!strnicmp (name, amiga_dev_path, strlen (amiga_dev_path)))
196: return readdevice (name + strlen (amiga_dev_path), dest);
1.1.1.2 root 197: #endif
198:
1.1 root 199: return 0;
200: }
201:
202: /*
203: * fopen() for a compressed file
204: */
1.1.1.4 root 205: FILE *zfile_open (const char *name, const char *mode)
1.1 root 206: {
207: struct zfile *l;
1.1.1.2 root 208: int fd = 0;
1.1.1.3 root 209:
1.1.1.4 root 210: if (! uncompress (name, NULL))
1.1.1.3 root 211: return fopen (name, mode);
212:
213: l = malloc (sizeof *l);
1.1.1.4 root 214: if (! l)
1.1.1.3 root 215: return NULL;
1.1 root 216:
1.1.1.4 root 217: tmpnam (l->name);
1.1.1.3 root 218:
1.1.1.4 root 219: fd = creat (l->name, S_IRUSR | S_IWUSR);
1.1 root 220: if (fd < 0)
221: return NULL;
1.1.1.3 root 222:
1.1.1.4 root 223: if (! uncompress (name, l->name)) {
1.1.1.3 root 224: close (fd);
1.1.1.4 root 225: unlink (l->name);
1.1.1.5 root 226: free (l);
1.1.1.3 root 227: return NULL;
1.1 root 228: }
229:
1.1.1.4 root 230: l->f = fopen (l->name, mode);
1.1 root 231:
232: close (fd);
1.1.1.3 root 233:
1.1 root 234: if (l->f == NULL) {
1.1.1.4 root 235: unlink (l->name);
236: free (l);
1.1 root 237: return NULL;
238: }
239:
240: l->next = zlist;
1.1.1.4 root 241: zlist = l;
1.1 root 242:
243: return l->f;
244: }
245:
246: #else
247:
248: /*
249: * Stubs for machines that this doesn't work on.
250: */
251:
1.1.1.4 root 252: void zfile_exit (void)
1.1 root 253: {
254: }
255:
1.1.1.4 root 256: int zfile_close (FILE *f)
1.1 root 257: {
258: return fclose(f);
259: }
260:
1.1.1.4 root 261: FILE *zfile_open (const char *name, const char *mode)
1.1 root 262: {
1.1.1.4 root 263: return fopen (name, mode);
1.1 root 264: }
265:
266: #endif
This archive runs on limited infrastructure. Preserving old code on modern bandwidth. Automated agents are requested to crawl responsibly.