Jari Aalto | d166f04 | 1997-06-05 14:59:13 +0000 | [diff] [blame] | 1 | /* hashcmd.c - functions for managing a hash table mapping command names to |
| 2 | full pathnames. */ |
| 3 | |
Jari Aalto | 3185942 | 2009-01-12 13:36:28 +0000 | [diff] [blame] | 4 | /* Copyright (C) 1997-2009 Free Software Foundation, Inc. |
Jari Aalto | d166f04 | 1997-06-05 14:59:13 +0000 | [diff] [blame] | 5 | |
| 6 | This file is part of GNU Bash, the Bourne Again SHell. |
| 7 | |
Jari Aalto | 3185942 | 2009-01-12 13:36:28 +0000 | [diff] [blame] | 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. |
Jari Aalto | d166f04 | 1997-06-05 14:59:13 +0000 | [diff] [blame] | 12 | |
Jari Aalto | 3185942 | 2009-01-12 13:36:28 +0000 | [diff] [blame] | 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. |
Jari Aalto | d166f04 | 1997-06-05 14:59:13 +0000 | [diff] [blame] | 17 | |
Jari Aalto | 3185942 | 2009-01-12 13:36:28 +0000 | [diff] [blame] | 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 | */ |
Jari Aalto | d166f04 | 1997-06-05 14:59:13 +0000 | [diff] [blame] | 21 | |
| 22 | #include <config.h> |
| 23 | |
| 24 | #include "bashtypes.h" |
| 25 | #include "posixstat.h" |
| 26 | |
| 27 | #if defined (HAVE_UNISTD_H) |
| 28 | # include <unistd.h> |
| 29 | #endif |
| 30 | |
| 31 | #include "bashansi.h" |
| 32 | |
| 33 | #include "shell.h" |
Jari Aalto | cce855b | 1998-04-17 19:52:44 +0000 | [diff] [blame] | 34 | #include "findcmd.h" |
Jari Aalto | d166f04 | 1997-06-05 14:59:13 +0000 | [diff] [blame] | 35 | #include "hashcmd.h" |
| 36 | |
| 37 | extern int hashing_enabled; |
| 38 | |
Jari Aalto | 7117c2d | 2002-07-17 14:10:11 +0000 | [diff] [blame] | 39 | HASH_TABLE *hashed_filenames = (HASH_TABLE *)NULL; |
Jari Aalto | d166f04 | 1997-06-05 14:59:13 +0000 | [diff] [blame] | 40 | |
Jari Aalto | 7117c2d | 2002-07-17 14:10:11 +0000 | [diff] [blame] | 41 | static void phash_freedata __P((PTR_T)); |
Jari Aalto | d166f04 | 1997-06-05 14:59:13 +0000 | [diff] [blame] | 42 | |
| 43 | void |
Jari Aalto | 7117c2d | 2002-07-17 14:10:11 +0000 | [diff] [blame] | 44 | phash_create () |
Jari Aalto | d166f04 | 1997-06-05 14:59:13 +0000 | [diff] [blame] | 45 | { |
Jari Aalto | 7117c2d | 2002-07-17 14:10:11 +0000 | [diff] [blame] | 46 | if (hashed_filenames == 0) |
| 47 | hashed_filenames = hash_create (FILENAME_HASH_BUCKETS); |
Jari Aalto | d166f04 | 1997-06-05 14:59:13 +0000 | [diff] [blame] | 48 | } |
| 49 | |
| 50 | static void |
Jari Aalto | 7117c2d | 2002-07-17 14:10:11 +0000 | [diff] [blame] | 51 | phash_freedata (data) |
Jari Aalto | f73dda0 | 2001-11-13 17:56:06 +0000 | [diff] [blame] | 52 | PTR_T data; |
Jari Aalto | d166f04 | 1997-06-05 14:59:13 +0000 | [diff] [blame] | 53 | { |
| 54 | free (((PATH_DATA *)data)->path); |
| 55 | free (data); |
| 56 | } |
| 57 | |
| 58 | void |
Jari Aalto | 7117c2d | 2002-07-17 14:10:11 +0000 | [diff] [blame] | 59 | phash_flush () |
Jari Aalto | d166f04 | 1997-06-05 14:59:13 +0000 | [diff] [blame] | 60 | { |
| 61 | if (hashed_filenames) |
Jari Aalto | 7117c2d | 2002-07-17 14:10:11 +0000 | [diff] [blame] | 62 | hash_flush (hashed_filenames, phash_freedata); |
Jari Aalto | d166f04 | 1997-06-05 14:59:13 +0000 | [diff] [blame] | 63 | } |
| 64 | |
| 65 | /* Remove FILENAME from the table of hashed commands. */ |
Jari Aalto | 7117c2d | 2002-07-17 14:10:11 +0000 | [diff] [blame] | 66 | int |
| 67 | phash_remove (filename) |
Jari Aalto | f73dda0 | 2001-11-13 17:56:06 +0000 | [diff] [blame] | 68 | const char *filename; |
Jari Aalto | d166f04 | 1997-06-05 14:59:13 +0000 | [diff] [blame] | 69 | { |
| 70 | register BUCKET_CONTENTS *item; |
| 71 | |
| 72 | if (hashing_enabled == 0 || hashed_filenames == 0) |
Jari Aalto | 7117c2d | 2002-07-17 14:10:11 +0000 | [diff] [blame] | 73 | return 0; |
Jari Aalto | d166f04 | 1997-06-05 14:59:13 +0000 | [diff] [blame] | 74 | |
Jari Aalto | 7117c2d | 2002-07-17 14:10:11 +0000 | [diff] [blame] | 75 | item = hash_remove (filename, hashed_filenames, 0); |
Jari Aalto | d166f04 | 1997-06-05 14:59:13 +0000 | [diff] [blame] | 76 | if (item) |
| 77 | { |
| 78 | if (item->data) |
Jari Aalto | 7117c2d | 2002-07-17 14:10:11 +0000 | [diff] [blame] | 79 | phash_freedata (item->data); |
Jari Aalto | d166f04 | 1997-06-05 14:59:13 +0000 | [diff] [blame] | 80 | free (item->key); |
| 81 | free (item); |
Jari Aalto | 7117c2d | 2002-07-17 14:10:11 +0000 | [diff] [blame] | 82 | return 0; |
Jari Aalto | d166f04 | 1997-06-05 14:59:13 +0000 | [diff] [blame] | 83 | } |
Jari Aalto | 7117c2d | 2002-07-17 14:10:11 +0000 | [diff] [blame] | 84 | return 1; |
Jari Aalto | d166f04 | 1997-06-05 14:59:13 +0000 | [diff] [blame] | 85 | } |
| 86 | |
Jari Aalto | f73dda0 | 2001-11-13 17:56:06 +0000 | [diff] [blame] | 87 | /* Place FILENAME (key) and FULL_PATH (data->path) into the |
Jari Aalto | d166f04 | 1997-06-05 14:59:13 +0000 | [diff] [blame] | 88 | hash table. CHECK_DOT if non-null is for future calls to |
Jari Aalto | 7117c2d | 2002-07-17 14:10:11 +0000 | [diff] [blame] | 89 | phash_search (); it means that this file was found |
Jari Aalto | d166f04 | 1997-06-05 14:59:13 +0000 | [diff] [blame] | 90 | in a directory in $PATH that is not an absolute pathname. |
| 91 | FOUND is the initial value for times_found. */ |
| 92 | void |
Jari Aalto | 7117c2d | 2002-07-17 14:10:11 +0000 | [diff] [blame] | 93 | phash_insert (filename, full_path, check_dot, found) |
Jari Aalto | f73dda0 | 2001-11-13 17:56:06 +0000 | [diff] [blame] | 94 | char *filename, *full_path; |
Jari Aalto | d166f04 | 1997-06-05 14:59:13 +0000 | [diff] [blame] | 95 | int check_dot, found; |
| 96 | { |
| 97 | register BUCKET_CONTENTS *item; |
| 98 | |
| 99 | if (hashing_enabled == 0) |
| 100 | return; |
| 101 | |
Jari Aalto | 7117c2d | 2002-07-17 14:10:11 +0000 | [diff] [blame] | 102 | if (hashed_filenames == 0) |
| 103 | phash_create (); |
Jari Aalto | d166f04 | 1997-06-05 14:59:13 +0000 | [diff] [blame] | 104 | |
Jari Aalto | 7117c2d | 2002-07-17 14:10:11 +0000 | [diff] [blame] | 105 | item = hash_insert (filename, hashed_filenames, 0); |
Jari Aalto | d166f04 | 1997-06-05 14:59:13 +0000 | [diff] [blame] | 106 | if (item->data) |
| 107 | free (pathdata(item)->path); |
| 108 | else |
| 109 | { |
| 110 | item->key = savestring (filename); |
Jari Aalto | 7117c2d | 2002-07-17 14:10:11 +0000 | [diff] [blame] | 111 | item->data = xmalloc (sizeof (PATH_DATA)); |
Jari Aalto | d166f04 | 1997-06-05 14:59:13 +0000 | [diff] [blame] | 112 | } |
Jari Aalto | f73dda0 | 2001-11-13 17:56:06 +0000 | [diff] [blame] | 113 | pathdata(item)->path = savestring (full_path); |
Jari Aalto | d166f04 | 1997-06-05 14:59:13 +0000 | [diff] [blame] | 114 | pathdata(item)->flags = 0; |
| 115 | if (check_dot) |
| 116 | pathdata(item)->flags |= HASH_CHKDOT; |
Jari Aalto | f73dda0 | 2001-11-13 17:56:06 +0000 | [diff] [blame] | 117 | if (*full_path != '/') |
Jari Aalto | d166f04 | 1997-06-05 14:59:13 +0000 | [diff] [blame] | 118 | pathdata(item)->flags |= HASH_RELPATH; |
| 119 | item->times_found = found; |
| 120 | } |
| 121 | |
| 122 | /* Return the full pathname that FILENAME hashes to. If FILENAME |
| 123 | is hashed, but (data->flags & HASH_CHKDOT) is non-zero, check |
| 124 | ./FILENAME and return that if it is executable. This always |
| 125 | returns a newly-allocated string; the caller is responsible |
| 126 | for freeing it. */ |
| 127 | char * |
Jari Aalto | 7117c2d | 2002-07-17 14:10:11 +0000 | [diff] [blame] | 128 | phash_search (filename) |
Jari Aalto | f73dda0 | 2001-11-13 17:56:06 +0000 | [diff] [blame] | 129 | const char *filename; |
Jari Aalto | d166f04 | 1997-06-05 14:59:13 +0000 | [diff] [blame] | 130 | { |
| 131 | register BUCKET_CONTENTS *item; |
| 132 | char *path, *dotted_filename, *tail; |
| 133 | int same; |
| 134 | |
| 135 | if (hashing_enabled == 0 || hashed_filenames == 0) |
| 136 | return ((char *)NULL); |
| 137 | |
Jari Aalto | 7117c2d | 2002-07-17 14:10:11 +0000 | [diff] [blame] | 138 | item = hash_search (filename, hashed_filenames, 0); |
Jari Aalto | d166f04 | 1997-06-05 14:59:13 +0000 | [diff] [blame] | 139 | |
| 140 | if (item == NULL) |
| 141 | return ((char *)NULL); |
| 142 | |
| 143 | /* If this filename is hashed, but `.' comes before it in the path, |
| 144 | see if ./filename is executable. If the hashed value is not an |
| 145 | absolute pathname, see if ./`hashed-value' exists. */ |
| 146 | path = pathdata(item)->path; |
| 147 | if (pathdata(item)->flags & (HASH_CHKDOT|HASH_RELPATH)) |
| 148 | { |
Jari Aalto | f73dda0 | 2001-11-13 17:56:06 +0000 | [diff] [blame] | 149 | tail = (pathdata(item)->flags & HASH_RELPATH) ? path : (char *)filename; /* XXX - fix const later */ |
Jari Aalto | cce855b | 1998-04-17 19:52:44 +0000 | [diff] [blame] | 150 | /* If the pathname does not start with a `./', add a `./' to it. */ |
| 151 | if (tail[0] != '.' || tail[1] != '/') |
| 152 | { |
Jari Aalto | f73dda0 | 2001-11-13 17:56:06 +0000 | [diff] [blame] | 153 | dotted_filename = (char *)xmalloc (3 + strlen (tail)); |
Jari Aalto | cce855b | 1998-04-17 19:52:44 +0000 | [diff] [blame] | 154 | dotted_filename[0] = '.'; dotted_filename[1] = '/'; |
| 155 | strcpy (dotted_filename + 2, tail); |
| 156 | } |
| 157 | else |
| 158 | dotted_filename = savestring (tail); |
Jari Aalto | d166f04 | 1997-06-05 14:59:13 +0000 | [diff] [blame] | 159 | |
| 160 | if (executable_file (dotted_filename)) |
| 161 | return (dotted_filename); |
| 162 | |
| 163 | free (dotted_filename); |
| 164 | |
| 165 | #if 0 |
| 166 | if (pathdata(item)->flags & HASH_RELPATH) |
| 167 | return ((char *)NULL); |
| 168 | #endif |
| 169 | |
| 170 | /* Watch out. If this file was hashed to "./filename", and |
| 171 | "./filename" is not executable, then return NULL. */ |
| 172 | |
| 173 | /* Since we already know "./filename" is not executable, what |
| 174 | we're really interested in is whether or not the `path' |
| 175 | portion of the hashed filename is equivalent to the current |
| 176 | directory, but only if it starts with a `.'. (This catches |
| 177 | ./. and so on.) same_file () tests general Unix file |
| 178 | equivalence -- same device and inode. */ |
| 179 | if (*path == '.') |
| 180 | { |
| 181 | same = 0; |
| 182 | tail = (char *)strrchr (path, '/'); |
| 183 | |
| 184 | if (tail) |
| 185 | { |
| 186 | *tail = '\0'; |
| 187 | same = same_file (".", path, (struct stat *)NULL, (struct stat *)NULL); |
| 188 | *tail = '/'; |
| 189 | } |
| 190 | |
| 191 | return same ? (char *)NULL : savestring (path); |
| 192 | } |
| 193 | } |
| 194 | |
| 195 | return (savestring (path)); |
| 196 | } |