home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 10 Tools / 10-Tools.zip / plbin.zip / pl / dld-3.2 / dld_find_executable.c < prev    next >
C/C++ Source or Header  |  1992-05-26  |  1KB  |  65 lines

  1. /* Given a filename, dld_find_executable searches the directories listed in the
  2.    environment variable PATH for a file with that filename.
  3.    A new copy of the complete path name of that file is returned.  This new
  4.    string may be disposed by free() later on.
  5. */
  6.  
  7. #include <sys/file.h>
  8. #include <sys/param.h>
  9. #include <strings.h>
  10.  
  11. #define DEFAULT_PATH ".:~/bin::/usr/local/bin:/usr/new:/usr/ucb:/usr/bin:/bin:/usr/hosts"
  12.  
  13. static char *
  14. copy_of (s)
  15. register char *s;
  16. {
  17.     register char *p = (char *) malloc (strlen(s)+1);
  18.  
  19.     if (!p) return 0;
  20.  
  21.     *p = 0;
  22.     strcpy (p, s);
  23.     return p;
  24. }
  25.  
  26.  
  27. char *
  28. dld_find_executable (file)
  29. char *file;
  30. {
  31.     char *search;
  32.     register char *p;
  33.     
  34.     if (*file == '/')
  35.     return copy_of (file);
  36.     
  37.     if ((search = (char *) getenv("PATH")) == 0)
  38.     search = DEFAULT_PATH;
  39.     
  40.     p = search;
  41.     
  42.     while (*p) {
  43.     char  name[MAXPATHLEN];
  44.     register char *next;
  45.  
  46.     next = name;
  47.     
  48.     /* copy directory name into [name] */
  49.     while (*p && *p != ':') *next++ = *p++;
  50.     *next = 0;
  51.     if (*p) p++;
  52.  
  53.     if (name[0] == '.' && name[1] == 0)
  54.         getwd (name);
  55.     
  56.     strcat (name, "/");
  57.     strcat (name, file);
  58.           
  59.     if (access (name, X_OK) == 0)
  60.         return copy_of (name);
  61.     }
  62.  
  63.     return 0;
  64. }
  65.