]> git.ipfire.org Git - thirdparty/bash.git/blob - hashcmd.c
change to test builtin for parenthesized expressions when there are more than four...
[thirdparty/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-2022 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
9 it under the terms of the GNU General Public License as published by
10 the Free Software Foundation, either version 3 of the License, or
11 (at your option) any later version.
12
13 Bash is distributed in the hope that it will be useful,
14 but WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 GNU General Public License for more details.
17
18 You should have received a copy of the GNU General Public License
19 along with Bash. If not, see <http://www.gnu.org/licenses/>.
20 */
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 "flags.h"
35 #include "findcmd.h"
36 #include "hashcmd.h"
37
38 HASH_TABLE *hashed_filenames = (HASH_TABLE *)NULL;
39
40 static void phash_freedata (PTR_T);
41
42 void
43 phash_create (void)
44 {
45 if (hashed_filenames == 0)
46 hashed_filenames = hash_create (FILENAME_HASH_BUCKETS);
47 }
48
49 static void
50 phash_freedata (PTR_T data)
51 {
52 free (((PATH_DATA *)data)->path);
53 free (data);
54 }
55
56 void
57 phash_flush (void)
58 {
59 if (hashed_filenames)
60 hash_flush (hashed_filenames, phash_freedata);
61 }
62
63 /* Remove FILENAME from the table of hashed commands. */
64 int
65 phash_remove (const char *filename)
66 {
67 register BUCKET_CONTENTS *item;
68
69 if (hashing_enabled == 0)
70 return 0;
71
72 if (hashed_filenames == 0)
73 return 1;
74
75 item = hash_remove (filename, hashed_filenames, 0);
76 if (item)
77 {
78 if (item->data)
79 phash_freedata (item->data);
80 free (item->key);
81 free (item);
82 return 0;
83 }
84 return 1;
85 }
86
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 (char *filename, char *full_path, int check_dot, int found)
94 {
95 register BUCKET_CONTENTS *item;
96
97 if (hashing_enabled == 0)
98 return;
99
100 if (hashed_filenames == 0)
101 phash_create ();
102
103 item = hash_insert (filename, hashed_filenames, 0);
104 if (item->data)
105 free (pathdata(item)->path);
106 else
107 {
108 item->key = savestring (filename);
109 item->data = xmalloc (sizeof (PATH_DATA));
110 }
111 pathdata(item)->path = savestring (full_path);
112 pathdata(item)->flags = 0;
113 if (check_dot)
114 pathdata(item)->flags |= HASH_CHKDOT;
115 if (*full_path != '/')
116 pathdata(item)->flags |= HASH_RELPATH;
117 item->times_found = found;
118 }
119
120 /* Return the full pathname that FILENAME hashes to. If FILENAME
121 is hashed, but (data->flags & HASH_CHKDOT) is non-zero, check
122 ./FILENAME and return that if it is executable. This always
123 returns a newly-allocated string; the caller is responsible
124 for freeing it. */
125 char *
126 phash_search (const char *filename)
127 {
128 register BUCKET_CONTENTS *item;
129 char *path, *dotted_filename, *tail;
130 int same;
131
132 if (hashing_enabled == 0 || hashed_filenames == 0)
133 return ((char *)NULL);
134
135 item = hash_search (filename, hashed_filenames, 0);
136
137 if (item == NULL)
138 return ((char *)NULL);
139
140 /* If this filename is hashed, but `.' comes before it in the path,
141 see if ./filename is executable. If the hashed value is not an
142 absolute pathname, see if ./`hashed-value' exists. */
143 path = pathdata(item)->path;
144 if (pathdata(item)->flags & (HASH_CHKDOT|HASH_RELPATH))
145 {
146 tail = (pathdata(item)->flags & HASH_RELPATH) ? path : (char *)filename; /* XXX - fix const later */
147 /* If the pathname does not start with a `./', add a `./' to it. */
148 if (tail[0] != '.' || tail[1] != '/')
149 {
150 dotted_filename = (char *)xmalloc (3 + strlen (tail));
151 dotted_filename[0] = '.'; dotted_filename[1] = '/';
152 strcpy (dotted_filename + 2, tail);
153 }
154 else
155 dotted_filename = savestring (tail);
156
157 if (executable_file (dotted_filename))
158 return (dotted_filename);
159
160 free (dotted_filename);
161
162 #if 0
163 if (pathdata(item)->flags & HASH_RELPATH)
164 return ((char *)NULL);
165 #endif
166
167 /* Watch out. If this file was hashed to "./filename", and
168 "./filename" is not executable, then return NULL. */
169
170 /* Since we already know "./filename" is not executable, what
171 we're really interested in is whether or not the `path'
172 portion of the hashed filename is equivalent to the current
173 directory, but only if it starts with a `.'. (This catches
174 ./. and so on.) same_file () tests general Unix file
175 equivalence -- same device and inode. */
176 if (*path == '.')
177 {
178 same = 0;
179 tail = (char *)strrchr (path, '/');
180
181 if (tail)
182 {
183 *tail = '\0';
184 same = same_file (".", path, (struct stat *)NULL, (struct stat *)NULL);
185 *tail = '/';
186 }
187
188 return same ? (char *)NULL : savestring (path);
189 }
190 }
191
192 return (savestring (path));
193 }