On Wed, 12 Jul 2006, Andr??s wrote:
> Some days ago I asked this on the mailing list, in a function like:
>
> static gboolean xunsetenv (char *varname) {
>
> if (unsetenv(varname) != 0) {
>
> return TRUE;
>
> }
>
> return FALSE;
>
> }
>
> How do replace if (unsetenv(varname) != 0) ? since OpenBSD's unsetenv is void.
>
> Jim Razmus answered that one can do:
>
> static gboolean xunsetenv (char *varname) {
>
> unsetenv (varname);
>
> return getenv (varname) == NULL ? TRUE : FALSE;
>
> }
>
> But, if I understand correctly, Thorsten Glaser rejected that answer,
> because getenv fails for different reasons.
unsetenv() can fail for several reasons:
"If the named variable does not exist in the current environment, the
environment shall be unchanged and the function is considered to have
completed successfully.
...
[EINVAL]
The name argument is a null pointer, points to an empty string, or
points to a string containing an '=' character.
"
So this should do it:
if (varname == NULL || *varname == '\0' || strchr(varname, '=') != NULL) {
errno = EINVAL;
return FALSE;
}
unsetenv(varname);
return TRUE;
I'll put fixing unsetenv on my list.
-Otto