ls: support --hyperlink to output file:// URIs
[coreutils.git] / src / env.c
blob38769f81346df69b6e59396342032d94c6f4b55b
1 /* env - run a program in a modified environment
2 Copyright (C) 1986-2017 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 {"chdir", required_argument, NULL, 'C'},
42 {GETOPT_HELP_OPTION_DECL},
43 {GETOPT_VERSION_OPTION_DECL},
44 {NULL, 0, NULL, 0}
47 void
48 usage (int status)
50 if (status != EXIT_SUCCESS)
51 emit_try_help ();
52 else
54 printf (_("\
55 Usage: %s [OPTION]... [-] [NAME=VALUE]... [COMMAND [ARG]...]\n"),
56 program_name);
57 fputs (_("\
58 Set each NAME to VALUE in the environment and run COMMAND.\n\
59 "), stdout);
61 emit_mandatory_arg_note ();
63 fputs (_("\
64 -i, --ignore-environment start with an empty environment\n\
65 -0, --null end each output line with NUL, not newline\n\
66 -u, --unset=NAME remove variable from the environment\n\
67 "), stdout);
68 fputs (_("\
69 -C, --chdir=DIR change working directory to DIR\n\
70 "), stdout);
71 fputs (HELP_OPTION_DESCRIPTION, stdout);
72 fputs (VERSION_OPTION_DESCRIPTION, stdout);
73 fputs (_("\
74 \n\
75 A mere - implies -i. If no COMMAND, print the resulting environment.\n\
76 "), stdout);
77 emit_ancillary_info (PROGRAM_NAME);
79 exit (status);
82 int
83 main (int argc, char **argv)
85 int optc;
86 bool ignore_environment = false;
87 bool opt_nul_terminate_output = false;
88 char const *newdir = NULL;
90 initialize_main (&argc, &argv);
91 set_program_name (argv[0]);
92 setlocale (LC_ALL, "");
93 bindtextdomain (PACKAGE, LOCALEDIR);
94 textdomain (PACKAGE);
96 initialize_exit_failure (EXIT_CANCELED);
97 atexit (close_stdout);
99 while ((optc = getopt_long (argc, argv, "+C:iu:0", longopts, NULL)) != -1)
101 switch (optc)
103 case 'i':
104 ignore_environment = true;
105 break;
106 case 'u':
107 break;
108 case '0':
109 opt_nul_terminate_output = true;
110 break;
111 case 'C':
112 newdir = optarg;
113 break;
114 case_GETOPT_HELP_CHAR;
115 case_GETOPT_VERSION_CHAR (PROGRAM_NAME, AUTHORS);
116 default:
117 usage (EXIT_CANCELED);
121 if (optind < argc && STREQ (argv[optind], "-"))
122 ignore_environment = true;
124 if (ignore_environment)
126 static char *dummy_environ[] = { NULL };
127 environ = dummy_environ;
130 optind = 0; /* Force GNU getopt to re-initialize. */
131 while ((optc = getopt_long (argc, argv, "+C:iu:0", longopts, NULL)) != -1)
132 if (optc == 'u' && unsetenv (optarg))
133 die (EXIT_CANCELED, errno, _("cannot unset %s"), quote (optarg));
135 if (optind < argc && STREQ (argv[optind], "-"))
136 ++optind;
138 char *eq;
139 while (optind < argc && (eq = strchr (argv[optind], '=')))
141 if (putenv (argv[optind]))
143 *eq = '\0';
144 die (EXIT_CANCELED, errno, _("cannot set %s"),
145 quote (argv[optind]));
147 optind++;
150 bool program_specified = optind < argc;
152 if (opt_nul_terminate_output && program_specified)
154 error (0, 0, _("cannot specify --null (-0) with command"));
155 usage (EXIT_CANCELED);
158 if (newdir && ! program_specified)
160 error (0, 0, _("must specify command with --chdir (-C)"));
161 usage (EXIT_CANCELED);
164 if (! program_specified)
166 /* Print the environment and exit. */
167 char *const *e = environ;
168 while (*e)
169 printf ("%s%c", *e++, opt_nul_terminate_output ? '\0' : '\n');
170 return EXIT_SUCCESS;
173 if (newdir)
175 if (chdir (newdir) != 0)
176 die (EXIT_CANCELED, errno, _("cannot change directory to %s"),
177 quoteaf (newdir));
180 execvp (argv[optind], &argv[optind]);
182 int exit_status = errno == ENOENT ? EXIT_ENOENT : EXIT_CANNOT_INVOKE;
183 error (0, errno, "%s", quote (argv[optind]));
184 return exit_status;