From 25f7c902d9077247333b95248e513cb28ac8fbd1 Mon Sep 17 00:00:00 2001 From: rosenrost <52100845+rosenrost@users.noreply.github.com> Date: Mon, 16 Nov 2026 15:47:37 +0100 Subject: [PATCH] Restore the current drive if chdir() fails chdir("X:\path") switched to drive X before calling Dsetpath(). If the path did not exist, chdir() reported an error but left the current drive changed. --- sources/chdir.c | 9 +++++++++ 1 file changed, 9 insertions(+) diff --git a/sources/chdir.c b/sources/chdir.c index 285515d..81a84d9 100644 --- a/sources/chdir.c +++ b/sources/chdir.c @@ -14,6 +14,8 @@ int chdir(const char* filename) { + int old_drv = -1; + if (filename == NULL) { __set_errno(EFAULT); @@ -29,6 +31,7 @@ int chdir(const char* filename) int drv; drv = toupper(filename[0]) - 'A'; + old_drv = Dgetdrv(); Dsetdrv(drv); filename += 2; @@ -36,6 +39,12 @@ int chdir(const char* filename) if (Dsetpath(filename) < 0) { + if (old_drv >= 0) + { + /* a failing chdir() must not change the current drive */ + Dsetdrv(old_drv); + } + __set_errno(ENOENT); return -1; }