diff options
author | c+1 | 2023-10-10 11:26:44 -0400 |
---|---|---|
committer | c+1 | 2023-10-10 11:26:44 -0400 |
commit | 58c7a71a50318940e747c365cc3f207dba432977 (patch) | |
tree | 7d173f5433fba1b01c531610a0bf70684b8ca1de /src/source.c | |
parent | 78befa147eccfb169bf994da3d9bfba9be3631a6 (diff) |
fixed source.c, fixed preprocessor mem leaks, implemented new lexer
Diffstat (limited to 'src/source.c')
-rw-r--r-- | src/source.c | 32 |
1 files changed, 7 insertions, 25 deletions
diff --git a/src/source.c b/src/source.c index b098751..4874d5a 100644 --- a/src/source.c +++ b/src/source.c @@ -6,13 +6,12 @@ char* source_get(char* arg) { return arg? source_get_from_fpath(arg): source_get_from_stdin(); - } char* source_get_from_fpath(char* path) { - FILE* f; // the file to read from - long f_size; // the size of the file - char* src; // the source code to return + FILE* f; + long f_size; + char* src; f = fopen(path, "rb"); if (!f) { die("source file not found: %s", path); } @@ -21,7 +20,7 @@ char* source_get_from_fpath(char* path) { f_size = ftell(f); rewind(f); - src = calloc(1, f_size + 1); + src = ecalloc(1, f_size + 1); if ((fread(src, f_size, 1, f) != 1) || !src) { fclose(f); @@ -33,28 +32,11 @@ char* source_get_from_fpath(char* path) { } char* source_get_from_stdin() { - size_t len; // the length of the given source - char* src; // the source code to return - - len = 0; - src = calloc(len, sizeof(char)); - - printf("> "); - - while (src[len - 1] != EOF) { - char c; // the character being read - - if (src[len - 1] == '\n') { printf("> "); } - - c = getchar(); - src = realloc(src, (len + sizeof(char))); - memcpy(src + len, &c, sizeof(char)); - len += sizeof(char); - } + char* src; - src[len - 1] = '\0'; // null terminate + src = ecalloc(256, sizeof(char)); - putchar('\n'); + src = fgets(src, 256, stdin); return src; } |