diff options
author | Glenn L McGrath <bug1@ihug.co.nz> | 2001-04-15 12:36:19 +0000 |
---|---|---|
committer | Glenn L McGrath <bug1@ihug.co.nz> | 2001-04-15 12:36:19 +0000 |
commit | 65708e4cd1b234501aec3af0970f21ad6736aa44 (patch) | |
tree | 8575911dae91c9e9c9f032e9a37c869be9159b0f | |
parent | 4a2e4635253e615678cea6f2c253485d26dde73a (diff) | |
download | busybox-65708e4cd1b234501aec3af0970f21ad6736aa44.tar.gz |
Read a FILE* till an empty line or eof and return it as a char buffer.
In future maybe add char *end_str to interface to allow calling function
to specify end point.
-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 @@ +#include <stdio.h> +#include <stdlib.h> +#include <string.h> +#include "libbb.h" + +/* + * Reads consecutive lines from file line that start with end_string + * read finishes at an empty line or eof + */ +extern char *read_text_file_to_buffer(FILE *src_file) +{ + char *line = NULL; + char *buffer = NULL; + int buffer_length = 0; + int line_length = 0; + + buffer = xmalloc(1); + strcpy(buffer, ""); + + /* Loop until line is empty, or just one char, which will be '\n' */ + do { + line = get_line_from_file(src_file); + if (line == NULL) { + break; + } + line_length = strlen(line); + buffer_length += line_length + 1; + buffer = (char *) xrealloc(buffer, buffer_length + 1); + strcat(buffer, line); + free(line); + } while (line_length > 1); + + if (strlen(buffer) == 0) { + return(NULL); + } else { + return(strdup(buffer)); + } +}
\ No newline at end of file |