1 /* Locating a program in PATH. 2 Copyright (C) 2001-2004, 2006 Free Software Foundation, Inc. 3 Written by Bruno Haible <haible@clisp.cons.org>, 2001. 4 5 This program is free software; you can redistribute it and/or modify 6 it under the terms of the GNU General Public License as published by 7 the Free Software Foundation; either version 2, or (at your option) 8 any later version. 9 10 This program is distributed in the hope that it will be useful, 11 but WITHOUT ANY WARRANTY; without even the implied warranty of 12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 13 GNU General Public License for more details. 14 15 You should have received a copy of the GNU General Public License 16 along with this program; if not, write to the Free Software Foundation, 17 Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. */ 18 19 20 #include <config.h> 21 22 /* Specification. */ 23 #include "findprog.h" 24 25 #include <stdbool.h> 26 #include <stdlib.h> 27 #include <string.h> 28 #include <unistd.h> 29 30 #include "xalloc.h" 31 #include "pathname.h" 32 33 34 const char * 35 find_in_path (const char *progname) 36 { 37 #if defined _WIN32 || defined __WIN32__ || defined __CYGWIN__ || defined __EMX__ || defined __DJGPP__ 38 /* Win32, Cygwin, OS/2, DOS */ 39 /* The searching rules with .COM, .EXE, .BAT, .CMD etc. suffixes are 40 too complicated. Leave it to the OS. */ 41 return progname; 42 #else 43 /* Unix */ 44 char *path; 45 char *path_rest; 46 char *cp; 47 48 if (strchr (progname, '/') != NULL) 49 /* If progname contains a slash, it is either absolute or relative to 50 the current directory. PATH is not used. */ 51 return progname; 52 53 path = getenv ("PATH"); 54 if (path == NULL || *path == '\0') 55 /* If PATH is not set, the default search path is implementation 56 dependent. */ 57 return progname; 58 59 /* Make a copy, to prepare for destructive modifications. */ 60 path = xstrdup (path); 61 for (path_rest = path; ; path_rest = cp + 1) 62 { 63 const char *dir; 64 bool last; 65 char *progpathname; 66 67 /* Extract next directory in PATH. */ 68 dir = path_rest; 69 for (cp = path_rest; *cp != '\0' && *cp != ':'; cp++) 70 ; 71 last = (*cp == '\0'); 72 *cp = '\0'; 73 74 /* Empty PATH components designate the current directory. */ 75 if (dir == cp) 76 dir = "."; 77 78 /* Concatenate dir and progname. */ 79 progpathname = concatenated_pathname (dir, progname, NULL); 80 81 /* On systems which have the eaccess() system call, let's use it. 82 On other systems, let's hope that this program is not installed 83 setuid or setgid, so that it is ok to call access() despite its 84 design flaw. */ 85 if (eaccess (progpathname, X_OK) == 0) 86 { 87 /* Found! */ 88 if (strcmp (progpathname, progname) == 0) 89 { 90 free (progpathname); 91 92 /* Add the "./" prefix for real, that concatenated_pathname() 93 optimized away. This avoids a second PATH search when the 94 caller uses execlp/execvp. */ 95 progpathname = xmalloc (2 + strlen (progname) + 1); 96 progpathname[0] = '.'; 97 progpathname[1] = '/'; 98 memcpy (progpathname + 2, progname, strlen (progname) + 1); 99 } 100 101 free (path); 102 return progpathname; 103 } 104 105 free (progpathname); 106 107 if (last) 108 break; 109 } 110 111 /* Not found in PATH. An error will be signalled at the first call. */ 112 free (path); 113 return progname; 114 #endif 115 } 116