home *** CD-ROM | disk | FTP | other *** search
/ Fresh Fish 5 / FreshFish_July-August1994.bin / bbs / gnu / gzip-1.2.4-src.lha / src / amiga / gzip-1.2.4 / util.c < prev    next >
C/C++ Source or Header  |  1993-08-13  |  14KB  |  463 lines

  1. /* util.c -- utility functions for gzip support
  2.  * Copyright (C) 1992-1993 Jean-loup Gailly
  3.  * This is free software; you can redistribute it and/or modify it under the
  4.  * terms of the GNU General Public License, see the file COPYING.
  5.  */
  6.  
  7. #ifdef RCSID
  8. static char rcsid[] = "$Id: util.c,v 0.15 1993/06/15 09:04:13 jloup Exp $";
  9. #endif
  10.  
  11. #include <ctype.h>
  12. #include <errno.h>
  13. #include <sys/types.h>
  14.  
  15. #include "tailor.h"
  16.  
  17. #ifdef HAVE_UNISTD_H
  18. #  include <unistd.h>
  19. #endif
  20. #ifndef NO_FCNTL_H
  21. #  include <fcntl.h>
  22. #endif
  23.  
  24. #if defined(STDC_HEADERS) || !defined(NO_STDLIB_H)
  25. #  include <stdlib.h>
  26. #else
  27.    extern int errno;
  28. #endif
  29.  
  30. #include "gzip.h"
  31. #include "crypt.h"
  32.  
  33. extern ulg crc_32_tab[];   /* crc table, defined below */
  34.  
  35. /* ===========================================================================
  36.  * Copy input to output unchanged: zcat == cat with --force.
  37.  * IN assertion: insize bytes have already been read in inbuf.
  38.  */
  39. int copy(in, out)
  40.     int in, out;   /* input and output file descriptors */
  41. {
  42.     errno = 0;
  43.     while (insize != 0 && (int)insize != EOF) {
  44.     write_buf(out, (char*)inbuf, insize);
  45.     bytes_out += insize;
  46.     insize = read(in, (char*)inbuf, INBUFSIZ);
  47.     }
  48.     if ((int)insize == EOF && errno != 0) {
  49.     read_error();
  50.     }
  51.     bytes_in = bytes_out;
  52.     return OK;
  53. }
  54.  
  55. /* ===========================================================================
  56.  * Run a set of bytes through the crc shift register.  If s is a NULL
  57.  * pointer, then initialize the crc shift register contents instead.
  58.  * Return the current crc in either case.
  59.  */
  60. ulg updcrc(s, n)
  61.     uch *s;                 /* pointer to bytes to pump through */
  62.     unsigned n;             /* number of bytes in s[] */
  63. {
  64.     register ulg c;         /* temporary variable */
  65.  
  66.     static ulg crc = (ulg)0xffffffffL; /* shift register contents */
  67.  
  68.     if (s == NULL) {
  69.     c = 0xffffffffL;
  70.     } else {
  71.     c = crc;
  72.         if (n) do {
  73.             c = crc_32_tab[((int)c ^ (*s++)) & 0xff] ^ (c >> 8);
  74.         } while (--n);
  75.     }
  76.     crc = c;
  77.     return c ^ 0xffffffffL;       /* (instead of ~c for 64-bit machines) */
  78. }
  79.  
  80. /* ===========================================================================
  81.  * Clear input and output buffers
  82.  */
  83. void clear_bufs()
  84. {
  85.     outcnt = 0;
  86.     insize = inptr = 0;
  87.     bytes_in = bytes_out = 0L;
  88. }
  89.  
  90. /* ===========================================================================
  91.  * Fill the input buffer. This is called only when the buffer is empty.
  92.  */
  93. int fill_inbuf(eof_ok)
  94.     int eof_ok;          /* set if EOF acceptable as a result */
  95. {
  96.     int len;
  97.  
  98.     /* Read as much as possible */
  99.     insize = 0;
  100.     errno = 0;
  101.     do {
  102.     len = read(ifd, (char*)inbuf+insize, INBUFSIZ-insize);
  103.         if (len == 0 || len == EOF) break;
  104.     insize += len;
  105.     } while (insize < INBUFSIZ);
  106.  
  107.     if (insize == 0) {
  108.     if (eof_ok) return EOF;
  109.     read_error();
  110.     }
  111.     bytes_in += (ulg)insize;
  112.     inptr = 1;
  113.     return inbuf[0];
  114. }
  115.  
  116. /* ===========================================================================
  117.  * Write the output buffer outbuf[0..outcnt-1] and update bytes_out.
  118.  * (used for the compressed data only)
  119.  */
  120. void flush_outbuf()
  121. {
  122.     if (outcnt == 0) return;
  123.  
  124.     write_buf(ofd, (char *)outbuf, outcnt);
  125.     bytes_out += (ulg)outcnt;
  126.     outcnt = 0;
  127. }
  128.  
  129. /* ===========================================================================
  130.  * Write the output window window[0..outcnt-1] and update crc and bytes_out.
  131.  * (Used for the decompressed data only.)
  132.  */
  133. void flush_window()
  134. {
  135.     if (outcnt == 0) return;
  136.     updcrc(window, outcnt);
  137.  
  138.     if (!test) {
  139.     write_buf(ofd, (char *)window, outcnt);
  140.     }
  141.     bytes_out += (ulg)outcnt;
  142.     outcnt = 0;
  143. }
  144.  
  145. /* ===========================================================================
  146.  * Does the same as write(), but also handles partial pipe writes and checks
  147.  * for error return.
  148.  */
  149. void write_buf(fd, buf, cnt)
  150.     int       fd;
  151.     voidp     buf;
  152.     unsigned  cnt;
  153. {
  154.     unsigned  n;
  155.  
  156.     while ((n = write(fd, buf, cnt)) != cnt) {
  157.     if (n == (unsigned)(-1)) {
  158.         write_error();
  159.     }
  160.     cnt -= n;
  161.     buf = (voidp)((char*)buf+n);
  162.     }
  163. }
  164.  
  165. /* ========================================================================
  166.  * Put string s in lower case, return s.
  167.  */
  168. char *strlwr(s)
  169.     char *s;
  170. {
  171.     char *t;
  172.     for (t = s; *t; t++) *t = tolow(*t);
  173.     return s;
  174. }
  175.  
  176. /* ========================================================================
  177.  * Return the base name of a file (remove any directory prefix and
  178.  * any version suffix). For systems with file names that are not
  179.  * case sensitive, force the base name to lower case.
  180.  */
  181. char *basename(fname)
  182.     char *fname;
  183. {
  184.     char *p;
  185.  
  186.     if ((p = strrchr(fname, PATH_SEP))  != NULL) fname = p+1;
  187. #ifdef PATH_SEP2
  188.     if ((p = strrchr(fname, PATH_SEP2)) != NULL) fname = p+1;
  189. #endif
  190. #ifdef PATH_SEP3
  191.     if ((p = strrchr(fname, PATH_SEP3)) != NULL) fname = p+1;
  192. #endif
  193. #ifdef SUFFIX_SEP
  194.     if ((p = strrchr(fname, SUFFIX_SEP)) != NULL) *p = '\0';
  195. #endif
  196.     if (casemap('A') == 'a') strlwr(fname);
  197.     return fname;
  198. }
  199.  
  200. /* ========================================================================
  201.  * Make a file name legal for file systems not allowing file names with
  202.  * multiple dots or starting with a dot (such as MSDOS), by changing
  203.  * all dots except the last one into underlines.  A target dependent
  204.  * function can be used instead of this simple function by defining the macro
  205.  * MAKE_LEGAL_NAME in tailor.h and providing the function in a target
  206.  * dependent module.
  207.  */
  208. void make_simple_name(name)
  209.     char *name;
  210. {
  211.     char *p = strrchr(name, '.');
  212.     if (p == NULL) return;
  213.     if (p == name) p++;
  214.     do {
  215.         if (*--p == '.') *p = '_';
  216.     } while (p != name);
  217. }
  218.  
  219.  
  220. #if defined(NO_STRING_H) && !defined(STDC_HEADERS)
  221.  
  222. /* Provide missing strspn and strcspn functions. */
  223.  
  224. #  ifndef __STDC__
  225. #    define const
  226. #  endif
  227.  
  228. int strspn  OF((const char *s, const char *accept));
  229. int strcspn OF((const char *s, const char *reject));
  230.  
  231. /* ========================================================================
  232.  * Return the length of the maximum initial segment
  233.  * of s which contains only characters in accept.
  234.  */
  235. int strspn(s, accept)
  236.     const char *s;
  237.     const char *accept;
  238. {
  239.     register const char *p;
  240.     register const char *a;
  241.     register int count = 0;
  242.  
  243.     for (p = s; *p != '\0'; ++p) {
  244.     for (a = accept; *a != '\0'; ++a) {
  245.         if (*p == *a) break;
  246.     }
  247.     if (*a == '\0') return count;
  248.     ++count;
  249.     }
  250.     return count;
  251. }
  252.  
  253. /* ========================================================================
  254.  * Return the length of the maximum inital segment of s
  255.  * which contains no characters from reject.
  256.  */
  257. int strcspn(s, reject)
  258.     const char *s;
  259.     const char *reject;
  260. {
  261.     register int count = 0;
  262.  
  263.     while (*s != '\0') {
  264.     if (strchr(reject, *s++) != NULL) return count;
  265.     ++count;
  266.     }
  267.     return count;
  268. }
  269.  
  270. #endif /* NO_STRING_H */
  271.  
  272. /* ========================================================================
  273.  * Add an environment variable (if any) before argv, and update argc.
  274.  * Return the expanded environment variable to be freed later, or NULL 
  275.  * if no options were added to argv.
  276.  */
  277. #define SEPARATOR    " \t"    /* separators in env variable */
  278.  
  279. char *add_envopt(argcp, argvp, env)
  280.     int *argcp;          /* pointer to argc */
  281.     char ***argvp;       /* pointer to argv */
  282.     char *env;           /* name of environment variable */
  283. {
  284.     char *p;             /* running pointer through env variable */
  285.     char **oargv;        /* runs through old argv array */
  286.     char **nargv;        /* runs through new argv array */
  287.     int     oargc = *argcp; /* old argc */
  288.     int  nargc = 0;      /* number of arguments in env variable */
  289.  
  290.     env = (char*)getenv(env);
  291.     if (env == NULL) return NULL;
  292.  
  293.     p = (char*)xmalloc(strlen(env)+1);
  294.     env = strcpy(p, env);                    /* keep env variable intact */
  295.  
  296.     for (p = env; *p; nargc++ ) {            /* move through env */
  297.     p += strspn(p, SEPARATOR);         /* skip leading separators */
  298.     if (*p == '\0') break;
  299.  
  300.     p += strcspn(p, SEPARATOR);         /* find end of word */
  301.     if (*p) *p++ = '\0';             /* mark it */
  302.     }
  303.     if (nargc == 0) {
  304.     free(env);
  305.     return NULL;
  306.     }
  307.     *argcp += nargc;
  308.     /* Allocate