A libio patch for egcs

H.J. Lu hjl@lucon.org
Sat Feb 14 15:06:00 GMT 1998


> 
> After thing about it, I agree it is better to use __uflow instead
> of __underflow.  However, I think the following is simpler and
> more robust.  For one thing, it does not break in the case that
> (1) A call to __uflow might allocate a buffer even if __underflow didn't,
> and:
> (2) __uflow does not not check that the buffer is empty.
> (I'm not sure what the standard says - whether uflow is allowed to
>  assume that the buffer is empty.)
> 
> Also, this change is smaller (except for white-space re-formatting).
> 
> However, I haven't tested it, beyond checking that it compiles.
> Could you try it?
> 

Thanks, Per. It seems to work fine. I added one more arg to _IO_getline
and renamed it to _IO_new_getline. I used it to fix the C++ getline
bug. Can you take a look at it.

Thanks.


H.J.
---
Sat Feb 14 14:36:56 1998  H.J. Lu  (hjl@gnu.org)

	* config/linux.mt (IO_OBJECTS): Add iogetline.o.
	* config/linuxlibc1.mt: Ditto.

	* iogetline.c (_IO_new_getline): Renamed from _IO_getline.
	(_IO_getline): Just call _IO_new_getline.

	* isgetline.cc (istream::getline, istream::get, _sb_readline):
	Call _IO_new_getline instead of _IO_getline and get the EOF
	information.
	* sbgetline.cc (streambuf::sgetline): Ditto.

	* libioP.h (_IO_new_getline): New declaration.

Fri Feb 13 23:14:49 1998  Per Bothner <bothner@cygnus.com>

	* iogetline.c (_IO_getline): Handle the case when there is no
	buffer.

diff -x CVS -ur --new-file ../../../import/egcs/libio/config/linux.mt ./config/linux.mt
--- ../../../import/egcs/libio/config/linux.mt	Thu Nov 27 00:34:23 1997
+++ ./config/linux.mt	Thu Feb 12 07:53:21 1998
@@ -16,7 +16,7 @@
 LIBIO_INCLUDE=
 
 # We have those in libc.a.
-IO_OBJECTS=
+IO_OBJECTS= iogetline.o
 STDIO_WRAP_OBJECTS=
 OSPRIM_OBJECTS=
 STDIO_OBJECTS=
diff -x CVS -ur --new-file ../../../import/egcs/libio/config/linuxlibc1.mt ./config/linuxlibc1.mt
--- ../../../import/egcs/libio/config/linuxlibc1.mt	Wed Oct 22 18:17:58 1997
+++ ./config/linuxlibc1.mt	Thu Feb 12 07:53:30 1998
@@ -11,7 +11,7 @@
 # We have those in libc.a.
 IO_OBJECTS=iogetc.o ioputc.o iofeof.o ioferror.o \
 	filedoalloc.o fileops.o genops.o iofclose.o \
-	iovsprintf.o iovsscanf.o strops.o
+	iovsprintf.o iovsscanf.o strops.o iogetline.o
 STDIO_WRAP_OBJECTS=
 OSPRIM_OBJECTS=
 STDIO_OBJECTS=
diff -x CVS -ur --new-file ../../../import/egcs/libio/iogetline.c ./iogetline.c
--- ../../../import/egcs/libio/iogetline.c	Sat Sep  6 00:43:18 1997
+++ ./iogetline.c	Sat Feb 14 14:57:21 1998
@@ -1,4 +1,4 @@
-/* Copyright (C) 1993, 1997 Free Software Foundation, Inc.
+/* Copyright (C) 1993, 1997, 1998 Free Software Foundation, Inc.
    This file is part of the GNU IO Library.
 
    This library is free software; you can redistribute it and/or
@@ -26,6 +26,19 @@
 #include "libioP.h"
 #include <string.h>
 
+#if defined(_LIBC) || !_G_HAVE_IO_NEW_GETLINE
+
+_IO_size_t
+_IO_getline (fp, buf, n, delim, extract_delim)
+     _IO_FILE *fp;
+     char *buf;
+     _IO_size_t n;
+     int delim;
+     int extract_delim;
+{
+  return _IO_new_getline (fp, buf, n, delim, extract_delim, (int *) 0);
+}
+
 /* Algorithm based on that used by Berkeley pre-4.4 fgets implementation.
 
    Read chars into buf (of size n), until delim is seen.
@@ -35,44 +48,65 @@
    If extract_delim > 0, insert delim in output. */
 
 _IO_size_t
-_IO_getline (fp, buf, n, delim, extract_delim)
+_IO_new_getline (fp, buf, n, delim, extract_delim, eof)
      _IO_FILE *fp;
      char *buf;
      _IO_size_t n;
      int delim;
      int extract_delim;
+     int *eof;
 {
   char *ptr = buf;
+  if (eof) *eof = 0;
   do
     {
       _IO_ssize_t len = fp->_IO_read_end - fp->_IO_read_ptr;
-      char *t;
       if (len <= 0)
-	if (__underflow (fp) == EOF)
-	  break;
-	else
-	  len = fp->_IO_read_end - fp->_IO_read_ptr;
-      if ((_IO_size_t) len >= n)
-	len = n;
-      t = (char *) memchr ((void *) fp->_IO_read_ptr, delim, len);
-      if (t != NULL)
 	{
-	  _IO_size_t old_len = ptr-buf;
-	  len = t - fp->_IO_read_ptr;
-	  if (extract_delim >= 0)
+	  int c = __uflow (fp);
+	  if (c == EOF)
+	    {
+	      if (eof) *eof = c;
+	      break;
+	    }
+	  if (c == delim)
 	    {
-	      ++t;
 	      if (extract_delim > 0)
-		++len;
+		*ptr++ = c;
+	      else if (extract_delim < 0)
+		_IO_sputbackc (fp, c);
+	      return ptr - buf;
 	    }
-	  memcpy ((void *) ptr, (void *) fp->_IO_read_ptr, len);
-	  fp->_IO_read_ptr = t;
-	  return old_len + len;
+	  *ptr++ = c;
+	  n--;
 	}
-      memcpy ((void *) ptr, (void *) fp->_IO_read_ptr, len);
-      fp->_IO_read_ptr += len;
-      ptr += len;
-      n -= len;
+	else
+	  {
+	    char *t;
+	    if ((_IO_size_t) len >= n)
+	      len = n;
+	    t = (char *) memchr ((void *) fp->_IO_read_ptr, delim, len);
+	    if (t != NULL)
+	      {
+		_IO_size_t old_len = ptr-buf;
+		len = t - fp->_IO_read_ptr;
+		if (extract_delim >= 0)
+		  {
+		    ++t;
+		    if (extract_delim > 0)
+		      ++len;
+		  }
+		memcpy ((void *) ptr, (void *) fp->_IO_read_ptr, len);
+		fp->_IO_read_ptr = t;
+		return old_len + len;
+	      }
+	    memcpy ((void *) ptr, (void *) fp->_IO_read_ptr, len);
+	    fp->_IO_read_ptr += len;
+	    ptr += len;
+	    n -= len;
+	  }
     } while (n != 0);
   return ptr - buf;
 }
+
+#endif /* Defined(_LIBC) || !_G_HAVE_IO_GETLINE_NEW */
diff -x CVS -ur --new-file ../../../import/egcs/libio/isgetline.cc ./isgetline.cc
--- ../../../import/egcs/libio/isgetline.cc	Thu Aug 21 15:58:19 1997
+++ ./isgetline.cc	Thu Feb 12 08:15:27 1998
@@ -38,8 +38,9 @@
   if (ipfx1())
     {
       streambuf *sb = rdbuf();
-      _gcount = _IO_getline(sb, buf, len - 1, delim, -1);
-      ch = sb->sbumpc();
+      _gcount = _IO_new_getline(sb, buf, len - 1, delim, -1, &ch);
+      if (ch != EOF)
+	ch = sb->sbumpc();
       if (ch == EOF)
 	set (_gcount == 0 ? (ios::failbit|ios::eofbit) : ios::eofbit);
       else if (ch != (unsigned char) delim)
@@ -67,8 +68,9 @@
   if (ipfx1())
     {
       streambuf *sbuf = rdbuf();
-      long count = _IO_getline(sbuf, buf, len - 1, delim, -1);
-      if (count == 0 && sbuf->sgetc() == EOF)
+      int ch;
+      long count = _IO_new_getline(sbuf, buf, len - 1, delim, -1, &ch);
+      if (_gcount == 0 && ch == EOF)
 	set(ios::failbit|ios::eofbit);
       else
 	_gcount = count;
@@ -92,8 +94,10 @@
     char *ptr;
     int ch;
     
-    _IO_size_t count = _IO_getline(sb, buf, CHUNK_SIZE, terminator, -1);
-    ch = sb->sbumpc();
+    _IO_size_t count = _IO_new_getline(sb, buf, CHUNK_SIZE, terminator,
+				       -1, &ch);
+    if (ch != EOF)
+      ch = sb->sbumpc();
     long old_total = total;
     total += count;
     if (ch != EOF && ch != terminator) {
diff -x CVS -ur --new-file ../../../import/egcs/libio/libioP.h ./libioP.h
--- ../../../import/egcs/libio/libioP.h	Wed Nov 26 15:14:35 1997
+++ ./libioP.h	Thu Feb 12 07:53:07 1998
@@ -377,6 +377,8 @@
 
 
 extern _IO_size_t _IO_getline __P ((_IO_FILE *,char *, _IO_size_t, int, int));
+extern _IO_size_t _IO_new_getline __P ((_IO_FILE *,char *, _IO_size_t,
+					int, int, int *));
 extern _IO_ssize_t _IO_getdelim __P ((char **, _IO_size_t *, int, _IO_FILE *));
 extern double _IO_strtod __P ((const char *, char **));
 extern char *_IO_dtoa __P ((double __d, int __mode, int __ndigits,
diff -x CVS -ur --new-file ../../../import/egcs/libio/sbgetline.cc ./sbgetline.cc
--- ../../../import/egcs/libio/sbgetline.cc	Thu Aug 21 15:58:21 1997
+++ ./sbgetline.cc	Thu Feb 12 07:53:07 1998
@@ -27,5 +27,5 @@
 
 long streambuf::sgetline(char* buf, _IO_size_t n, char delim, int extract_delim)
 {
-  return _IO_getline(this, buf, n, delim, extract_delim);
+  return _IO_new_getline(this, buf, n, delim, extract_delim, (int *) 0);
 }



More information about the Gcc mailing list