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