OpenCoverage

hash.def

Absolute File Name:/home/opencoverage/opencoverage/guest-scripts/bash/src/builtins/hash.def
Source codeSwitch to Preprocessed file
LineSourceCount
1This file is hash.def, from which is created hash.c.-
2It implements the builtin "hash" in Bash.-
3-
4Copyright (C) 1987-2015 Free Software Foundation, Inc.-
5-
6This file is part of GNU Bash, the Bourne Again SHell.-
7-
8Bash is free software: you can redistribute it and/or modify-
9it under the terms of the GNU General Public License as published by-
10the Free Software Foundation, either version 3 of the License, or-
11(at your option) any later version.-
12-
13Bash is distributed in the hope that it will be useful,-
14but WITHOUT ANY WARRANTY; without even the implied warranty of-
15MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the-
16GNU General Public License for more details.-
17-
18You should have received a copy of the GNU General Public License-
19along 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 ...]-
26Remember or display program locations.-
27-
28Determine and remember the full pathname of each command NAME. If-
29no arguments are given, information about remembered commands is displayed.-
30-
31Options:-
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-
39Arguments:-
40 NAME Each NAME is searched for in $PATH and added to the list-
41 of remembered commands.-
42-
43Exit Status:-
44Returns 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-
71extern int dot_found_in_search;-
72-
73static int add_hashed_command __P((char *, int));-
74static int print_hash_info __P((BUCKET_CONTENTS *));-
75static int print_portable_hash_info __P((BUCKET_CONTENTS *));-
76static int print_hashed_commands __P((int));-
77static 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. */-
82int-
83hash_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)
hashing_enabled == 0Description
TRUEevaluated 5 times by 1 test
Evaluated by:
  • Self test
FALSEevaluated 46 times by 1 test
Evaluated by:
  • Self test
5-46
90 {-
91 builtin_error (_("hashing disabled"));-
92 return (EXECUTION_FAILURE);
executed 5 times by 1 test: return (1);
Executed by:
  • Self test
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)
(opt = interna...lp:rt")) != -1Description
TRUEevaluated 23 times by 1 test
Evaluated by:
  • Self test
FALSEevaluated 41 times by 1 test
Evaluated by:
  • Self test
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:
  • Self test
6
109 pathname = list_optarg;-
110 break;
executed 6 times by 1 test: break;
Executed by:
  • Self test
6
111 case 'r':
executed 12 times by 1 test: case 'r':
Executed by:
  • Self test
12
112 expunge_hash_table = 1;-
113 break;
executed 12 times by 1 test: break;
Executed by:
  • Self test
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:
  • Self test
5
119 builtin_usage ();-
120 return (EX_USAGE);
executed 5 times by 1 test: return (258);
Executed by:
  • Self test
5
121 }-
122 }-
123 list = loptend;-
124-
125 /* hash -t requires at least one argument. */-
126 if (list == 0 && list_targets)
list == 0Description
TRUEevaluated 22 times by 1 test
Evaluated by:
  • Self test
FALSEevaluated 19 times by 1 test
Evaluated by:
  • Self test
list_targetsDescription
TRUEnever evaluated
FALSEevaluated 22 times by 1 test
Evaluated by:
  • Self test
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)
list == 0Description
TRUEevaluated 22 times by 1 test
Evaluated by:
  • Self test
FALSEevaluated 19 times by 1 test
Evaluated by:
  • Self test
expunge_hash_table == 0Description
TRUEevaluated 10 times by 1 test
Evaluated by:
  • Self test
FALSEevaluated 12 times by 1 test
Evaluated by:
  • Self test
10-22
135 {-
136 opt = print_hashed_commands (list_portably);-
137 if (opt == 0 && posixly_correct == 0)
opt == 0Description
TRUEevaluated 7 times by 1 test
Evaluated by:
  • Self test
FALSEevaluated 3 times by 1 test
Evaluated by:
  • Self test
posixly_correct == 0Description
TRUEevaluated 7 times by 1 test
Evaluated by:
  • Self test
FALSEnever evaluated
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:
  • Self test
7
139-
140 return (EXECUTION_SUCCESS);
executed 10 times by 1 test: return (0);
Executed by:
  • Self test
10
141 }-
142-
143 if (expunge_hash_table)
expunge_hash_tableDescription
TRUEevaluated 12 times by 1 test
Evaluated by:
  • Self test
FALSEevaluated 19 times by 1 test
Evaluated by:
  • Self test
12-19
144 phash_flush ();
executed 12 times by 1 test: phash_flush ();
Executed by:
  • Self test
12
145-
146 /* If someone runs `hash -r -t xyz' he will be disappointed. */-
147 if (list_targets)
list_targetsDescription
TRUEnever evaluated
FALSEevaluated 31 times by 1 test
Evaluated by:
  • Self test
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)
restrictedDescription
TRUEnever evaluated
FALSEevaluated 31 times by 1 test
Evaluated by:
  • Self test
pathnameDescription
TRUEnever evaluated
FALSEnever evaluated
0-31
152 {-
153 if (strchr (pathname, '/'))
(__extension__...name , '/' )))Description
TRUEnever evaluated
FALSEnever evaluated
__builtin_constant_p ( '/' )Description
TRUEnever evaluated
FALSEnever evaluated
!__builtin_con...p ( pathname )Description
TRUEnever evaluated
FALSEnever evaluated
( '/' ) == '\0'Description
TRUEnever evaluated
FALSEnever evaluated
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)
w == 0Description
TRUEnever evaluated
FALSEnever evaluated
*w == 0Description
TRUEnever evaluated
FALSEnever evaluated
executable_file (w) == 0Description
TRUEnever evaluated
FALSEnever evaluated
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)
listDescription
TRUEevaluated 19 times by 1 test
Evaluated by:
  • Self test
FALSEevaluated 31 times by 1 test
Evaluated by:
  • Self test
19-31
172 {-
173 /* Add, remove or rehash the specified commands. */-
174 w = list->word->word;-
175 if (absolute_program (w))
absolute_program (w)Description
TRUEnever evaluated
FALSEevaluated 19 times by 1 test
Evaluated by:
  • Self test
0-19
176 continue;
never executed: continue;
0
177 else if (pathname)
pathnameDescription
TRUEevaluated 6 times by 1 test
Evaluated by:
  • Self test
FALSEevaluated 13 times by 1 test
Evaluated by:
  • Self test
6-13
178 {-
179 if (is_directory (pathname))
is_directory (pathname)Description
TRUEnever evaluated
FALSEevaluated 6 times by 1 test
Evaluated by:
  • Self test
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:
  • Self test
6
190 }-
191 else if (delete)
deleteDescription
TRUEnever evaluated
FALSEevaluated 13 times by 1 test
Evaluated by:
  • Self test
0-13
192 {-
193 if (phash_remove (w))
phash_remove (w)Description
TRUEnever evaluated
FALSEnever evaluated
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))
add_hashed_command (w, 0)Description
TRUEevaluated 5 times by 1 test
Evaluated by:
  • Self test
FALSEevaluated 8 times by 1 test
Evaluated by:
  • Self test
5-8
200 opt = EXECUTION_FAILURE;
executed 5 times by 1 test: opt = 1;
Executed by:
  • Self test
5
201 }
executed 19 times by 1 test: end of block
Executed by:
  • Self test
19
202-
203 fflush (stdout);-
204 return (opt);
executed 31 times by 1 test: return (opt);
Executed by:
  • Self test
31
205}-
206-
207static int-
208add_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)
find_function (w) == 0Description
TRUEevaluated 13 times by 1 test
Evaluated by:
  • Self test
FALSEnever evaluated
find_shell_builtin (w) == 0Description
TRUEevaluated 13 times by 1 test
Evaluated by:
  • Self test
FALSEnever evaluated
0-13
217 {-
218 phash_remove (w);-
219 full_path = find_user_command (w);-
220 if (full_path && executable_file (full_path))
full_pathDescription
TRUEevaluated 8 times by 1 test
Evaluated by:
  • Self test
FALSEevaluated 5 times by 1 test
Evaluated by:
  • Self test
executable_file (full_path)Description
TRUEevaluated 8 times by 1 test
Evaluated by:
  • Self test
FALSEnever evaluated
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:
  • Self test
8
222 else-
223 {-
224 if (quiet == 0)
quiet == 0Description
TRUEevaluated 5 times by 1 test
Evaluated by:
  • Self test
FALSEnever evaluated
0-5
225 sh_notfound (w);
executed 5 times by 1 test: sh_notfound (w);
Executed by:
  • Self test
5
226 rv++;-
227 }
executed 5 times by 1 test: end of block
Executed by:
  • Self test
5
228 FREE (full_path);
executed 8 times by 1 test: sh_xfree((full_path), "./hash.def", 228);
Executed by:
  • Self test
full_pathDescription
TRUEevaluated 8 times by 1 test
Evaluated by:
  • Self test
FALSEevaluated 5 times by 1 test
Evaluated by:
  • Self test
5-8
229 }
executed 13 times by 1 test: end of block
Executed by:
  • Self test
13
230 return (rv);
executed 13 times by 1 test: return (rv);
Executed by:
  • Self test
13
231}-
232-
233/* Print information about current hashed info. */-
234static int-
235print_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:
  • Self test
8
240}-
241-
242static int-
243print_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)
fp != ((PATH_D...)->data)->pathDescription
TRUEnever evaluated
FALSEnever evaluated
0
252 free (fp);
never executed: sh_xfree((fp), "./hash.def", 252);
0
253 if (fn != item->key)
fn != item->keyDescription
TRUEnever evaluated
FALSEnever evaluated
0
254 free (fn);
never executed: sh_xfree((fn), "./hash.def", 254);
0
255 return 0;
never executed: return 0;
0
256}-
257-
258static int-
259print_hashed_commands (fmt)-
260 int fmt;-
261{-
262 if (hashed_filenames == 0 || HASH_ENTRIES (hashed_filenames) == 0)
(hashed_filenames)Description
TRUEevaluated 3 times by 1 test
Evaluated by:
  • Self test
FALSEnever evaluated
hashed_filenames == 0Description
TRUEevaluated 7 times by 1 test
Evaluated by:
  • Self test
FALSEevaluated 3 times by 1 test
Evaluated by:
  • Self test
((hashed_filen...ries : 0) == 0Description
TRUEnever evaluated
FALSEevaluated 3 times by 1 test
Evaluated by:
  • Self test
0-7
263 return (0);
executed 7 times by 1 test: return (0);
Executed by:
  • Self test
7
264-
265 if (fmt == 0)
fmt == 0Description
TRUEevaluated 3 times by 1 test
Evaluated by:
  • Self test
FALSEnever evaluated
0-3
266 printf (_("hits\tcommand\n"));
executed 3 times by 1 test: printf ( dcgettext (((void *)0), "hits\tcommand\n" , 5) );
Executed by:
  • Self test
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:
  • Self test
3
269}-
270-
271static int-
272list_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)
lDescription
TRUEnever evaluated
FALSEnever evaluated
0
284 {-
285 target = phash_search (l->word->word);-
286 if (target == 0)
target == 0Description
TRUEnever evaluated
FALSEnever evaluated
0
287 {-
288 all_found = 0;-
289 sh_notfound (l->word->word);-
290 continue;
never executed: continue;
0
291 }-
292 if (fmt)
fmtDescription
TRUEnever evaluated
FALSEnever evaluated
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)
multipleDescription
TRUEnever evaluated
FALSEnever evaluated
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 codeSwitch to Preprocessed file

Generated by Squish Coco 4.1.2