bug-coreutils
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: env behavior incorrect


From: Jim Meyering
Subject: Re: env behavior incorrect
Date: Tue, 03 Jun 2003 10:29:15 +0200

Jens Elkner <address@hidden> wrote:
...
> Furthermore according to XBD Utility Syntax Guidelines, long option support
> (i.e. --ignore-environment,  --unset) should be dropped.

An application may accept long-named options like --ignore-environment
and still be POSIX compliant.  There is no reason to remove such options.

> Last but not least - why not
>     ...
>     static char *pointer2null = NULL;
>     ...
>     main(....) {
>       ...
>       if (ignore_environment)
>           environ = &pointer2null;
>       ...
>     }
>     and drop the dummy_environ/uneccessary env copy stuff ... ?

Thank you for that suggestion.

Looking at that environment-copying code, at first I wondered
if it was there (before the second getopt loop) to ensure that
any envvars used by getopt_long were still set for the calls in
the second loop.  But the only envvar that getopt_long references
is POSIXLY_CORRECT, and it can't affect that particular call to
getopt_long, since the option string starts with `+'.

So I don't see any point in copying the environment, and have
checked in the change below.

        Avoid unnecessary copying of environment.
        * src/env.c (main): Rather than clearing the environment and --
        unless told to ignore environment -- copying all settings from
        the saved, original environment, clear the environment only when
        that is requested.  Suggested by Jens Elkner.

Index: env.c
===================================================================
RCS file: /fetish/cu/src/env.c,v
retrieving revision 1.49
diff -u -p -u -p -r1.49 env.c
--- env.c       10 May 2003 20:01:32 -0000      1.49
+++ env.c       3 Jun 2003 07:43:52 -0000
@@ -139,7 +139,6 @@ A mere - implies -i.  If no COMMAND, pri
 int
 main (register int argc, register char **argv, char **envp)
 {
-  char *dummy_environ[1];
   int optc;
   int ignore_environment = 0;
 
@@ -171,12 +170,11 @@ main (register int argc, register char *
   if (optind < argc && !strcmp (argv[optind], "-"))
     ignore_environment = 1;
 
-  environ = dummy_environ;
-  environ[0] = NULL;
-
-  if (!ignore_environment)
-    for (; *envp; envp++)
-      putenv (*envp);
+  if (ignore_environment)
+    {
+      static char *dummy_environ[] = { NULL };
+      environ = dummy_environ;
+    }
 
   optind = 0;                  /* Force GNU getopt to re-initialize. */
   while ((optc = getopt_long (argc, argv, "+iu:", longopts, NULL)) != -1)




reply via email to

[Prev in Thread] Current Thread [Next in Thread]