]>
Commit | Line | Data |
---|---|---|
cce855bc JA |
1 | /* findcmd.c -- Functions to search for commands by name. */ |
2 | ||
74091dd4 | 3 | /* Copyright (C) 1997-2022 Free Software Foundation, Inc. |
cce855bc JA |
4 | |
5 | This file is part of GNU Bash, the Bourne Again SHell. | |
6 | ||
3185942a JA |
7 | Bash is free software: you can redistribute it and/or modify |
8 | it under the terms of the GNU General Public License as published by | |
9 | the Free Software Foundation, either version 3 of the License, or | |
10 | (at your option) any later version. | |
cce855bc | 11 | |
3185942a JA |
12 | Bash is distributed in the hope that it will be useful, |
13 | but WITHOUT ANY WARRANTY; without even the implied warranty of | |
14 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
15 | GNU General Public License for more details. | |
cce855bc JA |
16 | |
17 | You should have received a copy of the GNU General Public License | |
3185942a JA |
18 | along with Bash. If not, see <http://www.gnu.org/licenses/>. |
19 | */ | |
cce855bc JA |
20 | |
21 | #include "config.h" | |
22 | ||
23 | #include <stdio.h> | |
f73dda09 | 24 | #include "chartypes.h" |
cce855bc | 25 | #include "bashtypes.h" |
b80f6443 | 26 | #if !defined (_MINIX) && defined (HAVE_SYS_FILE_H) |
cce855bc JA |
27 | # include <sys/file.h> |
28 | #endif | |
29 | #include "filecntl.h" | |
30 | #include "posixstat.h" | |
31 | ||
32 | #if defined (HAVE_UNISTD_H) | |
33 | # include <unistd.h> | |
34 | #endif | |
495aee44 | 35 | #include <errno.h> |
cce855bc | 36 | |
cce855bc JA |
37 | #include "bashansi.h" |
38 | ||
39 | #include "memalloc.h" | |
40 | #include "shell.h" | |
d233b485 | 41 | #include "execute_cmd.h" |
cce855bc JA |
42 | #include "flags.h" |
43 | #include "hashlib.h" | |
44 | #include "pathexp.h" | |
45 | #include "hashcmd.h" | |
f73dda09 | 46 | #include "findcmd.h" /* matching prototypes and declarations */ |
cce855bc | 47 | |
a0c0a00f CR |
48 | #include <glob/strmatch.h> |
49 | ||
495aee44 CR |
50 | #if !defined (errno) |
51 | extern int errno; | |
52 | #endif | |
53 | ||
cce855bc | 54 | /* Static functions defined and used in this file. */ |
8868edaf CR |
55 | static char *_find_user_command_internal PARAMS((const char *, int)); |
56 | static char *find_user_command_internal PARAMS((const char *, int)); | |
74091dd4 CR |
57 | static char *find_user_command_in_path PARAMS((const char *, char *, int, int *)); |
58 | static char *find_in_path_element PARAMS((const char *, char *, int, int, struct stat *, int *)); | |
8868edaf | 59 | static char *find_absolute_program PARAMS((const char *, int)); |
f73dda09 | 60 | |
8868edaf | 61 | static char *get_next_path_element PARAMS((char *, int *)); |
cce855bc JA |
62 | |
63 | /* The file name which we would try to execute, except that it isn't | |
64 | possible to execute it. This is the first file that matches the | |
65 | name that we are looking for while we are searching $PATH for a | |
66 | suitable one to execute. If we cannot find a suitable executable | |
67 | file, then we use this one. */ | |
68 | static char *file_to_lose_on; | |
69 | ||
70 | /* Non-zero if we should stat every command found in the hash table to | |
71 | make sure it still exists. */ | |
a0c0a00f | 72 | int check_hashed_filenames = CHECKHASH_DEFAULT; |
cce855bc JA |
73 | |
74 | /* DOT_FOUND_IN_SEARCH becomes non-zero when find_user_command () | |
75 | encounters a `.' as the directory pathname while scanning the | |
76 | list of possible pathnames; i.e., if `.' comes before the directory | |
77 | containing the file of interest. */ | |
78 | int dot_found_in_search = 0; | |
79 | ||
a0c0a00f CR |
80 | /* Set up EXECIGNORE; a blacklist of patterns that executable files should not |
81 | match. */ | |
82 | static struct ignorevar execignore = | |
83 | { | |
84 | "EXECIGNORE", | |
85 | NULL, | |
86 | 0, | |
87 | NULL, | |
88 | NULL | |
89 | }; | |
90 | ||
91 | void | |
92 | setup_exec_ignore (varname) | |
93 | char *varname; | |
94 | { | |
95 | setup_ignore_patterns (&execignore); | |
96 | } | |
97 | ||
98 | static int | |
99 | exec_name_should_ignore (name) | |
100 | const char *name; | |
101 | { | |
102 | struct ign *p; | |
103 | ||
104 | for (p = execignore.ignores; p && p->val; p++) | |
105 | if (strmatch (p->val, (char *)name, FNMATCH_EXTFLAG|FNM_CASEFOLD) != FNM_NOMATCH) | |
106 | return 1; | |
107 | return 0; | |
108 | } | |
109 | ||
cce855bc JA |
110 | /* Return some flags based on information about this file. |
111 | The EXISTS bit is non-zero if the file is found. | |
74091dd4 | 112 | The EXECABLE bit is non-zero the file is executable. |
cce855bc JA |
113 | Zero is returned if the file is not found. */ |
114 | int | |
115 | file_status (name) | |
f73dda09 | 116 | const char *name; |
cce855bc JA |
117 | { |
118 | struct stat finfo; | |
95732b49 | 119 | int r; |
cce855bc JA |
120 | |
121 | /* Determine whether this file exists or not. */ | |
122 | if (stat (name, &finfo) < 0) | |
123 | return (0); | |
124 | ||
125 | /* If the file is a directory, then it is not "executable" in the | |
126 | sense of the shell. */ | |
127 | if (S_ISDIR (finfo.st_mode)) | |
128 | return (FS_EXISTS|FS_DIRECTORY); | |
129 | ||
95732b49 JA |
130 | r = FS_EXISTS; |
131 | ||
0001803f CR |
132 | #if defined (HAVE_EACCESS) |
133 | /* Use eaccess(2) if we have it to take things like ACLs and other | |
134 | file access mechanisms into account. eaccess uses the effective | |
135 | user and group IDs, not the real ones. We could use sh_eaccess, | |
136 | but we don't want any special treatment for /dev/fd. */ | |
a0c0a00f | 137 | if (exec_name_should_ignore (name) == 0 && eaccess (name, X_OK) == 0) |
0001803f CR |
138 | r |= FS_EXECABLE; |
139 | if (eaccess (name, R_OK) == 0) | |
140 | r |= FS_READABLE; | |
141 | ||
142 | return r; | |
143 | #elif defined (AFS) | |
cce855bc JA |
144 | /* We have to use access(2) to determine access because AFS does not |
145 | support Unix file system semantics. This may produce wrong | |
146 | answers for non-AFS files when ruid != euid. I hate AFS. */ | |
a0c0a00f | 147 | if (exec_name_should_ignore (name) == 0 && access (name, X_OK) == 0) |
95732b49 JA |
148 | r |= FS_EXECABLE; |
149 | if (access (name, R_OK) == 0) | |
150 | r |= FS_READABLE; | |
151 | ||
152 | return r; | |
0001803f | 153 | #else /* !HAVE_EACCESS && !AFS */ |
cce855bc JA |
154 | |
155 | /* Find out if the file is actually executable. By definition, the | |
156 | only other criteria is that the file has an execute bit set that | |
95732b49 | 157 | we can use. The same with whether or not a file is readable. */ |
cce855bc JA |
158 | |
159 | /* Root only requires execute permission for any of owner, group or | |
95732b49 | 160 | others to be able to exec a file, and can read any file. */ |
cce855bc JA |
161 | if (current_user.euid == (uid_t)0) |
162 | { | |
95732b49 | 163 | r |= FS_READABLE; |
a0c0a00f | 164 | if (exec_name_should_ignore (name) == 0 && (finfo.st_mode & S_IXUGO)) |
95732b49 JA |
165 | r |= FS_EXECABLE; |
166 | return r; | |
cce855bc JA |
167 | } |
168 | ||
95732b49 | 169 | /* If we are the owner of the file, the owner bits apply. */ |
b80f6443 | 170 | if (current_user.euid == finfo.st_uid) |
95732b49 | 171 | { |
a0c0a00f | 172 | if (exec_name_should_ignore (name) == 0 && (finfo.st_mode & S_IXUSR)) |
95732b49 JA |
173 | r |= FS_EXECABLE; |
174 | if (finfo.st_mode & S_IRUSR) | |
175 | r |= FS_READABLE; | |
176 | } | |
cce855bc JA |
177 | |
178 | /* If we are in the owning group, the group permissions apply. */ | |
b80f6443 | 179 | else if (group_member (finfo.st_gid)) |
95732b49 | 180 | { |
a0c0a00f | 181 | if (exec_name_should_ignore (name) == 0 && (finfo.st_mode & S_IXGRP)) |
95732b49 JA |
182 | r |= FS_EXECABLE; |
183 | if (finfo.st_mode & S_IRGRP) | |
184 | r |= FS_READABLE; | |
185 | } | |
cce855bc | 186 | |
b80f6443 JA |
187 | /* Else we check whether `others' have permission to execute the file */ |
188 | else | |
95732b49 | 189 | { |
a0c0a00f | 190 | if (exec_name_should_ignore (name) == 0 && finfo.st_mode & S_IXOTH) |
95732b49 JA |
191 | r |= FS_EXECABLE; |
192 | if (finfo.st_mode & S_IROTH) | |
193 | r |= FS_READABLE; | |
194 | } | |
195 | ||
196 | return r; | |
cce855bc JA |
197 | #endif /* !AFS */ |
198 | } | |
199 | ||
200 | /* Return non-zero if FILE exists and is executable. | |
201 | Note that this function is the definition of what an | |
202 | executable file is; do not change this unless YOU know | |
203 | what an executable file is. */ | |
204 | int | |
205 | executable_file (file) | |
f73dda09 | 206 | const char *file; |
cce855bc JA |
207 | { |
208 | int s; | |
209 | ||
210 | s = file_status (file); | |
a0c0a00f | 211 | #if defined (EISDIR) |
495aee44 CR |
212 | if (s & FS_DIRECTORY) |
213 | errno = EISDIR; /* let's see if we can improve error messages */ | |
214 | #endif | |
cce855bc JA |
215 | return ((s & FS_EXECABLE) && ((s & FS_DIRECTORY) == 0)); |
216 | } | |
217 | ||
218 | int | |
219 | is_directory (file) | |
f73dda09 | 220 | const char *file; |
cce855bc JA |
221 | { |
222 | return (file_status (file) & FS_DIRECTORY); | |
223 | } | |
224 | ||
28ef6c31 JA |
225 | int |
226 | executable_or_directory (file) | |
f73dda09 | 227 | const char *file; |
28ef6c31 JA |
228 | { |
229 | int s; | |
230 | ||
231 | s = file_status (file); | |
232 | return ((s & FS_EXECABLE) || (s & FS_DIRECTORY)); | |
233 | } | |
234 | ||
cce855bc JA |
235 | /* Locate the executable file referenced by NAME, searching along |
236 | the contents of the shell PATH variable. Return a new string | |
237 | which is the full pathname to the file, or NULL if the file | |
238 | couldn't be found. If a file is found that isn't executable, | |
239 | and that is the only match, then return that. */ | |
240 | char * | |
241 | find_user_command (name) | |
f73dda09 | 242 | const char *name; |
cce855bc JA |
243 | { |
244 | return (find_user_command_internal (name, FS_EXEC_PREFERRED|FS_NODIRS)); | |
245 | } | |
246 | ||
247 | /* Locate the file referenced by NAME, searching along the contents | |
248 | of the shell PATH variable. Return a new string which is the full | |
249 | pathname to the file, or NULL if the file couldn't be found. This | |
95732b49 JA |
250 | returns the first readable file found; designed to be used to look |
251 | for shell scripts or files to source. */ | |
cce855bc JA |
252 | char * |
253 | find_path_file (name) | |
f73dda09 | 254 | const char *name; |
cce855bc | 255 | { |
95732b49 | 256 | return (find_user_command_internal (name, FS_READABLE)); |
cce855bc JA |
257 | } |
258 | ||
259 | static char * | |
260 | _find_user_command_internal (name, flags) | |
f73dda09 | 261 | const char *name; |
cce855bc JA |
262 | int flags; |
263 | { | |
264 | char *path_list, *cmd; | |
265 | SHELL_VAR *var; | |
266 | ||
7117c2d2 | 267 | /* Search for the value of PATH in both the temporary environments and |
cce855bc | 268 | in the regular list of variables. */ |
ac50fbac | 269 | if (var = find_variable_tempenv ("PATH")) /* XXX could be array? */ |
cce855bc JA |
270 | path_list = value_cell (var); |
271 | else | |
272 | path_list = (char *)NULL; | |
273 | ||
274 | if (path_list == 0 || *path_list == '\0') | |
275 | return (savestring (name)); | |
276 | ||
74091dd4 | 277 | cmd = find_user_command_in_path (name, path_list, flags, (int *)0); |
cce855bc | 278 | |
cce855bc JA |
279 | return (cmd); |
280 | } | |
281 | ||
282 | static char * | |
283 | find_user_command_internal (name, flags) | |
f73dda09 | 284 | const char *name; |
cce855bc JA |
285 | int flags; |
286 | { | |
287 | #ifdef __WIN32__ | |
288 | char *res, *dotexe; | |
289 | ||
f73dda09 | 290 | dotexe = (char *)xmalloc (strlen (name) + 5); |
cce855bc JA |
291 | strcpy (dotexe, name); |
292 | strcat (dotexe, ".exe"); | |
293 | res = _find_user_command_internal (dotexe, flags); | |
294 | free (dotexe); | |
295 | if (res == 0) | |
296 | res = _find_user_command_internal (name, flags); | |
297 | return res; | |
298 | #else | |
299 | return (_find_user_command_internal (name, flags)); | |
300 | #endif | |
301 | } | |
302 | ||
303 | /* Return the next element from PATH_LIST, a colon separated list of | |
304 | paths. PATH_INDEX_POINTER is the address of an index into PATH_LIST; | |
305 | the index is modified by this function. | |
306 | Return the next element of PATH_LIST or NULL if there are no more. */ | |
307 | static char * | |
308 | get_next_path_element (path_list, path_index_pointer) | |
309 | char *path_list; | |
310 | int *path_index_pointer; | |
311 | { | |
312 | char *path; | |
313 | ||
314 | path = extract_colon_unit (path_list, path_index_pointer); | |
315 | ||
bb70624e | 316 | if (path == 0) |
cce855bc JA |
317 | return (path); |
318 | ||
bb70624e | 319 | if (*path == '\0') |
cce855bc JA |
320 | { |
321 | free (path); | |
322 | path = savestring ("."); | |
323 | } | |
324 | ||
325 | return (path); | |
326 | } | |
327 | ||
328 | /* Look for PATHNAME in $PATH. Returns either the hashed command | |
329 | corresponding to PATHNAME or the first instance of PATHNAME found | |
a0c0a00f | 330 | in $PATH. If (FLAGS&CMDSRCH_HASH) is non-zero, insert the instance of |
74091dd4 CR |
331 | PATHNAME found in $PATH into the command hash table. |
332 | If (FLAGS&CMDSRCH_STDPATH) is non-zero, we are running in a `command -p' | |
333 | environment and should use the Posix standard path. | |
a0c0a00f | 334 | Returns a newly-allocated string. */ |
cce855bc | 335 | char * |
ac50fbac | 336 | search_for_command (pathname, flags) |
f73dda09 | 337 | const char *pathname; |
ac50fbac | 338 | int flags; |
cce855bc | 339 | { |
d233b485 | 340 | char *hashed_file, *command, *path_list; |
cce855bc JA |
341 | int temp_path, st; |
342 | SHELL_VAR *path; | |
343 | ||
344 | hashed_file = command = (char *)NULL; | |
345 | ||
346 | /* If PATH is in the temporary environment for this command, don't use the | |
347 | hash table to search for the full pathname. */ | |
ac50fbac | 348 | path = find_variable_tempenv ("PATH"); |
7117c2d2 | 349 | temp_path = path && tempvar_p (path); |
cce855bc JA |
350 | |
351 | /* Don't waste time trying to find hashed data for a pathname | |
352 | that is already completely specified or if we're using a command- | |
353 | specific value for PATH. */ | |
74091dd4 | 354 | if (temp_path == 0 && (flags & CMDSRCH_STDPATH) == 0 && absolute_program (pathname) == 0) |
7117c2d2 | 355 | hashed_file = phash_search (pathname); |
cce855bc JA |
356 | |
357 | /* If a command found in the hash table no longer exists, we need to | |
358 | look for it in $PATH. Thank you Posix.2. This forces us to stat | |
359 | every command found in the hash table. */ | |
360 | ||
361 | if (hashed_file && (posixly_correct || check_hashed_filenames)) | |
362 | { | |
363 | st = file_status (hashed_file); | |
f1be666c | 364 | if ((st & (FS_EXISTS|FS_EXECABLE)) != (FS_EXISTS|FS_EXECABLE)) |
cce855bc | 365 | { |
7117c2d2 | 366 | phash_remove (pathname); |
cce855bc JA |
367 | free (hashed_file); |
368 | hashed_file = (char *)NULL; | |
369 | } | |
370 | } | |
371 | ||
372 | if (hashed_file) | |
373 | command = hashed_file; | |
374 | else if (absolute_program (pathname)) | |
375 | /* A command containing a slash is not looked up in PATH or saved in | |
376 | the hash table. */ | |
377 | command = savestring (pathname); | |
378 | else | |
379 | { | |
a0c0a00f | 380 | if (flags & CMDSRCH_STDPATH) |
d233b485 | 381 | path_list = conf_standard_path (); |
a0c0a00f | 382 | else if (temp_path || path) |
d233b485 | 383 | path_list = value_cell (path); |
a0c0a00f | 384 | else |
d233b485 | 385 | path_list = 0; |
a0c0a00f | 386 | |
74091dd4 | 387 | command = find_user_command_in_path (pathname, path_list, FS_EXEC_PREFERRED|FS_NODIRS, &st); |
a0c0a00f CR |
388 | |
389 | if (command && hashing_enabled && temp_path == 0 && (flags & CMDSRCH_HASH)) | |
28ef6c31 | 390 | { |
a0c0a00f CR |
391 | /* If we found the full pathname the same as the command name, the |
392 | command probably doesn't exist. Don't put it into the hash | |
74091dd4 | 393 | table unless it's an executable file in the current directory. */ |
a0c0a00f CR |
394 | if (STREQ (command, pathname)) |
395 | { | |
a0c0a00f CR |
396 | if (st & FS_EXECABLE) |
397 | phash_insert ((char *)pathname, command, dot_found_in_search, 1); | |
398 | } | |
8868edaf CR |
399 | /* If we're in posix mode, don't add files without the execute bit |
400 | to the hash table. */ | |
74091dd4 | 401 | else if (posixly_correct || check_hashed_filenames) |
8868edaf | 402 | { |
8868edaf CR |
403 | if (st & FS_EXECABLE) |
404 | phash_insert ((char *)pathname, command, dot_found_in_search, 1); | |
405 | } | |
a0c0a00f CR |
406 | else |
407 | phash_insert ((char *)pathname, command, dot_found_in_search, 1); | |
28ef6c31 | 408 | } |
a0c0a00f CR |
409 | |
410 | if (flags & CMDSRCH_STDPATH) | |
d233b485 | 411 | free (path_list); |
cce855bc | 412 | } |
a0c0a00f | 413 | |
cce855bc JA |
414 | return (command); |
415 | } | |
416 | ||
417 | char * | |
418 | user_command_matches (name, flags, state) | |
f73dda09 | 419 | const char *name; |
cce855bc JA |
420 | int flags, state; |
421 | { | |
422 | register int i; | |
423 | int path_index, name_len; | |
424 | char *path_list, *path_element, *match; | |
425 | struct stat dotinfo; | |
426 | static char **match_list = NULL; | |
427 | static int match_list_size = 0; | |
428 | static int match_index = 0; | |
429 | ||
430 | if (state == 0) | |
431 | { | |
432 | /* Create the list of matches. */ | |
433 | if (match_list == 0) | |
434 | { | |
435 | match_list_size = 5; | |
7117c2d2 | 436 | match_list = strvec_create (match_list_size); |
cce855bc JA |
437 | } |
438 | ||
439 | /* Clear out the old match list. */ | |
440 | for (i = 0; i < match_list_size; i++) | |
441 | match_list[i] = 0; | |
442 | ||
443 | /* We haven't found any files yet. */ | |
444 | match_index = 0; | |
445 | ||
446 | if (absolute_program (name)) | |
447 | { | |
448 | match_list[0] = find_absolute_program (name, flags); | |
449 | match_list[1] = (char *)NULL; | |
450 | path_list = (char *)NULL; | |
451 | } | |
452 | else | |
453 | { | |
454 | name_len = strlen (name); | |
455 | file_to_lose_on = (char *)NULL; | |
456 | dot_found_in_search = 0; | |
ac50fbac CR |
457 | if (stat (".", &dotinfo) < 0) |
458 | dotinfo.st_dev = dotinfo.st_ino = 0; /* so same_file won't match */ | |
cce855bc JA |
459 | path_list = get_string_value ("PATH"); |
460 | path_index = 0; | |
461 | } | |
462 | ||
463 | while (path_list && path_list[path_index]) | |
464 | { | |
465 | path_element = get_next_path_element (path_list, &path_index); | |
466 | ||
467 | if (path_element == 0) | |
468 | break; | |
469 | ||
74091dd4 | 470 | match = find_in_path_element (name, path_element, flags, name_len, &dotinfo, (int *)0); |
cce855bc JA |
471 | free (path_element); |
472 | ||
473 | if (match == 0) | |
474 | continue; | |
475 | ||
476 | if (match_index + 1 == match_list_size) | |
477 | { | |
478 | match_list_size += 10; | |
7117c2d2 | 479 | match_list = strvec_resize (match_list, (match_list_size + 1)); |
cce855bc JA |
480 | } |
481 | ||
482 | match_list[match_index++] = match; | |
483 | match_list[match_index] = (char *)NULL; | |
484 | FREE (file_to_lose_on); | |
485 | file_to_lose_on = (char *)NULL; | |
486 | } | |
487 | ||
488 | /* We haven't returned any strings yet. */ | |
489 | match_index = 0; | |
490 | } | |
491 | ||
492 | match = match_list[match_index]; | |
493 | ||
494 | if (match) | |
495 | match_index++; | |
496 | ||
497 | return (match); | |
498 | } | |
499 | ||
cce855bc JA |
500 | static char * |
501 | find_absolute_program (name, flags) | |
f73dda09 | 502 | const char *name; |
cce855bc JA |
503 | int flags; |
504 | { | |
505 | int st; | |
506 | ||
507 | st = file_status (name); | |
508 | ||
509 | /* If the file doesn't exist, quit now. */ | |
510 | if ((st & FS_EXISTS) == 0) | |
511 | return ((char *)NULL); | |
512 | ||
513 | /* If we only care about whether the file exists or not, return | |
514 | this filename. Otherwise, maybe we care about whether this | |
515 | file is executable. If it is, and that is what we want, return it. */ | |
516 | if ((flags & FS_EXISTS) || ((flags & FS_EXEC_ONLY) && (st & FS_EXECABLE))) | |
517 | return (savestring (name)); | |
518 | ||
f73dda09 | 519 | return (NULL); |
cce855bc JA |
520 | } |
521 | ||
522 | static char * | |
74091dd4 | 523 | find_in_path_element (name, path, flags, name_len, dotinfop, rflagsp) |
f73dda09 JA |
524 | const char *name; |
525 | char *path; | |
cce855bc JA |
526 | int flags, name_len; |
527 | struct stat *dotinfop; | |
74091dd4 | 528 | int *rflagsp; |
cce855bc JA |
529 | { |
530 | int status; | |
531 | char *full_path, *xpath; | |
532 | ||
a0c0a00f | 533 | xpath = (posixly_correct == 0 && *path == '~') ? bash_tilde_expand (path, 0) : path; |
cce855bc JA |
534 | |
535 | /* Remember the location of "." in the path, in all its forms | |
536 | (as long as they begin with a `.', e.g. `./.') */ | |
74091dd4 CR |
537 | /* We could also do this or something similar for all relative pathnames |
538 | found while searching PATH. */ | |
cce855bc JA |
539 | if (dot_found_in_search == 0 && *xpath == '.') |
540 | dot_found_in_search = same_file (".", xpath, dotinfop, (struct stat *)NULL); | |
541 | ||
bb70624e | 542 | full_path = sh_makepath (xpath, name, 0); |
cce855bc JA |
543 | |
544 | status = file_status (full_path); | |
545 | ||
546 | if (xpath != path) | |
547 | free (xpath); | |
548 | ||
74091dd4 CR |
549 | if (rflagsp) |
550 | *rflagsp = status; | |
551 | ||
cce855bc JA |
552 | if ((status & FS_EXISTS) == 0) |
553 | { | |
554 | free (full_path); | |
555 | return ((char *)NULL); | |
556 | } | |
557 | ||
558 | /* The file exists. If the caller simply wants the first file, here it is. */ | |
559 | if (flags & FS_EXISTS) | |
560 | return (full_path); | |
561 | ||
95732b49 JA |
562 | /* If we have a readable file, and the caller wants a readable file, this |
563 | is it. */ | |
564 | if ((flags & FS_READABLE) && (status & FS_READABLE)) | |
565 | return (full_path); | |
566 | ||
cce855bc JA |
567 | /* If the file is executable, then it satisfies the cases of |
568 | EXEC_ONLY and EXEC_PREFERRED. Return this file unconditionally. */ | |
95732b49 | 569 | if ((status & FS_EXECABLE) && (flags & (FS_EXEC_ONLY|FS_EXEC_PREFERRED)) && |
cce855bc JA |
570 | (((flags & FS_NODIRS) == 0) || ((status & FS_DIRECTORY) == 0))) |
571 | { | |
572 | FREE (file_to_lose_on); | |
573 | file_to_lose_on = (char *)NULL; | |
574 | return (full_path); | |
575 | } | |
576 | ||
577 | /* The file is not executable, but it does exist. If we prefer | |
578 | an executable, then remember this one if it is the first one | |
579 | we have found. */ | |
a0c0a00f | 580 | if ((flags & FS_EXEC_PREFERRED) && file_to_lose_on == 0 && exec_name_should_ignore (full_path) == 0) |
cce855bc JA |
581 | file_to_lose_on = savestring (full_path); |
582 | ||
583 | /* If we want only executable files, or we don't want directories and | |
95732b49 JA |
584 | this file is a directory, or we want a readable file and this file |
585 | isn't readable, fail. */ | |
586 | if ((flags & (FS_EXEC_ONLY|FS_EXEC_PREFERRED)) || | |
587 | ((flags & FS_NODIRS) && (status & FS_DIRECTORY)) || | |
588 | ((flags & FS_READABLE) && (status & FS_READABLE) == 0)) | |
cce855bc JA |
589 | { |
590 | free (full_path); | |
591 | return ((char *)NULL); | |
592 | } | |
593 | else | |
594 | return (full_path); | |
595 | } | |
596 | ||
597 | /* This does the dirty work for find_user_command_internal () and | |
598 | user_command_matches (). | |
599 | NAME is the name of the file to search for. | |
600 | PATH_LIST is a colon separated list of directories to search. | |
601 | FLAGS contains bit fields which control the files which are eligible. | |
602 | Some values are: | |
603 | FS_EXEC_ONLY: The file must be an executable to be found. | |
604 | FS_EXEC_PREFERRED: If we can't find an executable, then the | |
605 | the first file matching NAME will do. | |
606 | FS_EXISTS: The first file found will do. | |
607 | FS_NODIRS: Don't find any directories. | |
608 | */ | |
609 | static char * | |
74091dd4 | 610 | find_user_command_in_path (name, path_list, flags, rflagsp) |
f73dda09 | 611 | const char *name; |
cce855bc | 612 | char *path_list; |
74091dd4 | 613 | int flags, *rflagsp; |
cce855bc JA |
614 | { |
615 | char *full_path, *path; | |
74091dd4 | 616 | int path_index, name_len, rflags; |
cce855bc JA |
617 | struct stat dotinfo; |
618 | ||
619 | /* We haven't started looking, so we certainly haven't seen | |
620 | a `.' as the directory path yet. */ | |
621 | dot_found_in_search = 0; | |
622 | ||
74091dd4 CR |
623 | if (rflagsp) |
624 | *rflagsp = 0; | |
625 | ||
cce855bc JA |
626 | if (absolute_program (name)) |
627 | { | |
628 | full_path = find_absolute_program (name, flags); | |
629 | return (full_path); | |
630 | } | |
631 | ||
632 | if (path_list == 0 || *path_list == '\0') | |
633 | return (savestring (name)); /* XXX */ | |
634 | ||
635 | file_to_lose_on = (char *)NULL; | |
636 | name_len = strlen (name); | |
ac50fbac CR |
637 | if (stat (".", &dotinfo) < 0) |
638 | dotinfo.st_dev = dotinfo.st_ino = 0; | |
cce855bc JA |
639 | path_index = 0; |
640 | ||
641 | while (path_list[path_index]) | |
642 | { | |
643 | /* Allow the user to interrupt out of a lengthy path search. */ | |
644 | QUIT; | |
645 | ||
646 | path = get_next_path_element (path_list, &path_index); | |
647 | if (path == 0) | |
648 | break; | |
649 | ||
650 | /* Side effects: sets dot_found_in_search, possibly sets | |
651 | file_to_lose_on. */ | |
74091dd4 | 652 | full_path = find_in_path_element (name, path, flags, name_len, &dotinfo, &rflags); |
cce855bc JA |
653 | free (path); |
654 | ||
74091dd4 CR |
655 | /* We use the file status flag bits to check whether full_path is a |
656 | directory, which we reject here. */ | |
657 | if (full_path && (rflags & FS_DIRECTORY)) | |
cce855bc JA |
658 | { |
659 | free (full_path); | |
660 | continue; | |
661 | } | |
662 | ||
663 | if (full_path) | |
664 | { | |
74091dd4 CR |
665 | if (rflagsp) |
666 | *rflagsp = rflags; | |
cce855bc JA |
667 | FREE (file_to_lose_on); |
668 | return (full_path); | |
669 | } | |
670 | } | |
671 | ||
672 | /* We didn't find exactly what the user was looking for. Return | |
673 | the contents of FILE_TO_LOSE_ON which is NULL when the search | |
674 | required an executable, or non-NULL if a file was found and the | |
28ef6c31 JA |
675 | search would accept a non-executable as a last resort. If the |
676 | caller specified FS_NODIRS, and file_to_lose_on is a directory, | |
677 | return NULL. */ | |
74091dd4 | 678 | if (file_to_lose_on && (flags & FS_NODIRS) && file_isdir (file_to_lose_on)) |
28ef6c31 JA |
679 | { |
680 | free (file_to_lose_on); | |
681 | file_to_lose_on = (char *)NULL; | |
682 | } | |
683 | ||
cce855bc JA |
684 | return (file_to_lose_on); |
685 | } | |
a0c0a00f CR |
686 | |
687 | /* External interface to find a command given a $PATH. Separate from | |
688 | find_user_command_in_path to allow future customization. */ | |
689 | char * | |
690 | find_in_path (name, path_list, flags) | |
691 | const char *name; | |
692 | char *path_list; | |
693 | int flags; | |
694 | { | |
74091dd4 | 695 | return (find_user_command_in_path (name, path_list, flags, (int *)0)); |
a0c0a00f | 696 | } |