Mercurial > hg > octave-lojdl > gnulib-hg
annotate lib/linebuffer.c @ 5318:7c24a825b51d
Remove dependencies on unlocked-io.
author | Paul Eggert <eggert@cs.ucla.edu> |
---|---|
date | Mon, 04 Oct 2004 20:17:39 +0000 (2004-10-04) |
parents | a90f003e26e9 |
children | a48fb0e98c8c |
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 |
5318
7c24a825b51d
Remove dependencies on unlocked-io.
Paul Eggert <eggert@cs.ucla.edu>
parents:
4835
diff
changeset
|
3 Copyright (C) 1986, 1991, 1998, 1999, 2001, 2003, 2004 Free |
7c24a825b51d
Remove dependencies on unlocked-io.
Paul Eggert <eggert@cs.ucla.edu>
parents:
4835
diff
changeset
|
4 Software Foundation, Inc. |
14 | 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 | |
1464 | 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. */ | |
14 | 19 |
20 /* Written by Richard Stallman. */ | |
21 | |
1463
f8f00a1e2989
(readline): Return zero upon error as well as upon
Jim Meyering <jim@meyering.net>
parents:
14
diff
changeset
|
22 #ifdef HAVE_CONFIG_H |
f8f00a1e2989
(readline): Return zero upon error as well as upon
Jim Meyering <jim@meyering.net>
parents:
14
diff
changeset
|
23 # include <config.h> |
f8f00a1e2989
(readline): Return zero upon error as well as upon
Jim Meyering <jim@meyering.net>
parents:
14
diff
changeset
|
24 #endif |
f8f00a1e2989
(readline): Return zero upon error as well as upon
Jim Meyering <jim@meyering.net>
parents:
14
diff
changeset
|
25 |
14 | 26 #include <stdio.h> |
4663 | 27 #include <stdlib.h> |
4835
a90f003e26e9
Include <string.h> for declaration of memset.
Jim Meyering <jim@meyering.net>
parents:
4827
diff
changeset
|
28 #include <string.h> |
1888
7bcd093998ec
Include <sys/types.h> now that linebuffer.h uses
Jim Meyering <jim@meyering.net>
parents:
1868
diff
changeset
|
29 #include <sys/types.h> |
14 | 30 #include "linebuffer.h" |
3678
72b3b19fece2
Remove explicit declarations of xmalloc and xrealloc,
Jim Meyering <jim@meyering.net>
parents:
3618
diff
changeset
|
31 #include "xalloc.h" |
14 | 32 |
5318
7c24a825b51d
Remove dependencies on unlocked-io.
Paul Eggert <eggert@cs.ucla.edu>
parents:
4835
diff
changeset
|
33 #if USE_UNLOCKED_IO |
7c24a825b51d
Remove dependencies on unlocked-io.
Paul Eggert <eggert@cs.ucla.edu>
parents:
4835
diff
changeset
|
34 # include "unlocked-io.h" |
7c24a825b51d
Remove dependencies on unlocked-io.
Paul Eggert <eggert@cs.ucla.edu>
parents:
4835
diff
changeset
|
35 #endif |
7c24a825b51d
Remove dependencies on unlocked-io.
Paul Eggert <eggert@cs.ucla.edu>
parents:
4835
diff
changeset
|
36 |
14 | 37 /* Initialize linebuffer LINEBUFFER for use. */ |
38 | |
39 void | |
1465 | 40 initbuffer (struct linebuffer *linebuffer) |
14 | 41 { |
4827
a6d03da0fa67
Simplify the code by using new xalloc.h features.
Paul Eggert <eggert@cs.ucla.edu>
parents:
4727
diff
changeset
|
42 memset (linebuffer, 0, sizeof *linebuffer); |
14 | 43 } |
44 | |
45 /* Read an arbitrarily long line of text from STREAM into LINEBUFFER. | |
1866
ac8a74ce72cd
(readline): Append trailing newline to line.
Jim Meyering <jim@meyering.net>
parents:
1465
diff
changeset
|
46 Keep the newline; append a newline if it's the last line of a file |
1991
e7ed29061994
(readline): Do not leave room for an extra
Jim Meyering <jim@meyering.net>
parents:
1888
diff
changeset
|
47 that ends in a non-newline character. Do not null terminate. |
3678
72b3b19fece2
Remove explicit declarations of xmalloc and xrealloc,
Jim Meyering <jim@meyering.net>
parents:
3618
diff
changeset
|
48 Therefore the stream can contain NUL bytes, and the length |
72b3b19fece2
Remove explicit declarations of xmalloc and xrealloc,
Jim Meyering <jim@meyering.net>
parents:
3618
diff
changeset
|
49 (including the newline) 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
|
50 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
|
51 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
|
52 invoking ferror (stream). |
3678
72b3b19fece2
Remove explicit declarations of xmalloc and xrealloc,
Jim Meyering <jim@meyering.net>
parents:
3618
diff
changeset
|
53 Otherwise, return LINEBUFFER. */ |
14 | 54 struct linebuffer * |
4168
4d31b2ff9bab
(readlinebuffer): Renamed from readline.
Paul Eggert <eggert@cs.ucla.edu>
parents:
3678
diff
changeset
|
55 readlinebuffer (struct linebuffer *linebuffer, FILE *stream) |
14 | 56 { |
57 int c; | |
58 char *buffer = linebuffer->buffer; | |
59 char *p = linebuffer->buffer; | |
1991
e7ed29061994
(readline): Do not leave room for an extra
Jim Meyering <jim@meyering.net>
parents:
1888
diff
changeset
|
60 char *end = buffer + linebuffer->size; /* Sentinel. */ |
14 | 61 |
4713
c3e2b42bdca3
linebuffer.c (readlinebuffer): Return NULL immediately upon input error.
Paul Eggert <eggert@cs.ucla.edu>
parents:
4663
diff
changeset
|
62 if (feof (stream)) |
3678
72b3b19fece2
Remove explicit declarations of xmalloc and xrealloc,
Jim Meyering <jim@meyering.net>
parents:
3618
diff
changeset
|
63 return NULL; |
14 | 64 |
1866
ac8a74ce72cd
(readline): Append trailing newline to line.
Jim Meyering <jim@meyering.net>
parents:
1465
diff
changeset
|
65 do |
14 | 66 { |
67 c = getc (stream); | |
1866
ac8a74ce72cd
(readline): Append trailing newline to line.
Jim Meyering <jim@meyering.net>
parents:
1465
diff
changeset
|
68 if (c == EOF) |
ac8a74ce72cd
(readline): Append trailing newline to line.
Jim Meyering <jim@meyering.net>
parents:
1465
diff
changeset
|
69 { |
4713
c3e2b42bdca3
linebuffer.c (readlinebuffer): Return NULL immediately upon input error.
Paul Eggert <eggert@cs.ucla.edu>
parents:
4663
diff
changeset
|
70 if (p == buffer || ferror (stream)) |
3678
72b3b19fece2
Remove explicit declarations of xmalloc and xrealloc,
Jim Meyering <jim@meyering.net>
parents:
3618
diff
changeset
|
71 return NULL; |
1866
ac8a74ce72cd
(readline): Append trailing newline to line.
Jim Meyering <jim@meyering.net>
parents:
1465
diff
changeset
|
72 if (p[-1] == '\n') |
ac8a74ce72cd
(readline): Append trailing newline to line.
Jim Meyering <jim@meyering.net>
parents:
1465
diff
changeset
|
73 break; |
ac8a74ce72cd
(readline): Append trailing newline to line.
Jim Meyering <jim@meyering.net>
parents:
1465
diff
changeset
|
74 c = '\n'; |
ac8a74ce72cd
(readline): Append trailing newline to line.
Jim Meyering <jim@meyering.net>
parents:
1465
diff
changeset
|
75 } |
14 | 76 if (p == end) |
77 { | |
4827
a6d03da0fa67
Simplify the code by using new xalloc.h features.
Paul Eggert <eggert@cs.ucla.edu>
parents:
4727
diff
changeset
|
78 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
|
79 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
|
80 p = buffer + oldsize; |
14 | 81 linebuffer->buffer = buffer; |
1991
e7ed29061994
(readline): Do not leave room for an extra
Jim Meyering <jim@meyering.net>
parents:
1888
diff
changeset
|
82 end = buffer + linebuffer->size; |
14 | 83 } |
84 *p++ = c; | |
85 } | |
1866
ac8a74ce72cd
(readline): Append trailing newline to line.
Jim Meyering <jim@meyering.net>
parents:
1465
diff
changeset
|
86 while (c != '\n'); |
14 | 87 |
1866
ac8a74ce72cd
(readline): Append trailing newline to line.
Jim Meyering <jim@meyering.net>
parents:
1465
diff
changeset
|
88 linebuffer->length = p - buffer; |
14 | 89 return linebuffer; |
90 } | |
91 | |
4727
9c493e421b7f
(freebuffer): Don't free the argument, just the buffer associated with
Paul Eggert <eggert@cs.ucla.edu>
parents:
4713
diff
changeset
|
92 /* Free the buffer that was allocated for linebuffer LINEBUFFER. */ |
14 | 93 |
94 void | |
1465 | 95 freebuffer (struct linebuffer *linebuffer) |
14 | 96 { |
97 free (linebuffer->buffer); | |
98 } |