stty: fix sane setting of susp to ^z on Solaris
[coreutils.git] / src / env.c
blob9994b2b83b5f7177e68a16ab90aa91acef9ae510
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 "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 "), stdout);
59 emit_mandatory_arg_note ();
61 fputs (_("\
62 -i, --ignore-environment start with an empty environment\n\
63 -0, --null end each output line with NUL, not newline\n\
64 -u, --unset=NAME remove variable from the environment\n\
65 "), stdout);
66 fputs (HELP_OPTION_DESCRIPTION, stdout);
67 fputs (VERSION_OPTION_DESCRIPTION, stdout);
68 fputs (_("\
69 \n\
70 A mere - implies -i. If no COMMAND, print the resulting environment.\n\
71 "), stdout);
72 emit_ancillary_info (PROGRAM_NAME);
74 exit (status);
77 int
78 main (int argc, char **argv)
80 int optc;
81 bool ignore_environment = false;
82 bool opt_nul_terminate_output = false;
84 initialize_main (&argc, &argv);
85 set_program_name (argv[0]);
86 setlocale (LC_ALL, "");
87 bindtextdomain (PACKAGE, LOCALEDIR);
88 textdomain (PACKAGE);
90 initialize_exit_failure (EXIT_CANCELED);
91 atexit (close_stdout);
93 while ((optc = getopt_long (argc, argv, "+iu:0", longopts, NULL)) != -1)
95 switch (optc)
97 case 'i':
98 ignore_environment = true;
99 break;
100 case 'u':
101 break;
102 case '0':
103 opt_nul_terminate_output = true;
104 break;
105 case_GETOPT_HELP_CHAR;
106 case_GETOPT_VERSION_CHAR (PROGRAM_NAME, AUTHORS);
107 default:
108 usage (EXIT_CANCELED);
112 if (optind < argc && STREQ (argv[optind], "-"))
113 ignore_environment = true;
115 if (ignore_environment)
117 static char *dummy_environ[] = { NULL };
118 environ = dummy_environ;
121 optind = 0; /* Force GNU getopt to re-initialize. */
122 while ((optc = getopt_long (argc, argv, "+iu:0", longopts, NULL)) != -1)
123 if (optc == 'u' && unsetenv (optarg))
124 error (EXIT_CANCELED, errno, _("cannot unset %s"), quote (optarg));
126 if (optind < argc && STREQ (argv[optind], "-"))
127 ++optind;
129 char *eq;
130 while (optind < argc && (eq = strchr (argv[optind], '=')))
132 if (putenv (argv[optind]))
134 *eq = '\0';
135 error (EXIT_CANCELED, errno, _("cannot set %s"),
136 quote (argv[optind]));
138 optind++;
141 /* If no program is specified, print the environment and exit. */
142 if (argc <= optind)
144 char *const *e = environ;
145 while (*e)
146 printf ("%s%c", *e++, opt_nul_terminate_output ? '\0' : '\n');
147 return EXIT_SUCCESS;
150 if (opt_nul_terminate_output)
152 error (0, errno, _("cannot specify --null (-0) with command"));
153 usage (EXIT_CANCELED);
156 execvp (argv[optind], &argv[optind]);
158 int exit_status = errno == ENOENT ? EXIT_ENOENT : EXIT_CANNOT_INVOKE;
159 error (0, errno, "%s", quote (argv[optind]));
160 return exit_status;