function System::which
The "which" command (show the full path of a command)
@author Stig Bakken <ssb@php.net>
Parameters
string $program The command to search for:
mixed $fallback Value to return if $program is not found:
Return value
mixed A string with the full path or false if not found
File
-
vendor/
pear/ pear-core-minimal/ src/ System.php, line 495
Class
- System
- System offers cross platform compatible system functions
Code
public static function which($program, $fallback = false) {
// enforce API
if (!is_string($program) || '' == $program) {
return $fallback;
}
// full path given
if (basename($program) != $program) {
$path_elements[] = dirname($program);
$program = basename($program);
}
else {
$path = getenv('PATH');
if (!$path) {
$path = getenv('Path');
// some OSes are just stupid enough to do this
}
$path_elements = explode(PATH_SEPARATOR, $path);
}
if (OS_WINDOWS) {
$exe_suffixes = getenv('PATHEXT') ? explode(PATH_SEPARATOR, getenv('PATHEXT')) : array(
'.exe',
'.bat',
'.cmd',
'.com',
);
// allow passing a command.exe param
if (strpos($program, '.') !== false) {
array_unshift($exe_suffixes, '');
}
}
else {
$exe_suffixes = array(
'',
);
}
foreach ($exe_suffixes as $suff) {
foreach ($path_elements as $dir) {
$file = $dir . DIRECTORY_SEPARATOR . $program . $suff;
// It's possible to run a .bat on Windows that is_executable
// would return false for. The is_executable check is meaningless...
if (OS_WINDOWS) {
if (file_exists($file)) {
return $file;
}
}
else {
if (is_executable($file)) {
return $file;
}
}
}
}
return $fallback;
}