On Wednesday 17 May 2017 05:08 PM, Rajula Vineet wrote:
Hi all,

I am working on improving pharo command line as a part of my GSoC project. I
have been looking at different current working directory implementations. I
have written a  blog post
<https://vineetreddy.wordpress.com/2017/05/17/pwd-vs-getcwd/>   on it. It
would be of great help, if you can take a look and give your feedback.
Vineet,

Don't depend on getenv(). It is the parent (e.g. sh) which sets PWD to the current working directory before exec-ing your program. When a child process is spawned, the parent can decide not to pass this down to child. e.g.

$ env -u PWD ./try

Or, if the child process changes its working directory using chdir(), PWD will not change. e.g.

--- try.c ---
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <limits.h>

int main(int argc, char *argv[])
{
        char cwd[PATH_MAX];

        for(;;) {
                if (getcwd(cwd, sizeof(cwd)) != NULL)
                        printf("getcwd: %s\n", cwd);
                else
                        perror("getcwd() error");
                printf("PWD: %s\n", getenv("PWD"));
                if (argv[1]) {
                        printf("chdir %s\n", argv[1]);
                        chdir(argv[1]);
                        argc--; argv++;
                }
                else
                        break;
        }
        return 0;
}
-------

Regards .. Subbu


Reply via email to