home *** CD-ROM | disk | FTP | other *** search
/ Amiga ISO Collection / AmigaUtilCD1.iso / Emulatoren / UAE061.LZH / uae-0.6.1 / zfile.c < prev   
Encoding:
C/C++ Source or Header  |  1996-08-28  |  3.5 KB  |  198 lines

  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. #if defined(AMIGA)
  79.   sprintf(cmd,"lha -q -N p %s >%s",src,dst);
  80. #else
  81.   sprintf(cmd,"lha pq %s >%s",src,dst);
  82. #endif
  83.   return !system(cmd);
  84. }
  85.  
  86. /*
  87.  * (pk)unzip decompression
  88.  */
  89. static int unzip(char *src,char *dst)
  90. {
  91.   char cmd[1024];
  92.   if(!dst) return 1;
  93. #if defined(AMIGA)
  94.   sprintf(cmd,"unzip -p %s '*.adf' >%s",src,dst);
  95.   return !system(cmd);
  96. #else
  97.   return gunzip(src,dst): /* I don't know for unix */
  98. #endif
  99. }
  100.  
  101. /*
  102.  * decompresses the file (or check if dest is null)
  103.  */
  104. static int uncompress(char *name,char *dest)
  105. {
  106.     char *ext = strrchr(name, '.');
  107.     char nam[1024];
  108.  
  109.     if (ext != NULL && access(name,0) >= 0) {
  110.     ext++;
  111.     if(!strcmp(ext,"z")  ||
  112.        !strcmp(ext,"Z")  ||
  113.        !strcmp(ext,"gz") ||
  114.        !strcmp(ext,"GZ") ||
  115.        0) return gunzip(name,dest);
  116.     if(!strcmp(ext,"lha") ||
  117.        !strcmp(ext,"LHA") ||
  118.        !strcmp(ext,"lzh") ||
  119.        !strcmp(ext,"LZH") ||
  120.        0) return lha(name,dest);
  121.     if(!strcmp(ext,"zip") ||
  122.        !strcmp(ext,"ZIP") ||
  123.        0) return unzip(name,dest);
  124.     }
  125.  
  126.     if(access(strcat(strcpy(nam,name),".z"),0)>=0  ||
  127.        access(strcat(strcpy(nam,name),".Z"),0)>=0  ||
  128.        access(strcat(strcpy(nam,name),".gz"),0)>=0 ||
  129.        access(strcat(strcpy(nam,name),".GZ"),0)>=0 ||
  130.        0) return gunzip(nam,dest);
  131.  
  132.     if(access(strcat(strcpy(nam,name),".lha"),0)>=0 ||
  133.        access(strcat(strcpy(nam,name),".LHA"),0)>=0 ||
  134.        access(strcat(strcpy(nam,name),".lzh"),0)>=0 ||
  135.        access(strcat(strcpy(nam,name),".LZH"),0)>=0 ||
  136.        0) return lha(nam,dest);
  137.  
  138.     if(access(strcat(strcpy(nam,name),".zip"),0)>=0 ||
  139.        access(strcat(strcpy(nam,name),".ZIP"),0)>=0 ||
  140.        0) return unzip(nam,dest);
  141.  
  142.     return 0;
  143. }
  144.  
  145. /*
  146.  * fopen() for a compressed file
  147.  */
  148. FILE *zfile_open(char *name,char *mode)
  149. {
  150.     struct zfile *l;
  151.   
  152.     if(!uncompress(name,NULL)) return fopen(name,mode);
  153.     
  154.     if(!(l = malloc(sizeof(*l)))) return NULL;
  155.  
  156.     tmpnam(l->name);
  157.     if(!uncompress(name,l->name)) {free(l);return NULL;}
  158.  
  159.     l->f=fopen(l->name,mode);
  160.     
  161.     /* Deleting the file now will cause the space for it to be freed
  162.      * as soon as we fclose() it. This saves bookkeeping work.
  163.      */
  164.     unlink (l->name);
  165.     
  166.     if (l->f == NULL) {
  167.     free(l);
  168.     return NULL;
  169.     }
  170.  
  171.     l->next = zlist;
  172.     zlist   = l;
  173.  
  174.     return l->f;
  175. }
  176.  
  177. #else
  178.  
  179. /*
  180.  * Stubs for machines that this doesn't work on.
  181.  */
  182.  
  183. void zfile_exit(void)
  184. {
  185. }
  186.  
  187. int zfile_close(FILE *f)
  188. {
  189.     return fclose(f);
  190. }
  191.  
  192. FILE *zfile_open(char *name,char *mode)
  193. {
  194.     return fopen(name, mode);
  195. }
  196.  
  197. #endif
  198.