Mercurial > hg > octave-nkf > gnulib-hg
annotate lib/linebuffer.c @ 7302:8a1a9361108c
* _fpending.c: Include <config.h> unconditionally, since we no
longer worry about uses that don't define HAVE_CONFIG_H.
* acl.c, alloca.c, argmatch.c, atexit.c, backupfile.c:
* basename.c, c-stack.c, c-strtod.c, calloc.c, canon-host.c:
* canonicalize.c, chdir-long.c, chdir-safer.c, chown.c:
* cloexec.c, close-stream.c, closeout.c, creat-safer.c:
* cycle-check.c, diacrit.c, dirchownmod.c, dirfd.c, dirname.c:
* dup-safer.c, dup2.c, error.c, euidaccess.c, exclude.c:
* exitfail.c, fchmodat.c, fchown-stub.c, fd-safer.c:
* file-type.c, fileblocks.c, filemode.c, filenamecat.c:
* fnmatch.c, fopen-safer.c, fprintftime.c, free.c, fsusage.c:
* ftruncate.c, fts-cycle.c, fts.c, full-write.c, gai_strerror.c:
* getcwd.c, getdate.y, getdomainname.c, getgroups.c:
* gethostname.c, gethrxtime.c, getloadavg.c, getlogin_r.c:
* getndelim2.c, getnline.c, getopt.c, getopt1.c, getpass.c:
* gettime.c, gettimeofday.c, getugroups.c, getusershell.c:
* glob.c, group-member.c, hard-locale.c, hash-pjw.c, hash.c:
* human.c, idcache.c, inet_ntop.c, inet_pton.c, inttostr.c:
* isdir.c, lchown.c, linebuffer.c, long-options.c, lstat.c:
* malloc.c, md5.c, memcasecmp.c, memchr.c, memcmp.c, memcoll.c:
* memcpy.c, memmove.c, memrchr.c, mkancesdirs.c, mkdir-p.c:
* mkdir.c, mkdirat.c, mkstemp-safer.c, mkstemp.c, modechange.c:
* mountlist.c, nanosleep.c, obstack.c, open-safer.c:
* openat-die.c, openat.c, pagealign_alloc.c, physmem.c:
* pipe-safer.c, posixtm.c, posixver.c, putenv.c, quote.c:
* quotearg.c, raise.c, readtokens.c, readtokens0.c, readutmp.c:
* realloc.c, regex.c, rename.c, rmdir.c, rpmatch.c, safe-read.c:
* same.c, save-cwd.c, savedir.c, setenv.c, settime.c, sha1.c:
* sig2str.c, snprintf.c, strdup.c, strerror.c, strftime.c:
* stripslash.c, strndup.c, strnlen.c, strpbrk.c, strtod.c:
* strtoimax.c, strtol.c, strverscmp.c, tempname.c, time_r.c:
* timegm.c, tmpfile-safer.c, unlinkdir.c, userspec.c, utime.c:
* utimecmp.c, utimens.c, version-etc-fsf.c, version-etc.c:
* xalloc-die.c, xgetcwd.c, xgethostname.c, xmalloc.c:
* xmemcoll.c, xnanosleep.c, xreadlink.c, xstrtod.c:
* xstrtoimax.c, xstrtol.c, xstrtoumax.c, yesno.c:
Likewise.
author | Paul Eggert <eggert@cs.ucla.edu> |
---|---|
date | Wed, 13 Sep 2006 22:38:14 +0000 |
parents | a48fb0e98c8c |
children | 28d0ef9a0a36 |
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 |
7302
8a1a9361108c
* _fpending.c: Include <config.h> unconditionally, since we no
Paul Eggert <eggert@cs.ucla.edu>
parents:
5848
diff
changeset
|
3 Copyright (C) 1986, 1991, 1998, 1999, 2001, 2003, 2004, 2006 Free |
5318
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, |
5848
a48fb0e98c8c
*** empty log message ***
Paul Eggert <eggert@cs.ucla.edu>
parents:
5318
diff
changeset
|
18 Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. */ |
14 | 19 |
20 /* 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
|
21 |
8a1a9361108c
* _fpending.c: Include <config.h> unconditionally, since we no
Paul Eggert <eggert@cs.ucla.edu>
parents:
5848
diff
changeset
|
22 #include <config.h> |
1463
f8f00a1e2989
(readline): Return zero upon error as well as upon
Jim Meyering <jim@meyering.net>
parents:
14
diff
changeset
|
23 |
14 | 24 #include <stdio.h> |
4663 | 25 #include <stdlib.h> |
4835
a90f003e26e9
Include <string.h> for declaration of memset.
Jim Meyering <jim@meyering.net>
parents:
4827
diff
changeset
|
26 #include <string.h> |
1888
7bcd093998ec
Include <sys/types.h> now that linebuffer.h uses
Jim Meyering <jim@meyering.net>
parents:
1868
diff
changeset
|
27 #include <sys/types.h> |
14 | 28 #include "linebuffer.h" |
3678
72b3b19fece2
Remove explicit declarations of xmalloc and xrealloc,
Jim Meyering <jim@meyering.net>
parents:
3618
diff
changeset
|
29 #include "xalloc.h" |
14 | 30 |
5318
7c24a825b51d
Remove dependencies on unlocked-io.
Paul Eggert <eggert@cs.ucla.edu>
parents:
4835
diff
changeset
|
31 #if USE_UNLOCKED_IO |
7c24a825b51d
Remove dependencies on unlocked-io.
Paul Eggert <eggert@cs.ucla.edu>
parents:
4835
diff
changeset
|
32 # include "unlocked-io.h" |
7c24a825b51d
Remove dependencies on unlocked-io.
Paul Eggert <eggert@cs.ucla.edu>
parents:
4835
diff
changeset
|
33 #endif |
7c24a825b51d
Remove dependencies on unlocked-io.
Paul Eggert <eggert@cs.ucla.edu>
parents:
4835
diff
changeset
|
34 |
14 | 35 /* Initialize linebuffer LINEBUFFER for use. */ |
36 | |
37 void | |
1465 | 38 initbuffer (struct linebuffer *linebuffer) |
14 | 39 { |
4827
a6d03da0fa67
Simplify the code by using new xalloc.h features.
Paul Eggert <eggert@cs.ucla.edu>
parents:
4727
diff
changeset
|
40 memset (linebuffer, 0, sizeof *linebuffer); |
14 | 41 } |
42 | |
43 /* 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
|
44 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
|
45 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
|
46 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
|
47 (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
|
48 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
|
49 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
|
50 invoking ferror (stream). |
3678
72b3b19fece2
Remove explicit declarations of xmalloc and xrealloc,
Jim Meyering <jim@meyering.net>
parents:
3618
diff
changeset
|
51 Otherwise, return LINEBUFFER. */ |
14 | 52 struct linebuffer * |
4168
4d31b2ff9bab
(readlinebuffer): Renamed from readline.
Paul Eggert <eggert@cs.ucla.edu>
parents:
3678
diff
changeset
|
53 readlinebuffer (struct linebuffer *linebuffer, FILE *stream) |
14 | 54 { |
55 int c; | |
56 char *buffer = linebuffer->buffer; | |
57 char *p = linebuffer->buffer; | |
1991
e7ed29061994
(readline): Do not leave room for an extra
Jim Meyering <jim@meyering.net>
parents:
1888
diff
changeset
|
58 char *end = buffer + linebuffer->size; /* Sentinel. */ |
14 | 59 |
4713
c3e2b42bdca3
linebuffer.c (readlinebuffer): Return NULL immediately upon input error.
Paul Eggert <eggert@cs.ucla.edu>
parents:
4663
diff
changeset
|
60 if (feof (stream)) |
3678
72b3b19fece2
Remove explicit declarations of xmalloc and xrealloc,
Jim Meyering <jim@meyering.net>
parents:
3618
diff
changeset
|
61 return NULL; |
14 | 62 |
1866
ac8a74ce72cd
(readline): Append trailing newline to line.
Jim Meyering <jim@meyering.net>
parents:
1465
diff
changeset
|
63 do |
14 | 64 { |
65 c = getc (stream); | |
1866
ac8a74ce72cd
(readline): Append trailing newline to line.
Jim Meyering <jim@meyering.net>
parents:
1465
diff
changeset
|
66 if (c == EOF) |
ac8a74ce72cd
(readline): Append trailing newline to line.
Jim Meyering <jim@meyering.net>
parents:
1465
diff
changeset
|
67 { |
4713
c3e2b42bdca3
linebuffer.c (readlinebuffer): Return NULL immediately upon input error.
Paul Eggert <eggert@cs.ucla.edu>
parents:
4663
diff
changeset
|
68 if (p == buffer || ferror (stream)) |
3678
72b3b19fece2
Remove explicit declarations of xmalloc and xrealloc,
Jim Meyering <jim@meyering.net>
parents:
3618
diff
changeset
|
69 return NULL; |
1866
ac8a74ce72cd
(readline): Append trailing newline to line.
Jim Meyering <jim@meyering.net>
parents:
1465
diff
changeset
|
70 if (p[-1] == '\n') |
ac8a74ce72cd
(readline): Append trailing newline to line.
Jim Meyering <jim@meyering.net>
parents:
1465
diff
changeset
|
71 break; |
ac8a74ce72cd
(readline): Append trailing newline to line.
Jim Meyering <jim@meyering.net>
parents:
1465
diff
changeset
|
72 c = '\n'; |
ac8a74ce72cd
(readline): Append trailing newline to line.
Jim Meyering <jim@meyering.net>
parents:
1465
diff
changeset
|
73 } |
14 | 74 if (p == end) |
75 { | |
4827
a6d03da0fa67
Simplify the code by using new xalloc.h features.
Paul Eggert <eggert@cs.ucla.edu>
parents:
4727
diff
changeset
|
76 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
|
77 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
|
78 p = buffer + oldsize; |
14 | 79 linebuffer->buffer = buffer; |
1991
e7ed29061994
(readline): Do not leave room for an extra
Jim Meyering <jim@meyering.net>
parents:
1888
diff
changeset
|
80 end = buffer + linebuffer->size; |
14 | 81 } |
82 *p++ = c; | |
83 } | |
1866
ac8a74ce72cd
(readline): Append trailing newline to line.
Jim Meyering <jim@meyering.net>
parents:
1465
diff
changeset
|
84 while (c != '\n'); |
14 | 85 |
1866
ac8a74ce72cd
(readline): Append trailing newline to line.
Jim Meyering <jim@meyering.net>
parents:
1465
diff
changeset
|
86 linebuffer->length = p - buffer; |
14 | 87 return linebuffer; |
88 } | |
89 | |
4727
9c493e421b7f
(freebuffer): Don't free the argument, just the buffer associated with
Paul Eggert <eggert@cs.ucla.edu>
parents:
4713
diff
changeset
|
90 /* Free the buffer that was allocated for linebuffer LINEBUFFER. */ |
14 | 91 |
92 void | |
1465 | 93 freebuffer (struct linebuffer *linebuffer) |
14 | 94 { |
95 free (linebuffer->buffer); | |
96 } |