473,326 Members | 2,061 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,326 software developers and data experts.

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

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

Jan 8 '07 #1
4 8151
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.

--
Richard Heathfield
"Usenet is a strange place" - dmr 29/7/1999
http://www.cpax.org.uk
email: rjh at the above domain, - www.
Jan 8 '07 #2
Ehsan wrote:
>
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.

--
Chuck F (cbfalconer at maineline dot net)
Available for consulting/temporary embedded and systems.
<http://cbfalconer.home.att.net>

Jan 8 '07 #3
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
Jan 8 '07 #4
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.
Jan 8 '07 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
by: Pierre | last post by:
I use the SetEnv directive in my httpd.conf (before loading LoadModule env_module /usr/lib/apache/1.3/mod_env.so) : SetEnv var value and i must have a php.ini with regsiter_globals = Off but...
2
by: david wolf | last post by:
I have a environment variable in Linux. I want to reset it to something else in my program, what I did is as follows: #include <stdlib.h> #include <stdio.h> int main(){ char *env1 =...
4
by: Erik Paul | last post by:
I got some maybe stupid questions about file output and functions. At first I have to say I'm programming on Linux. The first thing is I couldn't find a function which can tell me microseconds...
12
by: G. | last post by:
Hi all, During my degree, BEng (Hons) Electronics and Communications Engineering, we did C programming every year, but I never kept it up, as I had no interest and didn't see the point. But now...
10
by: stylecomputers | last post by:
Hey guys, I am absolutely new to Linux programming, with no w######s programming experience except a small amount of C++ console apps. Reasonably new to Linux, BSD etc, got good sound networking...
2
by: xiebopublic | last post by:
Hi all, Is there C/C++ corresponding function in Linux for Java's java.util.Locale.getCountry? Thanks! Best Regards, Xie, Bo
21
by: Chen ShuSheng | last post by:
Hey, In head file 'stdio.h', I only find the prototype of these two function but no body. Could someone pls tell me where their bodies are ? -------------- Life is magical.
89
by: Cuthbert | last post by:
After compiling the source code with gcc v.4.1.1, I got a warning message: "/tmp/ccixzSIL.o: In function 'main';ex.c: (.text+0x9a): warning: the 'gets' function is dangerous and should not be...
16
by: The Prisoner | last post by:
Hey- I was reading about interrupts, and I got to thinking it would be a real neat thing to use interrupts for program-wide exception handling. What I haven't been able to find is a clear guide...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.