1 This file is hash.def, from which is created hash.c.
2 It implements the builtin "hash" in Bash.
4 Copyright (C) 1987-2010 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 is 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"
65 #include "../findcmd.h"
66 #include "../hashcmd.h"
68 #include "bashgetopt.h"
70 extern int posixly_correct;
71 extern int dot_found_in_search;
72 extern char *this_command_name;
74 static int add_hashed_command __P((char *, int));
75 static int print_hash_info __P((BUCKET_CONTENTS *));
76 static int print_portable_hash_info __P((BUCKET_CONTENTS *));
77 static int print_hashed_commands __P((int));
78 static int list_hashed_filename_targets __P((WORD_LIST *, int));
80 /* Print statistics on the current state of hashed commands. If LIST is
81 not empty, then rehash (or hash in the first place) the specified
87 int expunge_hash_table, list_targets, list_portably, delete, opt;
90 if (hashing_enabled == 0)
92 builtin_error (_("hashing disabled"));
93 return (EXECUTION_FAILURE);
96 expunge_hash_table = list_targets = list_portably = delete = 0;
97 pathname = (char *)NULL;
98 reset_internal_getopt ();
99 while ((opt = internal_getopt (list, "dlp:rt")) != -1)
110 pathname = list_optarg;
113 expunge_hash_table = 1;
125 /* hash -t requires at least one argument. */
126 if (list == 0 && list_targets)
129 return (EXECUTION_FAILURE);
132 /* We want hash -r to be silent, but hash -- to print hashing info, so
133 we test expunge_hash_table. */
134 if (list == 0 && expunge_hash_table == 0)
136 opt = print_hashed_commands (list_portably);
137 if (opt == 0 && posixly_correct == 0)
138 printf (_("%s: hash table empty\n"), this_command_name);
140 return (EXECUTION_SUCCESS);
143 if (expunge_hash_table)
146 /* If someone runs `hash -r -t xyz' he will be disappointed. */
148 return (list_hashed_filename_targets (list, list_portably));
150 #if defined (RESTRICTED_SHELL)
151 if (restricted && pathname && strchr (pathname, '/'))
153 sh_restricted (pathname);
154 return (EXECUTION_FAILURE);
158 for (opt = EXECUTION_SUCCESS; list; list = list->next)
160 /* Add, remove or rehash the specified commands. */
161 w = list->word->word;
162 if (absolute_program (w))
166 if (is_directory (pathname))
169 builtin_error ("%s: %s", pathname, strerror (EISDIR));
171 builtin_error (_("%s: is a directory"), pathname);
173 opt = EXECUTION_FAILURE;
176 phash_insert (w, pathname, 0, 0);
180 if (phash_remove (w))
183 opt = EXECUTION_FAILURE;
186 else if (add_hashed_command (w, 0))
187 opt = EXECUTION_FAILURE;
195 add_hashed_command (w, quiet)
203 if (find_function (w) == 0 && find_shell_builtin (w) == 0)
206 full_path = find_user_command (w);
207 if (full_path && executable_file (full_path))
208 phash_insert (w, full_path, dot_found_in_search, 0);
220 /* Print information about current hashed info. */
222 print_hash_info (item)
223 BUCKET_CONTENTS *item;
225 printf ("%4d\t%s\n", item->times_found, pathdata(item)->path);
230 print_portable_hash_info (item)
231 BUCKET_CONTENTS *item;
233 printf ("builtin hash -p %s %s\n", pathdata(item)->path, item->key);
238 print_hashed_commands (fmt)
241 if (hashed_filenames == 0 || HASH_ENTRIES (hashed_filenames) == 0)
245 printf (_("hits\tcommand\n"));
246 hash_walk (hashed_filenames, fmt ? print_portable_hash_info : print_hash_info);
251 list_hashed_filename_targets (list, fmt)
255 int all_found, multiple;
260 multiple = list->next != 0;
262 for (l = list; l; l = l->next)
264 target = phash_search (l->word->word);
268 sh_notfound (l->word->word);
272 printf ("builtin hash -p %s %s\n", target, l->word->word);
276 printf ("%s\t", l->word->word);
277 printf ("%s\n", target);
281 return (all_found ? EXECUTION_SUCCESS : EXECUTION_FAILURE);