]>
git.ipfire.org Git - thirdparty/bash.git/blob - hashcmd.c
6f609776e80dcf5fcee4c3168c23269d5c13e721
1 /* hashcmd.c - functions for managing a hash table mapping command names to
4 /* Copyright (C) 1997 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. */
24 #include "bashtypes.h"
25 #include "posixstat.h"
27 #if defined (HAVE_UNISTD_H)
37 extern int hashing_enabled
;
39 static int hashing_initialized
= 0;
41 HASH_TABLE
*hashed_filenames
;
44 initialize_filename_hashing ()
46 if (hashing_initialized
== 0)
48 hashed_filenames
= make_hash_table (FILENAME_HASH_BUCKETS
);
49 hashing_initialized
= 1;
54 free_filename_data (data
)
57 free (((PATH_DATA
*)data
)->path
);
62 flush_hashed_filenames ()
65 flush_hash_table (hashed_filenames
, free_filename_data
);
68 /* Remove FILENAME from the table of hashed commands. */
70 remove_hashed_filename (filename
)
73 register BUCKET_CONTENTS
*item
;
75 if (hashing_enabled
== 0 || hashed_filenames
== 0)
78 item
= remove_hash_item (filename
, hashed_filenames
);
82 free_filename_data (item
->data
);
88 /* Place FILENAME (key) and FULL_PATHNAME (data->path) into the
89 hash table. CHECK_DOT if non-null is for future calls to
90 find_hashed_filename (); it means that this file was found
91 in a directory in $PATH that is not an absolute pathname.
92 FOUND is the initial value for times_found. */
94 remember_filename (filename
, full_pathname
, check_dot
, found
)
95 char *filename
, *full_pathname
;
98 register BUCKET_CONTENTS
*item
;
100 if (hashing_enabled
== 0)
103 if (hashed_filenames
== 0 || hashing_initialized
== 0)
104 initialize_filename_hashing ();
106 item
= add_hash_item (filename
, hashed_filenames
);
108 free (pathdata(item
)->path
);
111 item
->key
= savestring (filename
);
112 item
->data
= xmalloc (sizeof (PATH_DATA
));
114 pathdata(item
)->path
= savestring (full_pathname
);
115 pathdata(item
)->flags
= 0;
117 pathdata(item
)->flags
|= HASH_CHKDOT
;
118 if (*full_pathname
!= '/')
119 pathdata(item
)->flags
|= HASH_RELPATH
;
120 item
->times_found
= found
;
123 /* Return the full pathname that FILENAME hashes to. If FILENAME
124 is hashed, but (data->flags & HASH_CHKDOT) is non-zero, check
125 ./FILENAME and return that if it is executable. This always
126 returns a newly-allocated string; the caller is responsible
129 find_hashed_filename (filename
)
132 register BUCKET_CONTENTS
*item
;
133 char *path
, *dotted_filename
, *tail
;
136 if (hashing_enabled
== 0 || hashed_filenames
== 0)
137 return ((char *)NULL
);
139 item
= find_hash_item (filename
, hashed_filenames
);
142 return ((char *)NULL
);
144 /* If this filename is hashed, but `.' comes before it in the path,
145 see if ./filename is executable. If the hashed value is not an
146 absolute pathname, see if ./`hashed-value' exists. */
147 path
= pathdata(item
)->path
;
148 if (pathdata(item
)->flags
& (HASH_CHKDOT
|HASH_RELPATH
))
150 tail
= (pathdata(item
)->flags
& HASH_RELPATH
) ? path
: filename
;
151 /* If the pathname does not start with a `./', add a `./' to it. */
152 if (tail
[0] != '.' || tail
[1] != '/')
154 dotted_filename
= xmalloc (3 + strlen (tail
));
155 dotted_filename
[0] = '.'; dotted_filename
[1] = '/';
156 strcpy (dotted_filename
+ 2, tail
);
159 dotted_filename
= savestring (tail
);
161 if (executable_file (dotted_filename
))
162 return (dotted_filename
);
164 free (dotted_filename
);
167 if (pathdata(item
)->flags
& HASH_RELPATH
)
168 return ((char *)NULL
);
171 /* Watch out. If this file was hashed to "./filename", and
172 "./filename" is not executable, then return NULL. */
174 /* Since we already know "./filename" is not executable, what
175 we're really interested in is whether or not the `path'
176 portion of the hashed filename is equivalent to the current
177 directory, but only if it starts with a `.'. (This catches
178 ./. and so on.) same_file () tests general Unix file
179 equivalence -- same device and inode. */
183 tail
= (char *)strrchr (path
, '/');
188 same
= same_file (".", path
, (struct stat
*)NULL
, (struct stat
*)NULL
);
192 return same
? (char *)NULL
: savestring (path
);
196 return (savestring (path
));