]> git.lizzy.rs Git - nothing.git/blobdiff - src/system/file.c
Remove TODO(#943)
[nothing.git] / src / system / file.c
index 2f3ceb8df8855ee4979804d420993cb7bd6c381d..c0be2f57e7eb8957be9bf67e2c6a096436edcc6e 100644 (file)
@@ -1,6 +1,7 @@
 #include <stdio.h>
 #include <stdlib.h>
 #include <errno.h>
+#include "system/nth_alloc.h"
 #ifdef __linux__
 #include <sys/stat.h>
 #include <sys/types.h>
@@ -12,6 +13,7 @@
 
 #include "system/stacktrace.h"
 #include "file.h"
+#include "lt_adapters.h"
 
 int last_modified(const char *filepath, time_t *time)
 {
@@ -40,11 +42,11 @@ int last_modified(const char *filepath, time_t *time)
         0,
         NULL
     );
-    if (!hFile) {
-        // TODO: convert GetLastError() to errno
+    if (hFile == INVALID_HANDLE_VALUE) {
+        // TODO(#900): convert GetLastError() to errno
         // for now let's just assume that file was not found.
         errno = ENOENT;
-        return 0;
+        return -1;
     }
     FILETIME filetime = { 0 };
     BOOL res = GetFileTime(hFile, NULL, NULL, &filetime);
@@ -62,7 +64,7 @@ int last_modified(const char *filepath, time_t *time)
 
 #elif defined(__APPLE__)
 
-    // TODO: implement last_modified for Mac OS X
+    // TODO(#901): implement last_modified for Mac OS X
     #warning last_modified is not implemented
     return -1;
 
@@ -73,3 +75,70 @@ int last_modified(const char *filepath, time_t *time)
 
 #endif
 }
+
+#ifdef _WIN32
+struct DIR
+{
+    HANDLE hFind;
+    WIN32_FIND_DATA data;
+    struct dirent *dirent;
+};
+
+DIR *opendir(const char *dirpath)
+{
+    trace_assert(dirpath);
+
+    char buffer[MAX_PATH];
+    snprintf(buffer, MAX_PATH, "%s\\*", dirpath);
+
+    DIR *dir = nth_calloc(1, sizeof(DIR));
+
+    dir->hFind = FindFirstFile(buffer, &dir->data);
+    if (dir->hFind == INVALID_HANDLE_VALUE) {
+        goto fail;
+    }
+
+    return dir;
+
+fail:
+    if (dir) {
+        free(dir);
+    }
+
+    return NULL;
+}
+
+struct dirent *readdir(DIR *dirp)
+{
+    trace_assert(dirp);
+
+    if (dirp->dirent == NULL) {
+        dirp->dirent = nth_calloc(1, sizeof(struct dirent));
+    } else {
+        if(!FindNextFile(dirp->hFind, &dirp->data)) {
+            return NULL;
+        }
+    }
+
+    memset(dirp->dirent->d_name, 0, sizeof(dirp->dirent->d_name));
+
+    strncpy(
+        dirp->dirent->d_name,
+        dirp->data.cFileName,
+        sizeof(dirp->dirent->d_name) - 1);
+
+    return dirp->dirent;
+}
+
+void closedir(DIR *dirp)
+{
+    trace_assert(dirp);
+
+    FindClose(dirp->hFind);
+    if (dirp->dirent) {
+        free(dirp->dirent);
+    }
+    free(dirp);
+}
+
+#endif