Proper way to access executable's "environment"?
Stefan Ehmann
shoesoft at gmx.net
Sun May 26 17:55:14 UTC 2013
On 05/26/2013 19:20, David Wolfskill wrote:
> A while back, I volunteered to maintain x11-wm/piewm, as it had been
> assigned to "ports" and I'm one of the very few folks I know who uses
> it.
...
> So I hacked twm.c, per:
>
> --- twm.c 1998-06-12 13:28:07.000000000 -0700
> +++ twm.c 2013-05-10 21:02:32.000000000 -0700
> @@ -93,6 +93,7 @@
> #endif
>
> #include <stdio.h>
> +#include <stdlib.h>
> #include <signal.h>
> #include <fcntl.h>
> #include "twm.h"
> @@ -177,6 +178,8 @@
>
> unsigned long black, white;
>
> +extern char **environ;
> +
> /***********************************************************************
> *
> * Procedure:
> @@ -186,7 +189,7 @@
> */
>
> int
> -main(int argc, char **argv, char **environ)
> +main(int argc, char **argv)
> {
> Window root, parent, *children;
> unsigned int nchildren;
>
>
> rebuilt piewm, et voilà: no crash. :-)
>
> I thought this was encouraging, and sent a note to Russ Nelson at
> crynwr.com -- the upstream site. That was on 10 May; I've had no
> response.
>
> I subsequently rebuild piewm with the patch applied for my usual
> FreeBSD/i386 environment, and it's (also) been trouble-free. (That
> said, I don't actually do anything with the environment for piewm.
> Indeed, in nearly all respects, I could use tvtwm without any difference
> -- and those that would show up are too arcane to describe here.)
>
> So I have a couple of questions related to the above:
> * Is the patch correct? I don't do much with C, and even less with C
> code that accesses the environment. (When I write code, it's normally
> scripts, mostly in /bin/sh.) getenv(3) mentioned the "#include
> <stdlib.h>", so I did that based on theat man page. I didn't see
> anything about declaring environ as an "extern char **", but wasn't
> keen on making more changes than necessary to the code. (Mind, I
> fully support making "necessary" -- or even "strongly advised" --
> changes.)
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; "
Including <stdlib.h> is not necessary to access the environ variable.
--
Stefan
More information about the freebsd-ports
mailing list