Last active
October 20, 2019 16:29
-
-
Save edef1c/4dda02dbb9e7973700b7eb6e4c47f5de to your computer and use it in GitHub Desktop.
perl script to check what environment variables a program actually needs to run
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#! /usr/bin/env perl | |
use POSIX ":sys_wait_h"; | |
my @untested = keys %ENV; | |
sub choose { | |
return splice @untested, rand @untested, 1; | |
} | |
sub works { | |
my $pid = fork; | |
unless ($pid) { exec @ARGV or die "couldn't exec"; } | |
my $limit = time + 3; | |
do { return $? == 0 if waitpid($pid, WNOHANG) == $pid; } while time < $limit; | |
kill 'TERM', $pid; | |
return 1; | |
} | |
my @necessary; | |
sub status { | |
print @untested . " elements to go, " . @necessary . " needed\n"; | |
} | |
while (@untested) { | |
status; | |
my $var = choose; | |
my $val = delete $ENV{$var}; | |
next if works; | |
push @necessary, $var; | |
$ENV{$var} = $val; | |
} | |
print "@necessary"; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment