Skip to content

Commit

Permalink
Correct issue with \cr\lf in jsrReadFile
Browse files Browse the repository at this point in the history
Honestly getting pretty sick of Windows.
You see, Windows uses \cr\lf as default line endings in files, so, when
opening a file without specifying the `b` access mode, it does whatever
the fuck it wants and starts converting line feeds between unix-mode and
windows-mode messing up the file more often than not.
The real fun part is that the returned count of read bytes by `fwrite` is
not the same as the count returned by ftell(SEEK_END), causing all sorts of troubles.
I finally decided to slap the `b` access flag on all file accesses, and just deal with
stupid carriage returns rather than bugs in the file reading logic
caused by unsynchronized ftell/fread return values.
The more i program in C for Windows, the more it amazes me. And not in a
good way.
  • Loading branch information
bamless committed Jan 31, 2021
1 parent 473ba18 commit 40f4832
Showing 1 changed file with 3 additions and 9 deletions.
12 changes: 3 additions & 9 deletions src/jstar.c
Expand Up @@ -285,27 +285,21 @@ void jsrEnsureStack(JStarVM* vm, size_t needed) {
bool jsrReadFile(JStarVM* vm, const char* path, JStarBuffer* out) {
int saveErrno;
size_t read;
FILE* src = fopen(path, "r");
FILE* src = fopen(path, "rb");

if(src == NULL) {
return false;
}

bool isCompiled = false;
char header[SER_HEADER_SIZE];

read = fread(header, 1, SER_HEADER_SIZE, src);
if(ferror(src)) {
goto error;
}

if(read == SER_HEADER_SIZE) {
if(memcmp(SER_FILE_HEADER, header, read) == 0) {
isCompiled = true;
src = freopen(path, "rb", src);
}
}

bool isCompiled = read == SER_HEADER_SIZE && memcmp(SER_FILE_HEADER, header, read) == 0;

if(fseek(src, 0, SEEK_END)) {
goto error;
}
Expand Down

0 comments on commit 40f4832

Please sign in to comment.