(freebuffer): Don't free the argument, just the buffer associated with
[gnulib.git] / lib / linebuffer.c
1 /* linebuffer.c -- read arbitrarily long lines
2
3    Copyright (C) 1986, 1991, 1998, 1999, 2001, 2003 Free Software
4    Foundation, Inc.
5
6    This program is free software; you can redistribute it and/or modify
7    it under the terms of the GNU General Public License as published by
8    the Free Software Foundation; either version 2, or (at your option)
9    any later version.
10
11    This program is distributed in the hope that it will be useful,
12    but WITHOUT ANY WARRANTY; without even the implied warranty of
13    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14    GNU General Public License for more details.
15
16    You should have received a copy of the GNU General Public License
17    along with this program; if not, write to the Free Software Foundation,
18    Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.  */
19
20 /* Written by Richard Stallman. */
21 \f
22 #ifdef HAVE_CONFIG_H
23 # include <config.h>
24 #endif
25
26 #include <stdio.h>
27 #include <stdlib.h>
28 #include <sys/types.h>
29 #include "linebuffer.h"
30 #include "unlocked-io.h"
31 #include "xalloc.h"
32
33 /* Initialize linebuffer LINEBUFFER for use. */
34
35 void
36 initbuffer (struct linebuffer *linebuffer)
37 {
38   linebuffer->length = 0;
39   linebuffer->size = 200;
40   linebuffer->buffer = xmalloc (linebuffer->size);
41 }
42
43 /* Read an arbitrarily long line of text from STREAM into LINEBUFFER.
44    Keep the newline; append a newline if it's the last line of a file
45    that ends in a non-newline character.  Do not null terminate.
46    Therefore the stream can contain NUL bytes, and the length
47    (including the newline) is returned in linebuffer->length.
48    Return NULL when stream is empty.  Return NULL and set errno upon
49    error; callers can distinguish this case from the empty case by
50    invoking ferror (stream).
51    Otherwise, return LINEBUFFER.  */
52 struct linebuffer *
53 readlinebuffer (struct linebuffer *linebuffer, FILE *stream)
54 {
55   int c;
56   char *buffer = linebuffer->buffer;
57   char *p = linebuffer->buffer;
58   char *end = buffer + linebuffer->size; /* Sentinel. */
59
60   if (feof (stream))
61     return NULL;
62
63   do
64     {
65       c = getc (stream);
66       if (c == EOF)
67         {
68           if (p == buffer || ferror (stream))
69             return NULL;
70           if (p[-1] == '\n')
71             break;
72           c = '\n';
73         }
74       if (p == end)
75         {
76           linebuffer->size *= 2;
77           buffer = xrealloc (buffer, linebuffer->size);
78           p = p - linebuffer->buffer + buffer;
79           linebuffer->buffer = buffer;
80           end = buffer + linebuffer->size;
81         }
82       *p++ = c;
83     }
84   while (c != '\n');
85
86   linebuffer->length = p - buffer;
87   return linebuffer;
88 }
89
90 /* Free the buffer that was allocated for linebuffer LINEBUFFER.  */
91
92 void
93 freebuffer (struct linebuffer *linebuffer)
94 {
95   free (linebuffer->buffer);
96 }