getenv

In the following test case:

#include <stdio.h>
#include <stdlib.h>

int main(void) {

char *p;

p = getenv(“PWD”);
if ( p )
printf(“PWD = %s\n”, p );
else
printf(“no PWD\n”);

p = getenv(“PROCESSOR”);
if ( p )
printf(“PROCESSOR = %s\n”, p );
else
printf(“no PROCESSOR\n”);

return 0;

}


PWD is NULL while PROCESSOR is not? It seems I can access any environment
variable but PWD?

  • Mario

Mario Charest postmaster@127.0.0.1 wrote:

In the following test case:

#include <stdio.h
#include <stdlib.h

int main(void) {

char *p;

p = getenv(“PWD”);
if ( p )
printf(“PWD = %s\n”, p );
else
printf(“no PWD\n”);

p = getenv(“PROCESSOR”);
if ( p )
printf(“PROCESSOR = %s\n”, p );
else
printf(“no PROCESSOR\n”);

return 0;

}


PWD is NULL while PROCESSOR is not? It seems I can access any environment
variable but PWD?

  • Mario

maybe PWD is not exported? try running
export PWD
before you run your test program.

PWD is NULL while PROCESSOR is not? It seems I can access any
environment
variable but PWD?

  • Mario



    maybe PWD is not exported? try running export PWD before you run your test
    program.

Hum, indeed it’s not exported. Is that normal system behavior for PWD?

At anyrate I’ve change the code I’m porting from getenv(“PWD”) to getcwd()
which is probably more portable.

  • Mario

Mario Charest wrote:

Hum, indeed it’s not exported. Is that normal system behavior for PWD?

At anyrate I’ve change the code I’m porting from getenv(“PWD”) to getcwd()
which is probably more portable.

PWD isn’t exported because it applies only to the current process,
not the whole process tree.

Use “getcwd”. If you change the working directory with “chdir”,
that doesn’t update the environment variable “PWD”. “PWD” is
only updated by the shells.

John Nagle