Zsh Mailing List Archive
Messages sorted by: Reverse Date, Date, Thread, Author

Re: Cygwin/MSYS2: Trouble converting relative paths from / using :a or :A



> 2021/11/13 4:19, agkozak@xxxxxxxxx wrote:
> 
> In Cygwin or MSYS2, if I execute
>  
>     cd /
>     foo=bin
>     echo ${foo:a}
>     echo ${foo:A}
>     echo ${foo:P}
>  
> for the :a modifier I get
>  
>     //bin
>  
> for the :A modifier I get a long pause, followed by
>  
>     //bin
>  
> (Is it trying to access a network share?) But for the :P modifier I get the expected
>  
>     /bin

Confirmed.

The relative path "bin" is converted to the absolute path by chabspath() in hist.c.
It first convert "bin" to "$PWD/bin" = "//bin", and then, on systems other
than Cygwin, replace multiple slashes by just a single slash. But on Cygwin,
if the pathname starts with "//" then it is retained, probably to support the so
called UNC: //host/path_to_file.

In the following patch chabspath() is modified so that it does not add an extra '/'
if $PWD = "/". To be more precise, I have just copied the lines 932-936 of hist.c
(in function histsubchar(), for the :P modifier) to chabspath(). So the extra '/'
is not added if the result of zgetcwd() ends with '/'.

# When is it possible that the return value of zgetcwd() ends with '/' but is not
# equal to just "/"?



diff --git a/Src/hist.c b/Src/hist.c
index 6ac581fda..ea727d1f8 100644
--- a/Src/hist.c
+++ b/Src/hist.c
@@ -1861,7 +1861,11 @@ chabspath(char **junkptr)
 	return 1;
 
     if (**junkptr != '/') {
-	*junkptr = zhtricat(metafy(zgetcwd(), -1, META_HEAPDUP), "/", *junkptr);
+	char *here = zgetcwd();
+	if (here[strlen(here)-1] != '/')
+	    *junkptr = zhtricat(metafy(here, -1, META_HEAPDUP), "/", *junkptr);
+	else
+	    *junkptr = dyncat(here, *junkptr);
     }
 
     current = *junkptr;







Messages sorted by: Reverse Date, Date, Thread, Author