Zend - The PHP Company




External

Add Code


Whereis  

Type: code fragment
Added by: RLYNCH
Entered: 27/01/2000
Last modified: 08/12/1999
Rating: - (fewer than 3 votes)
Views: 7565
Will attempt to locate a given OS program.


<?php
    
function whereis ($filename){
        
$path GetEnv('path') or GetEnv('PATH') or GetEnv('Path');
        
$paths explode(';'$path);
        
        
//Throw in a few more places to look
        
$paths[] = '/usr/bin/';
        
$paths[] = '/usr/sbin/';
        
$paths[] = '/usr/local/bin/';
        
$paths[] = '/usr/local/sbin/';
        
$paths[] = './';
        
$paths[] = './cgi/';
        
        
$whereis '';
        
reset($paths);
        while (!
$whereis && (list(,$p) = each($paths))){
            if (
$p){
                
$end $p[strlen($p)-1];
                if (
$end != '/' && $end != '\'){
                    $p .= '
/';
                }
                $f = $p . $filename;
                if (file_exists($f)){
                    $perms = fileperms($f);
                    $owner = fileowner($f);
                    $myuid = getmyuid();
                    //Enhancement: If somebody could figure out how to chase down
                    //Group IDs without invoking exec() et al...
                    if (($perms & 1) || (($perms & 64) && ($owner == $myuid))){
                        $whereis = $f;
                    }
                }
            }
        }
        return $whereis;
    }
    
    /*
    echo "Whereis: ", whereis('
whereis'), "<BR>n";
    echo "Whoami: ", whereis('
whoami'), "<BR>n";
    echo "asdf: ", whereis('
asdf'), "<BR>n";
    */
?>


Usage Example


    echo "Whereis: ", whereis('whereis'), "<BR>n";
    echo "Whoami: ", whereis('whoami'), "<BR>n";
    echo "asdf: ", whereis('asdf'), "<BR>n";


Rate This Script





Search



This Category All Categories