Mercurial > hg > octave-nkf > gnulib-hg
annotate lib/linebuffer.c @ 9928:9a02133ad731
Add tentative support for Linux libc5.
author | Bruno Haible <bruno@clisp.org> |
---|---|
date | Thu, 17 Apr 2008 02:01:23 +0200 |
parents | bbbbbf4cd1c5 |
children | e8d2c6fc33ad |
rev | line source |
---|---|
14 | 1 /* linebuffer.c -- read arbitrarily long lines |
4168
4d31b2ff9bab
(readlinebuffer): Renamed from readline.
Paul Eggert <eggert@cs.ucla.edu>
parents:
3678
diff
changeset
|
2 |
8812
28d0ef9a0a36
* lib/linebuffer.c (readlinebuffer_delim): New function,
Jim Meyering <jim@meyering.net>
parents:
7302
diff
changeset
|
3 Copyright (C) 1986, 1991, 1998, 1999, 2001, 2003, 2004, 2006, 2007 |
28d0ef9a0a36
* lib/linebuffer.c (readlinebuffer_delim): New function,
Jim Meyering <jim@meyering.net>
parents:
7302
diff
changeset
|
4 Free Software Foundation, Inc. |
14 | 5 |
9309
bbbbbf4cd1c5
Change copyright notice from GPLv2+ to GPLv3+.
Bruno Haible <bruno@clisp.org>
parents:
8816
diff
changeset
|
6 This program is free software: you can redistribute it and/or modify |
14 | 7 it under the terms of the GNU General Public License as published by |
9309
bbbbbf4cd1c5
Change copyright notice from GPLv2+ to GPLv3+.
Bruno Haible <bruno@clisp.org>
parents:
8816
diff
changeset
|
8 the Free Software Foundation; either version 3 of the License, or |
bbbbbf4cd1c5
Change copyright notice from GPLv2+ to GPLv3+.
Bruno Haible <bruno@clisp.org>
parents:
8816
diff
changeset
|
9 (at your option) any later version. |
14 | 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 | |
9309
bbbbbf4cd1c5
Change copyright notice from GPLv2+ to GPLv3+.
Bruno Haible <bruno@clisp.org>
parents:
8816
diff
changeset
|
17 along with this program. If not, see <http://www.gnu.org/licenses/>. */ |
14 | 18 |
19 /* Written by Richard Stallman. */ | |
7302
8a1a9361108c
* _fpending.c: Include <config.h> unconditionally, since we no
Paul Eggert <eggert@cs.ucla.edu>
parents:
5848
diff
changeset
|
20 |
8a1a9361108c
* _fpending.c: Include <config.h> unconditionally, since we no
Paul Eggert <eggert@cs.ucla.edu>
parents:
5848
diff
changeset
|
21 #include <config.h> |
1463
f8f00a1e2989
(readline): Return zero upon error as well as upon
Jim Meyering <jim@meyering.net>
parents:
14
diff
changeset
|
22 |
14 | 23 #include <stdio.h> |
4663 | 24 #include <stdlib.h> |
4835
a90f003e26e9
Include <string.h> for declaration of memset.
Jim Meyering <jim@meyering.net>
parents:
4827
diff
changeset
|
25 #include <string.h> |
1888
7bcd093998ec
Include <sys/types.h> now that linebuffer.h uses
Jim Meyering <jim@meyering.net>
parents:
1868
diff
changeset
|
26 #include <sys/types.h> |
14 | 27 #include "linebuffer.h" |
3678
72b3b19fece2
Remove explicit declarations of xmalloc and xrealloc,
Jim Meyering <jim@meyering.net>
parents:
3618
diff
changeset
|
28 #include "xalloc.h" |
14 | 29 |
5318
7c24a825b51d
Remove dependencies on unlocked-io.
Paul Eggert <eggert@cs.ucla.edu>
parents:
4835
diff
changeset
|
30 #if USE_UNLOCKED_IO |
7c24a825b51d
Remove dependencies on unlocked-io.
Paul Eggert <eggert@cs.ucla.edu>
parents:
4835
diff
changeset
|
31 # include "unlocked-io.h" |
7c24a825b51d
Remove dependencies on unlocked-io.
Paul Eggert <eggert@cs.ucla.edu>
parents:
4835
diff
changeset
|
32 #endif |
7c24a825b51d
Remove dependencies on unlocked-io.
Paul Eggert <eggert@cs.ucla.edu>
parents:
4835
diff
changeset
|
33 |
14 | 34 /* Initialize linebuffer LINEBUFFER for use. */ |
35 | |
36 void | |
1465 | 37 initbuffer (struct linebuffer *linebuffer) |
14 | 38 { |
4827
a6d03da0fa67
Simplify the code by using new xalloc.h features.
Paul Eggert <eggert@cs.ucla.edu>
parents:
4727
diff
changeset
|
39 memset (linebuffer, 0, sizeof *linebuffer); |
14 | 40 } |
41 | |
8812
28d0ef9a0a36
* lib/linebuffer.c (readlinebuffer_delim): New function,
Jim Meyering <jim@meyering.net>
parents:
7302
diff
changeset
|
42 struct linebuffer * |
28d0ef9a0a36
* lib/linebuffer.c (readlinebuffer_delim): New function,
Jim Meyering <jim@meyering.net>
parents:
7302
diff
changeset
|
43 readlinebuffer (struct linebuffer *linebuffer, FILE *stream) |
28d0ef9a0a36
* lib/linebuffer.c (readlinebuffer_delim): New function,
Jim Meyering <jim@meyering.net>
parents:
7302
diff
changeset
|
44 { |
28d0ef9a0a36
* lib/linebuffer.c (readlinebuffer_delim): New function,
Jim Meyering <jim@meyering.net>
parents:
7302
diff
changeset
|
45 return readlinebuffer_delim (linebuffer, stream, '\n'); |
28d0ef9a0a36
* lib/linebuffer.c (readlinebuffer_delim): New function,
Jim Meyering <jim@meyering.net>
parents:
7302
diff
changeset
|
46 } |
28d0ef9a0a36
* lib/linebuffer.c (readlinebuffer_delim): New function,
Jim Meyering <jim@meyering.net>
parents:
7302
diff
changeset
|
47 |
14 | 48 /* Read an arbitrarily long line of text from STREAM into LINEBUFFER. |
8816 | 49 Consider lines to be terminated by DELIMITER. |
8812
28d0ef9a0a36
* lib/linebuffer.c (readlinebuffer_delim): New function,
Jim Meyering <jim@meyering.net>
parents:
7302
diff
changeset
|
50 Keep the delimiter; append DELIMITER if it's the last line of a file |
8816 | 51 that ends in a character other than DELIMITER. Do not NUL-terminate. |
3678
72b3b19fece2
Remove explicit declarations of xmalloc and xrealloc,
Jim Meyering <jim@meyering.net>
parents:
3618
diff
changeset
|
52 Therefore the stream can contain NUL bytes, and the length |
8812
28d0ef9a0a36
* lib/linebuffer.c (readlinebuffer_delim): New function,
Jim Meyering <jim@meyering.net>
parents:
7302
diff
changeset
|
53 (including the delimiter) is returned in linebuffer->length. |
4713
c3e2b42bdca3
linebuffer.c (readlinebuffer): Return NULL immediately upon input error.
Paul Eggert <eggert@cs.ucla.edu>
parents:
4663
diff
changeset
|
54 Return NULL when stream is empty. Return NULL and set errno upon |
c3e2b42bdca3
linebuffer.c (readlinebuffer): Return NULL immediately upon input error.
Paul Eggert <eggert@cs.ucla.edu>
parents:
4663
diff
changeset
|
55 error; callers can distinguish this case from the empty case by |
c3e2b42bdca3
linebuffer.c (readlinebuffer): Return NULL immediately upon input error.
Paul Eggert <eggert@cs.ucla.edu>
parents:
4663
diff
changeset
|
56 invoking ferror (stream). |
3678
72b3b19fece2
Remove explicit declarations of xmalloc and xrealloc,
Jim Meyering <jim@meyering.net>
parents:
3618
diff
changeset
|
57 Otherwise, return LINEBUFFER. */ |
14 | 58 struct linebuffer * |
8812
28d0ef9a0a36
* lib/linebuffer.c (readlinebuffer_delim): New function,
Jim Meyering <jim@meyering.net>
parents:
7302
diff
changeset
|
59 readlinebuffer_delim (struct linebuffer *linebuffer, FILE *stream, |
28d0ef9a0a36
* lib/linebuffer.c (readlinebuffer_delim): New function,
Jim Meyering <jim@meyering.net>
parents:
7302
diff
changeset
|
60 char delimiter) |
14 | 61 { |
62 int c; | |
63 char *buffer = linebuffer->buffer; | |
64 char *p = linebuffer->buffer; | |
1991
e7ed29061994
(readline): Do not leave room for an extra
Jim Meyering <jim@meyering.net>
parents:
1888
diff
changeset
|
65 char *end = buffer + linebuffer->size; /* Sentinel. */ |
14 | 66 |
4713
c3e2b42bdca3
linebuffer.c (readlinebuffer): Return NULL immediately upon input error.
Paul Eggert <eggert@cs.ucla.edu>
parents:
4663
diff
changeset
|
67 if (feof (stream)) |
3678
72b3b19fece2
Remove explicit declarations of xmalloc and xrealloc,
Jim Meyering <jim@meyering.net>
parents:
3618
diff
changeset
|
68 return NULL; |
14 | 69 |
1866
ac8a74ce72cd
(readline): Append trailing newline to line.
Jim Meyering <jim@meyering.net>
parents:
1465
diff
changeset
|
70 do |
14 | 71 { |
72 c = getc (stream); | |
1866
ac8a74ce72cd
(readline): Append trailing newline to line.
Jim Meyering <jim@meyering.net>
parents:
1465
diff
changeset
|
73 if (c == EOF) |
ac8a74ce72cd
(readline): Append trailing newline to line.
Jim Meyering <jim@meyering.net>
parents:
1465
diff
changeset
|
74 { |
4713
c3e2b42bdca3
linebuffer.c (readlinebuffer): Return NULL immediately upon input error.
Paul Eggert <eggert@cs.ucla.edu>
parents:
4663
diff
changeset
|
75 if (p == buffer || ferror (stream)) |
3678
72b3b19fece2
Remove explicit declarations of xmalloc and xrealloc,
Jim Meyering <jim@meyering.net>
parents:
3618
diff
changeset
|
76 return NULL; |
8812
28d0ef9a0a36
* lib/linebuffer.c (readlinebuffer_delim): New function,
Jim Meyering <jim@meyering.net>
parents:
7302
diff
changeset
|
77 if (p[-1] == delimiter) |
1866
ac8a74ce72cd
(readline): Append trailing newline to line.
Jim Meyering <jim@meyering.net>
parents:
1465
diff
changeset
|
78 break; |
8812
28d0ef9a0a36
* lib/linebuffer.c (readlinebuffer_delim): New function,
Jim Meyering <jim@meyering.net>
parents:
7302
diff
changeset
|
79 c = delimiter; |
1866
ac8a74ce72cd
(readline): Append trailing newline to line.
Jim Meyering <jim@meyering.net>
parents:
1465
diff
changeset
|
80 } |
14 | 81 if (p == end) |
82 { | |
4827
a6d03da0fa67
Simplify the code by using new xalloc.h features.
Paul Eggert <eggert@cs.ucla.edu>
parents:
4727
diff
changeset
|
83 size_t oldsize = linebuffer->size; |
a6d03da0fa67
Simplify the code by using new xalloc.h features.
Paul Eggert <eggert@cs.ucla.edu>
parents:
4727
diff
changeset
|
84 buffer = x2realloc (buffer, &linebuffer->size); |
a6d03da0fa67
Simplify the code by using new xalloc.h features.
Paul Eggert <eggert@cs.ucla.edu>
parents:
4727
diff
changeset
|
85 p = buffer + oldsize; |
14 | 86 linebuffer->buffer = buffer; |
1991
e7ed29061994
(readline): Do not leave room for an extra
Jim Meyering <jim@meyering.net>
parents:
1888
diff
changeset
|
87 end = buffer + linebuffer->size; |
14 | 88 } |
89 *p++ = c; | |
90 } | |
8812
28d0ef9a0a36
* lib/linebuffer.c (readlinebuffer_delim): New function,
Jim Meyering <jim@meyering.net>
parents:
7302
diff
changeset
|
91 while (c != delimiter); |
14 | 92 |
1866
ac8a74ce72cd
(readline): Append trailing newline to line.
Jim Meyering <jim@meyering.net>
parents:
1465
diff
changeset
|
93 linebuffer->length = p - buffer; |
14 | 94 return linebuffer; |
95 } | |
96 | |
4727
9c493e421b7f
(freebuffer): Don't free the argument, just the buffer associated with
Paul Eggert <eggert@cs.ucla.edu>
parents:
4713
diff
changeset
|
97 /* Free the buffer that was allocated for linebuffer LINEBUFFER. */ |
14 | 98 |
99 void | |
1465 | 100 freebuffer (struct linebuffer *linebuffer) |
14 | 101 { |
102 free (linebuffer->buffer); | |
103 } |