version 8.26
[coreutils.git] / src / env.c
blobe1f79d6c2d41c535ade54a7a08f481c2d2912359
1 /* env - run a program in a modified environment
2 Copyright (C) 1986-2016 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 "die.h"
26 #include "error.h"
27 #include "quote.h"
29 /* The official name of this program (e.g., no 'g' prefix). */
30 #define PROGRAM_NAME "env"
32 #define AUTHORS \
33 proper_name ("Richard Mlynarik"), \
34 proper_name ("David MacKenzie")
36 static struct option const longopts[] =
38 {"ignore-environment", no_argument, NULL, 'i'},
39 {"null", no_argument, NULL, '0'},
40 {"unset", required_argument, NULL, 'u'},
41 {GETOPT_HELP_OPTION_DECL},
42 {GETOPT_VERSION_OPTION_DECL},
43 {NULL, 0, NULL, 0}
46 void
47 usage (int status)
49 if (status != EXIT_SUCCESS)
50 emit_try_help ();
51 else
53 printf (_("\
54 Usage: %s [OPTION]... [-] [NAME=VALUE]... [COMMAND [ARG]...]\n"),
55 program_name);
56 fputs (_("\
57 Set each NAME to VALUE in the environment and run COMMAND.\n\
58 "), stdout);
60 emit_mandatory_arg_note ();
62 fputs (_("\
63 -i, --ignore-environment start with an empty environment\n\
64 -0, --null end each output line with NUL, not newline\n\
65 -u, --unset=NAME remove variable from the environment\n\
66 "), stdout);
67 fputs (HELP_OPTION_DESCRIPTION, stdout);
68 fputs (VERSION_OPTION_DESCRIPTION, stdout);
69 fputs (_("\
70 \n\
71 A mere - implies -i. If no COMMAND, print the resulting environment.\n\
72 "), stdout);
73 emit_ancillary_info (PROGRAM_NAME);
75 exit (status);
78 int
79 main (int argc, char **argv)
81 int optc;
82 bool ignore_environment = false;
83 bool opt_nul_terminate_output = false;
85 initialize_main (&argc, &argv);
86 set_program_name (argv[0]);
87 setlocale (LC_ALL, "");
88 bindtextdomain (PACKAGE, LOCALEDIR);
89 textdomain (PACKAGE);
91 initialize_exit_failure (EXIT_CANCELED);
92 atexit (close_stdout);
94 while ((optc = getopt_long (argc, argv, "+iu:0", longopts, NULL)) != -1)
96 switch (optc)
98 case 'i':
99 ignore_environment = true;
100 break;
101 case 'u':
102 break;
103 case '0':
104 opt_nul_terminate_output = true;
105 break;
106 case_GETOPT_HELP_CHAR;
107 case_GETOPT_VERSION_CHAR (PROGRAM_NAME, AUTHORS);
108 default:
109 usage (EXIT_CANCELED);
113 if (optind < argc && STREQ (argv[optind], "-"))
114 ignore_environment = true;
116 if (ignore_environment)
118 static char *dummy_environ[] = { NULL };
119 environ = dummy_environ;
122 optind = 0; /* Force GNU getopt to re-initialize. */
123 while ((optc = getopt_long (argc, argv, "+iu:0", longopts, NULL)) != -1)
124 if (optc == 'u' && unsetenv (optarg))
125 die (EXIT_CANCELED, errno, _("cannot unset %s"), quote (optarg));
127 if (optind < argc && STREQ (argv[optind], "-"))
128 ++optind;
130 char *eq;
131 while (optind < argc && (eq = strchr (argv[optind], '=')))
133 if (putenv (argv[optind]))
135 *eq = '\0';
136 die (EXIT_CANCELED, errno, _("cannot set %s"),
137 quote (argv[optind]));
139 optind++;
142 /* If no program is specified, print the environment and exit. */
143 if (argc <= optind)
145 char *const *e = environ;
146 while (*e)
147 printf ("%s%c", *e++, opt_nul_terminate_output ? '\0' : '\n');
148 return EXIT_SUCCESS;
151 if (opt_nul_terminate_output)
153 error (0, errno, _("cannot specify --null (-0) with command"));
154 usage (EXIT_CANCELED);
157 execvp (argv[optind], &argv[optind]);
159 int exit_status = errno == ENOENT ? EXIT_ENOENT : EXIT_CANNOT_INVOKE;
160 error (0, errno, "%s", quote (argv[optind]));
161 return exit_status;