home *** CD-ROM | disk | FTP | other *** search
/ Usenet 1994 January / usenetsourcesnewsgroupsinfomagicjanuary1994.iso / sources / unix / volume25 / trash / part01 / execvpath.c < prev    next >
Encoding:
C/C++ Source or Header  |  1992-03-22  |  790 b   |  52 lines

  1. #include    <unistd.h>
  2.  
  3. extern char    *getenv();
  4. extern char    *strchr();
  5.  
  6. static
  7. char    *
  8. next_attempted_path(prefix, last_component, result)
  9. char    *prefix;
  10. char    *last_component;
  11. char    *result;
  12. {
  13.     char    *s;
  14.  
  15.     s = result;
  16.  
  17.     while (*prefix != '\0' && *prefix != ':')
  18.         *s++ = *prefix++;
  19.  
  20.     if (result != s)
  21.         *s++ = '/';
  22.  
  23.     while (*last_component != '\0')
  24.         *s++ = *last_component++;
  25.  
  26.     *s = '\0';
  27.  
  28.     return (*prefix == '\0') ? (char *)0 : prefix + 1;
  29. }
  30.  
  31. char    *
  32. execvpath(name)
  33. char    *name;
  34. {
  35.     static char    fname[256];
  36.     char        *cp;
  37.  
  38.     if (name == (char *)0 || strchr(name, '/') != (char *)0)
  39.         return name;
  40.  
  41.     if ((cp = getenv("PATH")) == (char *)0)
  42.         cp = "";
  43.  
  44.     while ((cp = next_attempted_path(cp, name, &fname[0])) != (char *)0)
  45.     {
  46.         if (access(&fname[0], X_OK) == 0)
  47.             return &fname[0];
  48.     }
  49.  
  50.     return name;
  51. }
  52.