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

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

unix.superglobalmegacorp.com

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