Imported from ../bash-2.01.tar.gz.
[platform/upstream/bash.git] / hashcmd.c
1 /* hashcmd.c - functions for managing a hash table mapping command names to
2                full pathnames. */
3
4 /* Copyright (C) 1997 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 it under
9     the terms of the GNU General Public License as published by the Free
10     Software Foundation; either version 1, or (at your option) any later
11     version.
12
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
16     for more details.
17
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, 675 Mass Ave, Cambridge, MA 02139, USA. */
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"
34 #include "execute_cmd.h"
35 #include "hashcmd.h"
36
37 extern int hashing_enabled;
38
39 static int hashing_initialized = 0;
40
41 HASH_TABLE *hashed_filenames;
42
43 void
44 initialize_filename_hashing ()
45 {
46   if (hashing_initialized == 0)
47     {
48       hashed_filenames = make_hash_table (FILENAME_HASH_BUCKETS);
49       hashing_initialized = 1;
50     }
51 }
52
53 static void
54 free_filename_data (data)
55      char *data;
56 {
57   free (((PATH_DATA *)data)->path);
58   free (data);
59 }
60
61 void
62 flush_hashed_filenames ()
63 {
64   if (hashed_filenames)
65     flush_hash_table (hashed_filenames, free_filename_data);
66 }
67
68 /* Remove FILENAME from the table of hashed commands. */
69 void
70 remove_hashed_filename (filename)
71      char *filename;
72 {
73   register BUCKET_CONTENTS *item;
74
75   if (hashing_enabled == 0 || hashed_filenames == 0)
76     return;
77
78   item = remove_hash_item (filename, hashed_filenames);
79   if (item)
80     {
81       if (item->data)
82         free_filename_data (item->data);
83       free (item->key);
84       free (item);
85     }
86 }
87
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. */
93 void
94 remember_filename (filename, full_pathname, check_dot, found)
95      char *filename, *full_pathname;
96      int check_dot, found;
97 {
98   register BUCKET_CONTENTS *item;
99
100   if (hashing_enabled == 0)
101     return;
102
103   if (hashed_filenames == 0 || hashing_initialized == 0)
104     initialize_filename_hashing ();
105
106   item = add_hash_item (filename, hashed_filenames);
107   if (item->data)
108     free (pathdata(item)->path);
109   else
110     {
111       item->key = savestring (filename);
112       item->data = xmalloc (sizeof (PATH_DATA));
113     }
114   pathdata(item)->path = savestring (full_pathname);
115   pathdata(item)->flags = 0;
116   if (check_dot)
117     pathdata(item)->flags |= HASH_CHKDOT;
118   if (*full_pathname != '/')
119     pathdata(item)->flags |= HASH_RELPATH;
120   item->times_found = found;
121 }
122
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
127    for freeing it. */
128 char *
129 find_hashed_filename (filename)
130      char *filename;
131 {
132   register BUCKET_CONTENTS *item;
133   char *path, *dotted_filename, *tail;
134   int same;
135
136   if (hashing_enabled == 0 || hashed_filenames == 0)
137     return ((char *)NULL);
138
139   item = find_hash_item (filename, hashed_filenames);
140
141   if (item == NULL)
142     return ((char *)NULL);
143
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))
149     {
150       tail = (pathdata(item)->flags & HASH_RELPATH) ? path : filename;
151       dotted_filename = xmalloc (3 + strlen (tail));
152       dotted_filename[0] = '.'; dotted_filename[1] = '/';
153       strcpy (dotted_filename + 2, tail);
154
155       if (executable_file (dotted_filename))
156         return (dotted_filename);
157
158       free (dotted_filename);
159
160 #if 0
161       if (pathdata(item)->flags & HASH_RELPATH)
162         return ((char *)NULL);
163 #endif
164
165       /* Watch out.  If this file was hashed to "./filename", and
166          "./filename" is not executable, then return NULL. */
167
168       /* Since we already know "./filename" is not executable, what
169          we're really interested in is whether or not the `path'
170          portion of the hashed filename is equivalent to the current
171          directory, but only if it starts with a `.'.  (This catches
172          ./. and so on.)  same_file () tests general Unix file
173          equivalence -- same device and inode. */
174       if (*path == '.')
175         {
176           same = 0;
177           tail = (char *)strrchr (path, '/');
178
179           if (tail)
180             {
181               *tail = '\0';
182               same = same_file (".", path, (struct stat *)NULL, (struct stat *)NULL);
183               *tail = '/';
184             }
185
186           return same ? (char *)NULL : savestring (path);
187         }
188     }
189
190   return (savestring (path));
191 }