Proper way to access executable's "environment"?

Thomas Mueller tmueller at sysgo.com
Mon May 27 14:22:03 UTC 2013


On Sun, 26 May 2013 11:06:29 -0700, David Wolfskill wrote:
> On Sun, May 26, 2013 at 07:55:03PM +0200, Stefan Ehmann wrote:
> > ...
> > > So I have a couple of questions related to the above:
> > > * Is the patch correct?...
> > 
> > Should be fine. See environ(7) or
> > http://pubs.opengroup.org/onlinepubs/9699919799/functions/environ.html:
> > 
> > "In addition, the following variable, which must be declared by the user 
> > if it is to be used directly: extern char **environ; "
> 
> Cool; thanks.
> 
> (Aside: I'd be happy to hear of plausible reasons the earlier approach
> does not appear to fail in i386.  I'm suspecting some sort of
> compatibilty shim -- which was jettisoned for amd64, probably quite
> intentionally.)

On AMD64 sizeof(void *)!=sizeof(int), thus warnings such as
   twm.c:250: warning: assignment makes pointer from integer without a cast
should trigger alarms.

 ======= GDB session
 Program received signal SIGSEGV, Segmentation fault.
 0x0000000801b60697 in strlen () from /lib/libc.so.7
 (gdb) bt
 #0  0x0000000801b60697 in strlen () from /lib/libc.so.7
 #1  0x000000000040b397 in main (argc=<value optimized out>, argv=<value optimized out>, environ=<value optimized out>)
     at twm.c:254
 (gdb) frame 1
 #1  0x000000000040b397 in main (argc=<value optimized out>, argv=<value optimized out>, environ=<value optimized out>)
     at twm.c:254
 254         HomeLen = strlen(Home);
 (gdb) l
 249     
 250         Home = getenv("HOME");
 251         if (Home == NULL)
 252             Home = "./";
 253     
 254         HomeLen = strlen(Home);
 (gdb) p Home
 $1 = 0xffffffffffffdf3a <Error reading address 0xffffffffffffdf3a: Bad address>
 =========

Similarly,
   twm.c:306: warning: incompatible implicit declaration of built-in function 'calloc'
including <stdlib.h> provides a proper declaration for calloc().

-- 
Thomas Mueller


More information about the freebsd-ports mailing list