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 2, 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, 59 Temple Place, Suite 330, Boston, MA 02111 USA.
25 $FUNCTION hash_builtin
26 $SHORT_DOC hash [-r] [-p pathname] [name ...]
27 For each NAME, the full pathname of the command is determined and
28 remembered. If the -p option is supplied, PATHNAME is used as the
29 full pathname of NAME, and no path search is performed. The -r
30 option causes the shell to forget all remembered locations. If no
31 arguments are given, information about remembered commands is displayed.
38 #include "../bashtypes.h"
40 #if defined (HAVE_UNISTD_H)
46 #include "../bashansi.h"
49 #include "../builtins.h"
51 #include "../findcmd.h"
52 #include "../hashcmd.h"
54 #include "bashgetopt.h"
56 extern int dot_found_in_search;
57 extern char *this_command_name;
59 static int add_hashed_command ();
60 static int print_hashed_commands ();
62 /* Print statistics on the current state of hashed commands. If LIST is
63 not empty, then rehash (or hash in the first place) the specified
69 int expunge_hash_table, opt;
72 if (hashing_enabled == 0)
74 builtin_error ("hashing disabled");
75 return (EXECUTION_FAILURE);
78 expunge_hash_table = 0;
79 pathname = (char *)NULL;
80 reset_internal_getopt ();
81 while ((opt = internal_getopt (list, "rp:")) != -1)
86 expunge_hash_table = 1;
89 pathname = list_optarg;
98 /* We want hash -r to be silent, but hash -- to print hashing info. That
99 is the reason for the test of expunge_hash_table. */
100 if (list == 0 && expunge_hash_table == 0)
102 if (print_hashed_commands () == 0)
103 printf ("%s: hash table empty\n", this_command_name);
105 return (EXECUTION_SUCCESS);
108 if (expunge_hash_table)
109 flush_hashed_filenames ();
111 #if defined (RESTRICTED_SHELL)
112 if (restricted && pathname && strchr (pathname, '/'))
114 builtin_error ("%s: restricted", pathname);
115 return (EXECUTION_FAILURE);
119 for (opt = EXECUTION_SUCCESS; list; list = list->next)
121 /* Add or rehash the specified commands. */
122 w = list->word->word;
125 if (is_directory (pathname))
128 builtin_error ("%s: %s", pathname, strerror (EISDIR));
130 builtin_error ("%s: is a directory", pathname);
132 opt = EXECUTION_FAILURE;
135 remember_filename (w, pathname, 0, 0);
137 else if (absolute_program (w))
139 else if (add_hashed_command (w, 0))
140 opt = EXECUTION_FAILURE;
148 add_hashed_command (w, quiet)
156 if (find_function (w) == 0 && find_shell_builtin (w) == 0)
158 full_path = find_user_command (w);
159 if (full_path && executable_file (full_path))
160 remember_filename (w, full_path, dot_found_in_search, 0);
164 builtin_error ("%s: not found", w);
173 /* Print information about current hashed info. */
175 print_hashed_commands ()
177 BUCKET_CONTENTS *item_list;
178 int bucket, any_printed;
180 if (hashed_filenames == 0)
183 for (bucket = any_printed = 0; bucket < hashed_filenames->nbuckets; bucket++)
185 item_list = get_hash_bucket (bucket, hashed_filenames);
189 if (any_printed == 0)
191 printf ("hits\tcommand\n");
195 for ( ; item_list; item_list = item_list->next)
196 printf ("%4d\t%s\n", item_list->times_found, pathdata(item_list)->path);
199 return (any_printed);