1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
|
/* env - run a program in a modified environment
Copyright (C) 1986-2018 Free Software Foundation, Inc.
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <https://www.gnu.org/licenses/>. */
/* Richard Mlynarik and David MacKenzie */
#include <config.h>
#include <stdio.h>
#include <sys/types.h>
#include <getopt.h>
#include <c-ctype.h>
#include <assert.h>
#include "system.h"
#include "die.h"
#include "error.h"
#include "quote.h"
/* The official name of this program (e.g., no 'g' prefix). */
#define PROGRAM_NAME "env"
#define AUTHORS \
proper_name ("Richard Mlynarik"), \
proper_name ("David MacKenzie"), \
proper_name ("Assaf Gordon")
/* array of envvars to unset. */
static const char** usvars;
static size_t usvars_alloc;
static size_t usvars_used;
/* Annotate the output with extra info to aid the user. */
static bool dev_debug;
/* buffer and length of extracted envvars in -S strings. */
static char *varname;
static size_t vnlen;
static char const shortopts[] = "+C:iS:u:v0 \t";
static struct option const longopts[] =
{
{"ignore-environment", no_argument, NULL, 'i'},
{"null", no_argument, NULL, '0'},
{"unset", required_argument, NULL, 'u'},
{"chdir", required_argument, NULL, 'C'},
{"debug", no_argument, NULL, 'v'},
{"split-string", required_argument, NULL, 'S'},
{GETOPT_HELP_OPTION_DECL},
{GETOPT_VERSION_OPTION_DECL},
{NULL, 0, NULL, 0}
};
void
usage (int status)
{
if (status != EXIT_SUCCESS)
emit_try_help ();
else
{
printf (_("\
Usage: %s [OPTION]... [-] [NAME=VALUE]... [COMMAND [ARG]...]\n"),
program_name);
fputs (_("\
Set each NAME to VALUE in the environment and run COMMAND.\n\
"), stdout);
emit_mandatory_arg_note ();
fputs (_("\
-i, --ignore-environment start with an empty environment\n\
-0, --null end each output line with NUL, not newline\n\
-u, --unset=NAME remove variable from the environment\n\
"), stdout);
fputs (_("\
-C, --chdir=DIR change working directory to DIR\n\
"), stdout);
fputs (_("\
-S, --split-string=S process and split S into separate arguments;\n\
used to pass multiple arguments on shebang lines\n\
-v, --debug print verbose information for each processing step\n\
"), stdout);
fputs (HELP_OPTION_DESCRIPTION, stdout);
fputs (VERSION_OPTION_DESCRIPTION, stdout);
fputs (_("\
\n\
A mere - implies -i. If no COMMAND, print the resulting environment.\n\
"), stdout);
emit_ancillary_info (PROGRAM_NAME);
}
exit (status);
}
static void
append_unset_var (const char *var)
{
if (usvars_used == usvars_alloc)
usvars = x2nrealloc (usvars, &usvars_alloc, sizeof *usvars);
usvars[usvars_used++] = var;
}
static void
unset_envvars (void)
{
for (size_t i = 0; i < usvars_used; ++i)
{
devmsg ("unset: %s\n", usvars[i]);
if (unsetenv (usvars[i]))
die (EXIT_CANCELED, errno, _("cannot unset %s"),
quote (usvars[i]));
}
IF_LINT (free (usvars));
IF_LINT (usvars = NULL);
IF_LINT (usvars_used = 0);
IF_LINT (usvars_alloc = 0);
}
static bool _GL_ATTRIBUTE_PURE
valid_escape_sequence (const char c)
{
return (c == 'c' || c == 'f' || c == 'n' || c == 'r' || c == 't' || c == 'v' \
|| c == '#' || c == '$' || c == '_' || c == '"' || c == '\'' \
|| c == '\\');
}
static char _GL_ATTRIBUTE_PURE
escape_char (const char c)
{
switch (c)
{
/* \a,\b not supported by FreeBSD's env. */
case 'f': return '\f';
case 'n': return '\n';
case 'r': return '\r';
case 't': return '\t';
case 'v': return '\v';
default: assert (0); /* LCOV_EXCL_LINE */
}
}
/* Return a pointer to the end of a valid ${VARNAME} string, or NULL.
'str' should point to the '$' character.
First letter in VARNAME must be alpha or underscore,
rest of letters are alnum or underscore. Any other character is an error. */
static const char* _GL_ATTRIBUTE_PURE
scan_varname (const char* str)
{
assert (str && *str == '$'); /* LCOV_EXCL_LINE */
if ( *(str+1) == '{' && (c_isalpha (*(str+2)) || *(str+2) == '_'))
{
const char* end = str+3;
while (c_isalnum (*end) || *end == '_')
++end;
if (*end == '}')
return end;
}
return NULL;
}
/* Return a pointer to a static buffer containing the VARNAME as
extracted from a '${VARNAME}' string.
The returned string will be NUL terminated.
The returned pointer should not be freed.
Return NULL if not a valid ${VARNAME} syntax. */
static char*
extract_varname (const char* str)
{
ptrdiff_t i;
const char* p;
p = scan_varname (str);
if (!p)
return NULL;
/* -2 and +2 (below) account for the '${' prefix. */
i = p - str - 2;
if (i >= vnlen)
{
vnlen = i + 1;
varname = xrealloc (varname, vnlen);
}
memcpy (varname, str+2, i);
varname[i]=0;
return varname;
}
/* Validate the "-S" parameter, according to the syntax defined by FreeBSD's
env(1). Terminate with an error message if not valid.
Calculate and set two values:
bufsize - the size (in bytes) required to hold the resulting string
after ENVVAR expansion (the value is overestimated).
maxargc - the maximum number of arguments (the size of the new argv). */
static void
validate_split_str (const char* str, size_t* /*out*/ bufsize,
int* /*out*/ maxargc)
{
bool dq, sq, sp;
const char *pch;
size_t buflen;
int cnt = 1;
assert (str && str[0] && !isspace (str[0])); /* LCOV_EXCL_LINE */
dq = sq = sp = false;
buflen = strlen (str)+1;
while (*str)
{
const char next = *(str+1);
if (isspace (*str) && !dq && !sq)
{
sp = true;
}
else
{
if (sp)
++cnt;
sp = false;
}
switch (*str)
{
case '\'':
assert (!(sq && dq)); /* LCOV_EXCL_LINE */
sq = !sq && !dq;
break;
case '"':
assert (!(sq && dq)); /* LCOV_EXCL_LINE */
dq = !sq && !dq;
break;
case '\\':
if (dq && next == 'c')
die (EXIT_CANCELED, 0,
_("'\\c' must not appear in double-quoted -S string"));
if (next == '\0')
die (EXIT_CANCELED, 0,
_("invalid backslash at end of string in -S"));
if (!valid_escape_sequence (next))
die (EXIT_CANCELED, 0, _("invalid sequence '\\%c' in -S"), next);
if (next == '_')
++cnt;
++str;
break;
case '$':
if (sq)
break;
if (!(pch = extract_varname (str)))
die (EXIT_CANCELED, 0, _("only ${VARNAME} expansion is supported,"\
" error at: %s"), str);
if ((pch = getenv (pch)))
buflen += strlen (pch);
break;
}
++str;
}
if (dq || sq)
die (EXIT_CANCELED, 0, _("no terminating quote in -S string"));
*maxargc = cnt;
*bufsize = buflen;
}
/* Return a newly-allocated *arg[]-like array,
by parsing and splitting the input 'str'.
'extra_argc' is the number of additional elements to allocate
in the array (on top of the number of args required to split 'str').
Example:
char **argv = build_argv ("A=B uname -k', 3)
Results in:
argv[0] = "DUMMY" - dummy executable name, can be replaced later.
argv[1] = "A=B"
argv[2] = "uname"
argv[3] = "-k"
argv[4] = NULL
argv[5,6,7] = [allocated due to extra_argc, but not initialized]
The strings are stored in an allocated buffer, pointed by argv[0].
To free allocated memory:
free (argv[0]);
free (argv); */
static char**
build_argv (const char* str, int extra_argc)
{
bool dq = false, sq = false, sep = true;
char *dest; /* buffer to hold the new argv values. allocated as one buffer,
but will contain multiple NUL-terminate strings. */
char **newargv, **nextargv;
int newargc = 0;
size_t buflen = 0;
/* This macro is called before inserting any characters to the output
buffer. It checks if the previous character was a separator
and if so starts a new argv element. */
#define CHECK_START_NEW_ARG \
do { \
if (sep) \
{ \
*dest++ = '\0'; \
*nextargv++ = dest; \
sep = false; \
} \
} while (0)
assert (str && str[0] && !isspace (str[0])); /* LCOV_EXCL_LINE */
validate_split_str (str, &buflen, &newargc);
/* allocate buffer. +6 for the "DUMMY\0" executable name, +1 for NUL. */
dest = xmalloc (buflen + 6 + 1);
/* allocate the argv array.
+2 for the program name (argv[0]) and the last NULL pointer. */
nextargv = newargv = xmalloc ((newargc + extra_argc + 2) * sizeof (char *));
/* argv[0] = executable's name - will be replaced later. */
strcpy (dest, "DUMMY");
*nextargv++ = dest;
dest += 6;
/* In the following loop,
'break' causes the character 'newc' to be added to *dest,
'continue' skips the character. */
while (*str)
{
char newc = *str; /* default: add the next character. */
switch (*str)
{
case '\'':
if (dq)
break;
sq = !sq;
CHECK_START_NEW_ARG;
++str;
continue;
case '"':
if (sq)
break;
dq = !dq;
CHECK_START_NEW_ARG;
++str;
continue;
case ' ':
case '\t':
/* space/tab outside quotes starts a new argument. */
if (sq || dq)
break;
sep = true;
str += strspn (str, " \t"); /* skip whitespace. */
continue;
case '#':
if (!sep)
break;
goto eos; /* '#' as first char terminates the string. */
case '\\':
/* backslash inside single-quotes is not special, except \\ and \'. */
if (sq && *(str+1) != '\\' && *(str+1) != '\'')
break;
/* skip the backslash and examine the next character. */
newc = *(++str);
if ((newc == '\\' || newc == '\'')
|| (!sq && (newc == '#' || newc == '$' || newc == '"')))
{
/* Pass escaped character as-is. */
}
else if (newc == '_')
{
if (!dq)
{
++str; /* '\_' outside double-quotes is arg separator. */
sep = true;
continue;
}
else
newc = ' '; /* '\_' inside double-quotes is space. */
}
else if (newc == 'c')
goto eos; /* '\c' terminates the string. */
else
newc = escape_char (newc); /* other characters (e.g. '\n'). */
break;
case '$':
/* ${VARNAME} are not expanded inside single-quotes. */
if (sq)
break;
/* Store the ${VARNAME} value. Error checking omitted as
the ${VARNAME} was already validated. */
{
char *n = extract_varname (str);
char *v = getenv (n);
if (v)
{
CHECK_START_NEW_ARG;
devmsg ("expanding ${%s} into %s\n", n, quote (v));
dest = stpcpy (dest, v);
}
else
devmsg ("replacing ${%s} with null string\n", n);
str = strchr (str, '}') + 1;
continue;
}
}
CHECK_START_NEW_ARG;
*dest++ = newc;
++str;
}
eos:
*dest = '\0';
*nextargv = NULL; /* mark the last element in argv as NULL. */
return newargv;
}
/* Process an "-S" string and create the corresponding argv array.
Update the given argc/argv parameters with the new argv.
Example: if executed as:
$ env -S"-i -C/tmp A=B" foo bar
The input argv is:
argv[0] = 'env'
argv[1] = "-S-i -C/tmp A=B"
argv[2] = foo
argv[3] = bar
This function will modify argv to be:
argv[0] = 'env'
argv[1] = "-i"
argv[2] = "-C/tmp"
argv[3] = A=B"
argv[4] = foo
argv[5] = bar
argc will be updated from 4 to 6.
optind will be reset to 0 to force getopt_long to rescan all arguments. */
static void
parse_split_string (const char* str, int /*out*/ *orig_optind,
int /*out*/ *orig_argc, char*** /*out*/ orig_argv)
{
int i, newargc;
char **newargv, **nextargv;
while (isspace (*str))
str++;
if (*str == '\0')
return;
newargv = build_argv (str, *orig_argc - *orig_optind);
/* restore argv[0] - the 'env' executable name */
*newargv = (*orig_argv)[0];
/* Start from argv[1] */
nextargv = newargv + 1;
/* Print parsed arguments */
if (dev_debug && *nextargv)
{
devmsg ("split -S: %s\n", quote (str));
devmsg (" into: %s\n", quote (*nextargv++));
while (*nextargv)
devmsg (" & %s\n", quote (*nextargv++));
}
else
{
/* Ensure nextargv points to the last argument */
while (*nextargv)
++nextargv;
}
/* Add remaining arguments from original command line */
for (i = *orig_optind; i < *orig_argc; ++i)
*nextargv++ = (*orig_argv)[i];
*nextargv = NULL;
/* Count how many new arguments we have */
newargc = 0;
for (nextargv = newargv; *nextargv; ++nextargv)
++newargc;
/* set new values for original getopt variables */
*orig_argc = newargc;
*orig_argv = newargv;
*orig_optind = 0; /* tell getopt to restart from first argument */
}
int
main (int argc, char **argv)
{
int optc;
bool ignore_environment = false;
bool opt_nul_terminate_output = false;
char const *newdir = NULL;
initialize_main (&argc, &argv);
set_program_name (argv[0]);
setlocale (LC_ALL, "");
bindtextdomain (PACKAGE, LOCALEDIR);
textdomain (PACKAGE);
initialize_exit_failure (EXIT_CANCELED);
atexit (close_stdout);
while ((optc = getopt_long (argc, argv, shortopts, longopts, NULL)) != -1)
{
switch (optc)
{
case 'i':
ignore_environment = true;
break;
case 'u':
append_unset_var (optarg);
break;
case 'v':
dev_debug = true;
break;
case '0':
opt_nul_terminate_output = true;
break;
case 'C':
newdir = optarg;
break;
case 'S':
parse_split_string (optarg, &optind, &argc, &argv);
break;
case ' ':
case '\t':
/* These are undocumented options. Attempt to detect
incorrect shebang usage with extraneous space, e.g.:
#!/usr/bin/env -i command
In which case argv[1] == "-i command". */
error (0, 0, _("invalid option -- '%c'"), optc);
error (0, 0, _("use -[v]S to pass options in shebang lines"));
usage (EXIT_CANCELED);
case_GETOPT_HELP_CHAR;
case_GETOPT_VERSION_CHAR (PROGRAM_NAME, AUTHORS);
default:
usage (EXIT_CANCELED);
}
}
if (optind < argc && STREQ (argv[optind], "-"))
{
ignore_environment = true;
++optind;
}
if (ignore_environment)
{
devmsg ("cleaning environ\n");
static char *dummy_environ[] = { NULL };
environ = dummy_environ;
}
else
unset_envvars ();
char *eq;
while (optind < argc && (eq = strchr (argv[optind], '=')))
{
devmsg ("setenv: %s\n", argv[optind]);
if (putenv (argv[optind]))
{
*eq = '\0';
die (EXIT_CANCELED, errno, _("cannot set %s"),
quote (argv[optind]));
}
optind++;
}
bool program_specified = optind < argc;
if (opt_nul_terminate_output && program_specified)
{
error (0, 0, _("cannot specify --null (-0) with command"));
usage (EXIT_CANCELED);
}
if (newdir && ! program_specified)
{
error (0, 0, _("must specify command with --chdir (-C)"));
usage (EXIT_CANCELED);
}
if (! program_specified)
{
/* Print the environment and exit. */
char *const *e = environ;
while (*e)
printf ("%s%c", *e++, opt_nul_terminate_output ? '\0' : '\n');
return EXIT_SUCCESS;
}
if (newdir)
{
devmsg ("chdir: %s\n", quoteaf (newdir));
if (chdir (newdir) != 0)
die (EXIT_CANCELED, errno, _("cannot change directory to %s"),
quoteaf (newdir));
}
if (dev_debug)
{
devmsg ("executing: %s\n", argv[optind]);
for (int i=optind; i<argc; ++i)
devmsg (" arg[%d]= %s\n", i-optind, quote (argv[i]));
}
execvp (argv[optind], &argv[optind]);
int exit_status = errno == ENOENT ? EXIT_ENOENT : EXIT_CANNOT_INVOKE;
error (0, errno, "%s", quote (argv[optind]));
if (exit_status == EXIT_ENOENT && strchr (argv[optind], ' '))
error (0, 0, _("use -[v]S to pass options in shebang lines"));
return exit_status;
}
|