diff libcore/core/util.c @ 198:d6f217a5e4b1

molko-js: add support for modules, closes #2514 @1h
author David Demelier <markand@malikania.fr>
date Mon, 09 Nov 2020 13:24:49 +0100
parents 7103d6574062
children befa2e855d3b
line wrap: on
line diff
--- a/libcore/core/util.c	Mon Nov 09 10:37:36 2020 +0100
+++ b/libcore/core/util.c	Mon Nov 09 13:24:49 2020 +0100
@@ -16,11 +16,17 @@
  * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
  */
 
+#include <sys/stat.h>
 #include <assert.h>
+#include <errno.h>
+#include <fcntl.h>
 #include <stdlib.h>
+#include <unistd.h>
 
 #include <SDL.h>
 
+#include "alloc.h"
+#include "error.h"
 #include "util.h"
 
 void
@@ -29,6 +35,33 @@
 	SDL_Delay(ms);
 }
 
+char *
+readall(const char *path)
+{
+	int fd;
+	struct stat st;
+	char *str = NULL;
+	ssize_t nr;
+
+	if ((fd = open(path, O_RDONLY)) < 0 || fstat(fd, &st) < 0)
+		goto io_error;
+	if (!(str = alloc_zero(1, st.st_size + 1)))
+		goto alloc_error;
+	if ((nr = read(fd, str, st.st_size)) != st.st_size)
+		goto io_error;
+
+	return str;
+
+io_error:
+	errorf("%s", strerror(errno));
+
+alloc_error:
+	close(fd);
+	free(str);
+
+	return NULL;
+}
+
 unsigned int
 nrand(unsigned int lower, unsigned int upper)
 {