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)
44 #include "../bashansi.h"
47 #include "../builtins.h"
49 #include "../findcmd.h"
50 #include "../hashcmd.h"
52 #include "bashgetopt.h"
54 extern int dot_found_in_search;
55 extern char *this_command_name;
57 static int add_hashed_command ();
58 static int print_hashed_commands ();
60 /* Print statistics on the current state of hashed commands. If LIST is
61 not empty, then rehash (or hash in the first place) the specified
67 int expunge_hash_table, opt;
70 if (hashing_enabled == 0)
72 builtin_error ("hashing disabled");
73 return (EXECUTION_FAILURE);
76 expunge_hash_table = 0;
77 pathname = (char *)NULL;
78 reset_internal_getopt ();
79 while ((opt = internal_getopt (list, "rp:")) != -1)
84 expunge_hash_table = 1;
87 pathname = list_optarg;
96 /* We want hash -r to be silent, but hash -- to print hashing info. That
97 is the reason for the test of expunge_hash_table. */
98 if (list == 0 && expunge_hash_table == 0)
100 if (print_hashed_commands () == 0)
101 printf ("%s: hash table empty\n", this_command_name);
103 return (EXECUTION_SUCCESS);
106 if (expunge_hash_table)
107 flush_hashed_filenames ();
109 #if defined (RESTRICTED_SHELL)
110 if (restricted && pathname && strchr (pathname, '/'))
112 builtin_error ("%s: restricted", pathname);
113 return (EXECUTION_FAILURE);
117 for (opt = EXECUTION_SUCCESS; list; list = list->next)
119 /* Add or rehash the specified commands. */
120 w = list->word->word;
122 remember_filename (w, pathname, 0, 0);
123 else if (absolute_program (w))
125 else if (add_hashed_command (w, 0))
126 opt = EXECUTION_FAILURE;
134 add_hashed_command (w, quiet)
142 if (find_function (w) == 0 && find_shell_builtin (w) == 0)
144 full_path = find_user_command (w);
145 if (full_path && executable_file (full_path))
146 remember_filename (w, full_path, dot_found_in_search, 0);
150 builtin_error ("%s: not found", w);
159 /* Print information about current hashed info. */
161 print_hashed_commands ()
163 BUCKET_CONTENTS *item_list;
164 int bucket, any_printed;
166 if (hashed_filenames == 0)
169 for (bucket = any_printed = 0; bucket < hashed_filenames->nbuckets; bucket++)
171 item_list = get_hash_bucket (bucket, hashed_filenames);
175 if (any_printed == 0)
177 printf ("hits\tcommand\n");
181 for ( ; item_list; item_list = item_list->next)
182 printf ("%4d\t%s\n", item_list->times_found, pathdata(item_list)->path);
185 return (any_printed);