594d688cc14e5b5856594a996bf1c59aa2f66e7e
[platform/upstream/bash.git] / hashcmd.c
1 /* hashcmd.c - functions for managing a hash table mapping command names to
2                full pathnames. */
3
4 /* Copyright (C) 1997 Free Software Foundation, Inc.
5
6    This file is part of GNU Bash, the Bourne Again SHell.
7
8     Bash is free software; you can redistribute it and/or modify it under
9     the terms of the GNU General Public License as published by the Free
10     Software Foundation; either version 2, or (at your option) any later
11     version.
12
13     Bash is distributed in the hope that it will be useful, but WITHOUT ANY
14     WARRANTY; without even the implied warranty of MERCHANTABILITY or
15     FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
16     for more details.
17
18     You should have received a copy of the GNU General Public License along
19     with Bash; see the file COPYING.  If not, write to the Free Software
20     Foundation, 59 Temple Place, Suite 330, Boston, MA 02111 USA. */
21
22 #include <config.h>
23
24 #include "bashtypes.h"
25 #include "posixstat.h"
26
27 #if defined (HAVE_UNISTD_H)
28 #  include <unistd.h>
29 #endif
30
31 #include "bashansi.h"
32
33 #include "shell.h"
34 #include "findcmd.h"
35 #include "hashcmd.h"
36
37 extern int hashing_enabled;
38
39 static int hashing_initialized = 0;
40
41 HASH_TABLE *hashed_filenames;
42
43 void
44 initialize_filename_hashing ()
45 {
46   if (hashing_initialized == 0)
47     {
48       hashed_filenames = make_hash_table (FILENAME_HASH_BUCKETS);
49       hashing_initialized = 1;
50     }
51 }
52
53 static void
54 free_filename_data (data)
55      PTR_T data;
56 {
57   free (((PATH_DATA *)data)->path);
58   free (data);
59 }
60
61 void
62 flush_hashed_filenames ()
63 {
64   if (hashed_filenames)
65     flush_hash_table (hashed_filenames, free_filename_data);
66 }
67
68 /* Remove FILENAME from the table of hashed commands. */
69 void
70 remove_hashed_filename (filename)
71      const char *filename;
72 {
73   register BUCKET_CONTENTS *item;
74
75   if (hashing_enabled == 0 || hashed_filenames == 0)
76     return;
77
78   item = remove_hash_item (filename, hashed_filenames);
79   if (item)
80     {
81       if (item->data)
82         free_filename_data (item->data);
83       free (item->key);
84       free (item);
85     }
86 }
87
88 /* Place FILENAME (key) and FULL_PATH (data->path) into the
89    hash table.  CHECK_DOT if non-null is for future calls to
90    find_hashed_filename (); it means that this file was found
91    in a directory in $PATH that is not an absolute pathname.
92    FOUND is the initial value for times_found. */
93 void
94 remember_filename (filename, full_path, check_dot, found)
95      char *filename, *full_path;
96      int check_dot, found;
97 {
98   register BUCKET_CONTENTS *item;
99
100   if (hashing_enabled == 0)
101     return;
102
103   if (hashed_filenames == 0 || hashing_initialized == 0)
104     initialize_filename_hashing ();
105
106   item = add_hash_item (filename, hashed_filenames);
107   if (item->data)
108     free (pathdata(item)->path);
109   else
110     {
111       item->key = savestring (filename);
112       item->data = (char *)xmalloc (sizeof (PATH_DATA));
113     }
114   pathdata(item)->path = savestring (full_path);
115   pathdata(item)->flags = 0;
116   if (check_dot)
117     pathdata(item)->flags |= HASH_CHKDOT;
118   if (*full_path != '/')
119     pathdata(item)->flags |= HASH_RELPATH;
120   item->times_found = found;
121 }
122
123 /* Return the full pathname that FILENAME hashes to.  If FILENAME
124    is hashed, but (data->flags & HASH_CHKDOT) is non-zero, check
125    ./FILENAME and return that if it is executable.  This always
126    returns a newly-allocated string; the caller is responsible
127    for freeing it. */
128 char *
129 find_hashed_filename (filename)
130      const char *filename;
131 {
132   register BUCKET_CONTENTS *item;
133   char *path, *dotted_filename, *tail;
134   int same;
135
136   if (hashing_enabled == 0 || hashed_filenames == 0)
137     return ((char *)NULL);
138
139   item = find_hash_item (filename, hashed_filenames);
140
141   if (item == NULL)
142     return ((char *)NULL);
143
144   /* If this filename is hashed, but `.' comes before it in the path,
145      see if ./filename is executable.  If the hashed value is not an
146      absolute pathname, see if ./`hashed-value' exists. */
147   path = pathdata(item)->path;
148   if (pathdata(item)->flags & (HASH_CHKDOT|HASH_RELPATH))
149     {
150       tail = (pathdata(item)->flags & HASH_RELPATH) ? path : (char *)filename;  /* XXX - fix const later */
151       /* If the pathname does not start with a `./', add a `./' to it. */
152       if (tail[0] != '.' || tail[1] != '/')
153         {
154           dotted_filename = (char *)xmalloc (3 + strlen (tail));
155           dotted_filename[0] = '.'; dotted_filename[1] = '/';
156           strcpy (dotted_filename + 2, tail);
157         }
158       else
159         dotted_filename = savestring (tail);
160
161       if (executable_file (dotted_filename))
162         return (dotted_filename);
163
164       free (dotted_filename);
165
166 #if 0
167       if (pathdata(item)->flags & HASH_RELPATH)
168         return ((char *)NULL);
169 #endif
170
171       /* Watch out.  If this file was hashed to "./filename", and
172          "./filename" is not executable, then return NULL. */
173
174       /* Since we already know "./filename" is not executable, what
175          we're really interested in is whether or not the `path'
176          portion of the hashed filename is equivalent to the current
177          directory, but only if it starts with a `.'.  (This catches
178          ./. and so on.)  same_file () tests general Unix file
179          equivalence -- same device and inode. */
180       if (*path == '.')
181         {
182           same = 0;
183           tail = (char *)strrchr (path, '/');
184
185           if (tail)
186             {
187               *tail = '\0';
188               same = same_file (".", path, (struct stat *)NULL, (struct stat *)NULL);
189               *tail = '/';
190             }
191
192           return same ? (char *)NULL : savestring (path);
193         }
194     }
195
196   return (savestring (path));
197 }