diff options
author | Manuel Novoa III <mjn3@codepoet.org> | 2003-03-19 09:13:01 +0000 |
---|---|---|
committer | Manuel Novoa III <mjn3@codepoet.org> | 2003-03-19 09:13:01 +0000 |
commit | cad5364599eb5062d59e0c397ed638ddd61a8d5d (patch) | |
tree | a318d0f03aa076c74b576ea45dc543a5669e8e91 /libbb/get_line_from_file.c | |
parent | e01f9662a5bd5d91be4f6b3941b57fff73cd5af1 (diff) | |
download | busybox-w32-cad5364599eb5062d59e0c397ed638ddd61a8d5d.tar.gz busybox-w32-cad5364599eb5062d59e0c397ed638ddd61a8d5d.tar.bz2 busybox-w32-cad5364599eb5062d59e0c397ed638ddd61a8d5d.zip |
Major coreutils update.
Diffstat (limited to 'libbb/get_line_from_file.c')
-rw-r--r-- | libbb/get_line_from_file.c | 48 |
1 files changed, 32 insertions, 16 deletions
diff --git a/libbb/get_line_from_file.c b/libbb/get_line_from_file.c index 5e7062127..5af898934 100644 --- a/libbb/get_line_from_file.c +++ b/libbb/get_line_from_file.c | |||
@@ -21,41 +21,57 @@ | |||
21 | */ | 21 | */ |
22 | 22 | ||
23 | #include <stdio.h> | 23 | #include <stdio.h> |
24 | #include <stdlib.h> | ||
24 | #include "libbb.h" | 25 | #include "libbb.h" |
25 | 26 | ||
26 | 27 | /* get_line_from_file() - This function reads an entire line from a text file, | |
27 | |||
28 | /* get_line_from_file() - This function reads an entire line from a text file | ||
29 | * up to a newline. It returns a malloc'ed char * which must be stored and | 28 | * up to a newline. It returns a malloc'ed char * which must be stored and |
30 | * free'ed by the caller. */ | 29 | * free'ed by the caller. If 'c' is nonzero, the trailing '\n' (if any) |
31 | extern char *get_line_from_file(FILE *file) | 30 | * is removed. In event of a read error or EOF, NULL is returned. */ |
31 | |||
32 | static char *private_get_line_from_file(FILE *file, int c) | ||
32 | { | 33 | { |
33 | static const int GROWBY = 80; /* how large we will grow strings by */ | 34 | #define GROWBY (80) /* how large we will grow strings by */ |
34 | 35 | ||
35 | int ch; | 36 | int ch; |
36 | int idx = 0; | 37 | int idx = 0; |
37 | char *linebuf = NULL; | 38 | char *linebuf = NULL; |
38 | int linebufsz = 0; | 39 | int linebufsz = 0; |
39 | 40 | ||
40 | while (1) { | 41 | while ((ch = getc(file)) != EOF) { |
41 | ch = fgetc(file); | ||
42 | if (ch == EOF) | ||
43 | break; | ||
44 | /* grow the line buffer as necessary */ | 42 | /* grow the line buffer as necessary */ |
45 | while (idx > linebufsz-2) | 43 | if (idx > linebufsz-2) { |
46 | linebuf = xrealloc(linebuf, linebufsz += GROWBY); | 44 | linebuf = xrealloc(linebuf, linebufsz += GROWBY); |
45 | } | ||
47 | linebuf[idx++] = (char)ch; | 46 | linebuf[idx++] = (char)ch; |
48 | if (ch == '\n' || ch == '\0') | 47 | if (ch == '\n' || ch == '\0') { |
48 | if (c) { | ||
49 | --idx; | ||
50 | } | ||
49 | break; | 51 | break; |
52 | } | ||
50 | } | 53 | } |
51 | 54 | ||
52 | if (idx == 0) | 55 | if (linebuf) { |
53 | return NULL; | 56 | if (ferror(file)) { |
54 | 57 | free(linebuf); | |
55 | linebuf[idx] = 0; | 58 | return NULL; |
59 | } | ||
60 | linebuf[idx] = 0; | ||
61 | } | ||
56 | return linebuf; | 62 | return linebuf; |
57 | } | 63 | } |
58 | 64 | ||
65 | extern char *bb_get_line_from_file(FILE *file) | ||
66 | { | ||
67 | return private_get_line_from_file(file, 0); | ||
68 | } | ||
69 | |||
70 | extern char *bb_get_chomped_line_from_file(FILE *file) | ||
71 | { | ||
72 | return private_get_line_from_file(file, 1); | ||
73 | } | ||
74 | |||
59 | 75 | ||
60 | /* END CODE */ | 76 | /* END CODE */ |
61 | /* | 77 | /* |