forked from briandfoy/PerlPowerTools
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathenv
executable file
·128 lines (83 loc) · 2.36 KB
/
env
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
#!/usr/bin/perl
=begin metadata
Name: env
Description: run a program in a modified environment
Author: Matthew Bafford, dragons@scescape.net
License: perl
=end metadata
=cut
# Perl version of the env command.
# Matthew Bafford
# 2/28/1999
use strict;
use File::Basename qw(basename);
my $Program = basename($0);
while ( @ARGV && $ARGV[0] =~ /^-/ ) {
my $arg = shift;
if ( $arg eq '-i' ) {
%ENV = ();
} elsif ( $arg =~ /^-u(.*)/ ) {
my $val = length $1 ? $1 : shift;
if ($val =~ m/=/) {
warn "$Program: bad unset argument: '$val'\n";
exit 2;
}
delete $ENV{$val};
} else {
warn "$Program: invalid option -- $arg\n";
exit 2;
}
}
while ( @ARGV && $ARGV[0] =~ /=/ ) {
my ( $name, $value ) = split /=/, shift, 2;
$ENV{$name} = $value;
}
if ( !@ARGV ) {
for ( keys %ENV ) {
print "$_=$ENV{$_}\n";
}
exit 0;
}
my $cmd = $ARGV[0];
unless (exec {$cmd} @ARGV) {
warn "$Program: failed to exec '$cmd': $!\n";
exit 127;
}
__END__
=pod
=head1 NAME
env - Run a program in a modified environment
=head1 SYNOPSIS
env [B<-i>] [B<-u> name]... [name=value]... [command [args]...]
=head1 DESCRIPTION
I<env> runs a command with the environment modified as specified
by the command line. If no command is specified, I<env> prints
out the modified environment.
=head2 OPTIONS
I<env> accepts the following options:
=over 4
=item B<-i>
Clears the environment, passing only the values specifed to the command.
=item B<-u> I<name>
Clears the environment variable I<name> if it exists.
The value must not include the '=' character.
=back
=head1 ENVIRONMENT
The working of I<env> is not influenced by any environment variables.
=head1 DIAGNOSTICS
If the command is invoked, the exit status of I<env> will be the exit
status of the command. Otherwise, I<env> will return one of the following
values:
0 env completed successfully.
1-125 An error occured in env.
127 There was an error running the command specified.
=head1 BUGS
I<env> has no known bugs.
=head1 AUTHOR
This Perl version of I<env> was written by
Matthew Bafford, I<dragons@scescape.net>.
=head1 COPYRIGHT and LICENSE
This program is copyright (c) Matthew Bafford 1999.
This program is free and open software. You may use, modify, distribute,
and sell this program (and any modified variants) in any way you wish,
provided you do not restrict others from doing the same.