From eeae9208c50c6bb17b172817b13137ce4b242612 Mon Sep 17 00:00:00 2001 From: thevindu-w Date: Sun, 15 Dec 2024 23:51:57 +0530 Subject: [PATCH] Fix working_dir containing back-slashes and ending slash on Windows --- main.c | 10 ++++++++++ utils/utils.c | 11 ++--------- 2 files changed, 12 insertions(+), 9 deletions(-) diff --git a/main.c b/main.c index d8750d7..6cdd64b 100644 --- a/main.c +++ b/main.c @@ -157,6 +157,16 @@ static inline void _set_error_log_file(const char *path) { * Change working directory to the directory specified in the configuration */ static inline void _change_working_dir(void) { + if (!configuration.working_dir) error_exit("Invalid working directory"); + char *ptr; + for (ptr = configuration.working_dir; *ptr; ptr++) { + if (*ptr == PATH_SEP) *ptr = '/'; + } + ptr--; + while (*ptr == '/' && ptr > configuration.working_dir) { // do not remove / from root + *ptr = 0; + ptr--; + } if (!is_directory(configuration.working_dir, 1)) { char err[3072]; snprintf_check(err, 3072, "Error: Not existing working directory \'%s\'", configuration.working_dir); diff --git a/utils/utils.c b/utils/utils.c index 9d98f5f..efe08dd 100644 --- a/utils/utils.c +++ b/utils/utils.c @@ -189,18 +189,11 @@ int is_directory(const char *path, int follow_symlinks) { #elif defined(_WIN32) (void)follow_symlinks; wchar_t *wpath; - if (utf8_to_wchar_str(path, &wpath, NULL) != EXIT_SUCCESS) return -1; + if (utf8_to_wchar_str(path, &wpath, NULL) != EXIT_SUCCESS) return 0; stat_result = _wstat64(wpath, &sb); free(wpath); #endif - if (stat_result == 0) { - if (S_ISDIR(sb.st_mode)) { - return 1; - } else { - return 0; - } - } - return 0; + return (stat_result == 0) && S_ISDIR(sb.st_mode); } void png_mem_write_data(png_structp png_ptr, png_bytep data, png_size_t length) {