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