instead of patching lib/Makefile.in directly, try autoreconf in rules
[debian/gzip] / util.c
diff --git a/util.c b/util.c
index 65851631152aee7b285fd87a2496b6b201c3dd9e..f08c2c7d41419bec7f375f5b50c727045ee22ba1 100644 (file)
--- a/util.c
+++ b/util.c
@@ -1,13 +1,22 @@
 /* util.c -- utility functions for gzip support
- * Copyright (C) 1997, 1998, 1999, 2001 Free Software Foundation, Inc.
- * Copyright (C) 1992-1993 Jean-loup Gailly
- * This is free software; you can redistribute it and/or modify it under the
- * terms of the GNU General Public License, see the file COPYING.
- */
 
-#ifdef RCSID
-static char rcsid[] = "$Id: util.c,v 0.15 1993/06/15 09:04:13 jloup Exp $";
-#endif
+   Copyright (C) 1997-1999, 2001-2002, 2006, 2009-2010 Free Software
+   Foundation, Inc.
+   Copyright (C) 1992-1993 Jean-loup Gailly
+
+   This program is free software; you can redistribute it and/or modify
+   it under the terms of the GNU General Public License as published by
+   the Free Software Foundation; either version 3, or (at your option)
+   any later version.
+
+   This program is distributed in the hope that it will be useful,
+   but WITHOUT ANY WARRANTY; without even the implied warranty of
+   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+   GNU General Public License for more details.
+
+   You should have received a copy of the GNU General Public License
+   along with this program; if not, write to the Free Software Foundation,
+   Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.  */
 
 #include <config.h>
 #include <ctype.h>
@@ -15,29 +24,22 @@ static char rcsid[] = "$Id: util.c,v 0.15 1993/06/15 09:04:13 jloup Exp $";
 
 #include "tailor.h"
 
-#ifdef HAVE_LIMITS_H
-#  include <limits.h>
-#endif
-#ifdef HAVE_UNISTD_H
-#  include <unistd.h>
-#endif
-#ifdef HAVE_FCNTL_H
-#  include <fcntl.h>
-#endif
-
-#if defined STDC_HEADERS || defined HAVE_STDLIB_H
-#  include <stdlib.h>
-#else
-   extern int errno;
-#endif
+#include <limits.h>
+#include <unistd.h>
+#include <fcntl.h>
+#include <stdlib.h>
+#include <errno.h>
 
 #include "gzip.h"
 #include "crypt.h"
+#include <xalloc.h>
 
 #ifndef CHAR_BIT
 #  define CHAR_BIT 8
 #endif
 
+static int write_buffer OF((int, voidp, unsigned int));
+
 extern ulg crc_32_tab[];   /* crc table, defined below */
 
 /* ===========================================================================
@@ -51,7 +53,7 @@ int copy(in, out)
     while (insize != 0 && (int)insize != -1) {
        write_buf(out, (char*)inbuf, insize);
        bytes_out += insize;
-       insize = read(in, (char*)inbuf, INBUFSIZ);
+       insize = read_buffer (in, (char *) inbuf, INBUFSIZ);
     }
     if ((int)insize == -1) {
        read_error();
@@ -106,7 +108,7 @@ int fill_inbuf(eof_ok)
     /* Read as much as possible */
     insize = 0;
     do {
-       len = read(ifd, (char*)inbuf+insize, INBUFSIZ-insize);
+       len = read_buffer (ifd, (char *) inbuf + insize, INBUFSIZ - insize);
        if (len == 0) break;
        if (len == -1) {
          read_error();
@@ -126,6 +128,31 @@ int fill_inbuf(eof_ok)
     return inbuf[0];
 }
 
+/* Like the standard read function, except do not attempt to read more
+   than SSIZE_MAX bytes at a time.  */
+int
+read_buffer (fd, buf, cnt)
+     int fd;
+     voidp buf;
+     unsigned int cnt;
+{
+  if (INT_MAX < cnt)
+    cnt = INT_MAX;
+  return read (fd, buf, cnt);
+}
+
+/* Likewise for 'write'.  */
+static int
+write_buffer (fd, buf, cnt)
+     int fd;
+     voidp buf;
+     unsigned int cnt;
+{
+  if (INT_MAX < cnt)
+    cnt = INT_MAX;
+  return write (fd, buf, cnt);
+}
+
 /* ===========================================================================
  * Write the output buffer outbuf[0..outcnt-1] and update bytes_out.
  * (used for the compressed data only)
@@ -166,7 +193,7 @@ void write_buf(fd, buf, cnt)
 {
     unsigned  n;
 
-    while ((n = write(fd, buf, cnt)) != cnt) {
+    while ((n = write_buffer (fd, buf, cnt)) != cnt) {
        if (n == (unsigned)(-1)) {
            write_error();
        }
@@ -192,7 +219,8 @@ char *strlwr(s)
  * any version suffix). For systems with file names that are not
  * case sensitive, force the base name to lower case.
  */
-char *base_name(fname)
+char *
+gzip_base_name (fname)
     char *fname;
 {
     char *p;
@@ -255,84 +283,31 @@ void make_simple_name(name)
     } while (p != name);
 }
 
-
-#if !defined HAVE_STRING_H && !defined STDC_HEADERS
-
-/* Provide missing strspn and strcspn functions. */
-
-#  ifndef __STDC__
-#    define const
-#  endif
-
-int strspn  OF((const char *s, const char *accept));
-int strcspn OF((const char *s, const char *reject));
-
-/* ========================================================================
- * Return the length of the maximum initial segment
- * of s which contains only characters in accept.
- */
-int strspn(s, accept)
-    const char *s;
-    const char *accept;
-{
-    register const char *p;
-    register const char *a;
-    register int count = 0;
-
-    for (p = s; *p != '\0'; ++p) {
-       for (a = accept; *a != '\0'; ++a) {
-           if (*p == *a) break;
-       }
-       if (*a == '\0') return count;
-       ++count;
-    }
-    return count;
-}
-
-/* ========================================================================
- * Return the length of the maximum inital segment of s
- * which contains no characters from reject.
- */
-int strcspn(s, reject)
-    const char *s;
-    const char *reject;
-{
-    register int count = 0;
-
-    while (*s != '\0') {
-       if (strchr(reject, *s++) != NULL) return count;
-       ++count;
-    }
-    return count;
-}
-
-#endif
-
 /* ========================================================================
  * Add an environment variable (if any) before argv, and update argc.
- * Return the expanded environment variable to be freed later, or NULL 
+ * Return the expanded environment variable to be freed later, or NULL
  * if no options were added to argv.
  */
 #define SEPARATOR      " \t"   /* separators in env variable */
 
-char *add_envopt(argcp, argvp, env)
-    int *argcp;          /* pointer to argc */
-    char ***argvp;       /* pointer to argv */
-    char *env;           /* name of environment variable */
+char *add_envopt(
+    int *argcp,          /* pointer to argc */
+    char ***argvp,       /* pointer to argv */
+    char const *envvar_name) /* name of environment variable */
 {
     char *p;             /* running pointer through env variable */
     char **oargv;        /* runs through old argv array */
     char **nargv;        /* runs through new argv array */
     int         oargc = *argcp; /* old argc */
     int  nargc = 0;      /* number of arguments in env variable */
+    char *env_val;
 
-    env = (char*)getenv(env);
-    if (env == NULL) return NULL;
+    env_val = getenv(envvar_name);
+    if (env_val == NULL) return NULL;
 
-    p = (char*)xmalloc(strlen(env)+1);
-    env = strcpy(p, env);                    /* keep env variable intact */
+    env_val = xstrdup (env_val);
 
-    for (p = env; *p; nargc++ ) {            /* move through env */
+    for (p = env_val; *p; nargc++ ) {        /* move through env_val */
        p += strspn(p, SEPARATOR);           /* skip leading separators */
        if (*p == '\0') break;
 
@@ -340,24 +315,24 @@ char *add_envopt(argcp, argvp, env)
        if (*p) *p++ = '\0';                 /* mark it */
     }
     if (nargc == 0) {
-       free(env);
+       free(env_val);
        return NULL;
     }
     *argcp += nargc;
     /* Allocate the new argv array, with an extra element just in case
      * the original arg list did not end with a NULL.
      */
-    nargv = (char**)calloc(*argcp+1, sizeof(char *));
-    if (nargv == NULL) error("out of memory");
+    nargv = xcalloc (*argcp + 1, sizeof (char *));
     oargv  = *argvp;
     *argvp = nargv;
 
     /* Copy the program name first */
-    if (oargc-- < 0) error("argc<=0");
+    if (oargc-- < 0)
+      gzip_error ("argc<=0");
     *(nargv++) = *(oargv++);
 
     /* Then copy the environment args */
-    for (p = env; nargc > 0; nargc--) {
+    for (p = env_val; nargc > 0; nargc--) {
        p += strspn(p, SEPARATOR);           /* skip separators */
        *(nargv++) = p;                      /* store start */
        while (*p++) ;                       /* skip over word */
@@ -366,29 +341,35 @@ char *add_envopt(argcp, argvp, env)
     /* Finally copy the old args and add a NULL (usual convention) */
     while (oargc--) *(nargv++) = *(oargv++);
     *nargv = NULL;
-    return env;
+    return env_val;
 }
 
 /* ========================================================================
  * Error handlers.
  */
-void error(m)
-    char *m;
+void
+gzip_error (char const *m)
 {
-    fprintf(stderr, "\n%s: %s: %s\n", progname, ifname, m);
+    fprintf (stderr, "\n%s: %s: %s\n", program_name, ifname, m);
     abort_gzip();
 }
 
-void warning (m)
-    char *m;
+void
+xalloc_die ()
+{
+  fprintf (stderr, "\n%s: memory_exhausted\n", program_name);
+  abort_gzip ();
+}
+
+void warning (char const *m)
 {
-    WARN ((stderr, "%s: %s: warning: %s\n", progname, ifname, m));
+    WARN ((stderr, "%s: %s: warning: %s\n", program_name, ifname, m));
 }
 
 void read_error()
 {
     int e = errno;
-    fprintf(stderr, "\n%s: ", progname);
+    fprintf (stderr, "\n%s: ", program_name);
     if (e != 0) {
        errno = e;
        perror(ifname);
@@ -401,7 +382,7 @@ void read_error()
 void write_error()
 {
     int e = errno;
-    fprintf(stderr, "\n%s: ", progname);
+    fprintf (stderr, "\n%s: ", program_name);
     errno = e;
     perror(ofname);
     abort_gzip();
@@ -451,18 +432,6 @@ void fprint_off(file, offset, width)
        putc (*p, file);
 }
 
-/* ========================================================================
- * Semi-safe malloc -- never returns NULL.
- */
-voidp xmalloc (size)
-    unsigned size;
-{
-    voidp cp = (voidp)malloc (size);
-
-    if (cp == NULL) error("out of memory");
-    return cp;
-}
-
 /* ========================================================================
  * Table of CRC-32's of all single-byte values (made by makecrc.c)
  */