From b3fde419e6fe3f750b9a34b0dfe2c5348f557f77 Mon Sep 17 00:00:00 2001 From: Elias Aebi Date: Wed, 18 Sep 2024 10:41:58 +0200 Subject: [PATCH] gral_get_current_working_directory --- demos/file.c | 10 ++++++++++ gral.h | 1 + gral_linux.c | 4 ++++ gral_macos.m | 4 ++++ gral_windows.cpp | 10 ++++++++++ 5 files changed, 29 insertions(+) diff --git a/demos/file.c b/demos/file.c index 787d85d..ee62d61 100644 --- a/demos/file.c +++ b/demos/file.c @@ -1,6 +1,7 @@ #include #include #include +#include static void command_cat(int argc, char **argv) { if (argc < 3) { @@ -39,6 +40,12 @@ static void command_mkdir(int argc, char **argv) { gral_directory_create(argv[2]); } +static void command_pwd(int argc, char **argv) { + char *current_working_directory = gral_get_current_working_directory(); + printf("%s\n", current_working_directory); + free(current_working_directory); +} + static void command_rm(int argc, char **argv) { if (argc < 3) { printf("missing argument\n"); @@ -86,6 +93,9 @@ int main(int argc, char **argv) { else if (strcmp(argv[1], "mkdir") == 0) { command_mkdir(argc, argv); } + else if (strcmp(argv[1], "pwd") == 0) { + command_pwd(argc, argv); + } else if (strcmp(argv[1], "rm") == 0) { command_rm(argc, argv); } diff --git a/gral.h b/gral.h index 0dd51c5..6ff2002 100644 --- a/gral.h +++ b/gral.h @@ -195,6 +195,7 @@ void gral_file_remove(char const *path); void gral_directory_create(char const *path); void gral_directory_iterate(char const *path, void (*callback)(char const *name, void *user_data), void *user_data); void gral_directory_remove(char const *path); +char *gral_get_current_working_directory(void); struct gral_directory_watcher *gral_directory_watch(char const *path, void (*callback)(void *user_data), void *user_data); void gral_directory_watcher_delete(struct gral_directory_watcher *directory_watcher); diff --git a/gral_linux.c b/gral_linux.c index 4555165..a554f74 100644 --- a/gral_linux.c +++ b/gral_linux.c @@ -778,6 +778,10 @@ void gral_directory_remove(char const *path) { rmdir(path); } +char *gral_get_current_working_directory(void) { + return getcwd(NULL, 0); +} + typedef struct { void (*callback)(void *user_data); void *user_data; diff --git a/gral_macos.m b/gral_macos.m index 67f54af..cccd376 100644 --- a/gral_macos.m +++ b/gral_macos.m @@ -794,6 +794,10 @@ void gral_directory_remove(char const *path) { rmdir(path); } +char *gral_get_current_working_directory(void) { + return getcwd(NULL, 0); +} + @interface GralDirectoryWatcher: GralCallbackObject { @public int fd; diff --git a/gral_windows.cpp b/gral_windows.cpp index 3132d4a..88b9739 100644 --- a/gral_windows.cpp +++ b/gral_windows.cpp @@ -1191,6 +1191,16 @@ void gral_directory_remove(char const *path) { RemoveDirectory(utf8_to_utf16(path)); } +char *gral_get_current_working_directory() { + DWORD utf16_length = GetCurrentDirectory(0, NULL); + Buffer utf16_buffer(utf16_length); + GetCurrentDirectory(utf16_length, utf16_buffer); + int utf8_length = WideCharToMultiByte(CP_UTF8, 0, utf16_buffer, utf16_length, NULL, 0, NULL, NULL); + char *utf8_buffer = (char *)malloc(utf8_length); + WideCharToMultiByte(CP_UTF8, 0, utf16_buffer, utf16_length, utf8_buffer, utf8_length, NULL, NULL); + return utf8_buffer; +} + struct gral_directory_watcher { HANDLE directory; OVERLAPPED overlapped;