home *** CD-ROM | disk | FTP | other *** search
/ Mega Demo / MegaDemoCDRom1.cdr / os2 / archive / booz.exe / OOZEXT.C < prev    next >
C/C++ Source or Header  |  1991-07-11  |  8KB  |  260 lines

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