cygwincygpath

Always get path starting /cygdrive from cygpath?


On Cygwin, the cygpath application translates between Windows and Unix-style paths.

Consider the following examples:

$ cygpath -u "c:/"
/cygdrive/c

$ cygpath -u "c:/cygwin64/bin"
/usr/bin

Is there any way to get /cygdrive/c/cygwin64/bin from the second command?

I need this because sometimes Cygwin gets confused about where its root is, so I want an absolute path in order to be clear.


Solution

  • No, Cygwin's cygpath doesn't support this. The best you can do is manually fix it up with your own conversion tool; something along the lines of:

    #!/usr/bin/env bash
    
    if [[ "$1" == -z ]]; then
        # Invoked with -z, so skip normal cygpath processing and convert the path
        # here.
        #
        # The sed command replaces "c:" with "/cygdrive/c", and switches any
        # back slashes to forward slashes.
        shift
        printf "%s\n" "$*" | sed -r 's!(.):([\\\/].*)$!/cygdrive/\1\2!;s!\\!/!g'
    else
        # Not invoked with -z, so just call cygpath with the arguments this script
        # was called with.
        exec cygpath "$@"
    fi
    

    If you store the above script as mycygpath.sh then it will behave exactly as cygpath does unless you give it the -z argument, in which case it will simply convert n:/ to /cygdrive/n/:

    $ ./mycygpath.sh -u "c:/"
    /cygdrive/c
    
    $ ./mycygpath.sh -u "c:/cygwin64/bin"
    /usr/bin
    
    $ ./mycygpath.sh -z "c:/cygwin64/bin"
    /cygdrive/c/cygwin64/bin
    

    Of course, there's the obvious question of why "Cygwin gets confused about where it root is"; that's not something that should happen at all and implies there's something wrong with your Cygwin setup. But that's not the question you asked and you've not given enough detail to be able to start making suggestions.