From 243f1b1c71136171a4da635a84f8e32ba3eb3c94 Mon Sep 17 00:00:00 2001 From: Eric Blake Date: Mon, 26 Oct 2009 07:10:51 -0600 Subject: env: reject bogus -u arguments * src/env.c (main): Use unsetenv rather than putenv to remove items from environ, and check for failure. * bootstrap.conf (gnulib_modules): Add unsetenv. * tests/misc/env: Test this. * NEWS: Document it. --- src/env.c | 12 +++++++++--- 1 file changed, 9 insertions(+), 3 deletions(-) (limited to 'src/env.c') diff --git a/src/env.c b/src/env.c index ee5e6b6ce..02d155d42 100644 --- a/src/env.c +++ b/src/env.c @@ -83,6 +83,7 @@ #include "system.h" #include "error.h" +#include "quote.h" /* The official name of this program (e.g., no `g' prefix). */ #define PROGRAM_NAME "env" @@ -170,14 +171,19 @@ main (int argc, char **argv) optind = 0; /* Force GNU getopt to re-initialize. */ while ((optc = getopt_long (argc, argv, "+iu:", longopts, NULL)) != -1) - if (optc == 'u') - putenv (optarg); /* Requires GNU putenv. */ + if (optc == 'u' && unsetenv (optarg)) + error (EXIT_CANCELED, errno, _("cannot unset %s"), quote (optarg)); if (optind < argc && STREQ (argv[optind], "-")) ++optind; while (optind < argc && strchr (argv[optind], '=')) - putenv (argv[optind++]); + if (putenv (argv[optind++])) + { + char *name = argv[optind - 1]; + *(strchr (name, '=')) = '\0'; + error (EXIT_CANCELED, errno, _("cannot set %s"), quote (name)); + } /* If no program is specified, print the environment and exit. */ if (argc <= optind) -- cgit v1.2.3-54-g00ecf