Skip to content

Instantly share code, notes, and snippets.

@edef1c
Last active October 20, 2019 16:29
Show Gist options
  • Save edef1c/4dda02dbb9e7973700b7eb6e4c47f5de to your computer and use it in GitHub Desktop.
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
#! /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