zsh-workers
 help / color / mirror / code / Atom feed
* Cygwin/MSYS2: Trouble converting relative paths from / using :a or :A
@ 2021-11-12 19:19 agkozak
  2021-11-15  8:44 ` Jun T
  0 siblings, 1 reply; 5+ messages in thread
From: agkozak @ 2021-11-12 19:19 UTC (permalink / raw)
  To: zsh-workers

[-- Attachment #1: Type: text/plain, Size: 406 bytes --]

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

 

The //bin paths are unusable ("No such file or directory").

 

Alex


[-- Attachment #2: Type: text/html, Size: 2696 bytes --]

^ permalink raw reply	[flat|nested] 5+ messages in thread

* Re: Cygwin/MSYS2: Trouble converting relative paths from / using :a or :A
  2021-11-12 19:19 Cygwin/MSYS2: Trouble converting relative paths from / using :a or :A agkozak
@ 2021-11-15  8:44 ` Jun T
  2021-11-17  9:39   ` Jun T
  0 siblings, 1 reply; 5+ messages in thread
From: Jun T @ 2021-11-15  8:44 UTC (permalink / raw)
  To: zsh-workers


> 2021/11/13 4:19, agkozak@gmail.com 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;





^ permalink raw reply	[flat|nested] 5+ messages in thread

* Re: Cygwin/MSYS2: Trouble converting relative paths from / using :a or :A
  2021-11-15  8:44 ` Jun T
@ 2021-11-17  9:39   ` Jun T
  2021-12-01  4:58     ` Daniel Shahaf
  0 siblings, 1 reply; 5+ messages in thread
From: Jun T @ 2021-11-17  9:39 UTC (permalink / raw)
  To: zsh-workers


> 2021/11/15 17:44、I wrote:

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

> +	char *here = zgetcwd();
> +	if (here[strlen(here)-1] != '/')
> +	    *junkptr = zhtricat(metafy(here, -1, META_HEAPDUP), "/", *junkptr);
> +	else
> +	    *junkptr = dyncat(here, *junkptr);


Now I think that the return value of zgetcwd() ('here') can end with '/'
only if strlen(here)==1, so we need not metafy 'here'.
Am I correct?

The same code appears at lines 932 of hist.c, and line 4501 of subst.c.

^ permalink raw reply	[flat|nested] 5+ messages in thread

* Re: Cygwin/MSYS2: Trouble converting relative paths from / using :a or :A
  2021-11-17  9:39   ` Jun T
@ 2021-12-01  4:58     ` Daniel Shahaf
  2021-12-05 14:10       ` Jun. T
  0 siblings, 1 reply; 5+ messages in thread
From: Daniel Shahaf @ 2021-12-01  4:58 UTC (permalink / raw)
  To: Jun T; +Cc: zsh-workers

Jun T wrote on Wed, Nov 17, 2021 at 18:39:12 +0900:
> 
> > 2021/11/15 17:44、I wrote:
> 
> > # When is it possible that the return value of zgetcwd() ends with '/' but is not
> > # equal to just "/"?
> 
> Now I think that the return value of zgetcwd() ('here') can end with '/'
> only if strlen(here)==1, so we need not metafy 'here'.
> Am I correct?
> 

I don't know.

> > +	char *here = zgetcwd();
> > +	if (here[strlen(here)-1] != '/')
> > +	    *junkptr = zhtricat(metafy(here, -1, META_HEAPDUP), "/", *junkptr);
> > +	else
> > +	    *junkptr = dyncat(here, *junkptr);
> 
> The same code appears at lines 932 of hist.c, and line 4501 of subst.c.

This isn't a new issue in your patch, but I'd like to ask it anyway:

Can zgetcwd() return an empty string?  If so, the expression in the
condition will trigger undefined behaviour.

Cheers,

Daniel


^ permalink raw reply	[flat|nested] 5+ messages in thread

* Re: Cygwin/MSYS2: Trouble converting relative paths from / using :a or :A
  2021-12-01  4:58     ` Daniel Shahaf
@ 2021-12-05 14:10       ` Jun. T
  0 siblings, 0 replies; 5+ messages in thread
From: Jun. T @ 2021-12-05 14:10 UTC (permalink / raw)
  To: zsh-workers


> 2021/12/01 13:58, Daniel Shahaf <d.s@daniel.shahaf.name> wrote:
> 
> Can zgetcwd() return an empty string?

No. It returns "." if everyting else fails.


^ permalink raw reply	[flat|nested] 5+ messages in thread

end of thread, other threads:[~2021-12-05 14:11 UTC | newest]

Thread overview: 5+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2021-11-12 19:19 Cygwin/MSYS2: Trouble converting relative paths from / using :a or :A agkozak
2021-11-15  8:44 ` Jun T
2021-11-17  9:39   ` Jun T
2021-12-01  4:58     ` Daniel Shahaf
2021-12-05 14:10       ` Jun. T

Code repositories for project(s) associated with this public inbox

	https://git.vuxu.org/mirror/zsh/

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).