Skip to content

Commit

Permalink
gral_get_current_working_directory
Browse files Browse the repository at this point in the history
  • Loading branch information
eyelash committed Sep 18, 2024
1 parent e00e38b commit b3fde41
Show file tree
Hide file tree
Showing 5 changed files with 29 additions and 0 deletions.
10 changes: 10 additions & 0 deletions demos/file.c
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
#include <gral.h>
#include <stdio.h>
#include <string.h>
#include <stdlib.h>

static void command_cat(int argc, char **argv) {
if (argc < 3) {
Expand Down Expand Up @@ -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");
Expand Down Expand Up @@ -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);
}
Expand Down
1 change: 1 addition & 0 deletions gral.h
Original file line number Diff line number Diff line change
Expand Up @@ -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);

Expand Down
4 changes: 4 additions & 0 deletions gral_linux.c
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
4 changes: 4 additions & 0 deletions gral_macos.m
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
10 changes: 10 additions & 0 deletions gral_windows.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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<wchar_t> 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;
Expand Down

0 comments on commit b3fde41

Please sign in to comment.