includes/functions.php
changeset 851 b98798f6572d
parent 842 f13bb4f21890
child 857 f3a5a276208c
--- a/includes/functions.php	Mon Mar 02 16:45:28 2009 -0500
+++ b/includes/functions.php	Mon Mar 02 16:46:10 2009 -0500
@@ -5002,4 +5002,35 @@
   return false;
 }
 
+/**
+ * Implementation of the "which" command in native PHP.
+ * @param string command
+ * @return string path to executable, or false on failure
+ */
+
+function which($executable)
+{
+  $path = ( isset($_ENV['PATH']) ) ? $_ENV['PATH'] : ( isset($_SERVER['PATH']) ? $_SERVER['PATH'] : false );
+  if ( !$path )
+    // couldn't get OS's PATH
+    return false;
+    
+  $win32 = ( PHP_OS == 'WINNT' || PHP_OS == 'WIN32' );
+  $extensions = $win32 ? array('.exe', '.com', '.bat') : array('');
+  $separator = $win32 ? ';' : ':';
+  $paths = explode($separator, $path);
+  foreach ( $paths as $dir )
+  {
+    foreach ( $extensions as $ext )
+    {
+      $fullpath = "$dir/{$executable}{$ext}";
+      if ( file_exists($fullpath) && is_executable($fullpath) )
+      {
+        return $fullpath;
+      }
+    }
+  }
+  return false;
+}
+
 ?>