fix filename
This commit is contained in:
57
zig0.c
Normal file
57
zig0.c
Normal file
@@ -0,0 +1,57 @@
|
||||
#include <stdbool.h>
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
|
||||
// API:
|
||||
// - code = 0: program successfully terminated.
|
||||
// - code = 1: panicked, panic message in msg. Caller should free msg.
|
||||
// - code = 2: interpreter error, error in msg. Caller should free msg.
|
||||
int zig0Run(const char* program, char** msg) {
|
||||
(void)program;
|
||||
(void)msg;
|
||||
return 0;
|
||||
}
|
||||
|
||||
// API: run and:
|
||||
// code = 3: abnormal error, expect something in stderr.
|
||||
int zig0RunFile(const char* fname, char** msg) {
|
||||
FILE* f = fopen(fname, "r");
|
||||
if (f == NULL) {
|
||||
perror("fopen");
|
||||
return 3;
|
||||
}
|
||||
fseek(f, 0, SEEK_END);
|
||||
long fsizel = ftell(f);
|
||||
if (fsizel == -1) {
|
||||
perror("ftell");
|
||||
fclose(f);
|
||||
return 3;
|
||||
}
|
||||
unsigned long fsize = (unsigned long)fsizel;
|
||||
fseek(f, 0, SEEK_SET);
|
||||
|
||||
char* program = malloc(fsize + 1);
|
||||
if (program == NULL) {
|
||||
perror("malloc");
|
||||
fclose(f);
|
||||
return 3;
|
||||
}
|
||||
|
||||
size_t bytes_read = fread(program, 1, fsize, f);
|
||||
if (bytes_read < fsize) {
|
||||
if (ferror(f)) {
|
||||
perror("fread");
|
||||
} else {
|
||||
fprintf(stderr, "Unexpected end of file\n");
|
||||
}
|
||||
free(program);
|
||||
fclose(f);
|
||||
return 3;
|
||||
}
|
||||
fclose(f);
|
||||
program[fsize] = 0;
|
||||
|
||||
int code = zig0Run(program, msg);
|
||||
free(program);
|
||||
return code;
|
||||
}
|
||||
Reference in New Issue
Block a user