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

  1. #define  VERSION  "Version 1.02 (1988/08/25)\n"
  2.  
  3. /* booz.c -- small, memory-efficient Barebones Ooz to extract Zoo archives.
  4.  
  5. For maximum portability, Booz does not use the data type `unsigned long'.
  6.  
  7. The functions and system calls required by Tiny Booz are:
  8.    read()
  9.    write()
  10.    strlen()
  11.    exit()
  12.    malloc()
  13.    lseek()
  14.    open() and/or creat()
  15.    close()
  16.    unlink()                -- may be defined to be an empty function
  17.  
  18.  
  19. Small Booz additionally uses the following functions:
  20.    strcat()
  21.    strcpy()
  22.  
  23. Big Booz additionally use the following function:
  24.    strncat()
  25. */
  26.  
  27. /* 
  28. The contents of this file are hereby released to the public domain.
  29.  
  30.                                    -- Rahul Dhesi 1988/08/25
  31. */
  32.  
  33. #include "options.h"
  34. #include "func.h"
  35. #include "zoo.h"
  36.  
  37. main(argc,argv)
  38. register int argc;
  39. register char **argv;
  40. {
  41. #ifdef TINY
  42.    static char usage[]=
  43.       "Usage:  booz archive.zoo\n";
  44.    if (argc < 2) {
  45.       putstr ("Public domain Barebones Ooz\nZoo archive extractor (Tiny) by Rahul Dhesi\n");
  46.       putstr (VERSION);
  47.       putstr (usage);
  48.       exit (1);
  49.    }
  50. #endif
  51.  
  52. #ifdef SMALL
  53.    static char usage[]=
  54.       "Usage:  booz archive[.zoo] [ file ... ]\n";
  55.    if (argc < 2) {
  56.       putstr ("Public domain Barebones Ooz\nZoo archive extractor (Small) by Rahul Dhesi\n");
  57.       putstr (VERSION);
  58.       putstr (usage);
  59.       exit (1);
  60.    }
  61. #endif
  62.  
  63. #ifdef BIG
  64.    static char usage[]=
  65.       "Usage:  booz {lxt} archive[.zoo] [ file ... ]\n";
  66.    if (argc < 3) {
  67.       putstr ("Public domain Barebones Ooz\nZoo archive extractor/lister (Big) by Rahul Dhesi\n");
  68.       putstr (VERSION);
  69.       putstr (usage);
  70.       putstr ("l = list, x = extract, t = test\n");
  71.       exit (1);
  72.    }
  73. #endif
  74.  
  75. #ifdef TINY
  76.    oozext (argv[1]);
  77. #endif
  78.  
  79. #ifdef SMALL
  80.    oozext (argv[1], argc - 2, &argv[2]);
  81. #endif
  82.  
  83. #ifdef BIG
  84.    {
  85.       char *p;
  86.       p = argv[1];
  87.       if (*p == 'L')
  88.          *p = 'l';
  89.       if (*p == 'X')
  90.          *p = 'x';
  91.       if (*p == 'T')
  92.          *p = 't';
  93.       if (*p != 'l' && *p != 'x' && *p != 't') {
  94.          putstr (usage);
  95.          exit (1);
  96.       }
  97.       oozext (argv[2], p, argc - 3, &argv[3]);
  98.    }
  99. #endif
  100.  
  101.    exit (0);
  102. }
  103.  
  104. /**********************/
  105. /* putstr()
  106. This function prints a string to the standard output handle without
  107. using printf() or the standard I/O library.  If a null string, nothing 
  108. is printed (not even the null character).
  109. */
  110. int putstr (str)
  111. register char *str;
  112. {
  113.    if (str != (char *) 0)
  114.       write (1, str, strlen(str));
  115. }
  116.  
  117. /**********************/
  118. /* prterror()
  119. Prints an error message.  The first character controls the severity
  120. of the error and the result.
  121.  
  122.    'm'   informative message
  123.    'w'   warning     -- execution continues
  124.    'e'   error       -- execution continues
  125.    'f'   fatal error -- program exits
  126. */
  127.  
  128. int prterror (level, a, b, c)
  129. int level;
  130. char *a, *b, *c;
  131.  
  132. {
  133.  
  134. #ifdef DEBUG
  135.    {
  136.       char tmp[2];
  137.       tmp[0] = level & 0x7f;
  138.       tmp[1] = '\0';
  139.       putstr ("prterror:  level = ");
  140.       putstr (tmp);
  141.       putstr ("\n");
  142.    }
  143. #endif
  144.  
  145.    switch (level & 0x7f) {
  146.       case 'm': break;
  147.       case 'w': putstr ("WARNING:  "); break;
  148.       case 'e': putstr ("ERROR:  "); break;
  149.       case 'f': putstr ("FATAL:  "); break;
  150.       default: prterror ('f', "Internal error\n", ((char *) 0), ((char *) 0));
  151.    }
  152.    putstr (a);
  153.    putstr (b);
  154.    putstr (c);
  155.  
  156.    if (level == 'f')       /* and abort on fatal error 'f' but not 'F' */
  157.       exit (1);
  158. }
  159.  
  160. /*************
  161. This function copies count characters from the source file to the
  162. destination Function return value is 0 if no error, 2 if write error,
  163. and 3 if read error.  
  164.  
  165. The global variable crccode is updated.
  166. */
  167. extern char out_buf_adr[];
  168.  
  169. int getfile(input_han, output_han, count)
  170. int input_han, output_han;
  171. long count;
  172. {
  173.    register int how_much;
  174.  
  175.    while (count > 0) {
  176.       if (count > OUT_BUF_SIZE)
  177.          how_much = OUT_BUF_SIZE;
  178.       else
  179.          how_much = count;
  180.       count -= how_much;
  181.       if (read (input_han, out_buf_adr, how_much) != how_much)
  182.          return (3);
  183.       addbfcrc (out_buf_adr, how_much);
  184.       if (output_han != -2 &&
  185.             write (output_han, out_buf_adr, how_much) != how_much)
  186.          return (2);
  187.    }
  188.    return (0);
  189. }
  190.  
  191. #ifndef TINY
  192.  
  193. int needed (fname, argc, argv)
  194. char *fname;
  195. int argc;
  196. char *argv[];
  197. {
  198.    register int i;
  199.    if (argc == 0)
  200.       return (1);
  201.    for (i = 0; i < argc; i++) {
  202.       if (match (fname, argv[i]))
  203.          return (1);
  204.    }
  205.    return (0);
  206. }
  207.  
  208. /***********************/
  209. /*
  210. match() compares a pattern with a string.  Wildcards accepted in
  211. the pattern are:  "*" for zero or more arbitrary characters;  "?"
  212. for any one characters.  Unlike the MS-DOS wildcard match, "*" is
  213. correctly handled even if it isn't at the end of the pattern. ".'
  214. is not special.
  215.  
  216. Originally written by Jeff Damens of Columbia University Center for
  217. Computing Activities.  Taken from the source code for C-Kermit version
  218. 4C.
  219. */
  220.  
  221. int match (string, pattern) 
  222. register char *string, *pattern;
  223. {
  224.    char *psave,*ssave;        /* back up pointers for failure */
  225.    psave = ssave = ((char *) 0);
  226.    while (1) {
  227.       for (; *pattern == *string; pattern++,string++)  /* skip first */
  228.          if (*string == '\0') 
  229.             return(1);                          /* end of strings, succeed */
  230.       if (*string != '\0' && *pattern == '?') {
  231.          pattern++;                             /* '?', let it match */
  232.          string++;
  233.       } else if (*pattern == '*') {             /* '*' ... */
  234.          psave = ++pattern;                     /* remember where we saw it */
  235.          ssave = string;                        /* let it match 0 chars */
  236.       } else if (ssave != ((char *) 0) && *ssave != '\0') {   /* if not at end  */
  237.          /* ...have seen a star */
  238.          string = ++ssave;                      /* skip 1 char from string */
  239.          pattern = psave;                       /* and back up pattern */
  240.       } else 
  241.          return(0);                             /* otherwise just fail */
  242.    }
  243. }
  244.  
  245. #endif /* ndef TINY */
  246.  
  247. int memerr()
  248. {
  249.    prterror ('f', "Ran out of memory\n");
  250. }
  251.  
  252. #ifdef BIG
  253. /* cfactor() calculates the compression factor given a directory entry */
  254. int cfactor (org_size, size_now)
  255. long org_size, size_now;
  256. {
  257.    register int size_factor;
  258.  
  259.    while (org_size > 10000) { /* avoid overflow below */
  260.       org_size = org_size >> 4;
  261.       size_now = size_now >> 4;
  262.    }
  263.    if (org_size == 0)         /* avoid division by zero */
  264.       size_factor = 0;
  265.    else {
  266.       size_factor = 
  267.          (
  268.             (1000 * 
  269.                (org_size - size_now)
  270.             ) / org_size + 5
  271.          ) / 10;
  272.    }
  273.    return (size_factor);
  274. }
  275.  
  276. /******
  277. Function itoa() converts a positive long integer into a text string of
  278. digits.  The buffer pointer buf must point to a buffer to receive the
  279. digit string.  The digit string is stored right justified in the
  280. buffer with leading blanks.  If the supplied number is negative, or if
  281. overflow occurs, a single '*' is returned.
  282. */
  283.  
  284. char *itoa (pad_ch, n, buf, buflen)
  285. char pad_ch;                  /* leading pad character */
  286. long n;                       /* positive long int to convert */
  287. char *buf;                    /* buffer to receive digit string */
  288. int buflen;                   /* length of buffer */
  289. {
  290.    char *p;
  291.    int i;
  292.    for (i = 0;  i < buflen;  i++)         /* fill buffer with pad char */
  293.       buf[i] = pad_ch;
  294.    p = buf + buflen - 1;
  295.    *p-- = '\0';                           /* ensure null termination */
  296.    i = buflen - 1;
  297.    for (;;) {
  298.       if (n < 0) {                        /* can't handle negative n */
  299.          goto overflow;
  300.       } else {
  301.          *p-- = (int) (n % 10) + '0';     /* store a converted digit */
  302.          n = n / 10;
  303.          i--;
  304.          if (n == 0 || i == 0)
  305.             break;
  306.       } /* end of else of if (n < 0) */
  307.    } /* end while (buflen > 0) */
  308.    if (n != 0)                            /* buffer overflow */
  309.       goto overflow;
  310.    return (buf);
  311.  
  312. overflow:                                 /* bad value filled with stars */
  313.    for (i = 0; i < buflen; i++)
  314.       buf[i] = '*';
  315.    buf[buflen-1] = '\0';
  316.    return (buf);
  317. }
  318.  
  319. #endif /* BIG */
  320.