*** empty log message ***
[gnulib.git] / lib / linebuffer.c
1 /* linebuffer.c -- read arbitrarily long lines
2
3    Copyright (C) 1986, 1991, 1998, 1999, 2001, 2003, 2004 Free
4    Software 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., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, 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 <string.h>
29 #include <sys/types.h>
30 #include "linebuffer.h"
31 #include "xalloc.h"
32
33 #if USE_UNLOCKED_IO
34 # include "unlocked-io.h"
35 #endif
36
37 /* Initialize linebuffer LINEBUFFER for use. */
38
39 void
40 initbuffer (struct linebuffer *linebuffer)
41 {
42   memset (linebuffer, 0, sizeof *linebuffer);
43 }
44
45 /* Read an arbitrarily long line of text from STREAM into LINEBUFFER.
46    Keep the newline; append a newline if it's the last line of a file
47    that ends in a non-newline character.  Do not null terminate.
48    Therefore the stream can contain NUL bytes, and the length
49    (including the newline) is returned in linebuffer->length.
50    Return NULL when stream is empty.  Return NULL and set errno upon
51    error; callers can distinguish this case from the empty case by
52    invoking ferror (stream).
53    Otherwise, return LINEBUFFER.  */
54 struct linebuffer *
55 readlinebuffer (struct linebuffer *linebuffer, FILE *stream)
56 {
57   int c;
58   char *buffer = linebuffer->buffer;
59   char *p = linebuffer->buffer;
60   char *end = buffer + linebuffer->size; /* Sentinel. */
61
62   if (feof (stream))
63     return NULL;
64
65   do
66     {
67       c = getc (stream);
68       if (c == EOF)
69         {
70           if (p == buffer || ferror (stream))
71             return NULL;
72           if (p[-1] == '\n')
73             break;
74           c = '\n';
75         }
76       if (p == end)
77         {
78           size_t oldsize = linebuffer->size;
79           buffer = x2realloc (buffer, &linebuffer->size);
80           p = buffer + oldsize;
81           linebuffer->buffer = buffer;
82           end = buffer + linebuffer->size;
83         }
84       *p++ = c;
85     }
86   while (c != '\n');
87
88   linebuffer->length = p - buffer;
89   return linebuffer;
90 }
91
92 /* Free the buffer that was allocated for linebuffer LINEBUFFER.  */
93
94 void
95 freebuffer (struct linebuffer *linebuffer)
96 {
97   free (linebuffer->buffer);
98 }