From 7453ffb6a86769bd22ce01da43fbbe919bef861d Mon Sep 17 00:00:00 2001 From: Christopher James Lahey Date: Sat, 6 May 2000 17:04:10 +0000 Subject: Got rid of some warnings. 2000-05-06 Christopher James Lahey * e-html-utils.c: Got rid of some warnings. * e-util.c, e-util.h: Added e_read_file which takes a filename and returns a newly allocated string containing the contents of that file. svn path=/trunk/; revision=2828 --- e-util/e-util.c | 52 ++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 52 insertions(+) (limited to 'e-util/e-util.c') diff --git a/e-util/e-util.c b/e-util/e-util.c index f2d787f37e..d5603d3dfa 100644 --- a/e-util/e-util.c +++ b/e-util/e-util.c @@ -22,6 +22,9 @@ #include #include +#include +#include +#include #include "e-util.h" @@ -52,3 +55,52 @@ e_free_object_list (GList *list) g_list_free (list); } + +#define BUFF_SIZE 1024 + +char * +e_read_file(const char *filename) +{ + int fd; + char buffer[BUFF_SIZE]; + GList *list = NULL, *list_iterator; + GList *lengths = NULL, *lengths_iterator; + int length = 0; + int bytes; + char *ret_val; + + fd = open(filename, O_RDONLY); + if (fd == -1) + return NULL; + bytes = read(fd, buffer, BUFF_SIZE); + while (bytes) { + if (bytes > 0) { + list = g_list_prepend(list, g_strndup(buffer, bytes)); + lengths = g_list_prepend(lengths, GINT_TO_POINTER(bytes)); + length += bytes; + } else { + if (errno != EINTR) { + close(fd); + g_list_foreach(list, (GFunc) g_free, NULL); + g_list_free(list); + g_list_free(lengths); + return NULL; + } + } + bytes = read(fd, buffer, BUFF_SIZE); + } + ret_val = g_new(char, length + 1); + ret_val[length] = 0; + lengths_iterator = lengths; + list_iterator = list; + for ( ; list_iterator; list_iterator = list_iterator->next, lengths_iterator = lengths_iterator->next) { + int this_length = GPOINTER_TO_INT(lengths_iterator->data); + length -= this_length; + memcpy(ret_val + length, list_iterator->data, this_length); + } + close(fd); + g_list_foreach(list, (GFunc) g_free, NULL); + g_list_free(list); + g_list_free(lengths); + return ret_val; +} -- cgit v1.2.3