zdiff would exit 2 (error) rather than 1 for differences
[debian/gzip] / util.c
diff --git a/util.c b/util.c
index 32e24e7afc14ffdeba8ee7569851f68a3a242648..40b36fc3a1f4ac7eea0a6e6df326435673326691 100644 (file)
--- a/util.c
+++ b/util.c
@@ -1,12 +1,12 @@
 /* util.c -- utility functions for gzip support
 
-   Copyright (C) 1997, 1998, 1999, 2001, 2002, 2006 Free Software
-   Foundation, Inc.
+   Copyright (C) 1997-1999, 2001-2002, 2006, 2009 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 2, or (at your option)
+   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,
    along with this program; if not, write to the Free Software Foundation,
    Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.  */
 
-#ifdef RCSID
-static char rcsid[] = "$Id$";
-#endif
-
 #include <config.h>
 #include <ctype.h>
 #include <errno.h>
 
 #include "tailor.h"
 
+#ifdef HAVE_LIMITS_H
+#  include <limits.h>
+#endif
 #ifdef HAVE_UNISTD_H
 #  include <unistd.h>
 #endif
@@ -49,6 +48,8 @@ static char rcsid[] = "$Id$";
 #  define CHAR_BIT 8
 #endif
 
+static int write_buffer OF((int, voidp, unsigned int));
+
 extern ulg crc_32_tab[];   /* crc table, defined below */
 
 /* ===========================================================================
@@ -62,7 +63,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();
@@ -117,7 +118,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();
@@ -137,6 +138,35 @@ 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;
+{
+#ifdef SSIZE_MAX
+  if (SSIZE_MAX < cnt)
+    cnt = SSIZE_MAX;
+#endif
+  return read (fd, buf, cnt);
+}
+
+/* Likewise for 'write'.  */
+static int
+write_buffer (fd, buf, cnt)
+     int fd;
+     voidp buf;
+     unsigned int cnt;
+{
+#ifdef SSIZE_MAX
+  if (SSIZE_MAX < cnt)
+    cnt = SSIZE_MAX;
+#endif
+  return write (fd, buf, cnt);
+}
+
 /* ===========================================================================
  * Write the output buffer outbuf[0..outcnt-1] and update bytes_out.
  * (used for the compressed data only)
@@ -177,7 +207,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();
        }
@@ -358,7 +388,7 @@ char *add_envopt(argcp, argvp, env)
     /* Allocate the new argv array, with an extra element just in case
      * the original arg list did not end with a NULL.
      */
-    nargv = (char **) xcalloc (*argcp + 1, sizeof (char *));
+    nargv = xcalloc (*argcp + 1, sizeof (char *));
     oargv  = *argvp;
     *argvp = nargv;