aboutsummaryrefslogtreecommitdiff
path: root/libbb
diff options
context:
space:
mode:
authorbug1 <bug1@69ca8d6d-28ef-0310-b511-8ec308f3f277>2001-04-15 12:36:19 +0000
committerbug1 <bug1@69ca8d6d-28ef-0310-b511-8ec308f3f277>2001-04-15 12:36:19 +0000
commit7cbe9cb95c905642c199c5ff1ad3eef63d89eda2 (patch)
tree8575911dae91c9e9c9f032e9a37c869be9159b0f /libbb
parentaf79176198d64feb4b1791b4b2f7c1a36896403f (diff)
downloadbusybox-w32-7cbe9cb95c905642c199c5ff1ad3eef63d89eda2.tar.gz
busybox-w32-7cbe9cb95c905642c199c5ff1ad3eef63d89eda2.tar.bz2
busybox-w32-7cbe9cb95c905642c199c5ff1ad3eef63d89eda2.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. git-svn-id: svn://busybox.net/trunk/busybox@2345 69ca8d6d-28ef-0310-b511-8ec308f3f277
Diffstat (limited to 'libbb')
-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