|
Home > Archive > Unix Programming > June 2005 > portable unsetenv in c?
You are viewing an archived Text-only version of the thread.
To view this thread in it's original format and/or if you want to reply to
this thread please [click here]
| Author |
portable unsetenv in c?
|
|
| Mr. Uh Clem 2005-06-14, 5:56 pm |
| How does one portably clear an environment variable from a C
program? I see that some systems (BSD's?) have unsetenv(),
but others (Sys V's?) don't.
Am I missing something?
--
Clem
"If you push something hard enough, it will fall over."
- Fudd's first law of opposition
| |
| Pascal Bourguignon 2005-06-14, 5:57 pm |
| "Mr. Uh Clem" <uhclem@DutchElmSt.invalid> writes:
> How does one portably clear an environment variable from a C
> program? I see that some systems (BSD's?) have unsetenv(),
> but others (Sys V's?) don't.
>
> Am I missing something?
Environment variables are normal data structures in the process user space.
You can read and modify them as you want:
int main(int argc,char** argv,char** envv){
{ char** cur=envv;
while(*cur){printf("%s\n",*cur++);} }
{ char* name="LC_ALL"; /* assume we want to remove this one */
char** cur=envv;
while(*cur
&&(!strncmp(name,*cur,strlen(name))
||((*cur)[strlen(name)]!='='))){
/* not LC_ALL */
cur++; }
while(*cur){
/* we found it! Let's delete it. */
cur[0]=cur[1];
cur++;}
}
{ char** cur=envv;
while(*cur){printf("%s\n",*cur++);} }
{char* argv[2]={"env",0};
execve("env",argv,envv);}
return(0);
}
So what if unsetenv is not available? Just write your own! (or copy
and paste the source of one that gives you the freedom to do it).
--
__Pascal Bourguignon__ http://www.informatimago.com/
Nobody can fix the economy. Nobody can be trusted with their finger
on the button. Nobody's perfect. VOTE FOR NOBODY.
| |
| William Ahern 2005-06-14, 5:57 pm |
| Mr. Uh Clem <uhclem@dutchelmst.invalid> wrote:
> How does one portably clear an environment variable from a C
> program? I see that some systems (BSD's?) have unsetenv(),
> but others (Sys V's?) don't.
>
> Am I missing something?
I believe you can just remove the variable from the environ array yourself
(and move everything above it down a notch). But, I have no first-hand
evidence of the portability of this trick.
extern char **environ; /* Maybe not in a header but should exist. */
char **envp = environ;
int i;
while (envp && *envp) {
if (0 == strncmp(*envp,"foo=",sizeof "foo=" - 1)) {
i = 1;
do { envp[i - 1] = envp[i]; } while(envp[i]);
}
envp++;
}
| |
| Mr. Uh Clem 2005-06-15, 2:49 am |
| William Ahern wrote:
> Mr. Uh Clem <uhclem@dutchelmst.invalid> wrote:
>
>
>
> I believe you can just remove the variable from the environ array yourself
> (and move everything above it down a notch). But, I have no first-hand
> evidence of the portability of this trick.
>
> extern char **environ; /* Maybe not in a header but should exist. */
> char **envp = environ;
> int i;
>
> while (envp && *envp) {
> if (0 == strncmp(*envp,"foo=",sizeof "foo=" - 1)) {
> i = 1;
>
> do { envp[i - 1] = envp[i]; } while(envp[i]);
> }
>
> envp++;
> }
Not as magical as I thought. THANKS!
--
Clem
"If you push something hard enough, it will fall over."
- Fudd's first law of opposition
|
|
|
|
|