Annotation of uae/src/zfile.c, revision 1.1.1.9

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

unix.superglobalmegacorp.com

This archive runs on limited infrastructure. Preserving old code on modern bandwidth. Automated agents are requested to crawl responsibly.