Skip site navigation (1)Skip section navigation (2)
Date:      Sun, 16 Oct 2005 08:47:40 +0200
From:      Danny Braniss <danny@cs.huji.ac.il>
To:        Tony Maher <anthony.maher@uts.edu.au>
Cc:        hackers@freebsd.org
Subject:   Re: getenv semantics 
Message-ID:  <E1ER2J2-000Cyc-Qy@cs1.cs.huji.ac.il>
In-Reply-To: Message from Tony Maher <anthony.maher@uts.edu.au>  of "Sun, 16 Oct 2005 15:02:22 %2B1000." <4351DEDE.9000900@uts.edu.au> 

next in thread | previous in thread | raw e-mail | index | archive | help
> 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





Want to link to this message? Use this URL: <https://mail-archive.FreeBSD.org/cgi/mid.cgi?E1ER2J2-000Cyc-Qy>