New module 'striconveh'.
[gnulib.git] / lib / linebuffer.c
index 434d6d8..3f1e45b 100644 (file)
@@ -1,7 +1,7 @@
 /* linebuffer.c -- read arbitrarily long lines
 
-   Copyright (C) 1986, 1991, 1998, 1999, 2001, 2003 Free Software
-   Foundation, Inc.
+   Copyright (C) 1986, 1991, 1998, 1999, 2001, 2003, 2004, 2006 Free
+   Software Foundation, Inc.
 
    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
 
    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., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.  */
+   Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.  */
 
 /* Written by Richard Stallman. */
-\f
-#ifdef HAVE_CONFIG_H
-# include <config.h>
-#endif
+
+#include <config.h>
 
 #include <stdio.h>
 #include <stdlib.h>
+#include <string.h>
 #include <sys/types.h>
 #include "linebuffer.h"
-#include "unlocked-io.h"
 #include "xalloc.h"
 
+#if USE_UNLOCKED_IO
+# include "unlocked-io.h"
+#endif
+
 /* Initialize linebuffer LINEBUFFER for use. */
 
 void
 initbuffer (struct linebuffer *linebuffer)
 {
-  linebuffer->length = 0;
-  linebuffer->size = 200;
-  linebuffer->buffer = xmalloc (linebuffer->size);
+  memset (linebuffer, 0, sizeof *linebuffer);
 }
 
 /* Read an arbitrarily long line of text from STREAM into LINEBUFFER.
@@ -73,9 +73,9 @@ readlinebuffer (struct linebuffer *linebuffer, FILE *stream)
        }
       if (p == end)
        {
-         linebuffer->size *= 2;
-         buffer = xrealloc (buffer, linebuffer->size);
-         p = p - linebuffer->buffer + buffer;
+         size_t oldsize = linebuffer->size;
+         buffer = x2realloc (buffer, &linebuffer->size);
+         p = buffer + oldsize;
          linebuffer->buffer = buffer;
          end = buffer + linebuffer->size;
        }
@@ -87,11 +87,10 @@ readlinebuffer (struct linebuffer *linebuffer, FILE *stream)
   return linebuffer;
 }
 
-/* Free linebuffer LINEBUFFER and its data, all allocated with malloc. */
+/* Free the buffer that was allocated for linebuffer LINEBUFFER.  */
 
 void
 freebuffer (struct linebuffer *linebuffer)
 {
   free (linebuffer->buffer);
-  free (linebuffer);
 }