diff options
author | David Robillard <d@drobilla.net> | 2022-11-12 17:54:25 -0500 |
---|---|---|
committer | David Robillard <d@drobilla.net> | 2022-11-16 10:22:55 -0500 |
commit | a9323d9ccb4a1dccaf5c62bfc7db0c22c9011c61 (patch) | |
tree | 6abaced110b9405382db9b87c94677c3836465c6 | |
parent | 94b3980bcbe40bca8f361eb63a87f371337d067c (diff) | |
download | lilv-a9323d9ccb4a1dccaf5c62bfc7db0c22c9011c61.tar.gz lilv-a9323d9ccb4a1dccaf5c62bfc7db0c22c9011c61.tar.bz2 lilv-a9323d9ccb4a1dccaf5c62bfc7db0c22c9011c61.zip |
Use zix_current_path()
-rw-r--r-- | src/filesystem.c | 29 | ||||
-rw-r--r-- | src/filesystem.h | 8 | ||||
-rw-r--r-- | 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 <direct.h> # include <io.h> # include <windows.h> -# define S_ISDIR(mode) (((mode)&S_IFMT) == S_IFDIR) #else # include <dirent.h> -# include <unistd.h> #endif -#include <sys/stat.h> - #include <stdbool.h> #include <stdio.h> #include <stdlib.h> @@ -53,12 +48,6 @@ lilv_path_is_child(const char* path, const char* dir) } char* -lilv_path_current(void) -{ - return getcwd(NULL, 0); -} - -char* lilv_path_relative_to(const char* path, const char* base) { const size_t path_len = strlen(path); @@ -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 @@ -44,16 +44,6 @@ test_path_is_child(void) } 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) { assert(equals(lilv_path_relative_to("/a/b", "/a/"), "b")); @@ -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; |