need some info about unsetenv on other platforms

View: New views
4 Messages — Rating Filter:   Alert me  

need some info about unsetenv on other platforms

by Guenter Knauf-2 :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

Hi,
I was just looking into the setenv / unsetenv stuff in APR, and found
that we assume that unsetenv doesnt have a return value on all
platforms. I have searched a bit, and found these:
http://linux.die.net/man/3/setenv
http://www.manpagez.com/man/3/unsetenv/
from that it seems that there are newer versions of unsetenv() out which
return an int and set errno. I've tested this on Linux 2.6.27.29, and I
dont get an error back when I try to unsetenv a non-existent var ...
now I would like to get some info about how MacOSX and *BSD platforms
behave - see attached sample for a quick test. Form the docu at least
MacOSX should return EINVAL for a non-existent var ...

thanks, Gün.


#include <stdio.h>
#include <stdlib.h>
#include <errno.h>

int main(void) {
  char *envvar = "FOOBAR_EMPTY";
  char *value = "";
  int rv;

  rv = setenv(envvar, value, 1);
  printf("setenv(%s) rv = %d, errno = %d\n", envvar, rv, errno);

  rv = unsetenv(envvar);
  printf("unsetenv(%s) rv = %d, errno = %d\n", envvar, rv, errno);

  envvar = "FOOBAR_NONEXISTING";
  rv = unsetenv(envvar);
  printf("unsetenv(%s) rv = %d, errno = %d\n", envvar, rv, errno);

  return 0;
}


Re: need some info about unsetenv on other platforms

by Jonathan Leffler-2 :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message



On Tue, Oct 13, 2009 at 8:32 AM, Guenter Knauf <fuankg@...> wrote:
Hi,
I was just looking into the setenv / unsetenv stuff in APR, and found
that we assume that unsetenv doesnt have a return value on all
platforms. I have searched a bit, and found these:
http://linux.die.net/man/3/setenv
http://www.manpagez.com/man/3/unsetenv/
from that it seems that there are newer versions of unsetenv() out which
return an int and set errno. I've tested this on Linux 2.6.27.29, and I
dont get an error back when I try to unsetenv a non-existent var ...
now I would like to get some info about how MacOSX and *BSD platforms
behave - see attached sample for a quick test. Form the docu at least
MacOSX should return EINVAL for a non-existent var ...

thanks, Gün.


MacOS X 10.5.8 (MacBook Pro):

Osiris JL: gcc -o testenv -Wall testenv.c
Osiris JL: ./testenv
setenv(FOOBAR_EMPTY) rv = 0, errno = 0
unsetenv(FOOBAR_EMPTY) rv = 0, errno = 0
unsetenv(FOOBAR_NONEXISTING) rv = 0, errno = 0
Osiris JL:


Solaris 10 (SPARC )

Black JL: gcc -o testenv testenv.c -Wall
Black JL: ./testenv
setenv(FOOBAR_EMPTY) rv = 0, errno = 0
unsetenv(FOOBAR_EMPTY) rv = 0, errno = 0
unsetenv(FOOBAR_NONEXISTING) rv = 0, errno = 0
Black JL:




--
Jonathan Leffler <jonathan.leffler@...>  #include <disclaimer.h>
Guardian of DBD::Informix - v2008.0513 - http://dbi.perl.org
"Blessed are we who can laugh at ourselves, for we shall never cease to be amused."

Re: need some info about unsetenv on other platforms

by Jonathan Leffler-2 :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

Some parts of this message have been removed. Learn more about Nabble's security policy.


On Tue, Oct 13, 2009 at 8:32 AM, Guenter Knauf <fuankg@...> wrote:
Hi,
I was just looking into the setenv / unsetenv stuff in APR, and found
that we assume that unsetenv doesnt have a return value on all
platforms. I have searched a bit, and found these:
http://linux.die.net/man/3/setenv
http://www.manpagez.com/man/3/unsetenv/
from that it seems that there are newer versions of unsetenv() out which
return an int and set errno. I've tested this on Linux 2.6.27.29, and I
dont get an error back when I try to unsetenv a non-existent var ...
now I would like to get some info about how MacOSX and *BSD platforms
behave - see attached sample for a quick test. Form the docu at least
MacOSX should return EINVAL for a non-existent var ...


POSIX has a different view on the correct behaviour:

NAME

unsetenv - remove an environment variable

SYNOPSIS

[CX] [Option Start] #include <stdlib.h>

int unsetenv(const char *
name); [Option End]

DESCRIPTION

The unsetenv() function shall remove an environment variable from the environment of the calling process. The name argument points to a string, which is the name of the variable to be removed. The named argument shall not contain an '=' character. 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.

If the application modifies environ or the pointers to which it points, the behavior of unsetenv() is undefined. The unsetenv() function shall update the list of pointers to which environ points.

The unsetenv() function need not be thread-safe.

RETURN VALUE

Upon successful completion, zero shall be returned. Otherwise, -1 shall be returned, errno set to indicate the error, and the environment shall be unchanged.

ERRORS

The unsetenv() function shall fail if:

[EINVAL]
The name argument is a null pointer, points to an empty string, or points to a string containing an '=' character.
 

Note that unsetting a non-existent variable is explicitly documented as a successful no-op.  This info from

http://www.opengroup.org/onlinepubs/9699919799/functions/unsetenv.html

--
Jonathan Leffler <jonathan.leffler@...>  #include <disclaimer.h>
Guardian of DBD::Informix - v2008.0513 - http://dbi.perl.org
"Blessed are we who can laugh at ourselves, for we shall never cease to be amused."

Re: need some info about unsetenv on other platforms

by Guenter Knauf-2 :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

Hi,
Jonathan Leffler schrieb:

> POSIX has a different view on the correct behaviour:
>
>
>         NAME
>
>     unsetenv - remove an environment variable
>
>
>         SYNOPSIS
>
>     |^[CX <javascript:open_code('CX')>] [Option Start] #include
>     <stdlib.h
>     <http://www.opengroup.org/onlinepubs/9699919799/basedefs/stdlib.h.html>>
>
>     int unsetenv(const char */name/); [Option End]|
>
>
>         DESCRIPTION
>
>     The /unsetenv/() function shall remove an environment variable from
>     the environment of the calling process. The /name/ argument points
>     to a string, which is the name of the variable to be removed. The
>     named argument shall not contain an '=' character. 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.
>
>     If the application modifies /environ/ or the pointers to which it
>     points, the behavior of /unsetenv/() is undefined. The /unsetenv/()
>     function shall update the list of pointers to which /environ/ points.
>
>     The /unsetenv/() function need not be thread-safe.
>
>
>         RETURN VALUE
>
>     Upon successful completion, zero shall be returned. Otherwise, -1
>     shall be returned, /errno/ set to indicate the error, and the
>     environment shall be unchanged.
>
>
>         ERRORS
>
>     The /unsetenv/() function shall fail if:
>
>     [EINVAL]
>         The /name/ argument is a null pointer, points to an empty
>         string, or points to a string containing an '=' character.
>
>  
>
> Note that unsetting a non-existent variable is explicitly documented as
> a successful no-op.  This info from
>
> http://www.opengroup.org/onlinepubs/9699919799/functions/unsetenv.html
thanks very much for testing and the docu pointer!
Crazy that MacOSX documents its own way, but then actually seems to
behave posix-conform as desribed above :)

Got another mail that FreeBSD 7.2 behaves same too, so seems it makes no
sense to check for the return value; on NetWare I get -1 and errno 77
with a non-existent variable.

Gün.