findprog-in.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399
  1. /* Locating a program in a given path.
  2. Copyright (C) 2001-2004, 2006-2023 Free Software Foundation, Inc.
  3. Written by Bruno Haible <haible@clisp.cons.org>, 2001, 2019.
  4. This file is free software: you can redistribute it and/or modify
  5. it under the terms of the GNU Lesser General Public License as
  6. published by the Free Software Foundation; either version 2.1 of the
  7. License, or (at your option) any later version.
  8. This file is distributed in the hope that it will be useful,
  9. but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  11. GNU Lesser General Public License for more details.
  12. You should have received a copy of the GNU Lesser General Public License
  13. along with this program. If not, see <https://www.gnu.org/licenses/>. */
  14. #include <config.h>
  15. /* Specification. */
  16. #include "findprog.h"
  17. #include <errno.h>
  18. #include <stdlib.h>
  19. #include <string.h>
  20. #include <unistd.h>
  21. #include <sys/stat.h>
  22. #include "filename.h"
  23. #include "concat-filename.h"
  24. #if (defined _WIN32 && !defined __CYGWIN__) || defined __EMX__ || defined __DJGPP__
  25. /* Native Windows, OS/2, DOS */
  26. # define NATIVE_SLASH '\\'
  27. #else
  28. /* Unix */
  29. # define NATIVE_SLASH '/'
  30. #endif
  31. /* Separator in PATH like lists of pathnames. */
  32. #if (defined _WIN32 && !defined __CYGWIN__) || defined __EMX__ || defined __DJGPP__
  33. /* Native Windows, OS/2, DOS */
  34. # define PATH_SEPARATOR ';'
  35. #else
  36. /* Unix */
  37. # define PATH_SEPARATOR ':'
  38. #endif
  39. /* The list of suffixes that the execlp/execvp function tries when searching
  40. for the program. */
  41. static const char * const suffixes[] =
  42. {
  43. #if defined _WIN32 && !defined __CYGWIN__ /* Native Windows */
  44. "", ".com", ".exe", ".bat", ".cmd"
  45. /* Note: Files without any suffix are not considered executable. */
  46. /* Note: The cmd.exe program does a different lookup: It searches according
  47. to the PATHEXT environment variable.
  48. See <https://stackoverflow.com/questions/7839150/>.
  49. Also, it executes files ending in .bat and .cmd directly without letting
  50. the kernel interpret the program file. */
  51. #elif defined __CYGWIN__
  52. "", ".exe", ".com"
  53. #elif defined __EMX__
  54. "", ".exe"
  55. #elif defined __DJGPP__
  56. "", ".com", ".exe", ".bat"
  57. #else /* Unix */
  58. ""
  59. #endif
  60. };
  61. const char *
  62. find_in_given_path (const char *progname, const char *path,
  63. const char *directory, bool optimize_for_exec)
  64. {
  65. {
  66. bool has_slash = false;
  67. {
  68. const char *p;
  69. for (p = progname; *p != '\0'; p++)
  70. if (ISSLASH (*p))
  71. {
  72. has_slash = true;
  73. break;
  74. }
  75. }
  76. if (has_slash)
  77. {
  78. /* If progname contains a slash, it is either absolute or relative to
  79. the current directory. PATH is not used. */
  80. if (optimize_for_exec)
  81. /* The execl/execv/execlp/execvp functions will try the various
  82. suffixes anyway and fail if no executable is found. */
  83. return progname;
  84. else
  85. {
  86. /* Try the various suffixes and see whether one of the files
  87. with such a suffix is actually executable. */
  88. int failure_errno;
  89. size_t i;
  90. const char *directory_as_prefix =
  91. (directory != NULL && IS_RELATIVE_FILE_NAME (progname)
  92. ? directory
  93. : "");
  94. #if defined _WIN32 && !defined __CYGWIN__ /* Native Windows */
  95. const char *progbasename;
  96. {
  97. const char *p;
  98. progbasename = progname;
  99. for (p = progname; *p != '\0'; p++)
  100. if (ISSLASH (*p))
  101. progbasename = p + 1;
  102. }
  103. bool progbasename_has_dot = (strchr (progbasename, '.') != NULL);
  104. #endif
  105. /* Try all platform-dependent suffixes. */
  106. failure_errno = ENOENT;
  107. for (i = 0; i < sizeof (suffixes) / sizeof (suffixes[0]); i++)
  108. {
  109. const char *suffix = suffixes[i];
  110. #if defined _WIN32 && !defined __CYGWIN__ /* Native Windows */
  111. /* File names without a '.' are not considered executable, and
  112. for file names with a '.' no additional suffix is tried. */
  113. if ((*suffix != '\0') != progbasename_has_dot)
  114. #endif
  115. {
  116. /* Concatenate directory_as_prefix, progname, suffix. */
  117. char *progpathname =
  118. concatenated_filename (directory_as_prefix, progname,
  119. suffix);
  120. if (progpathname == NULL)
  121. return NULL; /* errno is set here */
  122. /* On systems which have the eaccess() system call, let's
  123. use it. On other systems, let's hope that this program
  124. is not installed setuid or setgid, so that it is ok to
  125. call access() despite its design flaw. */
  126. if (eaccess (progpathname, X_OK) == 0)
  127. {
  128. /* Check that the progpathname does not point to a
  129. directory. */
  130. struct stat statbuf;
  131. if (stat (progpathname, &statbuf) >= 0)
  132. {
  133. if (! S_ISDIR (statbuf.st_mode))
  134. {
  135. /* Found! */
  136. if (strcmp (progpathname, progname) == 0)
  137. {
  138. free (progpathname);
  139. return progname;
  140. }
  141. else
  142. return progpathname;
  143. }
  144. errno = EACCES;
  145. }
  146. }
  147. if (errno != ENOENT)
  148. failure_errno = errno;
  149. free (progpathname);
  150. }
  151. }
  152. #if defined _WIN32 && !defined __CYGWIN__ /* Native Windows */
  153. if (failure_errno == ENOENT && !progbasename_has_dot)
  154. {
  155. /* In the loop above, we skipped suffix = "". Do this loop
  156. round now, merely to provide a better errno than ENOENT. */
  157. char *progpathname =
  158. concatenated_filename (directory_as_prefix, progname, "");
  159. if (progpathname == NULL)
  160. return NULL; /* errno is set here */
  161. if (eaccess (progpathname, X_OK) == 0)
  162. {
  163. struct stat statbuf;
  164. if (stat (progpathname, &statbuf) >= 0)
  165. {
  166. if (! S_ISDIR (statbuf.st_mode))
  167. errno = ENOEXEC;
  168. else
  169. errno = EACCES;
  170. }
  171. }
  172. failure_errno = errno;
  173. free (progpathname);
  174. }
  175. #endif
  176. errno = failure_errno;
  177. return NULL;
  178. }
  179. }
  180. }
  181. if (path == NULL)
  182. /* If PATH is not set, the default search path is implementation dependent.
  183. In practice, it is treated like an empty PATH. */
  184. path = "";
  185. {
  186. /* Make a copy, to prepare for destructive modifications. */
  187. char *path_copy = strdup (path);
  188. if (path_copy == NULL)
  189. return NULL; /* errno is set here */
  190. int failure_errno;
  191. char *path_rest;
  192. char *cp;
  193. #if defined _WIN32 && !defined __CYGWIN__ /* Native Windows */
  194. bool progname_has_dot = (strchr (progname, '.') != NULL);
  195. #endif
  196. failure_errno = ENOENT;
  197. for (path_rest = path_copy; ; path_rest = cp + 1)
  198. {
  199. const char *dir;
  200. bool last;
  201. char *dir_as_prefix_to_free;
  202. const char *dir_as_prefix;
  203. size_t i;
  204. /* Extract next directory in PATH. */
  205. dir = path_rest;
  206. for (cp = path_rest; *cp != '\0' && *cp != PATH_SEPARATOR; cp++)
  207. ;
  208. last = (*cp == '\0');
  209. *cp = '\0';
  210. /* Empty PATH components designate the current directory. */
  211. if (dir == cp)
  212. dir = ".";
  213. /* Concatenate directory and dir. */
  214. if (directory != NULL && IS_RELATIVE_FILE_NAME (dir))
  215. {
  216. dir_as_prefix_to_free =
  217. concatenated_filename (directory, dir, NULL);
  218. if (dir_as_prefix_to_free == NULL)
  219. {
  220. /* errno is set here. */
  221. failure_errno = errno;
  222. goto failed;
  223. }
  224. dir_as_prefix = dir_as_prefix_to_free;
  225. }
  226. else
  227. {
  228. dir_as_prefix_to_free = NULL;
  229. dir_as_prefix = dir;
  230. }
  231. /* Try all platform-dependent suffixes. */
  232. for (i = 0; i < sizeof (suffixes) / sizeof (suffixes[0]); i++)
  233. {
  234. const char *suffix = suffixes[i];
  235. #if defined _WIN32 && !defined __CYGWIN__ /* Native Windows */
  236. /* File names without a '.' are not considered executable, and
  237. for file names with a '.' no additional suffix is tried. */
  238. if ((*suffix != '\0') != progname_has_dot)
  239. #endif
  240. {
  241. /* Concatenate dir_as_prefix, progname, and suffix. */
  242. char *progpathname =
  243. concatenated_filename (dir_as_prefix, progname, suffix);
  244. if (progpathname == NULL)
  245. {
  246. /* errno is set here. */
  247. failure_errno = errno;
  248. free (dir_as_prefix_to_free);
  249. goto failed;
  250. }
  251. /* On systems which have the eaccess() system call, let's
  252. use it. On other systems, let's hope that this program
  253. is not installed setuid or setgid, so that it is ok to
  254. call access() despite its design flaw. */
  255. if (eaccess (progpathname, X_OK) == 0)
  256. {
  257. /* Check that the progpathname does not point to a
  258. directory. */
  259. struct stat statbuf;
  260. if (stat (progpathname, &statbuf) >= 0)
  261. {
  262. if (! S_ISDIR (statbuf.st_mode))
  263. {
  264. /* Found! */
  265. if (strcmp (progpathname, progname) == 0)
  266. {
  267. free (progpathname);
  268. /* Add the "./" prefix for real, that
  269. concatenated_filename() optimized away.
  270. This avoids a second PATH search when the
  271. caller uses execl/execv/execlp/execvp. */
  272. progpathname =
  273. (char *) malloc (2 + strlen (progname) + 1);
  274. if (progpathname == NULL)
  275. {
  276. /* errno is set here. */
  277. failure_errno = errno;
  278. free (dir_as_prefix_to_free);
  279. goto failed;
  280. }
  281. progpathname[0] = '.';
  282. progpathname[1] = NATIVE_SLASH;
  283. memcpy (progpathname + 2, progname,
  284. strlen (progname) + 1);
  285. }
  286. free (dir_as_prefix_to_free);
  287. free (path_copy);
  288. return progpathname;
  289. }
  290. errno = EACCES;
  291. }
  292. }
  293. if (errno != ENOENT)
  294. failure_errno = errno;
  295. free (progpathname);
  296. }
  297. }
  298. #if defined _WIN32 && !defined __CYGWIN__ /* Native Windows */
  299. if (failure_errno == ENOENT && !progname_has_dot)
  300. {
  301. /* In the loop above, we skipped suffix = "". Do this loop
  302. round now, merely to provide a better errno than ENOENT. */
  303. char *progpathname =
  304. concatenated_filename (dir_as_prefix, progname, "");
  305. if (progpathname == NULL)
  306. {
  307. /* errno is set here. */
  308. failure_errno = errno;
  309. free (dir_as_prefix_to_free);
  310. goto failed;
  311. }
  312. if (eaccess (progpathname, X_OK) == 0)
  313. {
  314. struct stat statbuf;
  315. if (stat (progpathname, &statbuf) >= 0)
  316. {
  317. if (! S_ISDIR (statbuf.st_mode))
  318. errno = ENOEXEC;
  319. else
  320. errno = EACCES;
  321. }
  322. }
  323. failure_errno = errno;
  324. free (progpathname);
  325. }
  326. #endif
  327. free (dir_as_prefix_to_free);
  328. if (last)
  329. break;
  330. }
  331. failed:
  332. /* Not found in PATH. */
  333. free (path_copy);
  334. errno = failure_errno;
  335. return NULL;
  336. }
  337. }