aboutsummaryrefslogtreecommitdiff
path: root/src/system
diff options
context:
space:
mode:
authordec05eba <dec05eba@protonmail.com>2021-10-30 04:22:07 +0200
committerdec05eba <dec05eba@protonmail.com>2021-10-30 04:22:07 +0200
commit8a316a12481282cb2ab966c4cbc770656c258383 (patch)
tree678b4efcaa4e47cb4f4d2bd29c90badbeac8789c /src/system
parent275e851e8a2b11c2efe7c39787bf331dd3b75272 (diff)
Add option to make mgl_file_load result null terminated
Diffstat (limited to 'src/system')
-rw-r--r--src/system/fileutils.c9
1 files changed, 7 insertions, 2 deletions
diff --git a/src/system/fileutils.c b/src/system/fileutils.c
index 7bd6859..dc8eaca 100644
--- a/src/system/fileutils.c
+++ b/src/system/fileutils.c
@@ -4,7 +4,9 @@
#include <unistd.h>
#include <stdlib.h>
-int mgl_load_file(const char *filepath, mgl_filedata *filedata) {
+int mgl_load_file(const char *filepath, mgl_filedata *filedata, mgl_file_load_options *load_options) {
+ const bool null_terminated = load_options ? load_options->null_terminated : false;
+
int fd = open(filepath, O_RDONLY);
if(fd == -1)
return -1;
@@ -20,7 +22,7 @@ int mgl_load_file(const char *filepath, mgl_filedata *filedata) {
return -1;
}
- unsigned char *data = malloc(st.st_size);
+ unsigned char *data = malloc(st.st_size + (null_terminated ? 1 : 0));
if(!data) {
close(fd);
return -1;
@@ -32,6 +34,9 @@ int mgl_load_file(const char *filepath, mgl_filedata *filedata) {
return -1;
}
+ if(null_terminated)
+ data[st.st_size] = '\0';
+
filedata->data = data;
filedata->size = st.st_size;