aboutsummaryrefslogtreecommitdiff
path: root/src/utils/file.c
diff options
context:
space:
mode:
authorA404M <ahmadmahmoudiprogrammer@gmail.com>2024-09-18 19:46:38 +0330
committerA404M <ahmadmahmoudiprogrammer@gmail.com>2024-09-18 19:57:20 +0330
commitd6ba30b94a24607bce5db5e706eb20cc051a98f0 (patch)
tree146e74b0bc2e1636451257015210e3c7d1a0ecab /src/utils/file.c
initial commit
Diffstat (limited to 'src/utils/file.c')
-rw-r--r--src/utils/file.c22
1 files changed, 22 insertions, 0 deletions
diff --git a/src/utils/file.c b/src/utils/file.c
new file mode 100644
index 0000000..410c218
--- /dev/null
+++ b/src/utils/file.c
@@ -0,0 +1,22 @@
+#include "file.h"
+#include <stdio.h>
+#include <utils/memory/memory.h>
+
+char *read_whole_file(const char *path) {
+ FILE *file = fopen(path, "r");
+ if (!file) {
+ fprintf(stderr, "could not open file at path '%s'\n", path);
+ return NULL;
+ }
+ fseek(file, 0, SEEK_END);
+ const size_t file_size = ftell(file);
+ fseek(file, 0, SEEK_SET);
+
+ char *str = a404m_malloc((file_size + 1) * sizeof(char));
+
+ fread(str, file_size, 1, file);
+ str[file_size] = '\0';
+
+ fclose(file);
+ return str;
+}