home *** CD-ROM | disk | FTP | other *** search
/ The Fred Fish Collection 1.5 / ffcollection-1-5-1992-11.iso / ff_progs / fileutil / scan.lha / src / lhext.c < prev    next >
C/C++ Source or Header  |  1992-05-21  |  4KB  |  121 lines

  1. /*----------------------------------------------------------------------*/
  2. /*              LHarc Extract Command                                   */
  3. /*                                                                      */
  4. /*              Copyright(C) MCMLXXXIX  Yooichi.Tagawa                  */
  5. /*                                                                      */
  6. /*  V0.00  Original                             1988.05.23  Y.Tagawa    */
  7. /*----------------------------------------------------------------------*/
  8.  
  9. #include "lharc.h"
  10.  
  11. extern int decode_lzhuf(), decode_larc(), decode_lh5();
  12. extern int decode_stored_crc (), decode_stored_nocrc ();
  13. extern int NotInterruptedCall;
  14. extern int ForceReturn;
  15. extern char *WildLZH;
  16. extern char *LZHFileName;
  17.  
  18. static boolean
  19. make_parent_path (name)
  20.      char *name;
  21. {
  22.   char path[FILENAME_LENGTH];
  23.   struct stat stbuf;
  24.   register char *p;
  25.  
  26.   /* make parent directory name into PATH for recursive call */
  27.   strcpy (path, name);
  28.   for (p = path + strlen (path); p > path; p --)
  29.     if (p[-1] == '/')
  30.       {
  31.         *--p = '\0';
  32.         break;
  33.       }
  34.  
  35.   if (p == path)
  36.     {
  37.       message ("Why?", "ROOT");
  38.       return FALSE;             /* no more parent. */
  39.     }
  40.  
  41.   if (stat (path, &stbuf) >= 0)
  42.     {
  43.       return TRUE;
  44.     }
  45.   errno = 0;
  46.  
  47.   if (mkdir (path) == 0)  /* try */
  48.     return TRUE;                /* successful done. */
  49.   errno = 0;
  50.  
  51.   if (!make_parent_path (path))
  52.     return FALSE;
  53.  
  54.   if (mkdir (path) != 0)   /* try again */
  55.     {
  56.       message ("Cannot make directory", path);
  57.       return FALSE;
  58.     }
  59.  
  60.   return TRUE;
  61. }
  62.  
  63. static int (*analyze_method (hdr))()
  64.      LzHeader *hdr;
  65. {
  66.   int (*decode_proc)();
  67.  
  68.   if (bcmp (hdr->method, LZHUFF5_METHOD, METHOD_TYPE_STRAGE) == 0)
  69.     decode_proc = decode_lh5;
  70.   else if (bcmp (hdr->method, LZHUFF1_METHOD, METHOD_TYPE_STRAGE) == 0)
  71.     decode_proc = decode_lzhuf;
  72.   else if ((bcmp (hdr->method, LZHUFF0_METHOD, METHOD_TYPE_STRAGE) == 0) ||
  73.            (bcmp (hdr->method, LARC4_METHOD, METHOD_TYPE_STRAGE) == 0))
  74.     decode_proc = (hdr->has_crc) ? decode_stored_crc : decode_stored_nocrc;
  75.   else if (bcmp (hdr->method, LARC5_METHOD, METHOD_TYPE_STRAGE) == 0)
  76.     decode_proc = decode_larc;
  77.   else
  78.     decode_proc = (int (*)())0;
  79.   return decode_proc;
  80. }
  81.  
  82. void
  83. extract_one (afp, hdr)
  84.      FILE *afp;                 /* archive file */
  85.      LzHeader *hdr;
  86. {
  87.   static char *name;
  88.   static int crc;
  89.   static int (*decode_proc)();         /* (ifp,original_size,name) */
  90.  
  91.   if( NotInterruptedCall ) {
  92.      name = hdr->name;
  93.      if ((hdr->unix_mode & UNIX_FILE_TYPEMASK) == UNIX_FILE_REGULAR) {
  94.         decode_proc = analyze_method (hdr);
  95.         if (!decode_proc) {
  96.            error ("Sorry, cannot extract this method, skipped.", name);
  97.            return;
  98.         }
  99.         writting_filename = name;
  100.         LZHFileName = name;
  101.         errno = 0;
  102.         if( !newwildcmp( WildLZH, name ) ) return;
  103.         crc = (*decode_proc) (afp, hdr->original_size, name);
  104.         if( ForceReturn ) return;
  105.         errno = 0;
  106.         if (hdr->has_crc && crc != hdr->crc) error ("CRC error", name);
  107.      }
  108.      else if ((hdr->unix_mode & UNIX_FILE_TYPEMASK) == UNIX_FILE_DIRECTORY) {
  109.         /* NAME has trailing SLASH '/', (^_^) */
  110.         if (!make_parent_path (name)) return;
  111.      }
  112.      else { error ("Unknown information", name); }
  113.   }
  114.   else {
  115.      crc = (*decode_proc) (afp, hdr->original_size, name);
  116.      if( ForceReturn ) return;
  117.      errno = 0;
  118.      if (hdr->has_crc && crc != hdr->crc) error ("CRC error", name);
  119.   }
  120. }
  121.