Absolute File Name: | /home/opencoverage/opencoverage/guest-scripts/bash/src/builtins/hash.def |
Source code | Switch to Preprocessed file |
Line | Source | Count | ||||||||||||||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
1 | This file is hash.def, from which is created hash.c. | - | ||||||||||||||||||||||||
2 | It implements the builtin "hash" in Bash. | - | ||||||||||||||||||||||||
3 | - | |||||||||||||||||||||||||
4 | Copyright (C) 1987-2015 Free Software Foundation, Inc. | - | ||||||||||||||||||||||||
5 | - | |||||||||||||||||||||||||
6 | This file is part of GNU Bash, the Bourne Again SHell. | - | ||||||||||||||||||||||||
7 | - | |||||||||||||||||||||||||
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. | - | ||||||||||||||||||||||||
12 | - | |||||||||||||||||||||||||
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. | - | ||||||||||||||||||||||||
17 | - | |||||||||||||||||||||||||
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/>. | - | ||||||||||||||||||||||||
20 | - | |||||||||||||||||||||||||
21 | $PRODUCES hash.c | - | ||||||||||||||||||||||||
22 | - | |||||||||||||||||||||||||
23 | $BUILTIN hash | - | ||||||||||||||||||||||||
24 | $FUNCTION hash_builtin | - | ||||||||||||||||||||||||
25 | $SHORT_DOC hash [-lr] [-p pathname] [-dt] [name ...] | - | ||||||||||||||||||||||||
26 | Remember or display program locations. | - | ||||||||||||||||||||||||
27 | - | |||||||||||||||||||||||||
28 | Determine and remember the full pathname of each command NAME. If | - | ||||||||||||||||||||||||
29 | no arguments are given, information about remembered commands is displayed. | - | ||||||||||||||||||||||||
30 | - | |||||||||||||||||||||||||
31 | Options: | - | ||||||||||||||||||||||||
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 | - | ||||||||||||||||||||||||
38 | NAMEs are given | - | ||||||||||||||||||||||||
39 | Arguments: | - | ||||||||||||||||||||||||
40 | NAME Each NAME is searched for in $PATH and added to the list | - | ||||||||||||||||||||||||
41 | of remembered commands. | - | ||||||||||||||||||||||||
42 | - | |||||||||||||||||||||||||
43 | Exit Status: | - | ||||||||||||||||||||||||
44 | Returns success unless NAME is not found or an invalid option is given. | - | ||||||||||||||||||||||||
45 | $END | - | ||||||||||||||||||||||||
46 | - | |||||||||||||||||||||||||
47 | #include <config.h> | - | ||||||||||||||||||||||||
48 | - | |||||||||||||||||||||||||
49 | #include <stdio.h> | - | ||||||||||||||||||||||||
50 | - | |||||||||||||||||||||||||
51 | #include "../bashtypes.h" | - | ||||||||||||||||||||||||
52 | - | |||||||||||||||||||||||||
53 | #if defined (HAVE_UNISTD_H) | - | ||||||||||||||||||||||||
54 | # include <unistd.h> | - | ||||||||||||||||||||||||
55 | #endif | - | ||||||||||||||||||||||||
56 | - | |||||||||||||||||||||||||
57 | #include <errno.h> | - | ||||||||||||||||||||||||
58 | - | |||||||||||||||||||||||||
59 | #include "../bashansi.h" | - | ||||||||||||||||||||||||
60 | #include "../bashintl.h" | - | ||||||||||||||||||||||||
61 | - | |||||||||||||||||||||||||
62 | #include "../shell.h" | - | ||||||||||||||||||||||||
63 | #include "../builtins.h" | - | ||||||||||||||||||||||||
64 | #include "../execute_cmd.h" | - | ||||||||||||||||||||||||
65 | #include "../flags.h" | - | ||||||||||||||||||||||||
66 | #include "../findcmd.h" | - | ||||||||||||||||||||||||
67 | #include "../hashcmd.h" | - | ||||||||||||||||||||||||
68 | #include "common.h" | - | ||||||||||||||||||||||||
69 | #include "bashgetopt.h" | - | ||||||||||||||||||||||||
70 | - | |||||||||||||||||||||||||
71 | extern int dot_found_in_search; | - | ||||||||||||||||||||||||
72 | - | |||||||||||||||||||||||||
73 | static int add_hashed_command __P((char *, int)); | - | ||||||||||||||||||||||||
74 | static int print_hash_info __P((BUCKET_CONTENTS *)); | - | ||||||||||||||||||||||||
75 | static int print_portable_hash_info __P((BUCKET_CONTENTS *)); | - | ||||||||||||||||||||||||
76 | static int print_hashed_commands __P((int)); | - | ||||||||||||||||||||||||
77 | static int list_hashed_filename_targets __P((WORD_LIST *, int)); | - | ||||||||||||||||||||||||
78 | - | |||||||||||||||||||||||||
79 | /* Print statistics on the current state of hashed commands. If LIST is | - | ||||||||||||||||||||||||
80 | not empty, then rehash (or hash in the first place) the specified | - | ||||||||||||||||||||||||
81 | commands. */ | - | ||||||||||||||||||||||||
82 | int | - | ||||||||||||||||||||||||
83 | hash_builtin (list) | - | ||||||||||||||||||||||||
84 | WORD_LIST *list; | - | ||||||||||||||||||||||||
85 | { | - | ||||||||||||||||||||||||
86 | int expunge_hash_table, list_targets, list_portably, delete, opt; | - | ||||||||||||||||||||||||
87 | char *w, *pathname; | - | ||||||||||||||||||||||||
88 | - | |||||||||||||||||||||||||
89 | if (hashing_enabled == 0)
| 5-46 | ||||||||||||||||||||||||
90 | { | - | ||||||||||||||||||||||||
91 | builtin_error (_("hashing disabled")); | - | ||||||||||||||||||||||||
92 | return (EXECUTION_FAILURE); executed 5 times by 1 test: return (1); Executed by:
| 5 | ||||||||||||||||||||||||
93 | } | - | ||||||||||||||||||||||||
94 | - | |||||||||||||||||||||||||
95 | expunge_hash_table = list_targets = list_portably = delete = 0; | - | ||||||||||||||||||||||||
96 | pathname = (char *)NULL; | - | ||||||||||||||||||||||||
97 | reset_internal_getopt (); | - | ||||||||||||||||||||||||
98 | while ((opt = internal_getopt (list, "dlp:rt")) != -1)
| 23-41 | ||||||||||||||||||||||||
99 | { | - | ||||||||||||||||||||||||
100 | switch (opt) | - | ||||||||||||||||||||||||
101 | { | - | ||||||||||||||||||||||||
102 | case 'd': never executed: case 'd': | 0 | ||||||||||||||||||||||||
103 | delete = 1; | - | ||||||||||||||||||||||||
104 | break; never executed: break; | 0 | ||||||||||||||||||||||||
105 | case 'l': never executed: case 'l': | 0 | ||||||||||||||||||||||||
106 | list_portably = 1; | - | ||||||||||||||||||||||||
107 | break; never executed: break; | 0 | ||||||||||||||||||||||||
108 | case 'p': executed 6 times by 1 test: case 'p': Executed by:
| 6 | ||||||||||||||||||||||||
109 | pathname = list_optarg; | - | ||||||||||||||||||||||||
110 | break; executed 6 times by 1 test: break; Executed by:
| 6 | ||||||||||||||||||||||||
111 | case 'r': executed 12 times by 1 test: case 'r': Executed by:
| 12 | ||||||||||||||||||||||||
112 | expunge_hash_table = 1; | - | ||||||||||||||||||||||||
113 | break; executed 12 times by 1 test: break; Executed by:
| 12 | ||||||||||||||||||||||||
114 | case 't': never executed: case 't': | 0 | ||||||||||||||||||||||||
115 | list_targets = 1; | - | ||||||||||||||||||||||||
116 | break; never executed: break; | 0 | ||||||||||||||||||||||||
117 | CASE_HELPOPT; never executed: return (258); never executed: case -99: | 0 | ||||||||||||||||||||||||
118 | default: executed 5 times by 1 test: default: Executed by:
| 5 | ||||||||||||||||||||||||
119 | builtin_usage (); | - | ||||||||||||||||||||||||
120 | return (EX_USAGE); executed 5 times by 1 test: return (258); Executed by:
| 5 | ||||||||||||||||||||||||
121 | } | - | ||||||||||||||||||||||||
122 | } | - | ||||||||||||||||||||||||
123 | list = loptend; | - | ||||||||||||||||||||||||
124 | - | |||||||||||||||||||||||||
125 | /* hash -t requires at least one argument. */ | - | ||||||||||||||||||||||||
126 | if (list == 0 && list_targets)
| 0-22 | ||||||||||||||||||||||||
127 | { | - | ||||||||||||||||||||||||
128 | sh_needarg ("-t"); | - | ||||||||||||||||||||||||
129 | return (EXECUTION_FAILURE); never executed: return (1); | 0 | ||||||||||||||||||||||||
130 | } | - | ||||||||||||||||||||||||
131 | - | |||||||||||||||||||||||||
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)
| 10-22 | ||||||||||||||||||||||||
135 | { | - | ||||||||||||||||||||||||
136 | opt = print_hashed_commands (list_portably); | - | ||||||||||||||||||||||||
137 | if (opt == 0 && posixly_correct == 0)
| 0-7 | ||||||||||||||||||||||||
138 | printf (_("%s: hash table empty\n"), this_command_name); executed 7 times by 1 test: printf ( dcgettext (((void *)0), "%s: hash table empty\n" , 5) , this_command_name); Executed by:
| 7 | ||||||||||||||||||||||||
139 | - | |||||||||||||||||||||||||
140 | return (EXECUTION_SUCCESS); executed 10 times by 1 test: return (0); Executed by:
| 10 | ||||||||||||||||||||||||
141 | } | - | ||||||||||||||||||||||||
142 | - | |||||||||||||||||||||||||
143 | if (expunge_hash_table)
| 12-19 | ||||||||||||||||||||||||
144 | phash_flush (); executed 12 times by 1 test: phash_flush (); Executed by:
| 12 | ||||||||||||||||||||||||
145 | - | |||||||||||||||||||||||||
146 | /* If someone runs `hash -r -t xyz' he will be disappointed. */ | - | ||||||||||||||||||||||||
147 | if (list_targets)
| 0-31 | ||||||||||||||||||||||||
148 | return (list_hashed_filename_targets (list, list_portably)); never executed: return (list_hashed_filename_targets (list, list_portably)); | 0 | ||||||||||||||||||||||||
149 | - | |||||||||||||||||||||||||
150 | #if defined (RESTRICTED_SHELL) | - | ||||||||||||||||||||||||
151 | if (restricted && pathname)
| 0-31 | ||||||||||||||||||||||||
152 | { | - | ||||||||||||||||||||||||
153 | if (strchr (pathname, '/'))
| 0 | ||||||||||||||||||||||||
154 | { | - | ||||||||||||||||||||||||
155 | sh_restricted (pathname); | - | ||||||||||||||||||||||||
156 | return (EXECUTION_FAILURE); never executed: return (1); | 0 | ||||||||||||||||||||||||
157 | } | - | ||||||||||||||||||||||||
158 | /* If we are changing the hash table in a restricted shell, make sure the | - | ||||||||||||||||||||||||
159 | target pathname can be found using a $PATH search. */ | - | ||||||||||||||||||||||||
160 | w = find_user_command (pathname); | - | ||||||||||||||||||||||||
161 | if (w == 0 || *w == 0 || executable_file (w) == 0)
| 0 | ||||||||||||||||||||||||
162 | { | - | ||||||||||||||||||||||||
163 | sh_notfound (pathname); | - | ||||||||||||||||||||||||
164 | free (w); | - | ||||||||||||||||||||||||
165 | return (EXECUTION_FAILURE); never executed: return (1); | 0 | ||||||||||||||||||||||||
166 | } | - | ||||||||||||||||||||||||
167 | free (w); | - | ||||||||||||||||||||||||
168 | } never executed: end of block | 0 | ||||||||||||||||||||||||
169 | #endif | - | ||||||||||||||||||||||||
170 | - | |||||||||||||||||||||||||
171 | for (opt = EXECUTION_SUCCESS; list; list = list->next)
| 19-31 | ||||||||||||||||||||||||
172 | { | - | ||||||||||||||||||||||||
173 | /* Add, remove or rehash the specified commands. */ | - | ||||||||||||||||||||||||
174 | w = list->word->word; | - | ||||||||||||||||||||||||
175 | if (absolute_program (w))
| 0-19 | ||||||||||||||||||||||||
176 | continue; never executed: continue; | 0 | ||||||||||||||||||||||||
177 | else if (pathname)
| 6-13 | ||||||||||||||||||||||||
178 | { | - | ||||||||||||||||||||||||
179 | if (is_directory (pathname))
| 0-6 | ||||||||||||||||||||||||
180 | { | - | ||||||||||||||||||||||||
181 | #ifdef EISDIR | - | ||||||||||||||||||||||||
182 | builtin_error ("%s: %s", pathname, strerror (EISDIR)); | - | ||||||||||||||||||||||||
183 | #else | - | ||||||||||||||||||||||||
184 | builtin_error (_("%s: is a directory"), pathname); | - | ||||||||||||||||||||||||
185 | #endif | - | ||||||||||||||||||||||||
186 | opt = EXECUTION_FAILURE; | - | ||||||||||||||||||||||||
187 | } never executed: end of block | 0 | ||||||||||||||||||||||||
188 | else | - | ||||||||||||||||||||||||
189 | phash_insert (w, pathname, 0, 0); executed 6 times by 1 test: phash_insert (w, pathname, 0, 0); Executed by:
| 6 | ||||||||||||||||||||||||
190 | } | - | ||||||||||||||||||||||||
191 | else if (delete)
| 0-13 | ||||||||||||||||||||||||
192 | { | - | ||||||||||||||||||||||||
193 | if (phash_remove (w))
| 0 | ||||||||||||||||||||||||
194 | { | - | ||||||||||||||||||||||||
195 | sh_notfound (w); | - | ||||||||||||||||||||||||
196 | opt = EXECUTION_FAILURE; | - | ||||||||||||||||||||||||
197 | } never executed: end of block | 0 | ||||||||||||||||||||||||
198 | } never executed: end of block | 0 | ||||||||||||||||||||||||
199 | else if (add_hashed_command (w, 0))
| 5-8 | ||||||||||||||||||||||||
200 | opt = EXECUTION_FAILURE; executed 5 times by 1 test: opt = 1; Executed by:
| 5 | ||||||||||||||||||||||||
201 | } executed 19 times by 1 test: end of block Executed by:
| 19 | ||||||||||||||||||||||||
202 | - | |||||||||||||||||||||||||
203 | fflush (stdout); | - | ||||||||||||||||||||||||
204 | return (opt); executed 31 times by 1 test: return (opt); Executed by:
| 31 | ||||||||||||||||||||||||
205 | } | - | ||||||||||||||||||||||||
206 | - | |||||||||||||||||||||||||
207 | static int | - | ||||||||||||||||||||||||
208 | add_hashed_command (w, quiet) | - | ||||||||||||||||||||||||
209 | char *w; | - | ||||||||||||||||||||||||
210 | int quiet; | - | ||||||||||||||||||||||||
211 | { | - | ||||||||||||||||||||||||
212 | int rv; | - | ||||||||||||||||||||||||
213 | char *full_path; | - | ||||||||||||||||||||||||
214 | - | |||||||||||||||||||||||||
215 | rv = 0; | - | ||||||||||||||||||||||||
216 | if (find_function (w) == 0 && find_shell_builtin (w) == 0)
| 0-13 | ||||||||||||||||||||||||
217 | { | - | ||||||||||||||||||||||||
218 | phash_remove (w); | - | ||||||||||||||||||||||||
219 | full_path = find_user_command (w); | - | ||||||||||||||||||||||||
220 | if (full_path && executable_file (full_path))
| 0-8 | ||||||||||||||||||||||||
221 | phash_insert (w, full_path, dot_found_in_search, 0); executed 8 times by 1 test: phash_insert (w, full_path, dot_found_in_search, 0); Executed by:
| 8 | ||||||||||||||||||||||||
222 | else | - | ||||||||||||||||||||||||
223 | { | - | ||||||||||||||||||||||||
224 | if (quiet == 0)
| 0-5 | ||||||||||||||||||||||||
225 | sh_notfound (w); executed 5 times by 1 test: sh_notfound (w); Executed by:
| 5 | ||||||||||||||||||||||||
226 | rv++; | - | ||||||||||||||||||||||||
227 | } executed 5 times by 1 test: end of block Executed by:
| 5 | ||||||||||||||||||||||||
228 | FREE (full_path); executed 8 times by 1 test: sh_xfree((full_path), "./hash.def", 228); Executed by:
| 5-8 | ||||||||||||||||||||||||
229 | } executed 13 times by 1 test: end of block Executed by:
| 13 | ||||||||||||||||||||||||
230 | return (rv); executed 13 times by 1 test: return (rv); Executed by:
| 13 | ||||||||||||||||||||||||
231 | } | - | ||||||||||||||||||||||||
232 | - | |||||||||||||||||||||||||
233 | /* Print information about current hashed info. */ | - | ||||||||||||||||||||||||
234 | static int | - | ||||||||||||||||||||||||
235 | print_hash_info (item) | - | ||||||||||||||||||||||||
236 | BUCKET_CONTENTS *item; | - | ||||||||||||||||||||||||
237 | { | - | ||||||||||||||||||||||||
238 | printf ("%4d\t%s\n", item->times_found, pathdata(item)->path); | - | ||||||||||||||||||||||||
239 | return 0; executed 8 times by 1 test: return 0; Executed by:
| 8 | ||||||||||||||||||||||||
240 | } | - | ||||||||||||||||||||||||
241 | - | |||||||||||||||||||||||||
242 | static int | - | ||||||||||||||||||||||||
243 | print_portable_hash_info (item) | - | ||||||||||||||||||||||||
244 | BUCKET_CONTENTS *item; | - | ||||||||||||||||||||||||
245 | { | - | ||||||||||||||||||||||||
246 | char *fp, *fn; | - | ||||||||||||||||||||||||
247 | - | |||||||||||||||||||||||||
248 | fp = printable_filename (pathdata(item)->path, 1); | - | ||||||||||||||||||||||||
249 | fn = printable_filename (item->key, 1); | - | ||||||||||||||||||||||||
250 | printf ("builtin hash -p %s %s\n", fp, fn); | - | ||||||||||||||||||||||||
251 | if (fp != pathdata(item)->path)
| 0 | ||||||||||||||||||||||||
252 | free (fp); never executed: sh_xfree((fp), "./hash.def", 252); | 0 | ||||||||||||||||||||||||
253 | if (fn != item->key)
| 0 | ||||||||||||||||||||||||
254 | free (fn); never executed: sh_xfree((fn), "./hash.def", 254); | 0 | ||||||||||||||||||||||||
255 | return 0; never executed: return 0; | 0 | ||||||||||||||||||||||||
256 | } | - | ||||||||||||||||||||||||
257 | - | |||||||||||||||||||||||||
258 | static int | - | ||||||||||||||||||||||||
259 | print_hashed_commands (fmt) | - | ||||||||||||||||||||||||
260 | int fmt; | - | ||||||||||||||||||||||||
261 | { | - | ||||||||||||||||||||||||
262 | if (hashed_filenames == 0 || HASH_ENTRIES (hashed_filenames) == 0)
| 0-7 | ||||||||||||||||||||||||
263 | return (0); executed 7 times by 1 test: return (0); Executed by:
| 7 | ||||||||||||||||||||||||
264 | - | |||||||||||||||||||||||||
265 | if (fmt == 0)
| 0-3 | ||||||||||||||||||||||||
266 | printf (_("hits\tcommand\n")); executed 3 times by 1 test: printf ( dcgettext (((void *)0), "hits\tcommand\n" , 5) ); Executed by:
| 3 | ||||||||||||||||||||||||
267 | hash_walk (hashed_filenames, fmt ? print_portable_hash_info : print_hash_info); | - | ||||||||||||||||||||||||
268 | return (1); executed 3 times by 1 test: return (1); Executed by:
| 3 | ||||||||||||||||||||||||
269 | } | - | ||||||||||||||||||||||||
270 | - | |||||||||||||||||||||||||
271 | static int | - | ||||||||||||||||||||||||
272 | list_hashed_filename_targets (list, fmt) | - | ||||||||||||||||||||||||
273 | WORD_LIST *list; | - | ||||||||||||||||||||||||
274 | int fmt; | - | ||||||||||||||||||||||||
275 | { | - | ||||||||||||||||||||||||
276 | int all_found, multiple; | - | ||||||||||||||||||||||||
277 | char *target; | - | ||||||||||||||||||||||||
278 | WORD_LIST *l; | - | ||||||||||||||||||||||||
279 | - | |||||||||||||||||||||||||
280 | all_found = 1; | - | ||||||||||||||||||||||||
281 | multiple = list->next != 0; | - | ||||||||||||||||||||||||
282 | - | |||||||||||||||||||||||||
283 | for (l = list; l; l = l->next)
| 0 | ||||||||||||||||||||||||
284 | { | - | ||||||||||||||||||||||||
285 | target = phash_search (l->word->word); | - | ||||||||||||||||||||||||
286 | if (target == 0)
| 0 | ||||||||||||||||||||||||
287 | { | - | ||||||||||||||||||||||||
288 | all_found = 0; | - | ||||||||||||||||||||||||
289 | sh_notfound (l->word->word); | - | ||||||||||||||||||||||||
290 | continue; never executed: continue; | 0 | ||||||||||||||||||||||||
291 | } | - | ||||||||||||||||||||||||
292 | if (fmt)
| 0 | ||||||||||||||||||||||||
293 | printf ("builtin hash -p %s %s\n", target, l->word->word); never executed: printf ("builtin hash -p %s %s\n", target, l->word->word); | 0 | ||||||||||||||||||||||||
294 | else | - | ||||||||||||||||||||||||
295 | { | - | ||||||||||||||||||||||||
296 | if (multiple)
| 0 | ||||||||||||||||||||||||
297 | printf ("%s\t", l->word->word); never executed: printf ("%s\t", l->word->word); | 0 | ||||||||||||||||||||||||
298 | printf ("%s\n", target); | - | ||||||||||||||||||||||||
299 | } never executed: end of block | 0 | ||||||||||||||||||||||||
300 | free (target); | - | ||||||||||||||||||||||||
301 | } never executed: end of block | 0 | ||||||||||||||||||||||||
302 | - | |||||||||||||||||||||||||
303 | return (all_found ? EXECUTION_SUCCESS : EXECUTION_FAILURE); never executed: return (all_found ? 0 : 1); | 0 | ||||||||||||||||||||||||
304 | } | - | ||||||||||||||||||||||||
Source code | Switch to Preprocessed file |