home *** CD-ROM | disk | FTP | other *** search
/ ftp.barnyard.co.uk / 2015.02.ftp.barnyard.co.uk.tar / ftp.barnyard.co.uk / cpm / walnut-creek-CDROM / JSAGE / ZSUS / LBR / BOOZ4CPM.ARK / OOZEXT.C < prev    next >
Text File  |  1988-09-05  |  9KB  |  296 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.    /* If no dot in name, add ".zoo" extension */
  63.    char *p;
  64.     char foldname[13];        /* holds fname -> upper case (BP) */
  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. #endif
  77.  
  78. zoo_han = OPEN(zoo_path);
  79.  
  80. if (zoo_han == -1)
  81.    prterror ('f', "Could not open ", zoo_path, "\r\n");
  82.  
  83. /* read header */
  84. rd_zooh (&zoo_header, zoo_han);
  85. lseek (zoo_han, zoo_header.zoo_start, 0); /* seek to where data begins */
  86.  
  87. while (1) {
  88.    rd_dir (&direntry, zoo_han);
  89.  
  90.    if (direntry.lo_tag != LO_TAG || direntry.hi_tag != HI_TAG)
  91.          prterror ('f', "Bad entry in archive\r\n", 
  92.             ((char *) 0), ((char *) 0));
  93.    if (direntry.next == 0L) {                /* END OF CHAIN */
  94.       break;                                 /* EXIT on end of chain */
  95.    }
  96.    next_ptr = direntry.next;                 /* ptr to next dir entry */
  97.  
  98. #ifndef TINY
  99.       /* See if this file is wanted */
  100.         strcpy(foldname,direntry.fname);    /* extract named file(s) (BP) */
  101.         for (p = foldname; *p != '\0'; p++)
  102.             *p = toupper(*p);
  103.       if (!needed (foldname, argc, argv)) {
  104.          goto loop_again;
  105.       }
  106. #endif
  107.  
  108. #ifdef BIG
  109.   /* If list needed, give information and loop again */
  110.    if (*option == 'l') {
  111.       char outstr[80];
  112.       char buf[20];
  113.       int year, month, day, hours, min, sec;
  114.       int size_factor;
  115.       size_factor = cfactor (direntry.org_size, direntry.size_now);
  116.    
  117.       year  =  ((unsigned int) direntry.date >> 9) & 0x7f;
  118.       month =  ((unsigned int) direntry.date >> 5) & 0x0f;
  119.       day   =  direntry.date        & 0x1f;
  120.       
  121.       hours =  ((unsigned int) direntry.time >> 11)& 0x1f;
  122.       min   =  ((unsigned int) direntry.time >> 5) & 0x3f;
  123.       sec   =  ((unsigned int) direntry.time & 0x1f) * 2;
  124.  
  125.       if (first_time) {
  126.          putstr ("Length    CF  Size Now  Date      Time\r\n");
  127.          putstr ("--------  --- --------  --------- --------\r\n");
  128.          first_time = 0;
  129.       }
  130.       strcpy (outstr, itoa(' ', direntry.org_size, buf, 9));
  131.       strcat (outstr, itoa(' ',(long) size_factor, buf, 5));
  132.       strcat (outstr, "% ");
  133.       strcat (outstr, itoa(' ',direntry.size_now, buf, 9));
  134.       strcat (outstr, "  ");
  135.       strcat (outstr, itoa(' ',(long) day, buf, 3));
  136.       strcat (outstr, " ");
  137.       strncat (outstr, &month_list[month*3], 3);
  138.       strcat (outstr, " ");
  139.       if (day && month)
  140.          strcat (outstr, itoa(' ',(long) (year+80) % 100, buf, 3));
  141.       else
  142.          strcat (outstr, itoa(' ',0L, buf, 3));
  143.       strcat (outstr, " ");
  144.       strcat (outstr, itoa('0',(long) hours, buf, 3));
  145.       strcat (outstr, ":");
  146.       strcat (outstr, itoa('0',(long) min, buf, 3));
  147.       strcat (outstr, ":");
  148.       strcat (outstr, itoa('0',(long) sec, buf, 3));
  149.       strcat (outstr, "  ");
  150.       strcat (outstr, direntry.fname);
  151.       strcat (outstr, "\r\n");
  152.       putstr (outstr);
  153.  
  154. #ifdef COMMENT
  155.       printf ("%8lu %3u%% %8lu  %2d %-.3s %02d %02d:%02d:%02d  ",  
  156.                direntry.org_size, 
  157.                size_factor, direntry.size_now, 
  158.                day, &month_list[month*3], 
  159.                (day && month) ?  (year+80) % 100 : 0,
  160.                hours, min, sec);
  161.       printf ("%s\n", direntry.fname);
  162. #endif
  163.  
  164.      goto loop_again;
  165.    }
  166. #endif /* SMALL */
  167.  
  168.  
  169.    if (direntry.major_ver > 1 ||
  170.          (direntry.major_ver == 1 && direntry.minor_ver > 0)) {
  171.       prterror ('e', vermsg, direntry.fname, "\r\n");
  172.       goto loop_again;
  173.    }
  174.  
  175. #ifdef FIXFNAME
  176.    /* Make the filename syntax acceptable to the host system */
  177.    fixfname (direntry.fname);
  178. #endif
  179.  
  180.  
  181. #ifndef TINY
  182.    /* See if this file already exists */
  183.  
  184. #ifdef BIG
  185.    if (*option != 't' && !all)
  186. #else
  187.    if (!all)
  188. #endif
  189.    {
  190.  
  191.       this_han = OPEN(direntry.fname);
  192.       if (this_han != -1) {
  193.          char ans[2];
  194.          char ans2[2];
  195.          close (this_han);
  196.    
  197.          do {
  198.             prterror ('m', "Overwrite ", direntry.fname, " (Yes/No/All)? ");
  199.             read (0, ans, 1);
  200.             do {
  201.                read (0, ans2, 1);
  202.             } while (*ans2 != '\n');
  203.          }  while (*ans != 'y' && *ans != 'Y' && 
  204.                    *ans != 'n' && *ans != 'N' &&
  205.                    *ans != 'a' && *ans != 'A');    
  206.    
  207.          if (*ans == 'a' || *ans == 'A')
  208.             all++;
  209.          if (*ans == 'n' || *ans == 'N') {
  210.             prterror ('m', "Skipping ", direntry.fname, "\r\n");
  211.             goto loop_again;
  212.          }
  213.       }
  214.    }
  215. #endif /* ndef TINY */
  216.  
  217.  
  218. #ifdef BIG
  219.    if (*option == 't')
  220.       this_han = -2;
  221.    else
  222. #endif
  223.       this_han = CREATE(direntry.fname);
  224.  
  225.    if (this_han == -1) {
  226.       prterror ('e', "Could not open ", direntry.fname, " for output.\r\n");
  227.    } else {
  228.       lseek (zoo_han, direntry.offset, 0);
  229.       crccode = 0;      /* Initialize CRC before extraction */
  230.       prterror ('m', direntry.fname, " ", ((char *) 0));
  231.  
  232.       if (direntry.packing_method == 0)
  233.          status = getfile (zoo_han, this_han, direntry.size_now);
  234.       else if (direntry.packing_method == 1)
  235.          status = lzd (zoo_han, this_han); /* uncompress */
  236.       else
  237.          prterror ('e', vermsg, direntry.fname, "\r\n");
  238.       if (status != 0) {
  239.          unlink(direntry.fname);
  240.          if (status == 1) {
  241.             memerr();
  242.          } else 
  243.             prterror ('e', "I/O error writing ", direntry.fname, "\r\n");
  244.       } else {
  245.          if (direntry.file_crc != crccode) {
  246.             putstr ("<--\007WARNING:  Bad CRC.\r\n");
  247.             exitstat = 1;
  248.          } else
  249.             putstr ("\r\n");
  250.       } /* end if */
  251.    } /* end if */
  252.  
  253. #ifdef BIG
  254.    if (*option != 't')
  255. #endif
  256.       close (this_han);
  257.  
  258. loop_again:
  259.    lseek (zoo_han, next_ptr, 0); /* ..seek to next dir entry */
  260. } /* end while */
  261. close (zoo_han);
  262. exit (exitstat);
  263. } /* end oozext */
  264.  
  265. /*
  266. Function fixfname() fixes the syntax of the supplied filename so it is
  267. acceptable to the host system.  We call the standard string function 
  268. strchr(s, c) which searches a string s for a character c and returns
  269. a pointer to it or returns NULL if not found.  The scheme below
  270. strips the 8th bit and changes control characters to corresponding 
  271. printable characters (e.g.  ^A becomes A).
  272.  
  273. As supplied, fixfname() is activated only if the symbol FIXFNAME
  274. is defined.
  275. */
  276.  
  277.  
  278. #ifdef FIXFNAME
  279. /* set of all characters legal for our system */
  280. char legal[]="ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz.";
  281.  
  282. char *strchr ();
  283.  
  284. int fixfname (fname)
  285. char *fname;
  286. {
  287.    char *p;
  288.    for (p = fname;  *p != '\0';  p++) {
  289.       *p &= 0x7f;
  290.       if (strchr (legal, *p) == (char *) 0) {
  291.          *p = legal[(*p) % 26];
  292.       }
  293.    }
  294. }
  295. #endif /* FIXFNAME */
  296.