Skip to content

Commit

Permalink
fix: correct listdir behavior on Windows
Browse files Browse the repository at this point in the history
  • Loading branch information
jacopodl committed Jan 23, 2024
1 parent a48660c commit 2661bc6
Showing 1 changed file with 69 additions and 47 deletions.
116 changes: 69 additions & 47 deletions argon/vm/mod/os.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -836,10 +836,29 @@ ARGON_FUNCTION(os_listdir, listdir,
return nullptr;

#ifdef _ARGON_PLATFORM_WINDOWS
unsigned char *buffer = ARGON_RAW_STRING(path);
ArSize buflen = ARGON_RAW_STRING_LENGTH(path);

WIN32_FIND_DATA entry;

HANDLE hFind = FindFirstFile((LPCSTR) ARGON_RAW_STRING(path), &entry);
if(hFind == INVALID_HANDLE_VALUE) {
if (!StringEndswith(path, "\\*")) {
if ((buffer = (unsigned char *) memory::Alloc(buflen + 3)) == nullptr) {
Release(ldir);

return nullptr;
}

auto *next = (unsigned char *) memory::MemoryCopy(buffer, ARGON_RAW_STRING(path), buflen);
*next++ = '\\';
*next++ = '*';
*next = '\0';
}

HANDLE hFind = FindFirstFile((LPCSTR) buffer, &entry);
if (hFind == INVALID_HANDLE_VALUE) {
if(ARGON_RAW_STRING(path) != buffer)
memory::Free(buffer);

Release(ldir);

ErrorFromWinErr();
Expand All @@ -862,9 +881,12 @@ ARGON_FUNCTION(os_listdir, listdir,
}

Release(name);
} while(FindNextFile(hFind, &entry) != 0);
} while (FindNextFile(hFind, &entry) != 0);

FindClose(hFind);

if(ARGON_RAW_STRING(path) != buffer)
memory::Free(buffer);
#else
auto *dir = opendir((const char *) ARGON_RAW_STRING(path));
if (dir == nullptr) {
Expand Down Expand Up @@ -1189,50 +1211,50 @@ bool OSInit(Module *self) {
if (!ModuleAddIntConstant(self, "TIMEOUT_INFINITE", INFINITE))
return false;
#else
// SIGNALS
AddIntConstant(SIGHUP);
AddIntConstant(SIGINT);
AddIntConstant(SIGQUIT);
AddIntConstant(SIGILL);
AddIntConstant(SIGTRAP);
AddIntConstant(SIGABRT);
AddIntConstant(SIGIOT);
AddIntConstant(SIGBUS);
//AddIntConstant(SIGEMT);
AddIntConstant(SIGFPE);
AddIntConstant(SIGKILL);
AddIntConstant(SIGUSR1);
AddIntConstant(SIGSEGV);
AddIntConstant(SIGUSR2);
AddIntConstant(SIGPIPE);
AddIntConstant(SIGALRM);
AddIntConstant(SIGTERM);
//AddIntConstant(SIGSTKFLT);
AddIntConstant(SIGCHLD);
//AddIntConstant(SIGCLD);
AddIntConstant(SIGCONT);
AddIntConstant(SIGSTOP);
AddIntConstant(SIGTSTP);
AddIntConstant(SIGTTIN);
AddIntConstant(SIGTTOU);
AddIntConstant(SIGURG);
AddIntConstant(SIGXCPU);
AddIntConstant(SIGXFSZ);
AddIntConstant(SIGVTALRM);
AddIntConstant(SIGPROF);
AddIntConstant(SIGWINCH);
AddIntConstant(SIGIO);
//AddIntConstant(SIGPOLL);
//AddIntConstant(SIGPWR);
//AddIntConstant(SIGINFO);
//AddIntConstant(SIGLOST);
AddIntConstant(SIGSYS);
//AddIntConstant(SIGUNUSED);

// WAITPID OPTIONS
AddIntConstant(WNOHANG);
AddIntConstant(WUNTRACED);
AddIntConstant(WCONTINUED);
// SIGNALS
AddIntConstant(SIGHUP);
AddIntConstant(SIGINT);
AddIntConstant(SIGQUIT);
AddIntConstant(SIGILL);
AddIntConstant(SIGTRAP);
AddIntConstant(SIGABRT);
AddIntConstant(SIGIOT);
AddIntConstant(SIGBUS);
//AddIntConstant(SIGEMT);
AddIntConstant(SIGFPE);
AddIntConstant(SIGKILL);
AddIntConstant(SIGUSR1);
AddIntConstant(SIGSEGV);
AddIntConstant(SIGUSR2);
AddIntConstant(SIGPIPE);
AddIntConstant(SIGALRM);
AddIntConstant(SIGTERM);
//AddIntConstant(SIGSTKFLT);
AddIntConstant(SIGCHLD);
//AddIntConstant(SIGCLD);
AddIntConstant(SIGCONT);
AddIntConstant(SIGSTOP);
AddIntConstant(SIGTSTP);
AddIntConstant(SIGTTIN);
AddIntConstant(SIGTTOU);
AddIntConstant(SIGURG);
AddIntConstant(SIGXCPU);
AddIntConstant(SIGXFSZ);
AddIntConstant(SIGVTALRM);
AddIntConstant(SIGPROF);
AddIntConstant(SIGWINCH);
AddIntConstant(SIGIO);
//AddIntConstant(SIGPOLL);
//AddIntConstant(SIGPWR);
//AddIntConstant(SIGINFO);
//AddIntConstant(SIGLOST);
AddIntConstant(SIGSYS);
//AddIntConstant(SIGUNUSED);

// WAITPID OPTIONS
AddIntConstant(WNOHANG);
AddIntConstant(WUNTRACED);
AddIntConstant(WCONTINUED);
#endif

AddIntConstant(EXIT_SUCCESS);
Expand Down

0 comments on commit 2661bc6

Please sign in to comment.