From a9323d9ccb4a1dccaf5c62bfc7db0c22c9011c61 Mon Sep 17 00:00:00 2001 From: David Robillard Date: Sat, 12 Nov 2022 17:54:25 -0500 Subject: Use zix_current_path() --- src/filesystem.c | 29 ++++------------------------- src/filesystem.h | 8 -------- test/test_filesystem.c | 34 ---------------------------------- 3 files changed, 4 insertions(+), 67 deletions(-) diff --git a/src/filesystem.c b/src/filesystem.c index 572b8ea..584eaad 100644 --- a/src/filesystem.c +++ b/src/filesystem.c @@ -10,17 +10,12 @@ #include "zix/path.h" #ifdef _WIN32 -# include # include # include -# define S_ISDIR(mode) (((mode)&S_IFMT) == S_IFDIR) #else # include -# include #endif -#include - #include #include #include @@ -52,12 +47,6 @@ lilv_path_is_child(const char* path, const char* dir) return false; } -char* -lilv_path_current(void) -{ - return getcwd(NULL, 0); -} - char* lilv_path_relative_to(const char* path, const char* base) { @@ -158,26 +147,13 @@ lilv_path_filename(const char* path) return ret; } -bool -lilv_is_directory(const char* path) -{ -#if defined(_WIN32) - const DWORD attrs = GetFileAttributes(path); - - return (attrs != INVALID_FILE_ATTRIBUTES) && - (attrs & FILE_ATTRIBUTE_DIRECTORY); -#else - struct stat st; - return !stat(path, &st) && S_ISDIR(st.st_mode); -#endif -} - void lilv_dir_for_each(const char* path, void* data, void (*f)(const char* path, const char* name, void* data)) { #ifdef _WIN32 + char* pat = zix_path_join(NULL, path, "*"); WIN32_FIND_DATA fd; HANDLE fh = FindFirstFile(pat, &fd); @@ -190,7 +166,9 @@ lilv_dir_for_each(const char* path, } FindClose(fh); free(pat); + #else + DIR* dir = opendir(path); if (dir) { for (struct dirent* entry = NULL; (entry = readdir(dir));) { @@ -200,6 +178,7 @@ lilv_dir_for_each(const char* path, } closedir(dir); } + #endif } diff --git a/src/filesystem.h b/src/filesystem.h index db6114e..d2f14b6 100644 --- a/src/filesystem.h +++ b/src/filesystem.h @@ -7,10 +7,6 @@ bool lilv_path_is_child(const char* path, const char* dir); -/// Return the current working directory -char* -lilv_path_current(void); - /** Return `path` relative to `base` if possible. @@ -37,10 +33,6 @@ lilv_path_parent(const char* path); char* lilv_path_filename(const char* path); -/// Return true iff `path` points to an existing directory -bool -lilv_is_directory(const char* path); - /** Visit every file in the directory at `path`. diff --git a/test/test_filesystem.c b/test/test_filesystem.c index 1453892..f788e90 100644 --- a/test/test_filesystem.c +++ b/test/test_filesystem.c @@ -43,16 +43,6 @@ test_path_is_child(void) assert(!lilv_path_is_child("/a/b/", "/c/")); } -static void -test_path_current(void) -{ - char* cwd = lilv_path_current(); - - assert(lilv_is_directory(cwd)); - - free(cwd); -} - static void test_path_relative_to(void) { @@ -114,28 +104,6 @@ test_path_filename(void) #endif } -static void -test_is_directory(void) -{ - char* const temp_dir = lilv_create_temporary_directory("lilvXXXXXX"); - char* const file_path = zix_path_join(NULL, temp_dir, "lilv_test_file"); - - assert(lilv_is_directory(temp_dir)); - assert(!lilv_is_directory(file_path)); // Nonexistent - - FILE* f = fopen(file_path, "w"); - fprintf(f, "test\n"); - fclose(f); - - assert(!lilv_is_directory(file_path)); // File - - assert(!zix_remove(file_path)); - assert(!zix_remove(temp_dir)); - - free(file_path); - free(temp_dir); -} - typedef struct { size_t n_names; char** names; @@ -192,11 +160,9 @@ int main(void) { test_path_is_child(); - test_path_current(); test_path_relative_to(); test_path_parent(); test_path_filename(); - test_is_directory(); test_dir_for_each(); return 0; -- cgit v1.2.1