1 /* env - run a program in a modified environment
2 Copyright (C) 1986, 1991-2005, 2007-2010 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 */
21 #include <sys/types.h>
28 /* The official name of this program (e.g., no `g' prefix). */
29 #define PROGRAM_NAME "env"
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
},
48 if (status
!= EXIT_SUCCESS
)
49 fprintf (stderr
, _("Try `%s --help' for more information.\n"),
54 Usage: %s [OPTION]... [-] [NAME=VALUE]... [COMMAND [ARG]...]\n"),
57 Set each NAME to VALUE in the environment and run COMMAND.\n\
59 -i, --ignore-environment start with an empty environment\n\
60 -0, --null end each output line with 0 byte rather than newline\n\
61 -u, --unset=NAME remove variable from the environment\n\
63 fputs (HELP_OPTION_DESCRIPTION
, stdout
);
64 fputs (VERSION_OPTION_DESCRIPTION
, stdout
);
67 A mere - implies -i. If no COMMAND, print the resulting environment.\n\
69 emit_ancillary_info ();
75 main (int argc
, char **argv
)
78 bool ignore_environment
= false;
79 bool opt_nul_terminate_output
= false;
81 initialize_main (&argc
, &argv
);
82 set_program_name (argv
[0]);
83 setlocale (LC_ALL
, "");
84 bindtextdomain (PACKAGE
, LOCALEDIR
);
87 initialize_exit_failure (EXIT_CANCELED
);
88 atexit (close_stdout
);
90 while ((optc
= getopt_long (argc
, argv
, "+iu:0", longopts
, NULL
)) != -1)
95 ignore_environment
= true;
100 opt_nul_terminate_output
= true;
102 case_GETOPT_HELP_CHAR
;
103 case_GETOPT_VERSION_CHAR (PROGRAM_NAME
, AUTHORS
);
105 usage (EXIT_CANCELED
);
109 if (optind
< argc
&& STREQ (argv
[optind
], "-"))
110 ignore_environment
= true;
112 if (ignore_environment
)
114 static char *dummy_environ
[] = { NULL
};
115 environ
= dummy_environ
;
118 optind
= 0; /* Force GNU getopt to re-initialize. */
119 while ((optc
= getopt_long (argc
, argv
, "+iu:0", longopts
, NULL
)) != -1)
120 if (optc
== 'u' && unsetenv (optarg
))
121 error (EXIT_CANCELED
, errno
, _("cannot unset %s"), quote (optarg
));
123 if (optind
< argc
&& STREQ (argv
[optind
], "-"))
126 while (optind
< argc
&& strchr (argv
[optind
], '='))
127 if (putenv (argv
[optind
++]))
129 char *name
= argv
[optind
- 1];
130 *(strchr (name
, '=')) = '\0';
131 error (EXIT_CANCELED
, errno
, _("cannot set %s"), quote (name
));
134 /* If no program is specified, print the environment and exit. */
137 char *const *e
= environ
;
139 printf ("%s%c", *e
++, opt_nul_terminate_output
? '\0' : '\n');
143 if (opt_nul_terminate_output
)
145 error (0, errno
, _("cannot specify --null (-0) with command"));
146 usage (EXIT_CANCELED
);
149 execvp (argv
[optind
], &argv
[optind
]);
152 int exit_status
= (errno
== ENOENT
? EXIT_ENOENT
: EXIT_CANNOT_INVOKE
);
153 error (0, errno
, "%s", argv
[optind
]);