roxen.lists.pike.general

Subject Author Date
Process.search_path is not compatible with windows ¹ùÑ©ËÉú—­zºZŸ ¤¢x¢ <¹ùÑ©ËÉú—­zºZŸ ¤¢x¢[at]nirgendwo> 03-06-2009
getenv("PATH") should be splited with ¡°;¡± other than ¡°:¡±

 

//!

string search_path(string command)

{

   if (command=="" || command[0]=='/') return command;

 

   if (!search_path_entries)

   {

      array(string) e=(getenv("PATH")||"")/":"-();

 

      multiset(string) filter=(<>);

      search_path_entries=({});

      foreach (e,string s)

      {

         string t;

         if (s[0]=='~')  // some shells allow ~-expansion in PATH

         {

            if (s[0..1]=="~/" && (t=[string]getenv("HOME")))

               s=t+s[1..];

            else

            {

               // expand user?

            }

         }

         if (!filter[s] /* && directory exist */ )

         {

            search_path_entries+=();

            filter[s]=1;

         }

      }

   }

   foreach (search_path_entries, string path)

   {

      string p=combine_path(path,command);

      Stdio.Stat s=file_stat(p);

      if (s && s->mode&0111) return p;

   }

   return 0;

}

 

Xuesong Guo