How do you change directories in a c program using environmental variables?

532 views Asked by At

I tried using setenv("CWD", path, 1), but it keeps saying implicit declaration even though I included stdlib.h.

1

There are 1 answers

2
alk On

Assuming the compiler warns about the "implicit declaration" of setenv() and not of path you need to make the prototype to setenv() available to the compiler by #defineing one of the following:

_BSD_SOURCE

or

_POSIX_C_SOURCE >= 200112L

or

_XOPEN_SOURCE >= 600

Also you cannot set the current work directory by setting the environment variable CWD. Use chdir() instead.