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

1.1       root        1:  /*
                      2:   * UAE - The Un*x Amiga Emulator
                      3:   *
                      4:   * routines to handle compressed file automatically
                      5:   *
                      6:   * (c) 1996 Samuel Devulder
                      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: 
                     18: static struct zfile
                     19: {
                     20:   struct zfile *next;
                     21:   FILE *f;
                     22:   char name[L_tmpnam];
                     23: } *zlist;
                     24: 
                     25: /*
                     26:  * called on exit()
                     27:  */
                     28: void zfile_exit(void)
                     29: {
                     30:   struct zfile *l;
                     31: 
                     32:   while((l = zlist))
                     33:     {
                     34:       zlist = l->next;
                     35:       fclose(l->f);
                     36:       free(l);
                     37:     }
                     38: }
                     39: 
                     40: /*
                     41:  * fclose() but for a compressed file
                     42:  */
                     43: int zfile_close(FILE *f)
                     44: {
                     45:   struct zfile *pl = NULL,
                     46:                *l  = zlist;
                     47:   int ret;
                     48: 
                     49:   while(l && l->f!=f) {pl = l;l=l->next;}
                     50:   if(!l) return fclose(f);
                     51:   ret = fclose(l->f);
                     52: 
                     53:   if(!pl) zlist = l->next;
                     54:   else pl->next = l->next;
                     55:   free(l);
                     56: 
                     57:   return ret;
                     58: }
                     59: 
                     60: /*
                     61:  * gzip decompression
                     62:  */
                     63: static int gunzip(char *src,char *dst)
                     64: {
                     65:   char cmd[1024];
                     66:   if(!dst) return 1;
                     67:   sprintf(cmd,"gzip -d -c %s >%s",src,dst);
                     68:   return !system(cmd);
                     69: }
                     70: 
                     71: /*
                     72:  * lha decompression
                     73:  */
                     74: static int lha(char *src,char *dst)
                     75: {
                     76:   char cmd[1024];
                     77:   if(!dst) return 1;
                     78:   sprintf(cmd,"lha -q -N p %s >%s",src,dst);
                     79:   return !system(cmd);
                     80: }
                     81: 
                     82: /*
                     83:  * decompresses the file (or check if dest is null)
                     84:  */
                     85: static int uncompress(char *name,char *dest)
                     86: {
                     87:     char *ext = strrchr(name, '.');
                     88:     char nam[1024];
                     89: 
                     90:     if (ext != NULL && access(name,0) >= 0) {
                     91:        ext++;
                     92:        if(!strcmp(ext,"z")  ||
                     93:           !strcmp(ext,"Z")  ||
                     94:           !strcmp(ext,"gz") ||
                     95:           !strcmp(ext,"GZ") ||
                     96:           0) return gunzip(name,dest);
                     97:        if(!strcmp(ext,"lha") ||
                     98:           !strcmp(ext,"LHA") ||
                     99:           !strcmp(ext,"lzh") ||
                    100:           !strcmp(ext,"LZH") ||
                    101:           0) return lha(name,dest);
                    102:     }
                    103: 
                    104:     if(access(strcat(strcpy(nam,name),".z"),0)>=0  ||
                    105:        access(strcat(strcpy(nam,name),".Z"),0)>=0  ||
                    106:        access(strcat(strcpy(nam,name),".gz"),0)>=0 ||
                    107:        access(strcat(strcpy(nam,name),".GZ"),0)>=0 ||
                    108:        0) return gunzip(nam,dest);
                    109: 
                    110:     if(access(strcat(strcpy(nam,name),".lha"),0)>=0 ||
                    111:        access(strcat(strcpy(nam,name),".LHA"),0)>=0 ||
                    112:        access(strcat(strcpy(nam,name),".lzh"),0)>=0 ||
                    113:        access(strcat(strcpy(nam,name),".LZH"),0)>=0 ||
                    114:        0) return lha(nam,dest);
                    115: 
                    116:     return 0;
                    117: }
                    118: 
                    119: /*
                    120:  * fopen() for a compressed file
                    121:  */
                    122: FILE *zfile_open(char *name,char *mode)
                    123: {
                    124:     struct zfile *l;
                    125:     int fd;
                    126:     
                    127:     if(!uncompress(name,NULL)) return fopen(name,mode);
                    128:     
                    129:     if(!(l = malloc(sizeof(*l)))) return NULL;
                    130: 
                    131:     tmpnam(l->name);
                    132:     fd = creat(l->name, 0666);
                    133:     if (fd < 0)
                    134:        return NULL;
                    135:     
                    136:     if(!uncompress(name,l->name)) 
                    137:     {
                    138:        free(l); close (fd); unlink(l->name); return NULL; 
                    139:     }
                    140: 
                    141:     l->f=fopen(l->name,mode);
                    142:     
                    143:     /* Deleting the file now will cause the space for it to be freed
                    144:      * as soon as we fclose() it. This saves bookkeeping work.
                    145:      */
                    146:     unlink (l->name);
                    147: 
                    148:     close (fd);
                    149:     
                    150:     if (l->f == NULL) {
                    151:        free(l);
                    152:        return NULL;
                    153:     }
                    154: 
                    155:     l->next = zlist;
                    156:     zlist   = l;
                    157: 
                    158:     return l->f;
                    159: }
                    160: 
                    161: #else
                    162: 
                    163: /*
                    164:  * Stubs for machines that this doesn't work on.
                    165:  */
                    166: 
                    167: void zfile_exit(void)
                    168: {
                    169: }
                    170: 
                    171: int zfile_close(FILE *f)
                    172: {
                    173:     return fclose(f);
                    174: }
                    175: 
                    176: FILE *zfile_open(char *name,char *mode)
                    177: {
                    178:     return fopen(name, mode);
                    179: }
                    180: 
                    181: #endif

unix.superglobalmegacorp.com

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