diff options
-rw-r--r-- | libbb/read_text_file_to_buffer.c | 38 |
1 files changed, 38 insertions, 0 deletions
diff --git a/libbb/read_text_file_to_buffer.c b/libbb/read_text_file_to_buffer.c new file mode 100644 index 000000000..ef64ad712 --- /dev/null +++ b/libbb/read_text_file_to_buffer.c | |||
@@ -0,0 +1,38 @@ | |||
1 | #include <stdio.h> | ||
2 | #include <stdlib.h> | ||
3 | #include <string.h> | ||
4 | #include "libbb.h" | ||
5 | |||
6 | /* | ||
7 | * Reads consecutive lines from file line that start with end_string | ||
8 | * read finishes at an empty line or eof | ||
9 | */ | ||
10 | extern char *read_text_file_to_buffer(FILE *src_file) | ||
11 | { | ||
12 | char *line = NULL; | ||
13 | char *buffer = NULL; | ||
14 | int buffer_length = 0; | ||
15 | int line_length = 0; | ||
16 | |||
17 | buffer = xmalloc(1); | ||
18 | strcpy(buffer, ""); | ||
19 | |||
20 | /* Loop until line is empty, or just one char, which will be '\n' */ | ||
21 | do { | ||
22 | line = get_line_from_file(src_file); | ||
23 | if (line == NULL) { | ||
24 | break; | ||
25 | } | ||
26 | line_length = strlen(line); | ||
27 | buffer_length += line_length + 1; | ||
28 | buffer = (char *) xrealloc(buffer, buffer_length + 1); | ||
29 | strcat(buffer, line); | ||
30 | free(line); | ||
31 | } while (line_length > 1); | ||
32 | |||
33 | if (strlen(buffer) == 0) { | ||
34 | return(NULL); | ||
35 | } else { | ||
36 | return(strdup(buffer)); | ||
37 | } | ||
38 | } \ No newline at end of file | ||