Click to See Complete Forum and Search --> : Confused about setenv()..
nobby
08-04-2001, 11:07 AM
I recently received a University assignment that requires us amongst other things to set environment variables.
This has got a pretty stuck although I have read the man's etc. I still can't get a simple program to write to the env.
The below code I assumed would set an environment variable, although I doesn't.
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define BUFFER 20
int main(void) {
int rc;
char name[BUFFER], value[BUFFER];
char *envName, *envValue;
fgets(name, BUFFER, stdin);
envName = strtok(name, "\t \n");
fgets(value, BUFFER, stdin);
envValue = strtok(value, "\t \n");
/* Should this part work ?*/
rc = setenv(envName, envValue, 1);
return rc;
}
I assumed that with setenv(...) this would then update the env, just as it would if I used the bash cmd "setenv NAME VALUE". However this doesn't seem to be the case.
P.S Might it possibly be something to do with the shell ?
Thanks
~ n0bbY ~
The Kooman
08-06-2001, 12:14 AM
Originally posted by nobby:
<STRONG>I assumed that with setenv(...) this would then update the env, just as it would if I used the bash cmd "setenv NAME VALUE". However this doesn't seem to be the case.
P.S Might it possibly be something to do with the shell ?</STRONG>
Yup - it does! ... That is if I understand correctly what you're trying to do!
My guess is that you expect to run your program which sets some environment variable and then, after exit
ing from your program, you expect your newly set variable to be visible in your shell. Am I right?
If that is the case then the reason your program won't run is as follows:
You type the command to execute your application at the shell prompt
Shell fork()s, giving another child shell which duplicates the parent data space (which includes your environment)
Shell execs()s your application with certain data elements still shared - file descriptors and environment
Your application does the setenv() which affects its environment and not the parent environ
ment
Your application quits - the child process has now terminated ... its data space is now freed bac
k to the OS
You get your shell prompt back - which has the environment of your parent process.
You never set a variable in this environment - you had set it in the child environment which does not exi
st anymore!
You access your environment variable here. Obviously its not defined :)!
So, setenv does not "update the env, just as it would if you used the bash cmd." It only sets the environment variable in its own environment space which is available only to itself and to the child programs it may subsequently fork().
Hopefully this clears your doubt!
pinoy
08-06-2001, 02:05 AM
As dchkoo explained. You can't set export environments to your parent. You can do it for your children though.
Run your command in the current shell context using the '.' command under BASH.
e.g.
$ . ./program
nobby
08-06-2001, 03:07 AM
Fanastic thanks for the replies, that's exactly what I was trying to do dchkoo. :D
I did get it working, once I clean it up a little more I'll post it up incase anyone wishes to see it.
~ n0bbY ~
The Kooman
08-06-2001, 07:21 AM
Originally posted by LloydM:
<STRONG>Run your command in the current shell context using the '.' command under BASH.
e.g.
$ . ./program
</STRONG>
That won't work! The '.' command is just an alias for the "source" command which just sources a shell script. But in this case, we have a binary executable and not a shell script!
BTW, you guys can simply call me "koo" :D!
pinoy
08-06-2001, 05:54 PM
Yes you're right koo. binaries still does involve a fork and exec.