1 This file is hash.def, from which is created hash.c.
2 It implements the builtin "hash" in Bash.
4 Copyright (C) 1987-2024 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
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.
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.
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/>.
24 $FUNCTION hash_builtin
25 $SHORT_DOC hash [-lr] [-p pathname] [-dt] [name ...]
26 Remember or display program locations.
28 Determine and remember the full pathname of each command NAME. If
29 no arguments are given, information about remembered commands is displayed.
32 -d forget the remembered location of each NAME
33 -l display in a format that may be reused as input
34 -p pathname use PATHNAME as the full pathname of NAME
35 -r forget all remembered locations
36 -t print the remembered location of each NAME, preceding
37 each location with the corresponding NAME if multiple
40 NAME Each NAME is searched for in $PATH and added to the list
41 of remembered commands.
44 Returns success unless NAME is not found or an invalid option is given.
51 #include "../bashtypes.h"
53 #if defined (HAVE_UNISTD_H)
59 #include "../bashansi.h"
60 #include "../bashintl.h"
63 #include "../builtins.h"
64 #include "../execute_cmd.h"
66 #include "../findcmd.h"
67 #include "../hashcmd.h"
69 #include "bashgetopt.h"
71 static int add_hashed_command (char *, int);
72 static int print_hash_info (BUCKET_CONTENTS *);
73 static int print_portable_hash_info (BUCKET_CONTENTS *);
74 static int print_hashed_commands (int);
75 static int list_hashed_filename_targets (WORD_LIST *, int);
77 /* Print statistics on the current state of hashed commands. If LIST is
78 not empty, then rehash (or hash in the first place) the specified
81 hash_builtin (WORD_LIST *list)
83 int expunge_hash_table, list_targets, list_portably, delete, opt;
86 if (hashing_enabled == 0)
88 builtin_error (_("hashing disabled"));
89 return (EXECUTION_FAILURE);
92 expunge_hash_table = list_targets = list_portably = delete = 0;
93 pathname = (char *)NULL;
94 reset_internal_getopt ();
95 while ((opt = internal_getopt (list, "dlp:rt")) != -1)
106 pathname = list_optarg;
109 expunge_hash_table = 1;
122 /* hash -t requires at least one argument. */
123 if (list == 0 && (delete || list_targets))
125 sh_needarg (delete ? "-d" : "-t");
126 return (sh_chkwrite (EXECUTION_FAILURE));
129 /* It's an error to specify a pathname to hash to, but no name to hash. */
130 if (pathname && list == 0)
136 /* We want hash -r to be silent, but hash -- to print hashing info, so
137 we test expunge_hash_table. */
139 if (list == 0 && expunge_hash_table == 0)
141 opt = print_hashed_commands (list_portably);
142 if (opt == 0 && posixly_correct == 0 &&
143 (list_portably == 0 || shell_compatibility_level <= 50))
144 printf (_("%s: hash table empty\n"), this_command_name);
146 return (sh_chkwrite (EXECUTION_SUCCESS));
149 if (expunge_hash_table)
152 /* If someone runs `hash -r -t xyz' he will be disappointed. */
154 return (list_hashed_filename_targets (list, list_portably));
156 #if defined (RESTRICTED_SHELL)
157 if (restricted && pathname)
159 if (absolute_program (pathname))
161 sh_restricted (pathname);
162 return (EXECUTION_FAILURE);
164 /* If we are changing the hash table in a restricted shell, make sure the
165 target pathname can be found using a $PATH search. */
166 w = find_user_command (pathname);
167 if (w == 0 || *w == 0 || executable_file (w) == 0)
169 sh_notfound (pathname);
171 return (EXECUTION_FAILURE);
177 for (opt = EXECUTION_SUCCESS; list; list = list->next)
179 /* Add, remove or rehash the specified commands. */
180 w = list->word->word;
181 if (absolute_program (w))
185 if (file_isdir (pathname))
188 builtin_error ("%s: %s", pathname, strerror (EISDIR));
190 builtin_error (_("%s: is a directory"), pathname);
192 opt = EXECUTION_FAILURE;
195 phash_insert (w, pathname, 0, 0);
199 if (phash_remove (w))
202 opt = EXECUTION_FAILURE;
205 else if (add_hashed_command (w, 0))
206 opt = EXECUTION_FAILURE;
214 add_hashed_command (char *w, int quiet)
220 if (find_function (w) == 0 && find_shell_builtin (w) == 0)
223 full_path = find_user_command (w);
224 if (full_path && executable_file (full_path))
225 phash_insert (w, full_path, dot_found_in_search, 0);
237 /* Print information about current hashed info. */
239 print_hash_info (BUCKET_CONTENTS *item)
241 printf ("%4d\t%s\n", item->times_found, pathdata(item)->path);
246 print_portable_hash_info (BUCKET_CONTENTS *item)
250 fp = printable_filename (pathdata(item)->path, 1);
251 fn = printable_filename (item->key, 1);
252 printf ("builtin hash -p %s %s\n", fp, fn);
253 if (fp != pathdata(item)->path)
261 print_hashed_commands (int fmt)
263 if (hashed_filenames == 0 || HASH_ENTRIES (hashed_filenames) == 0)
267 printf (_("hits\tcommand\n"));
268 hash_walk (hashed_filenames, fmt ? print_portable_hash_info : print_hash_info);
273 list_hashed_filename_targets (WORD_LIST *list, int fmt)
275 int all_found, multiple;
280 multiple = list->next != 0;
282 for (l = list; l; l = l->next)
284 target = phash_search (l->word->word);
288 sh_notfound (l->word->word);
292 printf ("builtin hash -p %s %s\n", target, l->word->word);
296 printf ("%s\t", l->word->word);
297 printf ("%s\n", target);
302 return (sh_chkwrite (all_found ? EXECUTION_SUCCESS : EXECUTION_FAILURE));