4 #include "system/nth_alloc.h"
10 #define WINDOWS_TICK 10000000
11 #define SEC_TO_UNIX_EPOCH 11644473600LL
14 #include "system/stacktrace.h"
17 int last_modified(const char *filepath, time_t *time)
19 trace_assert(filepath);
25 if (stat(filepath, &attr) < 0) {
26 // errno is set by stat
29 *time = attr.st_mtime;
34 // CreateFile opens file (see flag OPEN_EXISTING)
35 HANDLE hFile = CreateFile(
38 FILE_SHARE_READ | FILE_SHARE_WRITE | FILE_SHARE_DELETE,
44 if (hFile == INVALID_HANDLE_VALUE) {
45 // TODO(#900): convert GetLastError() to errno
46 // for now let's just assume that file was not found.
50 FILETIME filetime = { 0 };
51 BOOL res = GetFileTime(hFile, NULL, NULL, &filetime);
57 unsigned long long mod_time = filetime.dwHighDateTime;
59 mod_time |= filetime.dwLowDateTime;
60 // Taken from https://stackoverflow.com/a/6161842/1901561
61 *time = mod_time / WINDOWS_TICK - SEC_TO_UNIX_EPOCH;
64 #elif defined(__APPLE__)
66 // TODO(#901): implement last_modified for Mac OS X
67 #warning last_modified is not implemented
83 struct dirent *dirent;
86 DIR *opendir(const char *dirpath)
88 trace_assert(dirpath);
90 char buffer[MAX_PATH];
91 snprintf(buffer, MAX_PATH, "%s\\*", dirpath);
93 DIR *dir = nth_calloc(1, sizeof(DIR));
95 dir->hFind = FindFirstFile(buffer, &dir->data);
96 if (dir->hFind == INVALID_HANDLE_VALUE) {
110 struct dirent *readdir(DIR *dirp)
114 if (dirp->dirent == NULL) {
115 dirp->dirent = nth_calloc(1, sizeof(struct dirent));
117 if(!FindNextFile(dirp->hFind, &dirp->data)) {
122 memset(dirp->dirent->d_name, 0, sizeof(dirp->dirent->d_name));
125 dirp->dirent->d_name,
126 dirp->data.cFileName,
127 sizeof(dirp->dirent->d_name) - 1);
132 void closedir(DIR *dirp)
136 FindClose(dirp->hFind);