From a3600bf05f8721767bc3116c9d320a661586e3d8 Mon Sep 17 00:00:00 2001 From: Mikachu2333 Date: Mon, 6 Oct 2025 01:55:12 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BF=AE=E5=A4=8D=20xy=5Fdir=5Fexist=20?= =?UTF-8?q?=E7=9A=84=E5=86=85=E5=AD=98=E6=B3=84=E9=9C=B2?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- lib/xy.h | 21 ++++++++++++--------- 1 file changed, 12 insertions(+), 9 deletions(-) diff --git a/lib/xy.h b/lib/xy.h index 4582631..bbbd450 100644 --- a/lib/xy.h +++ b/lib/xy.h @@ -1186,12 +1186,14 @@ xy_file_exist (const char *path) static bool xy_dir_exist (const char *path) { + char *allocated_dir = NULL; const char *dir = path; if (xy.on_windows) { if (xy_str_start_with (path, "~")) { - dir = xy_2strcat (xy_os_home, path + 1); + allocated_dir = xy_2strcat (xy_os_home, path + 1); + dir = allocated_dir; } } @@ -1201,29 +1203,30 @@ xy_dir_exist (const char *path) // 也可以用 opendir() #include DWORD attr = GetFileAttributesA (dir); + bool result = false; if (attr == INVALID_FILE_ATTRIBUTES) { // Q: 我们应该报错吗? - return false; + result = false; } else if (attr & FILE_ATTRIBUTE_DIRECTORY) { - return true; + result = true; } else { - return false; + result = false; } + if (allocated_dir) free (allocated_dir); + return result; #endif } else { int status = system (xy_2strcat ("test -d ", dir)); - - if (0==status) - return true; - else - return false; + bool result = (0==status); + if (allocated_dir) free (allocated_dir); + return result; } return false;