tests: work around F17-valgrind sort-stale-thread-mem failure
[coreutils.git] / src / env.c
blob20d5fb8bb7463ea04e772a537a85ca84f5c8a158
1 /* env - run a program in a modified environment
2 Copyright (C) 1986-2012 Free Software Foundation, Inc.
4 This program is free software: you can redistribute it and/or modify
5 it under the terms of the GNU General Public License as published by
6 the Free Software Foundation, either version 3 of the License, or
7 (at your option) any later version.
9 This program is distributed in the hope that it will be useful,
10 but WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 GNU General Public License for more details.
14 You should have received a copy of the GNU General Public License
15 along with this program. If not, see <http://www.gnu.org/licenses/>. */
17 /* Richard Mlynarik and David MacKenzie */
19 #include <config.h>
20 #include <stdio.h>
21 #include <sys/types.h>
22 #include <getopt.h>
24 #include "system.h"
25 #include "error.h"
26 #include "quote.h"
28 /* The official name of this program (e.g., no 'g' prefix). */
29 #define PROGRAM_NAME "env"
31 #define AUTHORS \
32 proper_name ("Richard Mlynarik"), \
33 proper_name ("David MacKenzie")
35 static struct option const longopts[] =
37 {"ignore-environment", no_argument, NULL, 'i'},
38 {"null", no_argument, NULL, '0'},
39 {"unset", required_argument, NULL, 'u'},
40 {GETOPT_HELP_OPTION_DECL},
41 {GETOPT_VERSION_OPTION_DECL},
42 {NULL, 0, NULL, 0}
45 void
46 usage (int status)
48 if (status != EXIT_SUCCESS)
49 emit_try_help ();
50 else
52 printf (_("\
53 Usage: %s [OPTION]... [-] [NAME=VALUE]... [COMMAND [ARG]...]\n"),
54 program_name);
55 fputs (_("\
56 Set each NAME to VALUE in the environment and run COMMAND.\n\
57 \n\
58 -i, --ignore-environment start with an empty environment\n\
59 -0, --null end each output line with 0 byte rather than newline\n\
60 -u, --unset=NAME remove variable from the environment\n\
61 "), stdout);
62 fputs (HELP_OPTION_DESCRIPTION, stdout);
63 fputs (VERSION_OPTION_DESCRIPTION, stdout);
64 fputs (_("\
65 \n\
66 A mere - implies -i. If no COMMAND, print the resulting environment.\n\
67 "), stdout);
68 emit_ancillary_info ();
70 exit (status);
73 int
74 main (int argc, char **argv)
76 int optc;
77 bool ignore_environment = false;
78 bool opt_nul_terminate_output = false;
80 initialize_main (&argc, &argv);
81 set_program_name (argv[0]);
82 setlocale (LC_ALL, "");
83 bindtextdomain (PACKAGE, LOCALEDIR);
84 textdomain (PACKAGE);
86 initialize_exit_failure (EXIT_CANCELED);
87 atexit (close_stdout);
89 while ((optc = getopt_long (argc, argv, "+iu:0", longopts, NULL)) != -1)
91 switch (optc)
93 case 'i':
94 ignore_environment = true;
95 break;
96 case 'u':
97 break;
98 case '0':
99 opt_nul_terminate_output = true;
100 break;
101 case_GETOPT_HELP_CHAR;
102 case_GETOPT_VERSION_CHAR (PROGRAM_NAME, AUTHORS);
103 default:
104 usage (EXIT_CANCELED);
108 if (optind < argc && STREQ (argv[optind], "-"))
109 ignore_environment = true;
111 if (ignore_environment)
113 static char *dummy_environ[] = { NULL };
114 environ = dummy_environ;
117 optind = 0; /* Force GNU getopt to re-initialize. */
118 while ((optc = getopt_long (argc, argv, "+iu:0", longopts, NULL)) != -1)
119 if (optc == 'u' && unsetenv (optarg))
120 error (EXIT_CANCELED, errno, _("cannot unset %s"), quote (optarg));
122 if (optind < argc && STREQ (argv[optind], "-"))
123 ++optind;
125 char *eq;
126 while (optind < argc && (eq = strchr (argv[optind], '=')))
128 if (putenv (argv[optind]))
130 *eq = '\0';
131 error (EXIT_CANCELED, errno, _("cannot set %s"),
132 quote (argv[optind]));
134 optind++;
137 /* If no program is specified, print the environment and exit. */
138 if (argc <= optind)
140 char *const *e = environ;
141 while (*e)
142 printf ("%s%c", *e++, opt_nul_terminate_output ? '\0' : '\n');
143 exit (EXIT_SUCCESS);
146 if (opt_nul_terminate_output)
148 error (0, errno, _("cannot specify --null (-0) with command"));
149 usage (EXIT_CANCELED);
152 execvp (argv[optind], &argv[optind]);
155 int exit_status = (errno == ENOENT ? EXIT_ENOENT : EXIT_CANNOT_INVOKE);
156 error (0, errno, "%s", argv[optind]);
157 exit (exit_status);