findprog-in.c 13 KB

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