Skip site navigation (1)Skip section navigation (2)
Date:      Mon, 27 May 2013 18:33:15 +0200
From:      Stefan Ehmann <shoesoft@gmx.net>
To:        freebsd-ports@freebsd.org
Subject:   Re: Proper way to access executable's "environment"?
Message-ID:  <51A38ACB.8060008@gmx.net>
In-Reply-To: <20130527133259.GF1334@albert.catwhisker.org>
References:  <20130526172015.GA1334@albert.catwhisker.org> <20130527054313.372869c1@X220.ovitrap.com> <20130527133259.GF1334@albert.catwhisker.org>

next in thread | previous in thread | raw e-mail | index | archive | help
On 05/27/2013 15:32, David Wolfskill wrote:
> On Mon, May 27, 2013 at 05:43:13AM +0700, Erich Dollansky wrote:
>> ...
>>> piewm's twm.c declares main as:
>>>
>>> int
>>> main(int argc, char **argv, char **environ)
>>> {
>>
>> I use this concept since decades but with different names. Could it be
>> a problem of overlapping names?
>> ...
>
> It was pointed out to me that -- other than assigning "Environ =
> environ" -- the code in piewm's twm.c didn't actually use the values of
> environ or Environ.  [Thanks, Stefan!]
>
> Elsewhere, there is a putenv() implementation for environments that
> lack it, and the code uses getenv(), as well.
>
> Doing a few more comparisons with twm.c from tvtwm and from twm itself;
> I have a few more observations:
>
> * tvtwm's twm.c is more recent than that of piewm:
>    - * $XConsortium: twm.c,v 1.124 91/05/08 11:01:54 dave Exp $
>    + * $XConsortium: twm.c,v 1.111 90/03/23 13:23:34 jim Exp $
>
> * twm's twm.c has a copyright block dated 2005 from Hitachi, Ltd.
>
> * Merely inserting the "#include <stdlib.h>" in piewm's twm.c appears
>    to be a minimal effective change: Once that's done, the SIGSEGV goes
>    away.
>
> * Neither tvtwm's nor twm's twm.c has the "#include <stdlib.h>" (and
>    neither gets a SIGSEGV).
>
> * As Stefan pointed out, I was able to completely remove the references
>    to both "environ" and "Environ" from piewm's twm.c; the result builds
>    and runs without problem.
>
> * tvtwm's twm.c has these environ and Environ variables (and, as above,
>    lacks the "#include <stdlib.h>" and doesn't get the SIGSEGV).
>
> * twm's twm.c lacks the environ and Environ variables (and main() is
>    defined as a function that takes but 2 arguments).
>
> I suspect that I'm failing to understand at least part of what's causing
> the actual problem in piewm.

The environ variable had nothing to do with the segfault.
The third argument to main() is supported historically, but it's not 
standardized. Using the extern declaration is more portable. But 
apparently the main argument just works fine in FreeBSD.

Thomas explained why getenv() causes a segfault when <stdlib.h> is not 
included. This is the actual problem that needs to be fixed.

twm.c indirectly includes <stdlib.h> via twm.h. So that's why tvtwm and 
twm work fine.

IOW, just including <stdlib.h> should be the right fix.

Hope all mysteries are solved now :)

-- 
Stefan



Want to link to this message? Use this URL: <https://mail-archive.FreeBSD.org/cgi/mid.cgi?51A38ACB.8060008>