5f94863d9e0ee786c0f5ce05ef74b90428b5c784
[platform/upstream/bash.git] / findcmd.c
1 /* findcmd.c -- Functions to search for commands by name. */
2
3 /* Copyright (C) 1997 Free Software Foundation, Inc.
4
5    This file is part of GNU Bash, the Bourne Again SHell.
6
7    Bash is free software; you can redistribute it and/or modify it
8    under the terms of the GNU General Public License as published by
9    the Free Software Foundation; either version 2, or (at your option)
10    any later version.
11
12    Bash is distributed in the hope that it will be useful, but WITHOUT
13    ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
14    or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public
15    License for more details.
16
17    You should have received a copy of the GNU General Public License
18    along with Bash; see the file COPYING.  If not, write to the
19    Free Software Foundation Inc.,
20    59 Temple Place, Suite 330, Boston, MA 02111-1307, USA. */
21
22 #include "config.h"
23
24 #include <stdio.h>
25 #include "chartypes.h"
26 #include "bashtypes.h"
27 #ifndef _MINIX
28 #  include <sys/file.h>
29 #endif
30 #include "filecntl.h"
31 #include "posixstat.h"
32
33 #if defined (HAVE_UNISTD_H)
34 #  include <unistd.h>
35 #endif
36
37 #include "bashansi.h"
38
39 #include "memalloc.h"
40 #include "shell.h"
41 #include "flags.h"
42 #include "hashlib.h"
43 #include "pathexp.h"
44 #include "hashcmd.h"
45 #include "findcmd.h"    /* matching prototypes and declarations */
46
47 extern int posixly_correct;
48
49 /* Static functions defined and used in this file. */
50 static char *_find_user_command_internal __P((const char *, int));
51 static char *find_user_command_internal __P((const char *, int));
52 static char *find_user_command_in_path __P((const char *, char *, int));
53 static char *find_in_path_element __P((const char *, char *, int, int, struct stat *));
54 static char *find_absolute_program __P((const char *, int));
55
56 static char *get_next_path_element __P((char *, int *));
57
58 /* The file name which we would try to execute, except that it isn't
59    possible to execute it.  This is the first file that matches the
60    name that we are looking for while we are searching $PATH for a
61    suitable one to execute.  If we cannot find a suitable executable
62    file, then we use this one. */
63 static char *file_to_lose_on;
64
65 /* Non-zero if we should stat every command found in the hash table to
66    make sure it still exists. */
67 int check_hashed_filenames;
68
69 /* DOT_FOUND_IN_SEARCH becomes non-zero when find_user_command ()
70    encounters a `.' as the directory pathname while scanning the
71    list of possible pathnames; i.e., if `.' comes before the directory
72    containing the file of interest. */
73 int dot_found_in_search = 0;
74
75 #define u_mode_bits(x) (((x) & 0000700) >> 6)
76 #define g_mode_bits(x) (((x) & 0000070) >> 3)
77 #define o_mode_bits(x) (((x) & 0000007) >> 0)
78 #define X_BIT(x) ((x) & 1)
79
80 /* Return some flags based on information about this file.
81    The EXISTS bit is non-zero if the file is found.
82    The EXECABLE bit is non-zero the file is executble.
83    Zero is returned if the file is not found. */
84 int
85 file_status (name)
86      const char *name;
87 {
88   struct stat finfo;
89
90   /* Determine whether this file exists or not. */
91   if (stat (name, &finfo) < 0)
92     return (0);
93
94   /* If the file is a directory, then it is not "executable" in the
95      sense of the shell. */
96   if (S_ISDIR (finfo.st_mode))
97     return (FS_EXISTS|FS_DIRECTORY);
98
99 #if defined (AFS)
100   /* We have to use access(2) to determine access because AFS does not
101      support Unix file system semantics.  This may produce wrong
102      answers for non-AFS files when ruid != euid.  I hate AFS. */
103   if (access (name, X_OK) == 0)
104     return (FS_EXISTS | FS_EXECABLE);
105   else
106     return (FS_EXISTS);
107 #else /* !AFS */
108
109   /* Find out if the file is actually executable.  By definition, the
110      only other criteria is that the file has an execute bit set that
111      we can use. */
112
113   /* Root only requires execute permission for any of owner, group or
114      others to be able to exec a file. */
115   if (current_user.euid == (uid_t)0)
116     {
117       int bits;
118
119       bits = (u_mode_bits (finfo.st_mode) |
120               g_mode_bits (finfo.st_mode) |
121               o_mode_bits (finfo.st_mode));
122
123       if (X_BIT (bits))
124         return (FS_EXISTS | FS_EXECABLE);
125     }
126
127   /* If we are the owner of the file, the owner execute bit applies. */
128   if (current_user.euid == finfo.st_uid && X_BIT (u_mode_bits (finfo.st_mode)))
129     return (FS_EXISTS | FS_EXECABLE);
130
131   /* If we are in the owning group, the group permissions apply. */
132   if (group_member (finfo.st_gid) && X_BIT (g_mode_bits (finfo.st_mode)))
133     return (FS_EXISTS | FS_EXECABLE);
134
135   /* If `others' have execute permission to the file, then so do we,
136      since we are also `others'. */
137   if (X_BIT (o_mode_bits (finfo.st_mode)))
138     return (FS_EXISTS | FS_EXECABLE);
139
140   return (FS_EXISTS);
141 #endif /* !AFS */
142 }
143
144 /* Return non-zero if FILE exists and is executable.
145    Note that this function is the definition of what an
146    executable file is; do not change this unless YOU know
147    what an executable file is. */
148 int
149 executable_file (file)
150      const char *file;
151 {
152   int s;
153
154   s = file_status (file);
155   return ((s & FS_EXECABLE) && ((s & FS_DIRECTORY) == 0));
156 }
157
158 int
159 is_directory (file)
160      const char *file;
161 {
162   return (file_status (file) & FS_DIRECTORY);
163 }
164
165 int
166 executable_or_directory (file)
167      const char *file;
168 {
169   int s;
170
171   s = file_status (file);
172   return ((s & FS_EXECABLE) || (s & FS_DIRECTORY));
173 }
174
175 /* Locate the executable file referenced by NAME, searching along
176    the contents of the shell PATH variable.  Return a new string
177    which is the full pathname to the file, or NULL if the file
178    couldn't be found.  If a file is found that isn't executable,
179    and that is the only match, then return that. */
180 char *
181 find_user_command (name)
182      const char *name;
183 {
184   return (find_user_command_internal (name, FS_EXEC_PREFERRED|FS_NODIRS));
185 }
186
187 /* Locate the file referenced by NAME, searching along the contents
188    of the shell PATH variable.  Return a new string which is the full
189    pathname to the file, or NULL if the file couldn't be found.  This
190    returns the first file found. */
191 char *
192 find_path_file (name)
193      const char *name;
194 {
195   return (find_user_command_internal (name, FS_EXISTS));
196 }
197
198 static char *
199 _find_user_command_internal (name, flags)
200      const char *name;
201      int flags;
202 {
203   char *path_list, *cmd;
204   SHELL_VAR *var;
205
206   /* Search for the value of PATH in both the temporary environment, and
207      in the regular list of variables. */
208   if (var = find_variable_internal ("PATH", 1)) /* XXX could be array? */
209     path_list = value_cell (var);
210   else
211     path_list = (char *)NULL;
212
213   if (path_list == 0 || *path_list == '\0')
214     return (savestring (name));
215
216   cmd = find_user_command_in_path (name, path_list, flags);
217
218   if (var && tempvar_p (var))
219     dispose_variable (var);
220
221   return (cmd);
222 }
223
224 static char *
225 find_user_command_internal (name, flags)
226      const char *name;
227      int flags;
228 {
229 #ifdef __WIN32__
230   char *res, *dotexe;
231
232   dotexe = (char *)xmalloc (strlen (name) + 5);
233   strcpy (dotexe, name);
234   strcat (dotexe, ".exe");
235   res = _find_user_command_internal (dotexe, flags);
236   free (dotexe);
237   if (res == 0)
238     res = _find_user_command_internal (name, flags);
239   return res;
240 #else
241   return (_find_user_command_internal (name, flags));
242 #endif
243 }
244
245 /* Return the next element from PATH_LIST, a colon separated list of
246    paths.  PATH_INDEX_POINTER is the address of an index into PATH_LIST;
247    the index is modified by this function.
248    Return the next element of PATH_LIST or NULL if there are no more. */
249 static char *
250 get_next_path_element (path_list, path_index_pointer)
251      char *path_list;
252      int *path_index_pointer;
253 {
254   char *path;
255
256   path = extract_colon_unit (path_list, path_index_pointer);
257
258   if (path == 0)
259     return (path);
260
261   if (*path == '\0')
262     {
263       free (path);
264       path = savestring (".");
265     }
266
267   return (path);
268 }
269
270 /* Look for PATHNAME in $PATH.  Returns either the hashed command
271    corresponding to PATHNAME or the first instance of PATHNAME found
272    in $PATH.  Returns a newly-allocated string. */
273 char *
274 search_for_command (pathname)
275      const char *pathname;
276 {
277   char *hashed_file, *command;
278   int temp_path, st;
279   SHELL_VAR *path;
280
281   hashed_file = command = (char *)NULL;
282
283   /* If PATH is in the temporary environment for this command, don't use the
284      hash table to search for the full pathname. */
285   path = find_tempenv_variable ("PATH");
286   temp_path = path != 0;
287
288   /* Don't waste time trying to find hashed data for a pathname
289      that is already completely specified or if we're using a command-
290      specific value for PATH. */
291   if (path == 0 && absolute_program (pathname) == 0)
292     hashed_file = find_hashed_filename (pathname);
293
294   /* If a command found in the hash table no longer exists, we need to
295      look for it in $PATH.  Thank you Posix.2.  This forces us to stat
296      every command found in the hash table. */
297
298   if (hashed_file && (posixly_correct || check_hashed_filenames))
299     {
300       st = file_status (hashed_file);
301       if ((st ^ (FS_EXISTS | FS_EXECABLE)) != 0)
302         {
303           remove_hashed_filename (pathname);
304           free (hashed_file);
305           hashed_file = (char *)NULL;
306         }
307     }
308
309   if (hashed_file)
310     command = hashed_file;
311   else if (absolute_program (pathname))
312     /* A command containing a slash is not looked up in PATH or saved in
313        the hash table. */
314     command = savestring (pathname);
315   else
316     {
317       /* If $PATH is in the temporary environment, we've already retrieved
318          it, so don't bother trying again. */
319       if (temp_path)
320         {
321           command = find_user_command_in_path (pathname, value_cell (path),
322                                                FS_EXEC_PREFERRED|FS_NODIRS);
323           if (tempvar_p (path))
324             dispose_variable (path);
325         }
326       else
327         command = find_user_command (pathname);
328       if (command && hashing_enabled && temp_path == 0)
329         remember_filename ((char *)pathname, command, dot_found_in_search, 1);  /* XXX fix const later */
330     }
331   return (command);
332 }
333
334 char *
335 user_command_matches (name, flags, state)
336      const char *name;
337      int flags, state;
338 {
339   register int i;
340   int  path_index, name_len;
341   char *path_list, *path_element, *match;
342   struct stat dotinfo;
343   static char **match_list = NULL;
344   static int match_list_size = 0;
345   static int match_index = 0;
346
347   if (state == 0)
348     {
349       /* Create the list of matches. */
350       if (match_list == 0)
351         {
352           match_list_size = 5;
353           match_list = alloc_array (match_list_size);
354         }
355
356       /* Clear out the old match list. */
357       for (i = 0; i < match_list_size; i++)
358         match_list[i] = 0;
359
360       /* We haven't found any files yet. */
361       match_index = 0;
362
363       if (absolute_program (name))
364         {
365           match_list[0] = find_absolute_program (name, flags);
366           match_list[1] = (char *)NULL;
367           path_list = (char *)NULL;
368         }
369       else
370         {
371           name_len = strlen (name);
372           file_to_lose_on = (char *)NULL;
373           dot_found_in_search = 0;
374           stat (".", &dotinfo);
375           path_list = get_string_value ("PATH");
376           path_index = 0;
377         }
378
379       while (path_list && path_list[path_index])
380         {
381           path_element = get_next_path_element (path_list, &path_index);
382
383           if (path_element == 0)
384             break;
385
386           match = find_in_path_element (name, path_element, flags, name_len, &dotinfo);
387
388           free (path_element);
389
390           if (match == 0)
391             continue;
392
393           if (match_index + 1 == match_list_size)
394             {
395               match_list_size += 10;
396               match_list = (char **)xrealloc (match_list, (match_list_size + 1) * sizeof (char *));
397             }
398
399           match_list[match_index++] = match;
400           match_list[match_index] = (char *)NULL;
401           FREE (file_to_lose_on);
402           file_to_lose_on = (char *)NULL;
403         }
404
405       /* We haven't returned any strings yet. */
406       match_index = 0;
407     }
408
409   match = match_list[match_index];
410
411   if (match)
412     match_index++;
413
414   return (match);
415 }
416
417 static char *
418 find_absolute_program (name, flags)
419      const char *name;
420      int flags;
421 {
422   int st;
423
424   st = file_status (name);
425
426   /* If the file doesn't exist, quit now. */
427   if ((st & FS_EXISTS) == 0)
428     return ((char *)NULL);
429
430   /* If we only care about whether the file exists or not, return
431      this filename.  Otherwise, maybe we care about whether this
432      file is executable.  If it is, and that is what we want, return it. */
433   if ((flags & FS_EXISTS) || ((flags & FS_EXEC_ONLY) && (st & FS_EXECABLE)))
434     return (savestring (name));
435
436   return (NULL);
437 }
438
439 static char *
440 find_in_path_element (name, path, flags, name_len, dotinfop)
441      const char *name;
442      char *path;
443      int flags, name_len;
444      struct stat *dotinfop;
445 {
446   int status;
447   char *full_path, *xpath;
448
449   xpath = (*path == '~') ? bash_tilde_expand (path) : path;
450
451   /* Remember the location of "." in the path, in all its forms
452      (as long as they begin with a `.', e.g. `./.') */
453   if (dot_found_in_search == 0 && *xpath == '.')
454     dot_found_in_search = same_file (".", xpath, dotinfop, (struct stat *)NULL);
455
456   full_path = sh_makepath (xpath, name, 0);
457
458   status = file_status (full_path);
459
460   if (xpath != path)
461     free (xpath);
462
463   if ((status & FS_EXISTS) == 0)
464     {
465       free (full_path);
466       return ((char *)NULL);
467     }
468
469   /* The file exists.  If the caller simply wants the first file, here it is. */
470   if (flags & FS_EXISTS)
471     return (full_path);
472
473   /* If the file is executable, then it satisfies the cases of
474       EXEC_ONLY and EXEC_PREFERRED.  Return this file unconditionally. */
475   if ((status & FS_EXECABLE) &&
476       (((flags & FS_NODIRS) == 0) || ((status & FS_DIRECTORY) == 0)))
477     {
478       FREE (file_to_lose_on);
479       file_to_lose_on = (char *)NULL;
480       return (full_path);
481     }
482
483   /* The file is not executable, but it does exist.  If we prefer
484      an executable, then remember this one if it is the first one
485      we have found. */
486   if ((flags & FS_EXEC_PREFERRED) && file_to_lose_on == 0)
487     file_to_lose_on = savestring (full_path);
488
489   /* If we want only executable files, or we don't want directories and
490      this file is a directory, fail. */
491   if ((flags & FS_EXEC_ONLY) || (flags & FS_EXEC_PREFERRED) ||
492       ((flags & FS_NODIRS) && (status & FS_DIRECTORY)))
493     {
494       free (full_path);
495       return ((char *)NULL);
496     }
497   else
498     return (full_path);
499 }
500
501 /* This does the dirty work for find_user_command_internal () and
502    user_command_matches ().
503    NAME is the name of the file to search for.
504    PATH_LIST is a colon separated list of directories to search.
505    FLAGS contains bit fields which control the files which are eligible.
506    Some values are:
507       FS_EXEC_ONLY:             The file must be an executable to be found.
508       FS_EXEC_PREFERRED:        If we can't find an executable, then the
509                                 the first file matching NAME will do.
510       FS_EXISTS:                The first file found will do.
511       FS_NODIRS:                Don't find any directories.
512 */
513 static char *
514 find_user_command_in_path (name, path_list, flags)
515      const char *name;
516      char *path_list;
517      int flags;
518 {
519   char *full_path, *path;
520   int path_index, name_len;
521   struct stat dotinfo;
522
523   /* We haven't started looking, so we certainly haven't seen
524      a `.' as the directory path yet. */
525   dot_found_in_search = 0;
526
527   if (absolute_program (name))
528     {
529       full_path = find_absolute_program (name, flags);
530       return (full_path);
531     }
532
533   if (path_list == 0 || *path_list == '\0')
534     return (savestring (name));         /* XXX */
535
536   file_to_lose_on = (char *)NULL;
537   name_len = strlen (name);
538   stat (".", &dotinfo);
539   path_index = 0;
540
541   while (path_list[path_index])
542     {
543       /* Allow the user to interrupt out of a lengthy path search. */
544       QUIT;
545
546       path = get_next_path_element (path_list, &path_index);
547       if (path == 0)
548         break;
549
550       /* Side effects: sets dot_found_in_search, possibly sets
551          file_to_lose_on. */
552       full_path = find_in_path_element (name, path, flags, name_len, &dotinfo);
553       free (path);
554
555       /* This should really be in find_in_path_element, but there isn't the
556          right combination of flags. */
557       if (full_path && is_directory (full_path))
558         {
559           free (full_path);
560           continue;
561         }
562
563       if (full_path)
564         {
565           FREE (file_to_lose_on);
566           return (full_path);
567         }
568     }
569
570   /* We didn't find exactly what the user was looking for.  Return
571      the contents of FILE_TO_LOSE_ON which is NULL when the search
572      required an executable, or non-NULL if a file was found and the
573      search would accept a non-executable as a last resort.  If the
574      caller specified FS_NODIRS, and file_to_lose_on is a directory,
575      return NULL. */
576   if (file_to_lose_on && (flags & FS_NODIRS) && is_directory (file_to_lose_on))
577     {
578       free (file_to_lose_on);
579       file_to_lose_on = (char *)NULL;
580     }
581
582   return (file_to_lose_on);
583 }