Patch-ID: bash32-029
[bash.git] / hashcmd.c
blob895b485065ff7f6f2eef4f76b875be991fcb5c90
1 /* hashcmd.c - functions for managing a hash table mapping command names to
2 full pathnames. */
4 /* Copyright (C) 1997-2002 Free Software Foundation, Inc.
6 This file is part of GNU Bash, the Bourne Again SHell.
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.
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.
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. */
22 #include <config.h>
24 #include "bashtypes.h"
25 #include "posixstat.h"
27 #if defined (HAVE_UNISTD_H)
28 # include <unistd.h>
29 #endif
31 #include "bashansi.h"
33 #include "shell.h"
34 #include "findcmd.h"
35 #include "hashcmd.h"
37 extern int hashing_enabled;
39 HASH_TABLE *hashed_filenames = (HASH_TABLE *)NULL;
41 static void phash_freedata __P((PTR_T));
43 void
44 phash_create ()
46 if (hashed_filenames == 0)
47 hashed_filenames = hash_create (FILENAME_HASH_BUCKETS);
50 static void
51 phash_freedata (data)
52 PTR_T data;
54 free (((PATH_DATA *)data)->path);
55 free (data);
58 void
59 phash_flush ()
61 if (hashed_filenames)
62 hash_flush (hashed_filenames, phash_freedata);
65 /* Remove FILENAME from the table of hashed commands. */
66 int
67 phash_remove (filename)
68 const char *filename;
70 register BUCKET_CONTENTS *item;
72 if (hashing_enabled == 0 || hashed_filenames == 0)
73 return 0;
75 item = hash_remove (filename, hashed_filenames, 0);
76 if (item)
78 if (item->data)
79 phash_freedata (item->data);
80 free (item->key);
81 free (item);
82 return 0;
84 return 1;
87 /* Place FILENAME (key) and FULL_PATH (data->path) into the
88 hash table. CHECK_DOT if non-null is for future calls to
89 phash_search (); it means that this file was found
90 in a directory in $PATH that is not an absolute pathname.
91 FOUND is the initial value for times_found. */
92 void
93 phash_insert (filename, full_path, check_dot, found)
94 char *filename, *full_path;
95 int check_dot, found;
97 register BUCKET_CONTENTS *item;
99 if (hashing_enabled == 0)
100 return;
102 if (hashed_filenames == 0)
103 phash_create ();
105 item = hash_insert (filename, hashed_filenames, 0);
106 if (item->data)
107 free (pathdata(item)->path);
108 else
110 item->key = savestring (filename);
111 item->data = xmalloc (sizeof (PATH_DATA));
113 pathdata(item)->path = savestring (full_path);
114 pathdata(item)->flags = 0;
115 if (check_dot)
116 pathdata(item)->flags |= HASH_CHKDOT;
117 if (*full_path != '/')
118 pathdata(item)->flags |= HASH_RELPATH;
119 item->times_found = found;
122 /* Return the full pathname that FILENAME hashes to. If FILENAME
123 is hashed, but (data->flags & HASH_CHKDOT) is non-zero, check
124 ./FILENAME and return that if it is executable. This always
125 returns a newly-allocated string; the caller is responsible
126 for freeing it. */
127 char *
128 phash_search (filename)
129 const char *filename;
131 register BUCKET_CONTENTS *item;
132 char *path, *dotted_filename, *tail;
133 int same;
135 if (hashing_enabled == 0 || hashed_filenames == 0)
136 return ((char *)NULL);
138 item = hash_search (filename, hashed_filenames, 0);
140 if (item == NULL)
141 return ((char *)NULL);
143 /* If this filename is hashed, but `.' comes before it in the path,
144 see if ./filename is executable. If the hashed value is not an
145 absolute pathname, see if ./`hashed-value' exists. */
146 path = pathdata(item)->path;
147 if (pathdata(item)->flags & (HASH_CHKDOT|HASH_RELPATH))
149 tail = (pathdata(item)->flags & HASH_RELPATH) ? path : (char *)filename; /* XXX - fix const later */
150 /* If the pathname does not start with a `./', add a `./' to it. */
151 if (tail[0] != '.' || tail[1] != '/')
153 dotted_filename = (char *)xmalloc (3 + strlen (tail));
154 dotted_filename[0] = '.'; dotted_filename[1] = '/';
155 strcpy (dotted_filename + 2, tail);
157 else
158 dotted_filename = savestring (tail);
160 if (executable_file (dotted_filename))
161 return (dotted_filename);
163 free (dotted_filename);
165 #if 0
166 if (pathdata(item)->flags & HASH_RELPATH)
167 return ((char *)NULL);
168 #endif
170 /* Watch out. If this file was hashed to "./filename", and
171 "./filename" is not executable, then return NULL. */
173 /* Since we already know "./filename" is not executable, what
174 we're really interested in is whether or not the `path'
175 portion of the hashed filename is equivalent to the current
176 directory, but only if it starts with a `.'. (This catches
177 ./. and so on.) same_file () tests general Unix file
178 equivalence -- same device and inode. */
179 if (*path == '.')
181 same = 0;
182 tail = (char *)strrchr (path, '/');
184 if (tail)
186 *tail = '\0';
187 same = same_file (".", path, (struct stat *)NULL, (struct stat *)NULL);
188 *tail = '/';
191 return same ? (char *)NULL : savestring (path);
195 return (savestring (path));