1 This file is hash.def, from which is created hash.c.
2 It implements the builtin "hash" in Bash.
4 Copyright (C) 1987, 1989, 1991 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 1, or (at your option) any later
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
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, 675 Mass Ave, Cambridge, MA 02139, USA.
25 $FUNCTION hash_builtin
26 $SHORT_DOC hash [-r] [name ...]
27 For each NAME, the full pathname of the command is determined and
28 remembered. The -r option causes the shell to forget all remembered
29 locations. If no arguments are given, information about remembered
30 commands is presented.
33 #include <sys/types.h>
34 #include "../posixstat.h"
38 #if defined (HAVE_STRING_H)
40 #else /* !HAVE_STRING_H */
42 #endif /* !HAVE_STRING_H */
45 #include "../builtins.h"
49 #include "../execute_cmd.h"
51 extern int dot_found_in_search;
54 initialize_filename_hashing ()
56 hashed_filenames = make_hash_table (FILENAME_HASH_BUCKETS);
59 /* Print statistics on the current state of hashed commands. If LIST is
60 not empty, then rehash (or hash in the first place) the specified
65 int expunge_hash_table = 0;
70 builtin_error ("hashing disabled");
71 return (EXECUTION_FAILURE);
76 char *arg = list->word->word;
78 if (ISOPTION (arg, 'r'))
80 expunge_hash_table = 1;
83 else if (ISOPTION (arg, '-'))
90 bad_option (list->word->word);
91 builtin_error ("usage: hash [-r] [command ...]");
98 /* We want hash -r to be silent, but hash -- to print hashing info. That
99 is the reason for the !expunge_hash_table. */
100 if (!list && !expunge_hash_table)
102 /* Print information about current hashed info. */
105 register BUCKET_CONTENTS *item_list;
107 while (bucket < hashed_filenames->nbuckets)
109 item_list = get_hash_bucket (bucket, hashed_filenames);
114 printf ("hits\tcommand\n");
120 item_list->times_found, pathdata(item_list)->path);
121 item_list = item_list->next;
128 printf ("No commands in hash table.\n");
130 return (EXECUTION_SUCCESS);
133 if (expunge_hash_table)
136 register BUCKET_CONTENTS *item_list, *prev;
138 while (bucket < hashed_filenames->nbuckets)
140 item_list = get_hash_bucket (bucket, hashed_filenames);
146 free (item_list->key);
147 free (pathdata(item_list)->path);
148 free (item_list->data);
149 item_list = item_list->next;
152 hashed_filenames->bucket_array[bucket] = (BUCKET_CONTENTS *)NULL;
160 /* Add or rehash the specified commands. */
165 word = list->word->word;
166 if (absolute_program (word))
171 full_path = find_user_command (word);
172 var = find_function (word);
174 if (!find_shell_builtin (word) && (!var))
176 if (full_path && executable_file (full_path))
177 remember_filename (word, full_path, dot_found_in_search, 0);
180 builtin_error ("%s: not found", word);
193 return (EXECUTION_FAILURE);
195 return (EXECUTION_SUCCESS);
198 /* Place FILENAME (key) and FULL_PATHNAME (data->path) into the
199 hash table. CHECK_DOT if non-null is for future calls to
200 find_hashed_filename (). FOUND is the initial value for
203 remember_filename (filename, full_pathname, check_dot, found)
204 char *filename, *full_pathname;
205 int check_dot, found;
207 register BUCKET_CONTENTS *item;
209 if (hashing_disabled)
211 item = add_hash_item (filename, hashed_filenames);
213 free (pathdata(item)->path);
216 item->key = savestring (filename);
217 item->data = (char *)xmalloc (sizeof (PATH_DATA));
219 pathdata(item)->path = savestring (full_pathname);
220 pathdata(item)->check_dot = check_dot;
221 item->times_found = found;