jacob navia <ja***@jacob.remcomp.frwrites:
Ehsan a écrit :
>I wrote this simple code for setting environmental variable in linux
but it's seems deosn' work.
/************************************************** **********************/
#include <stdio.h>
#include <stdlib.h>
int main (int argc, char * argv[]){
int i=0;
if (argc!=3){
printf("wrong command line\n");
return 1;
}
else {
i=setenv(argv[1],argv[2],1);
if (i==0)
printf("%s is set to %s \n",argv[1],argv[2]);
else if (i==-1)
printf("no enough memory");
system("/bin/sh"); /* ADD THIS HERE */
> return i;
}
}
/************************************************** *************************/
it was compiled succesfully but nothing's changed after execution of
this program. I type "env" in shell but my variables weren't added to
env list!!!!!!
thanx for oyur probable help
When the shell executes you will see your modifications
After your program is finished, you come back to the original
shell where nothing is changed. That is why you do not see
the changes
That's correct, but it's system-specific. The C standard does not
provide the "setenv" function, nor does it guarantee that "/bin/sh" is
a valid program name. Standard C provides no way to set environment
variables; given a system-specific way to do so, it doesn't specify
the scope of any such changes. There are, if I'm not mistaken,
systems on which modified environment variables will be visible after
the program terminates.
(Yes, I know the OP said he's using Linux.)
The OP was already redirected to comp.unix.programmer, and he's
already received several correct answers there, including a citation
of the Unix FAQ.
--
Keith Thompson (The_Other_Keith)
ks***@mib.org <http://www.ghoti.net/~kst>
San Diego Supercomputer Center <* <http://users.sdsc.edu/~kst>
We must do something. This is something. Therefore, we must do this.