Skip to content

Instantly share code, notes, and snippets.

@takamin
Created February 9, 2015 03:53
Show Gist options
  • Save takamin/8f9efef26dfb7bf73f9d to your computer and use it in GitHub Desktop.
Save takamin/8f9efef26dfb7bf73f9d to your computer and use it in GitHub Desktop.
<?php
if(count($argv) != 2) {
exit;
}
$executable_suffixies = array("cmd", "bat", "dll", "exe");
$filename = $argv[1];
$no_suffix = !has_suffix($filename, $executable_suffixies);
$pathes = preg_split('/;/', getenv('PATH'));
foreach( $pathes as $path ) {
echo_file_exists("$path\\$filename");
if($no_suffix) {
foreach($executable_suffixies as $suffix) {
echo_file_exists("$path\\$filename.$suffix");
}
}
}
function has_suffix($filename, $suffixies) {
foreach($suffixies as $suffix) {
if(preg_match('/\.' . $suffix . '$/', $filename)) {
return true;
}
}
return false;
}
function echo_file_exists($filename) {
if(file_exists("$filename")) {
echo "$filename\n";
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment