how to use setenv function in GNU C for programming in linux?

E

Ehsan

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");
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
 
R

Richard Heathfield

Ehsan said:
I wrote this simple code for setting environmental variable in linux
but it's seems deosn' work.

If you ask the kind folks in comp.unix.programmer, they can explain to you
why it does in fact work, but not in the way you want it to.
 
C

CBFalconer

Ehsan said:
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);

That could have something to do with using the non-existing
setenv() function. Remember, we only discuss portable standard C
here. Things might be different on comp.unix.programmer.
 
J

jacob navia

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

jacob
 
K

Keith Thompson

jacob navia said:
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.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Staff online

Members online

Forum statistics

Threads
473,770
Messages
2,569,583
Members
45,072
Latest member
trafficcone

Latest Threads

Top