bug-bash
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

bash 2.05 coding cleanup for int->ssize_t, plus CYGWIN fix


From: Paul Eggert
Subject: bash 2.05 coding cleanup for int->ssize_t, plus CYGWIN fix
Date: Fri, 13 Apr 2001 02:02:59 -0700 (PDT)

Configuration Information [Automatically generated, do not change]:
Machine: sparc
OS: solaris2.8
Compiler: gcc
Compilation CFLAGS:  -DPROGRAM='bash' -DCONF_HOSTTYPE='sparc' 
-DCONF_OSTYPE='solaris2.8' -DCONF_MACHTYPE='sparc-sun-solaris2.8' 
-DCONF_VENDOR='sun' -DSHELL  -DHAVE_CONFIG_H   -I.  -I. -I./include -I./lib 
-I/opt/reb/include -g -O2 -Wall
uname output: SunOS shade.twinsun.com 5.8 Generic_108528-06 sun4u sparc 
SUNW,Ultra-1
Machine Type: sparc-sun-solaris2.8

Bash Version: 2.05
Patch Level: 0
Release Status: release

Description:
        Bash has three routines zread, zread1, zreadc that are
        supposed to mimic 'read', but they return int whereas modern
        'read' returns ssize_t.  While looking into this problem I
        noticed some related glitches for b_fill_buffer in the CYGWIN
        case: the wrong characters were being examine for CRLF, and
        subscript errors were possible on short lines.

        Other than the CYGWIN stuff, this patch is just a code cleanup.

Repeat-By:

Fix:

2001-04-13  Paul Eggert  <address@hidden>

        * configure.in (AC_CHECK_TYPE): Add ssize_t.
        * config.h.in (ssize_t): New undef.

        * input.c (b_fill_buffer):
        Fix glitches if __CYGWIN__: the wrong characters
        were being examined for EOF, and subscript violations were possible.
        Reset b_used to 0, not -1, on read error.

        * builtins/evalstring.c (cat_file):
        Use ssize_t, not int, for results of reads.
        * subst.c (read_comsub): Likewise.

        * input.h (struct BSTREAM):
        b_used and b_inputp are now size_t, not int.

        * externs.h (zread, zread1, zreadc): Now return ssize_t, not int.
        * lib/sh/zread.c (zread, zread1, zreadc): Likewise.
        (lbuf): Now char, not unsigned char.
        (lind, lused): Now size_t, not int.
        (zreadc): Do not set lused to a negative value.
        Remove cast that is no longer needed.
        (zsyncfd): Use off_t, not int, to store file offset.

===================================================================
RCS file: builtins/evalstring.c,v
retrieving revision 2.5
retrieving revision 2.5.0.1
diff -pu -r2.5 -r2.5.0.1
--- builtins/evalstring.c       2001/02/14 22:05:23     2.5
+++ builtins/evalstring.c       2001/04/13 08:30:59     2.5.0.1
@@ -297,7 +297,8 @@ cat_file (r)
      REDIRECT *r;
 {
   char lbuf[128], *fn;
-  int nr, fd, rval;
+  ssize_t nr;
+  int fd, rval;
 
   if (r->instruction != r_input_direction)
     return -1;
===================================================================
RCS file: config.h.in,v
retrieving revision 2.5.0.1
retrieving revision 2.5.0.2
diff -pu -r2.5.0.1 -r2.5.0.2
--- config.h.in 2001/04/10 19:15:14     2.5.0.1
+++ config.h.in 2001/04/13 08:30:59     2.5.0.2
@@ -217,6 +217,9 @@
 #undef size_t
 
 /* Define to `int' if <sys/types.h> doesn't define.  */
+#undef ssize_t
+
+/* Define to `int' if <sys/types.h> doesn't define.  */
 #undef uid_t
 
 /* Define to `long' if <sys/types.h> doesn't define.  */
===================================================================
RCS file: configure.in,v
retrieving revision 2.5.0.1
retrieving revision 2.5.0.2
diff -pu -r2.5.0.1 -r2.5.0.2
--- configure.in        2001/04/10 19:15:14     2.5.0.1
+++ configure.in        2001/04/13 08:30:59     2.5.0.2
@@ -543,6 +543,7 @@ AC_TYPE_MODE_T
 AC_TYPE_UID_T
 AC_TYPE_PID_T
 AC_TYPE_SIZE_T
+AC_CHECK_TYPE(ssize_t, int)
 AC_CHECK_TYPE(time_t, long)
 
 AC_TYPE_SIGNAL
===================================================================
RCS file: externs.h,v
retrieving revision 2.5.0.1
retrieving revision 2.5.0.2
diff -pu -r2.5.0.1 -r2.5.0.2
--- externs.h   2001/04/13 07:08:33     2.5.0.1
+++ externs.h   2001/04/13 08:30:59     2.5.0.2
@@ -254,9 +254,9 @@ extern int sh_mktmpfd __P((char *, int, 
 /* extern FILE *sh_mktmpfp __P((char *, int, char **)); */
 
 /* declarations for functions defined in lib/sh/zread.c */
-extern int zread __P((int, char *, size_t));
-extern int zread1 __P((int, char *, size_t));
-extern int zreadc __P((int, char *));
+extern ssize_t zread __P((int, char *, size_t));
+extern ssize_t zread1 __P((int, char *, size_t));
+extern ssize_t zreadc __P((int, char *));
 extern void zreset __P((void));
 extern void zsyncfd __P((int));
 
===================================================================
RCS file: input.c,v
retrieving revision 2.5
retrieving revision 2.5.0.1
diff -pu -r2.5 -r2.5.0.1
--- input.c     2001/02/13 20:39:59     2.5
+++ input.c     2001/04/13 08:30:59     2.5.0.1
@@ -426,21 +426,26 @@ static int
 b_fill_buffer (bp)
      BUFFERED_STREAM *bp;
 {
-  bp->b_used = zread (bp->b_fd, bp->b_buffer, bp->b_size);
-#if defined (__CYGWIN__)
-  /* If on cygwin, translate \r\n to \n. */
-  if (bp->b_buffer[bp->b_used - 1] == '\r' && bp->b_buffer[bp->b_used] == '\n')
-    bp->b_buffer[--bp->b_used] = '\n';
-#endif
-  if (bp->b_used <= 0)
+  ssize_t nr = zread (bp->b_fd, bp->b_buffer, bp->b_size);
+  if (nr <= 0)
     {
+      bp->b_used = 0;
       bp->b_buffer[0] = 0;
-      if (bp->b_used == 0)
+      if (nr == 0)
        bp->b_flag |= B_EOF;
       else
        bp->b_flag |= B_ERROR;
       return (EOF);
     }
+#if defined (__CYGWIN__)
+  /* If on cygwin, translate \r\n to \n. */
+  if (2 <= nr && bp->b_buffer[nr - 2] == '\r' && bp->b_buffer[nr - 1] == '\n')
+    {
+      bp->b_buffer[nr - 2] = '\n';
+      nr--;
+    }
+#endif
+  bp->b_used = nr;
   bp->b_inputp = 0;
   return (bp->b_buffer[bp->b_inputp++] & 0xFF);
 }
===================================================================
RCS file: input.h,v
retrieving revision 2.5
retrieving revision 2.5.0.1
diff -pu -r2.5 -r2.5.0.1
--- input.h     2001/02/13 20:02:25     2.5
+++ input.h     2001/04/13 08:30:59     2.5.0.1
@@ -52,9 +52,9 @@ typedef struct BSTREAM
   int  b_fd;
   char *b_buffer;              /* The buffer that holds characters read. */
   size_t b_size;               /* How big the buffer is. */
-  int  b_used;                 /* How much of the buffer we're using, */
+  size_t b_used;               /* How much of the buffer we're using, */
+  size_t b_inputp;             /* The input pointer, index into b_buffer. */
   int  b_flag;                 /* Flag values. */
-  int  b_inputp;               /* The input pointer, index into b_buffer. */
 } BUFFERED_STREAM;
 
 #if 0
===================================================================
RCS file: lib/sh/zread.c,v
retrieving revision 2.5
retrieving revision 2.5.0.1
diff -pu -r2.5 -r2.5.0.1
--- lib/sh/zread.c      2001/02/14 22:03:59     2.5
+++ lib/sh/zread.c      2001/04/13 08:30:59     2.5.0.1
@@ -36,13 +36,13 @@ extern int errno;
 
 /* Read LEN bytes from FD into BUF.  Retry the read on EINTR.  Any other
    error causes the loop to break. */
-int
+ssize_t
 zread (fd, buf, len)
      int fd;
      char *buf;
      size_t len;
 {
-  int r;
+  ssize_t r;
 
   while ((r = read (fd, buf, len)) < 0 && errno == EINTR)
     ;
@@ -57,13 +57,14 @@ zread (fd, buf, len)
 #endif
 #define NUM_INTR 3
 
-int
+ssize_t
 zread1 (fd, buf, len)
      int fd;
      char *buf;
      size_t len;
 {
-  int r, nintr;
+  ssize_t r;
+  int nintr;
 
   for (nintr = 0; ; )
     {
@@ -84,25 +85,27 @@ zread1 (fd, buf, len)
    in read(2).  This does some local buffering to avoid many one-character
    calls to read(2), like those the `read' builtin performs. */
 
-static unsigned char lbuf[128];
-static int lind, lused;
+static char lbuf[128];
+static size_t lind, lused;
 
-int
+ssize_t
 zreadc (fd, cp)
      int fd;
      char *cp;
 {
-  int r;
-
   if (lind == lused || lused == 0)
     {
-      lused = zread (fd, lbuf, sizeof (lbuf));
+      ssize_t nr = zread (fd, lbuf, sizeof (lbuf));
       lind = 0;
-      if (lused <= 0)
-       return (lused);
+      if (nr <= 0)
+       {
+         lused = 0;
+         return (nr);
+       }
+      lused = nr;
     }
   if (cp)
-    *cp = (char)lbuf[lind++];
+    *cp = lbuf[lind++];
   return 1;
 }
 
@@ -118,7 +121,7 @@ void
 zsyncfd (fd)
      int fd;
 {
-  int off;
+  off_t off;
 
   off = lused - lind;
   if (off > 0)
===================================================================
RCS file: subst.c,v
retrieving revision 2.5.0.3
retrieving revision 2.5.0.4
diff -pu -r2.5.0.3 -r2.5.0.4
--- subst.c     2001/04/13 08:15:26     2.5.0.3
+++ subst.c     2001/04/13 08:30:59     2.5.0.4
@@ -3342,7 +3342,8 @@ read_comsub (fd, quoted)
      int fd, quoted;
 {
   char *istring, buf[128], *bufp;
-  int bufn, istring_index, istring_size, c;
+  ssize_t bufn;
+  int istring_index, istring_size, c;
 
   istring = (char *)NULL;
   istring_index = istring_size = bufn = 0;



reply via email to

[Prev in Thread] Current Thread [Next in Thread]