From 90c87dc65452889cd79debdc58d46fee76d17726 Mon Sep 17 00:00:00 2001 From: Ron Yorston Date: Tue, 19 Nov 2024 09:18:56 +0000 Subject: ash: match behaviour of cmd.exe in cd builtin The Windows API strips trailing dots and spaces from the last component of a path. cmd.exe handles this quirk when changing directory by adjusting its idea of the current directory to match reality. The shell in busybox-w32 didn't do this, leading to some confusion. Fix the shell's cd builtin so it works more like cmd.exe. Adds 64-80 bytes. (GitHub issue #478) --- include/mingw.h | 1 + shell/ash.c | 1 + win32/mingw.c | 13 +++++++++++++ 3 files changed, 15 insertions(+) diff --git a/include/mingw.h b/include/mingw.h index 5a3c75ff6..c41c0f91e 100644 --- a/include/mingw.h +++ b/include/mingw.h @@ -631,6 +631,7 @@ MINGW_BB_WCHAR_T *bs_to_slash_u(MINGW_BB_WCHAR_T *p) FAST_FUNC; char *bs_to_slash(char *p) FAST_FUNC; void slash_to_bs(char *p) FAST_FUNC; +void strip_dot_space(char *p) FAST_FUNC; size_t remove_cr(char *p, size_t len) FAST_FUNC; int err_win_to_posix(void); diff --git a/shell/ash.c b/shell/ash.c index 1fc2a44b1..3919118f0 100644 --- a/shell/ash.c +++ b/shell/ash.c @@ -3338,6 +3338,7 @@ updatepwd(const char *dir) if (new > lim) STUNPUTC(new); *new = 0; + strip_dot_space((char *)stackblock()); fix_path_case((char *)stackblock()); return bs_to_slash((char *)stackblock()); #else diff --git a/win32/mingw.c b/win32/mingw.c index 87e7ca602..6842dba48 100644 --- a/win32/mingw.c +++ b/win32/mingw.c @@ -2178,6 +2178,19 @@ void FAST_FUNC slash_to_bs(char *p) } } +/* Windows strips trailing dots and spaces from the last component of + * a file path. This routine emulates that behaviour so we can preempt + * Windows if necessary. */ +void FAST_FUNC strip_dot_space(char *p) +{ + char *start = (char *)bb_basename(p); + char *end = start + strlen(start); + + while (end > start && (end[-1] == '.' || end[-1] == ' ')) { + *--end = '\0'; + } +} + size_t FAST_FUNC remove_cr(char *p, size_t len) { ssize_t i, j; -- cgit v1.2.3-55-g6feb