Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

How can I determine the current default/working directory?

2 views
Skip to first unread message

Ramon F Herrera

unread,
Dec 27, 2009, 12:10:19 AM12/27/09
to

The closest I have is this:

getenv("PWD");

But that only tells the value of such environment variable at the time
the program was started.

I need to know (for debugging purposes) the current working directory,
dynamically.

TIA,

-Ramon

Ramon F Herrera

unread,
Dec 27, 2009, 12:12:12 AM12/27/09
to

Bonus question:

Are the commands 'pushd' and 'popd' implemented programmatically?

Gordon Burditt

unread,
Dec 27, 2009, 1:06:25 AM12/27/09
to
>The closest I have is this:
>
>getenv("PWD");

That can lie if the invoker of your program is malicious.
The invoker of your program might not be a shell, and it might
not know to update $PWD or even provide it.

>But that only tells the value of such environment variable at the time
>the program was started.

Look at getcwd() (POSIX). This can fail due to permissions problems
(having x but not r permission on directories, for example.)

Ramon F Herrera

unread,
Dec 27, 2009, 2:17:23 AM12/27/09
to
On Dec 27, 1:06 am, gordonb.pr...@burditt.org (Gordon Burditt) wrote:
> >The closest I have is this:
>
> >getenv("PWD");
>
> That can lie if the invoker of your program is malicious.
> The invoker of your program might not be a shell, and it might
> not know to update $PWD or even provide it.
>
> >But that only tells the value of such environment variable at the time
> >the program was started.
>


> Look at getcwd() (POSIX).

My program works great now, thanks Gordon!

-Ramon


Ramon F Herrera

unread,
Dec 27, 2009, 10:04:09 AM12/27/09
to

I guess a good way to achieve the above is through a combination of
`getcwd()' and a std::vector which behaves like a stack. That should
be simple enough, at least for those of the C++ persuasion.

Thx,

-Ramon


0 new messages