(AC_CHECK_FUNCS): Remove strtoull, strtoumax, strtouq.
[coreutils.git] / lib / linebuffer.c
blobc1a696acc9067ad9919ae71322a463d6f5b206da
1 /* linebuffer.c -- read arbitrarily long lines
2 Copyright (C) 1986, 1991, 1998 Free Software Foundation, Inc.
4 This program is free software; you can redistribute it and/or modify
5 it under the terms of the GNU General Public License as published by
6 the Free Software Foundation; either version 2, or (at your option)
7 any later version.
9 This program is distributed in the hope that it will be useful,
10 but WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 GNU General Public License for more details.
14 You should have received a copy of the GNU General Public License
15 along with this program; if not, write to the Free Software Foundation,
16 Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. */
18 /* Written by Richard Stallman. */
20 #ifdef HAVE_CONFIG_H
21 # include <config.h>
22 #endif
24 #include <stdio.h>
25 #include "linebuffer.h"
27 char *xmalloc ();
28 char *xrealloc ();
29 void free ();
31 /* Initialize linebuffer LINEBUFFER for use. */
33 void
34 initbuffer (struct linebuffer *linebuffer)
36 linebuffer->length = 0;
37 linebuffer->size = 200;
38 linebuffer->buffer = (char *) xmalloc (linebuffer->size);
41 /* Read an arbitrarily long line of text from STREAM into LINEBUFFER.
42 Remove any newline. Does not null terminate.
43 Return zero upon error or upon end of file.
44 Otherwise, return LINEBUFFER. */
46 struct linebuffer *
47 readline (struct linebuffer *linebuffer, FILE *stream)
49 int c;
50 char *buffer = linebuffer->buffer;
51 char *p = linebuffer->buffer;
52 char *end = buffer + linebuffer->size; /* Sentinel. */
54 if (feof (stream) || ferror (stream))
56 linebuffer->length = 0;
57 return 0;
60 while (1)
62 c = getc (stream);
63 if (p == end)
65 linebuffer->size *= 2;
66 buffer = (char *) xrealloc (buffer, linebuffer->size);
67 p += buffer - linebuffer->buffer;
68 linebuffer->buffer = buffer;
69 end = buffer + linebuffer->size;
71 if (c == EOF || c == '\n')
72 break;
73 *p++ = c;
76 if (feof (stream) && p == buffer)
78 linebuffer->length = 0;
79 return 0;
81 linebuffer->length = p - linebuffer->buffer;
82 return linebuffer;
85 /* Free linebuffer LINEBUFFER and its data, all allocated with malloc. */
87 void
88 freebuffer (struct linebuffer *linebuffer)
90 free (linebuffer->buffer);
91 free (linebuffer);