home *** CD-ROM | disk | FTP | other *** search
/ Garbo / Garbo.cdr / pc / source / boozshar / oozext.c < prev    next >
C/C++ Source or Header  |  1989-05-29  |  8KB  |  294 lines

  1. /* oozext.c -- extracts files from archive */
  2.  
  3. /* The contents of this file are hereby released to the public domain.
  4.  
  5.                                    -- Rahul Dhesi 1987/02/08
  6.  
  7. The function fixfname() is currently defined to be empty but may be
  8. activated if needed to fix filename syntax to be legal for your
  9. computer system.  Look near the end of this file for instructions and
  10. a sample implementation of fixfname().  Currently, fixfname() is used
  11. only if the symbol FIXFNAME is defined.
  12. */
  13.  
  14. #include "options.h"
  15. #include "zoo.h"
  16. #include "oozio.h"
  17. #include "func.h"
  18.  
  19. extern unsigned int crccode;
  20.  
  21. #ifndef TINY
  22. int needed ();
  23. #endif
  24.  
  25. #ifdef TINY
  26. int oozext (zoo_path)
  27. char *zoo_path;
  28. #endif
  29.  
  30. #ifdef SMALL
  31. int oozext (zoo_path, argc, argv)
  32. char *zoo_path;
  33. int argc;
  34. char *argv[];
  35. #endif
  36.  
  37. #ifdef BIG
  38. int oozext (zoo_path, option, argc, argv)
  39. char *zoo_path;
  40. char *option;
  41. int argc;
  42. char *argv[];
  43. #endif
  44.  
  45. {
  46. int zoo_han;                              /* handle for open archive */
  47. int this_han;                             /* handle of file to extract */
  48. long next_ptr;                            /* pointer to within archive */
  49. struct zoo_header zoo_header;             /* header for archive */
  50. int status;                               /* error status */
  51. struct direntry direntry;                 /* directory entry */
  52. int all = 0;                              /* overwrite all? */
  53. static char vermsg[] = "A higher version of Ooz is needed to extract ";
  54. int exitstat = 0;                         /* return code */
  55.  
  56. #ifdef BIG
  57. int first_time = 1;
  58. static char *month_list="000JanFebMarAprMayJunJulAugSepOctNovDec";
  59. #endif
  60.  
  61. #ifndef TINY
  62. {
  63.    /* If no dot in name, add ".zoo" extension */
  64.    char *p;
  65.    p = zoo_path;
  66.    while (*p != '\0' && *p != '.')
  67.       p++;
  68.    if (*p != '.') {  /* no dot found */
  69.       p = malloc (strlen (zoo_path) + 5);
  70.       if (p == (char *) 0)
  71.          memerr();
  72.       strcpy (p, zoo_path);
  73.       strcat (p, ".zoo");
  74.       zoo_path = p;
  75.    }
  76. }
  77. #endif
  78.  
  79. zoo_han = OPEN(zoo_path);
  80.  
  81. if (zoo_han == -1)
  82.    prterror ('f', "Could not open ", zoo_path, "\n");
  83.  
  84. /* read header */
  85. rd_zooh (&zoo_header, zoo_han);
  86. lseek (zoo_han, zoo_header.zoo_start, 0); /* seek to where data begins */
  87.  
  88. while (1) {
  89.    rd_dir (&direntry, zoo_han);
  90.  
  91.    if (direntry.lo_tag != LO_TAG || direntry.hi_tag != HI_TAG)
  92.          prterror ('f', "Bad entry in archive\n", 
  93.             ((char *) 0), ((char *) 0));
  94.    if (direntry.next == 0L) {                /* END OF CHAIN */
  95.       break;                                 /* EXIT on end of chain */
  96.    }
  97.    next_ptr = direntry.next;                 /* ptr to next dir entry */
  98.  
  99. #ifndef TINY
  100.       /* See if this file is wanted */
  101.       if (!needed (direntry.fname, argc, argv)) {
  102.          goto loop_again;
  103.       }
  104. #endif
  105.  
  106. #ifdef BIG
  107.   /* If list needed, give information and loop again */
  108.    if (*option == 'l') {
  109.       char outstr[80];
  110.       char buf[20];
  111.       int year, month, day, hours, min, sec;
  112.       int size_factor;
  113.       size_factor = cfactor (direntry.org_size, direntry.size_now);
  114.    
  115.       year  =  ((unsigned int) direntry.date >> 9) & 0x7f;
  116.       month =  ((unsigned int) direntry.date >> 5) & 0x0f;
  117.       day   =  direntry.date        & 0x1f;
  118.       
  119.       hours =  ((unsigned int) direntry.time >> 11)& 0x1f;
  120.       min   =  ((unsigned int) direntry.time >> 5) & 0x3f;
  121.       sec   =  ((unsigned int) direntry.time & 0x1f) * 2;
  122.  
  123.       if (first_time) {
  124.          putstr ("Length    CF  Size Now  Date      Time\n");
  125.          putstr ("--------  --- --------  --------- --------\n");
  126.          first_time = 0;
  127.       }
  128.       strcpy (outstr, itoa(' ', direntry.org_size, buf, 9));
  129.       strcat (outstr, itoa(' ',(long) size_factor, buf, 5));
  130.       strcat (outstr, "% ");
  131.       strcat (outstr, itoa(' ',direntry.size_now, buf, 9));
  132.       strcat (outstr, "  ");
  133.       strcat (outstr, itoa(' ',(long) day, buf, 3));
  134.       strcat (outstr, " ");
  135.       strncat (outstr, &month_list[month*3], 3);
  136.       strcat (outstr, " ");
  137.       if (day && month)
  138.          strcat (outstr, itoa(' ',(long) (year+80) % 100, buf, 3));
  139.       else
  140.          strcat (outstr, itoa(' ',0L, buf, 3));
  141.       strcat (outstr, " ");
  142.       strcat (outstr, itoa('0',(long) hours, buf, 3));
  143.       strcat (outstr, ":");
  144.       strcat (outstr, itoa('0',(long) min, buf, 3));
  145.       strcat (outstr, ":");
  146.       strcat (outstr, itoa('0',(long) sec, buf, 3));
  147.       strcat (outstr, "  ");
  148.       strcat (outstr, direntry.fname);
  149.       strcat (outstr, "\n");
  150.       putstr (outstr);
  151.  
  152. #ifdef COMMENT
  153.       printf ("%8lu %3u%% %8lu  %2d %-.3s %02d %02d:%02d:%02d  ",  
  154.                direntry.org_size, 
  155.                size_factor, direntry.size_now, 
  156.                day, &month_list[month*3], 
  157.                (day && month) ?  (year+80) % 100 : 0,
  158.                hours, min, sec);
  159.       printf ("%s\n", direntry.fname);
  160. #endif
  161.  
  162.      goto loop_again;
  163.    }
  164. #endif /* SMALL */
  165.  
  166.  
  167.    if (direntry.major_ver > 1 ||
  168.          (direntry.major_ver == 1 && direntry.minor_ver > 0)) {
  169.       prterror ('e', vermsg, direntry.fname, "\n");
  170.       goto loop_again;
  171.    }
  172.  
  173. #ifdef FIXFNAME
  174.    /* Make the filename syntax acceptable to the host system */
  175.    fixfname (direntry.fname);
  176. #endif
  177.  
  178.  
  179. #ifndef TINY
  180.    /* See if this file already exists */
  181.  
  182. #ifdef BIG
  183.    if (*option != 't' && !all)
  184. #else
  185.    if (!all)
  186. #endif
  187.    {
  188.  
  189.       this_han = OPEN(direntry.fname);
  190.       if (this_han != -1) {
  191.          char ans[2];
  192.          char ans2[2];
  193.          close (this_han);
  194.    
  195.          do {
  196.             prterror ('m', "Overwrite ", direntry.fname, " (Yes/No/All)? ");
  197.             read (0, ans, 1);
  198.             do {
  199.                read (0, ans2, 1);
  200.             } while (*ans2 != '\n');
  201.          }  while (*ans != 'y' && *ans != 'Y' && 
  202.                    *ans != 'n' && *ans != 'N' &&
  203.                    *ans != 'a' && *ans != 'A');    
  204.    
  205.          if (*ans == 'a' || *ans == 'A')
  206.             all++;
  207.          if (*ans == 'n' || *ans == 'N') {
  208.             prterror ('m', "Skipping ", direntry.fname, "\n");
  209.             goto loop_again;
  210.          }
  211.       }
  212.    }
  213. #endif /* ndef TINY */
  214.  
  215.  
  216. #ifdef BIG
  217.    if (*option == 't')
  218.       this_han = -2;
  219.    else
  220. #endif
  221.       this_han = CREATE(direntry.fname);
  222.  
  223.    if (this_han == -1) {
  224.       prterror ('e', "Could not open ", direntry.fname, " for output.\n");
  225.    } else {
  226.       lseek (zoo_han, direntry.offset, 0);
  227.       crccode = 0;      /* Initialize CRC before extraction */
  228.       prterror ('m', direntry.fname, " ", ((char *) 0));
  229.  
  230.       if (direntry.packing_method == 0)
  231.          status = getfile (zoo_han, this_han, direntry.size_now);
  232.       else if (direntry.packing_method == 1)
  233.          status = lzd (zoo_han, this_han); /* uncompress */
  234.       else
  235.          prterror ('e', vermsg, direntry.fname, "\n");
  236.       if (status != 0) {
  237.          unlink(direntry.fname);
  238.          if (status == 1) {
  239.             memerr();
  240.          } else 
  241.             prterror ('e', "I/O error writing ", direntry.fname, "\n");
  242.       } else {
  243.          if (direntry.file_crc != crccode) {
  244.             putstr ("<--\007WARNING:  Bad CRC.\n");
  245.             exitstat = 1;
  246.          } else
  247.             putstr ("\n");
  248.       } /* end if */
  249.    } /* end if */
  250.  
  251. #ifdef BIG
  252.    if (*option != 't')
  253. #endif
  254.       close (this_han);
  255.  
  256. loop_again:
  257.    lseek (zoo_han, next_ptr, 0); /* ..seek to next dir entry */
  258. } /* end while */
  259. close (zoo_han);
  260. exit (exitstat);
  261. } /* end oozext */
  262.  
  263. /*
  264. Function fixfname() fixes the syntax of the supplied filename so it is
  265. acceptable to the host system.  We call the standard string function 
  266. strchr(s, c) which searches a string s for a character c and returns
  267. a pointer to it or returns NULL if not found.  The scheme below
  268. strips the 8th bit and changes control characters to corresponding 
  269. printable characters (e.g.  ^A becomes A).
  270.  
  271. As supplied, fixfname() is activated only if the symbol FIXFNAME
  272. is defined.
  273. */
  274.  
  275.  
  276. #ifdef FIXFNAME
  277. /* set of all characters legal for our system */
  278. char legal[]="ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz.";
  279.  
  280. char *strchr ();
  281.  
  282. int fixfname (fname)
  283. char *fname;
  284. {
  285.    char *p;
  286.    for (p = fname;  *p != '\0';  p++) {
  287.       *p &= 0x7f;
  288.       if (strchr (legal, *p) == (char *) 0) {
  289.          *p = legal[(*p) % 26];
  290.       }
  291.    }
  292. }
  293. #endif /* FIXFNAME */
  294.