public inbox for cygwin@cygwin.com
 help / color / mirror / Atom feed
From: Eric Blake <eblake@redhat.com>
To: cygwin@cygwin.com
Subject: Re: Problem with chdir and GetCurrentDirectory on Windows 2016
Date: Wed, 07 Dec 2016 21:08:00 -0000	[thread overview]
Message-ID: <a26c3edd-8221-12e3-9bc5-c03c7b13bca1@redhat.com> (raw)
In-Reply-To: <CADs2-=TCsi-vpQxa+Nhwgc6GHo734qpGh+DOYcuYo36C5r4t0g@mail.gmail.com>


[-- Attachment #1.1: Type: text/plain, Size: 1695 bytes --]

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 http://libvirt.org


[-- Attachment #2: OpenPGP digital signature --]
[-- Type: application/pgp-signature, Size: 604 bytes --]

  parent reply	other threads:[~2016-12-07 21:08 UTC|newest]

Thread overview: 12+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2016-12-07 20:23 Dipak Gaigole
2016-12-07 20:34 ` Jeffrey Altman
2016-12-07 21:03 ` Dipak Gaigole
2016-12-07 21:08 ` Eric Blake [this message]
2016-12-07 22:22   ` Dipak Gaigole
2016-12-07 23:06     ` Eric Blake
2016-12-07 23:52       ` Dipak Gaigole
2016-12-08 12:35         ` Dipak Gaigole
2016-12-08 18:50           ` Andrey Repin
     [not found]             ` <CADs2-=R_NxbDNom1MDyyBYgtCfifQ3V-D4jy2wNkHb7fUv0Org@mail.gmail.com>
2016-12-09  7:24               ` Dipak Gaigole
2016-12-09  8:42                 ` Csaba Raduly
2016-12-12  9:12                   ` Dipak Gaigole

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=a26c3edd-8221-12e3-9bc5-c03c7b13bca1@redhat.com \
    --to=eblake@redhat.com \
    --cc=cygwin@cygwin.com \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
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 read-only IMAP folder(s) and NNTP newsgroup(s).