OpenCoverage

env.c

Absolute File Name:/home/opencoverage/opencoverage/guest-scripts/coreutils/src/src/env.c
Source codeSwitch to Preprocessed file
LineSourceCount
1/* env - run a program in a modified environment-
2 Copyright (C) 1986-2018 Free Software Foundation, Inc.-
3-
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.-
8-
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.-
13-
14 You should have received a copy of the GNU General Public License-
15 along with this program. If not, see <https://www.gnu.org/licenses/>. */-
16-
17/* Richard Mlynarik and David MacKenzie */-
18-
19#include <config.h>-
20#include <stdio.h>-
21#include <sys/types.h>-
22#include <getopt.h>-
23-
24#include "system.h"-
25#include "die.h"-
26#include "error.h"-
27#include "quote.h"-
28-
29/* The official name of this program (e.g., no 'g' prefix). */-
30#define PROGRAM_NAME "env"-
31-
32#define AUTHORS \-
33 proper_name ("Richard Mlynarik"), \-
34 proper_name ("David MacKenzie")-
35-
36static struct option const longopts[] =-
37{-
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}-
45};-
46-
47void-
48usage (int status)-
49{-
50 if (status != EXIT_SUCCESS)
status != 0Description
TRUEevaluated 21 times by 1 test
Evaluated by:
  • env
FALSEevaluated 74 times by 1 test
Evaluated by:
  • env
21-74
51 emit_try_help ();
executed 21 times by 1 test: end of block
Executed by:
  • env
21
52 else-
53 {-
54 printf (_("\-
55Usage: %s [OPTION]... [-] [NAME=VALUE]... [COMMAND [ARG]...]\n"),-
56 program_name);-
57 fputs (_("\-
58Set each NAME to VALUE in the environment and run COMMAND.\n\-
59"), stdout);-
60-
61 emit_mandatory_arg_note ();-
62-
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\-
75A mere - implies -i. If no COMMAND, print the resulting environment.\n\-
76"), stdout);-
77 emit_ancillary_info (PROGRAM_NAME);-
78 }
executed 74 times by 1 test: end of block
Executed by:
  • env
74
79 exit (status);
executed 95 times by 1 test: exit (status);
Executed by:
  • env
95
80}-
81-
82int-
83main (int argc, char **argv)-
84{-
85 int optc;-
86 bool ignore_environment = false;-
87 bool opt_nul_terminate_output = false;-
88 char const *newdir = NULL;-
89-
90 initialize_main (&argc, &argv);-
91 set_program_name (argv[0]);-
92 setlocale (LC_ALL, "");-
93 bindtextdomain (PACKAGE, LOCALEDIR);-
94 textdomain (PACKAGE);-
95-
96 initialize_exit_failure (EXIT_CANCELED);-
97 atexit (close_stdout);-
98-
99 while ((optc = getopt_long (argc, argv, "+C:iu:0", longopts, NULL)) != -1)
(optc = getopt... *)0) )) != -1Description
TRUEevaluated 177 times by 1 test
Evaluated by:
  • env
FALSEevaluated 25 times by 1 test
Evaluated by:
  • env
25-177
100 {-
101 switch (optc)-
102 {-
103 case 'i':
executed 20 times by 1 test: case 'i':
Executed by:
  • env
20
104 ignore_environment = true;-
105 break;
executed 20 times by 1 test: break;
Executed by:
  • env
20
106 case 'u':
executed 21 times by 1 test: case 'u':
Executed by:
  • env
21
107 break;
executed 21 times by 1 test: break;
Executed by:
  • env
21
108 case '0':
executed 20 times by 1 test: case '0':
Executed by:
  • env
20
109 opt_nul_terminate_output = true;-
110 break;
executed 20 times by 1 test: break;
Executed by:
  • env
20
111 case 'C':
executed 18 times by 1 test: case 'C':
Executed by:
  • env
18
112 newdir = optarg;-
113 break;
executed 18 times by 1 test: break;
Executed by:
  • env
18
114 case_GETOPT_HELP_CHAR;
never executed: break;
executed 74 times by 1 test: case GETOPT_HELP_CHAR:
Executed by:
  • env
0-74
115 case_GETOPT_VERSION_CHAR (PROGRAM_NAME, AUTHORS);
executed 5 times by 1 test: exit ( 0 );
Executed by:
  • env
never executed: break;
executed 5 times by 1 test: case GETOPT_VERSION_CHAR:
Executed by:
  • env
0-5
116 default:
executed 19 times by 1 test: default:
Executed by:
  • env
19
117 usage (EXIT_CANCELED);-
118 }
never executed: end of block
0
119 }-
120-
121 if (optind < argc && STREQ (argv[optind], "-"))
never executed: __result = (((const unsigned char *) (const char *) ( argv[optind] ))[3] - __s2[3]);
never executed: end of block
never executed: end of block
never executed: __result = (((const unsigned char *) (const char *) ( "-" ))[3] - __s2[3]);
never executed: end of block
executed 1 time by 1 test: end of block
Executed by:
  • env
optind < argcDescription
TRUEevaluated 14 times by 1 test
Evaluated by:
  • env
FALSEevaluated 11 times by 1 test
Evaluated by:
  • env
( __extension_...)))); }) == 0)Description
TRUEevaluated 1 time by 1 test
Evaluated by:
  • env
FALSEevaluated 13 times by 1 test
Evaluated by:
  • env
__s1_len > 0Description
TRUEnever evaluated
FALSEnever evaluated
__result == 0Description
TRUEnever evaluated
FALSEnever evaluated
__s1_len > 1Description
TRUEnever evaluated
FALSEnever evaluated
__result == 0Description
TRUEnever evaluated
FALSEnever evaluated
__s1_len > 2Description
TRUEnever evaluated
FALSEnever evaluated
__result == 0Description
TRUEnever evaluated
FALSEnever evaluated
__s2_len > 0Description
TRUEevaluated 14 times by 1 test
Evaluated by:
  • env
FALSEnever evaluated
__result == 0Description
TRUEevaluated 1 time by 1 test
Evaluated by:
  • env
FALSEevaluated 13 times by 1 test
Evaluated by:
  • env
__s2_len > 1Description
TRUEnever evaluated
FALSEevaluated 1 time by 1 test
Evaluated by:
  • env
__result == 0Description
TRUEnever evaluated
FALSEnever evaluated
__s2_len > 2Description
TRUEnever evaluated
FALSEnever evaluated
__result == 0Description
TRUEnever evaluated
FALSEnever evaluated
0-14
122 ignore_environment = true;
executed 1 time by 1 test: ignore_environment = 1 ;
Executed by:
  • env
1
123-
124 if (ignore_environment)
ignore_environmentDescription
TRUEevaluated 5 times by 1 test
Evaluated by:
  • env
FALSEevaluated 20 times by 1 test
Evaluated by:
  • env
5-20
125 {-
126 static char *dummy_environ[] = { NULL };-
127 environ = dummy_environ;-
128 }
executed 5 times by 1 test: end of block
Executed by:
  • env
5
129-
130 optind = 0; /* Force GNU getopt to re-initialize. */-
131 while ((optc = getopt_long (argc, argv, "+C:iu:0", longopts, NULL)) != -1)
(optc = getopt... *)0) )) != -1Description
TRUEevaluated 15 times by 1 test
Evaluated by:
  • env
FALSEevaluated 23 times by 1 test
Evaluated by:
  • env
15-23
132 if (optc == 'u' && unsetenv (optarg))
optc == 'u'Description
TRUEevaluated 5 times by 1 test
Evaluated by:
  • env
FALSEevaluated 10 times by 1 test
Evaluated by:
  • env
unsetenv (optarg)Description
TRUEevaluated 2 times by 1 test
Evaluated by:
  • env
FALSEevaluated 3 times by 1 test
Evaluated by:
  • env
2-10
133 die (EXIT_CANCELED, errno, _("cannot unset %s"), quote (optarg));
executed 2 times by 1 test: ((!!sizeof (struct { _Static_assert (EXIT_CANCELED, "verify_expr (" "EXIT_CANCELED" ", " "(error (EXIT_CANCELED, (*__errno_location ()), dcgettext (((void *)0), \"cannot unset %s\", 5), quote (optarg)), assume (false))" ")"); int _gl_dummy; })) ? ((error ...cannot unset %s" , 5) , quote (optarg)), (( 0 ) ? (void) 0 : __builtin_unreachable ()))) : ((error (EXIT_CANCELED, (*__errno_location ()) , dcgettext (((void *)0), "cannot unset %s" , 5) , quote (optarg)), (( 0 ) ? (void) 0 : __builtin_unreachable ()))));
Executed by:
  • env
2
134-
135 if (optind < argc && STREQ (argv[optind], "-"))
never executed: __result = (((const unsigned char *) (const char *) ( argv[optind] ))[3] - __s2[3]);
never executed: end of block
never executed: end of block
never executed: __result = (((const unsigned char *) (const char *) ( "-" ))[3] - __s2[3]);
never executed: end of block
executed 1 time by 1 test: end of block
Executed by:
  • env
optind < argcDescription
TRUEevaluated 12 times by 1 test
Evaluated by:
  • env
FALSEevaluated 11 times by 1 test
Evaluated by:
  • env
( __extension_...)))); }) == 0)Description
TRUEevaluated 1 time by 1 test
Evaluated by:
  • env
FALSEevaluated 11 times by 1 test
Evaluated by:
  • env
__s1_len > 0Description
TRUEnever evaluated
FALSEnever evaluated
__result == 0Description
TRUEnever evaluated
FALSEnever evaluated
__s1_len > 1Description
TRUEnever evaluated
FALSEnever evaluated
__result == 0Description
TRUEnever evaluated
FALSEnever evaluated
__s1_len > 2Description
TRUEnever evaluated
FALSEnever evaluated
__result == 0Description
TRUEnever evaluated
FALSEnever evaluated
__s2_len > 0Description
TRUEevaluated 12 times by 1 test
Evaluated by:
  • env
FALSEnever evaluated
__result == 0Description
TRUEevaluated 1 time by 1 test
Evaluated by:
  • env
FALSEevaluated 11 times by 1 test
Evaluated by:
  • env
__s2_len > 1Description
TRUEnever evaluated
FALSEevaluated 1 time by 1 test
Evaluated by:
  • env
__result == 0Description
TRUEnever evaluated
FALSEnever evaluated
__s2_len > 2Description
TRUEnever evaluated
FALSEnever evaluated
__result == 0Description
TRUEnever evaluated
FALSEnever evaluated
0-12
136 ++optind;
executed 1 time by 1 test: ++optind;
Executed by:
  • env
1
137-
138 char *eq;-
139 while (optind < argc && (eq = strchr (argv[optind], '=')))
optind < argcDescription
TRUEevaluated 12 times by 1 test
Evaluated by:
  • env
FALSEevaluated 17 times by 1 test
Evaluated by:
  • env
(eq = (__exten...d] , '=' ))) )Description
TRUEevaluated 6 times by 1 test
Evaluated by:
  • env
FALSEevaluated 6 times by 1 test
Evaluated by:
  • env
__builtin_constant_p ( '=' )Description
TRUEevaluated 12 times by 1 test
Evaluated by:
  • env
FALSEnever evaluated
!__builtin_con...argv[optind] )Description
TRUEevaluated 12 times by 1 test
Evaluated by:
  • env
FALSEnever evaluated
( '=' ) == '\0'Description
TRUEnever evaluated
FALSEevaluated 12 times by 1 test
Evaluated by:
  • env
0-17
140 {-
141 if (putenv (argv[optind]))
putenv (argv[optind])Description
TRUEnever evaluated
FALSEevaluated 6 times by 1 test
Evaluated by:
  • env
0-6
142 {-
143 *eq = '\0';-
144 die (EXIT_CANCELED, errno, _("cannot set %s"),-
145 quote (argv[optind]));-
146 }
never executed: end of block
0
147 optind++;-
148 }
executed 6 times by 1 test: end of block
Executed by:
  • env
6
149-
150 bool program_specified = optind < argc;-
151-
152 if (opt_nul_terminate_output && program_specified)
opt_nul_terminate_outputDescription
TRUEevaluated 4 times by 1 test
Evaluated by:
  • env
FALSEevaluated 19 times by 1 test
Evaluated by:
  • env
program_specifiedDescription
TRUEevaluated 1 time by 1 test
Evaluated by:
  • env
FALSEevaluated 3 times by 1 test
Evaluated by:
  • env
1-19
153 {-
154 error (0, 0, _("cannot specify --null (-0) with command"));-
155 usage (EXIT_CANCELED);-
156 }
never executed: end of block
0
157-
158 if (newdir && ! program_specified)
newdirDescription
TRUEevaluated 2 times by 1 test
Evaluated by:
  • env
FALSEevaluated 20 times by 1 test
Evaluated by:
  • env
! program_specifiedDescription
TRUEevaluated 1 time by 1 test
Evaluated by:
  • env
FALSEevaluated 1 time by 1 test
Evaluated by:
  • env
1-20
159 {-
160 error (0, 0, _("must specify command with --chdir (-C)"));-
161 usage (EXIT_CANCELED);-
162 }
never executed: end of block
0
163-
164 if (! program_specified)
! program_specifiedDescription
TRUEevaluated 16 times by 1 test
Evaluated by:
  • env
FALSEevaluated 5 times by 1 test
Evaluated by:
  • env
5-16
165 {-
166 /* Print the environment and exit. */-
167 char *const *e = environ;-
168 while (*e)
*eDescription
TRUEevaluated 568 times by 1 test
Evaluated by:
  • env
FALSEevaluated 16 times by 1 test
Evaluated by:
  • env
16-568
169 printf ("%s%c", *e++, opt_nul_terminate_output ? '\0' : '\n');
executed 568 times by 1 test: printf ("%s%c", *e++, opt_nul_terminate_output ? '\0' : '\n');
Executed by:
  • env
568
170 return EXIT_SUCCESS;
executed 16 times by 1 test: return 0 ;
Executed by:
  • env
16
171 }-
172-
173 if (newdir)
newdirDescription
TRUEevaluated 1 time by 1 test
Evaluated by:
  • env
FALSEevaluated 4 times by 1 test
Evaluated by:
  • env
1-4
174 {-
175 if (chdir (newdir) != 0)
chdir (newdir) != 0Description
TRUEevaluated 1 time by 1 test
Evaluated by:
  • env
FALSEnever evaluated
0-1
176 die (EXIT_CANCELED, errno, _("cannot change directory to %s"),
executed 1 time by 1 test: ((!!sizeof (struct { _Static_assert (EXIT_CANCELED, "verify_expr (" "EXIT_CANCELED" ", " "(error (EXIT_CANCELED, (*__errno_location ()), dcgettext (((void *)0), \"cannot change directory to %s\", 5), quotearg_style (shell_escape_always_quoting_style, newd...: __builtin_unreachable ()))) : ((error (EXIT_CANCELED, (*__errno_location ()) , dcgettext (((void *)0), "cannot change directory to %s" , 5) , quotearg_style (shell_escape_always_quoting_style, newdir)), (( 0 ) ? (void) 0 : __builtin_unreachable ())))) ;
Executed by:
  • env
1
177 quoteaf (newdir));
executed 1 time by 1 test: ((!!sizeof (struct { _Static_assert (EXIT_CANCELED, "verify_expr (" "EXIT_CANCELED" ", " "(error (EXIT_CANCELED, (*__errno_location ()), dcgettext (((void *)0), \"cannot change directory to %s\", 5), quotearg_style (shell_escape_always_quoting_style, newd...: __builtin_unreachable ()))) : ((error (EXIT_CANCELED, (*__errno_location ()) , dcgettext (((void *)0), "cannot change directory to %s" , 5) , quotearg_style (shell_escape_always_quoting_style, newdir)), (( 0 ) ? (void) 0 : __builtin_unreachable ())))) ;
Executed by:
  • env
1
178 }
never executed: end of block
0
179-
180 execvp (argv[optind], &argv[optind]);-
181-
182 int exit_status = errno == ENOENT ? EXIT_ENOENT : EXIT_CANNOT_INVOKE;
(*__errno_location ()) == 2Description
TRUEevaluated 3 times by 1 test
Evaluated by:
  • env
FALSEevaluated 1 time by 1 test
Evaluated by:
  • env
1-3
183 error (0, errno, "%s", quote (argv[optind]));-
184 return exit_status;
executed 4 times by 1 test: return exit_status;
Executed by:
  • env
4
185}-
Source codeSwitch to Preprocessed file

Generated by Squish Coco 4.1.2