This is the mail archive of the cygwin mailing list for the Cygwin project.


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]
Other format: [Raw text]

Re: Problem with chdir and GetCurrentDirectory on Windows 2016


On 12/07/2016 04:22 PM, Dipak Gaigole wrote:

> C:\Temp\appdir>.\test_cwd.exe "bin"
> chdir (bin) reuturned <0>
> GetCurrentDirectory returned <C:\Temp\appdir\bin>, ret = <18>
> getcwd returned </appdir/bin>, ret = </appdir/bin>
> 
> C:\Temp\appdir>
> ##############################################
> Please note that I have the cygwin1.dll in the "C:\Temp\appdir"
> directory. So after changing the directory to "bin", the getcwd()
> returned "/appdir/bin". And our requirement was to get
> "/cygdrive/c/temp/appdir/bin", so the GetCurrentDirectory() approach
> was used.

If you want to convert a POSIX name to the corresponding Windows name,
chdir() and getcwd() is not the best approach; and
chdir()/GetCurrentDirectory() is the wrong approach.  Instead, either
call out to the 'cygpath' utility, or use the cygwin_conv_path() API
(https://cygwin.com/cygwin-api/func-cygwin-conv-path.html) (which is
what 'cygpath' uses under the hood).

> 
> So in this situation, is it possible to get the cwd value as
> "/cygdrive/c/temp/appdir/bin" using some cygwin API?

No, it is generally not possible to get the POSIX path to anything in
the cygwin root (/) tree to be prefixed by the /cygdrive prefix. The
/cygdrive prefix exists solely to resolve paths that are outside of /,
so paths inside that tree don't need the /cygdrive prefix.

But you seem to be asking the wrong question.  If the only reason you
want a "/cygdrive/c" prefix is so that you can supply textual conversion
of "/cygdrive/c" to "c:\", then it seems like your question is really
"how do I convert the POSIX path name given by getcwd() into the
corresponding Windows directory, if one exists".  Then you don't need to
do ANY post-processing of the string, because cygwin will have already
handed you the fully-converted name, by using cygwin_conv_path() correctly.

-- 
Eric Blake   eblake redhat com    +1-919-301-3266
Libvirt virtualization library http://libvirt.org

Attachment: signature.asc
Description: OpenPGP digital signature


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]