aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorGlenn L McGrath <bug1@ihug.co.nz>2001-04-15 12:36:19 +0000
committerGlenn L McGrath <bug1@ihug.co.nz>2001-04-15 12:36:19 +0000
commit65708e4cd1b234501aec3af0970f21ad6736aa44 (patch)
tree8575911dae91c9e9c9f032e9a37c869be9159b0f
parent4a2e4635253e615678cea6f2c253485d26dde73a (diff)
downloadbusybox-w32-65708e4cd1b234501aec3af0970f21ad6736aa44.tar.gz
busybox-w32-65708e4cd1b234501aec3af0970f21ad6736aa44.tar.bz2
busybox-w32-65708e4cd1b234501aec3af0970f21ad6736aa44.zip
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.c38
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 */
10extern 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