Skip site navigation (1)Skip section navigation (2)
Date:      Sun, 16 Oct 2005 15:02:22 +1000
From:      Tony Maher <anthony.maher@uts.edu.au>
To:        hackers@freebsd.org
Subject:   getenv semantics
Message-ID:  <4351DEDE.9000900@uts.edu.au>

next 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

-- 
UTS CRICOS Provider Code:  00099F
DISCLAIMER: This email message and any accompanying attachments may contain
confidential information.  If you are not the intended recipient, do not
read, use, disseminate, distribute or copy this message or attachments.  If
you have received this message in error, please notify the sender immediately
and delete this message. Any views expressed in this message are those of the
individual sender, except where the sender expressly, and with authority,
states them to be the views the University of Technology Sydney. Before
opening any attachments, please check them for viruses and defects.



Want to link to this message? Use this URL: <https://mail-archive.FreeBSD.org/cgi/mid.cgi?4351DEDE.9000900>