4 #include "system/nth_alloc.h"
10 #define WINDOWS_TICK 10000000
11 #define SEC_TO_UNIX_EPOCH 11644473600LL
14 #include "system/stacktrace.h"
16 #include "lt_adapters.h"
18 int last_modified(const char *filepath, time_t *time)
20 trace_assert(filepath);
26 if (stat(filepath, &attr) < 0) {
27 // errno is set by stat
30 *time = attr.st_mtime;
35 // CreateFile opens file (see flag OPEN_EXISTING)
36 HANDLE hFile = CreateFile(
39 FILE_SHARE_READ | FILE_SHARE_WRITE | FILE_SHARE_DELETE,
45 if (hFile == INVALID_HANDLE_VALUE) {
46 // TODO(#900): convert GetLastError() to errno
47 // for now let's just assume that file was not found.
51 FILETIME filetime = { 0 };
52 BOOL res = GetFileTime(hFile, NULL, NULL, &filetime);
58 unsigned long long mod_time = filetime.dwHighDateTime;
60 mod_time |= filetime.dwLowDateTime;
61 // Taken from https://stackoverflow.com/a/6161842/1901561
62 *time = mod_time / WINDOWS_TICK - SEC_TO_UNIX_EPOCH;
65 #elif defined(__APPLE__)
67 // TODO(#901): implement last_modified for Mac OS X
68 #warning last_modified is not implemented
84 struct dirent *dirent;
87 DIR *opendir(const char *dirpath)
89 trace_assert(dirpath);
91 char buffer[MAX_PATH];
92 snprintf(buffer, MAX_PATH, "%s\\*", dirpath);
94 DIR *dir = nth_calloc(1, sizeof(DIR));
96 dir->hFind = FindFirstFile(buffer, &dir->data);
97 if (dir->hFind == INVALID_HANDLE_VALUE) {
111 struct dirent *readdir(DIR *dirp)
115 if (dirp->dirent == NULL) {
116 dirp->dirent = nth_calloc(1, sizeof(struct dirent));
118 if(!FindNextFile(dirp->hFind, &dirp->data)) {
123 memset(dirp->dirent->d_name, 0, sizeof(dirp->dirent->d_name));
126 dirp->dirent->d_name,
127 dirp->data.cFileName,
128 sizeof(dirp->dirent->d_name) - 1);
133 void closedir(DIR *dirp)
137 FindClose(dirp->hFind);