diff options
| author | David Moc <personal@cdatgoose.org> | 2026-05-30 21:53:05 +0200 |
|---|---|---|
| committer | David Moc <personal@cdatgoose.org> | 2026-05-30 21:53:05 +0200 |
| commit | e930cc6bdc7f62befac063d7d9d016ffb0a64f1a (patch) | |
| tree | 52118a1e990ae88f5f0410c8caea129609e22e19 /src/util.c | |
Added the old repo, refactored it, added the C jit.
Diffstat (limited to 'src/util.c')
| -rw-r--r-- | src/util.c | 71 |
1 files changed, 71 insertions, 0 deletions
diff --git a/src/util.c b/src/util.c new file mode 100644 index 0000000..feb3c0e --- /dev/null +++ b/src/util.c @@ -0,0 +1,71 @@ +#include "util.h" + +#include <stdio.h> +#include <stdlib.h> + +int ecex_file_exists(const char *path) { + if (!path || !path[0]) return 0; + + FILE *file = fopen(path, "rb"); + if (!file) return 0; + + fclose(file); + return 1; +} + +char *ecex_read_entire_file(const char *path, size_t *out_size) { + FILE *file = fopen(path, "rb"); + if (!file) return NULL; + + if (fseek(file, 0, SEEK_END) != 0) { + fclose(file); + return NULL; + } + + long size = ftell(file); + if (size < 0) { + fclose(file); + return NULL; + } + + rewind(file); + + char *data = malloc((size_t)size + 1); + if (!data) { + fclose(file); + return NULL; + } + + size_t read_count = fread(data, 1, (size_t)size, file); + fclose(file); + + if (read_count != (size_t)size) { + free(data); + return NULL; + } + + data[size] = '\0'; + + if (out_size) { + *out_size = (size_t)size; + } + + return data; +} + + +char *ecex_strdup(const char *s) { + if (!s) return NULL; + + size_t len = 0; + while (s[len]) len++; + + char *out = malloc(len + 1); + if (!out) return NULL; + + for (size_t i = 0; i <= len; i++) { + out[i] = s[i]; + } + + return out; +} |
