Problem with chdir and GetCurrentDirectory on Windows 2016

Eric Blake
Wed Dec 7 21:08:00 GMT 2016

On 12/07/2016 02:23 PM, Dipak Gaigole wrote:
> Hello,
> I am facing a very strange problem with chdir and GetCurrentDirectory.

Not strange at all.  The two are incompatible.  When writing Cygwin
programs, stick to the POSIX-y interface, NOT the windows interface.

> After calling chdir (), the call to GetCurrentDirectory () returns

chdir() is POSIX, and the POSIX counterpart is getcwd(), which will
return the right value always.  GetCurrentDirectory() is the windows
API, which may or may not have a sane value at any given time, but more
often than not does NOT match the value in getcwd(), and that is by
design - because the Cygwin notion of the working directory is more
powerful than the windows notion of the working directory, and it is
just too expensive for cygwin to try and always keep windows up-to-date
with the current directory when there are times that you can be in a
cygwin directory that has no windows counterpart.

> Administrator@windows2k16vika /cygdrive/c/src
> $ cat test_cwd.c
> #include <stdio.h>
> #include <unistd.h>
> #include <windows.h>

Any code that does this is already suspect.

> #define BUF_SIZE 512
> int main()
> {
>     int ret;
>     char *dir;
>     char dirname [BUF_SIZE];
>     dir = "/cygdrive/c/Program Files";
>     ret = chdir (dir);
>     fprintf (stderr, "chdir (%s) reuturned <%d>\n", dir, ret);
>     ret = GetCurrentDirectory(BUF_SIZE, dirname);

Again, DON'T use windows API calls from a cygwin program, use POSIX
calls instead. You WANT to use getcwd() here.

Eric Blake   eblake redhat com    +1-919-301-3266
Libvirt virtualization library

-------------- next part --------------
A non-text attachment was scrubbed...
Name: signature.asc
Type: application/pgp-signature
Size: 604 bytes
Desc: OpenPGP digital signature
URL: <>

More information about the Cygwin mailing list