getenv semantics

Danny Braniss danny at cs.huji.ac.il
Sat Oct 15 23:47:43 PDT 2005


> Hello,
> 
> I am trying to create a port of some 3rd party software and while
> I can get it to compile ok and (mostly) run there are a few anomalies in
> it detecting environment variables.  It appears to run ok on linux (I do
> not have a convenient linux box for testing with).  I believe its the
> way the code get the environment variables that is the cause. But if
> thats the case then it would appear that getenv semantics differs 
> slightly on different platforms.
> 
>  From http://notabug.com/2002/coherent/man/getenv.html
>      "When VARIABLE is not found or has no value, getenv() returns NULL."
> 
> But on FreeBSD it would appear that if VARIABLE is found but has no
> value it returns a pointer to a NUL ('\0') string.
> 
> Is this analysis correct?  Can someone point me to the (a?) standard
> that describes this.  The FreeBSD behaviour makes sense, I am trying to 
> understand what is the expected behaviour on other platforms.
> 
> --------------------------------------------------------------------------
> #!/bin/sh
> 
> echo "unset foobar"
> unset foobar
> ./test-getenv
> 
> echo "export foobar"
> export foobar
> ./test-getenv
> 
> echo "export foobar=isset"
> export foobar=isset
> ./test-getenv
> 
> echo "unset foobar"
> unset foobar
> ./test-getenv
> 
> ----------------------------------------------------------------------------
> 
> 
> /* test-getenv.c */
> 
> #include <stdio.h>
> #include <stdlib.h>
> 
> int
> main(int argc, char *argv[])
>    {
>    char *p;
>    p = getenv("foobar");
>    fprintf(stderr, "getenv('foobar') string is >%s< pointer is 
>  >%x<\n",p, p);
>    exit(0);
>    }
> -------------------------------------------------------------------------------
> 
>   ./test-getenv.sh
> unset foobar
> getenv('foobar') string is >(null)< pointer is >0<
> export foobar
> getenv('foobar') string is >< pointer is >bfbfe933<
> export foobar=isset
> getenv('foobar') string is >isset< pointer is >bfbfe92f<
> unset foobar
> getenv('foobar') string is >(null)< pointer is >0<
> 
> thanks
> -- 
> tonym

now why would FreeBSD supply sources?
from /usr/src/lib/libc/stlib/getenv.c:
...
/*
 * getenv --
 *      Returns ptr to value associated with name, if any, else NULL.
 */
char *
getenv(name)
        const char *name;
{
        int offset;

        return (__findenv(name, &offset));
}

danny




More information about the freebsd-hackers mailing list