[klibc] [klibc:update-dash] dash: [CD] support drive letters on Cygwin

klibc-bot for Eric Blake eblake at redhat.com
Sat Mar 28 14:48:25 PDT 2020

Commit-ID:  8703ad5517c811716e37a2605acc8ddc8c66362a
Gitweb:     http://git.kernel.org/?p=libs/klibc/klibc.git;a=commit;h=8703ad5517c811716e37a2605acc8ddc8c66362a
Author:     Eric Blake <eblake at redhat.com>
AuthorDate: Mon, 27 Oct 2014 11:38:43 +0800
Committer:  Ben Hutchings <ben at decadent.org.uk>
CommitDate: Sat, 28 Mar 2020 21:42:54 +0000

[klibc] dash: [CD] support drive letters on Cygwin

[ dash commit 586463c3cf247dbead8553da7284a346b2faf1fb ]

The Cygwin platform supports DOS style drive-letter paths such
as "C:\\dir", even though the preferred form is a POSIX-style
"/cygdrive/c/dir".  This can be seen by doing things such as
chdir("c:") (which succeeds) followed by getcwd(NULL, 0) (which
returns the normalized "/cygdrive/c").  However, dash was trying
to perform local manipulations on the argument to 'cd' prior to
calling into libc, in order to update the state of $PWD and
friends; these manipulations were assuming that the user meant
to change to a relative subdirectory of the current location,
as in './c:', instead of honoring the drive letter.  None of
the other dash builtins take a filename and manipulate it to
affect shell state (some, like 'test', take a file name, but as
stat("c:") works just fine, there is no need to normalize).

This patch has no impact outside of cygwin; on cygwin, it takes
advantage of a native function call to canonicalize any
incoming name into preferred form before updating shell state.

$ dash -c 'cd c: && echo $PWD'
dash: 1: cd: can't cd to c:

$ dash -c 'cd c: && echo $PWD'

Signed-off-by: Eric Blake <eblake at redhat.com>
Signed-off-by: Herbert Xu <herbert at gondor.apana.org.au>
Signed-off-by: Ben Hutchings <ben at decadent.org.uk>

 usr/dash/cd.c | 14 ++++++++++++++
 1 file changed, 14 insertions(+)

diff --git a/usr/dash/cd.c b/usr/dash/cd.c
index 2d9d4b52..a4e024d8 100644
--- a/usr/dash/cd.c
+++ b/usr/dash/cd.c
@@ -38,6 +38,9 @@
 #include <string.h>
 #include <unistd.h>
 #include <limits.h>
+#ifdef __CYGWIN__
+#include <sys/cygwin.h>
  * The cd and pwd commands.
@@ -194,6 +197,17 @@ updatepwd(const char *dir)
 	char *cdcomppath;
 	const char *lim;
+#ifdef __CYGWIN__
+	/* On cygwin, thanks to drive letters, some absolute paths do
+	   not begin with slash; but cygwin includes a function that
+	   forces normalization to the posix form */
+	char pathbuf[PATH_MAX];
+	if (cygwin_conv_path(CCP_WIN_A_TO_POSIX | CCP_RELATIVE, dir, pathbuf,
+			     sizeof(pathbuf)) < 0)
+		sh_error("can't normalize %s", dir);
+	dir = pathbuf;
 	cdcomppath = sstrdup(dir);
 	if (*dir != '/') {

More information about the klibc mailing list