From: Oscar F. <of...@wa...> - 2002-09-18 18:44:39
|
Daniel Taupin <ta...@wa...> writes: > If, inside a program XXX, I code > > putenv("set environt_var=its_value"); > > and then > > system("some-program.exe"); > > will the program some-program.exe have "environt_var" set to the > specified value? Changes introduced by 'putenv' affects child processes. Now, I'm not 100% sure if processes created by 'system' are considered child processes or independent ones. Let's perform a quick check: #include <stdlib.h> #include <stdio.h> #include <string.h> int main(int argc, char *argv[]) { char command[100]; command[0] = 0; if(argc > 1) { printf("FOO value on secondary process: %s\n", getenv("FOO")); } else { printf("Setting FOO to BAR on initial process.\n"); putenv("FOO=BAR"); strcat(command, argv[0]); /* This program's name */ strcat(command, " dummy"); printf("Calling system...\n"); system(command); } } See the output of the above program. > Second question: will the variable "environt_var" stay at "its_value" > when XXX has terminated. A child process can't change the environment variables of its parent process. This includes the case when the parent process is a shell and the child process is a command issued to the shell. > Note that I ask that quesiton to know whether that coding could have a > behaviour different of what happens when using "system(...)" to invoque > a BATCH file which performs the two operations. > > Last question: is that valid whatever the system: NT, 9x, 2k, XP ? It should. *nix systems too. -- Oscar |