aboutsummaryrefslogtreecommitdiff
path: root/bin/sh/exec.c
diff options
context:
space:
mode:
Diffstat (limited to 'bin/sh/exec.c')
-rw-r--r--bin/sh/exec.c56
1 files changed, 17 insertions, 39 deletions
diff --git a/bin/sh/exec.c b/bin/sh/exec.c
index cd495895e03d..7a6dbae4172e 100644
--- a/bin/sh/exec.c
+++ b/bin/sh/exec.c
@@ -78,7 +78,6 @@ __FBSDID("$FreeBSD$");
#define CMDTABLESIZE 31 /* should be prime */
-#define ARB 1 /* actual size determined at run time */
@@ -86,13 +85,13 @@ struct tblentry {
struct tblentry *next; /* next entry in hash chain */
union param param; /* definition of builtin function */
int special; /* flag for special builtin commands */
- short cmdtype; /* index identifying command */
- char rehash; /* if set, cd done since entry created */
- char cmdname[ARB]; /* name of command */
+ signed char cmdtype; /* index identifying command */
+ char cmdname[]; /* name of command */
};
static struct tblentry *cmdtable[CMDTABLESIZE];
+static int cmdtable_cd = 0; /* cmdtable contains cd-dependent entries */
int exerrno = 0; /* Last exec error */
@@ -306,8 +305,6 @@ printentry(struct tblentry *cmdp, int verbose)
error("internal error: cmdtype %d", cmdp->cmdtype);
#endif
}
- if (cmdp->rehash)
- out1c('*');
out1c('\n');
}
@@ -324,12 +321,12 @@ find_command(const char *name, struct cmdentry *entry, int act,
{
struct tblentry *cmdp, loc_cmd;
int idx;
- int prev;
char *fullname;
struct stat statb;
int e;
int i;
int spec;
+ int cd;
/* If name contains a slash, don't use the hash table */
if (strchr(name, '/') != NULL) {
@@ -338,8 +335,10 @@ find_command(const char *name, struct cmdentry *entry, int act,
return;
}
+ cd = 0;
+
/* If name is in the table, and not invalidated by cd, we're done */
- if ((cmdp = cmdlookup(name, 0)) != NULL && cmdp->rehash == 0) {
+ if ((cmdp = cmdlookup(name, 0)) != NULL) {
if (cmdp->cmdtype == CMDFUNCTION && act & DO_NOFUNC)
cmdp = NULL;
else
@@ -360,13 +359,6 @@ find_command(const char *name, struct cmdentry *entry, int act,
}
/* We have to search path. */
- prev = -1; /* where to start */
- if (cmdp) { /* doing a rehash */
- if (cmdp->cmdtype == CMDBUILTIN)
- prev = -1;
- else
- prev = cmdp->param.index;
- }
e = ENOENT;
idx = -1;
@@ -381,13 +373,8 @@ loop:
goto loop; /* ignore unimplemented options */
}
}
- /* if rehash, don't redo absolute path names */
- if (fullname[0] == '/' && idx <= prev) {
- if (idx < prev)
- goto loop;
- TRACE(("searchexec \"%s\": no change\n", name));
- goto success;
- }
+ if (fullname[0] != '/')
+ cd = 1;
if (stat(fullname, &statb) < 0) {
if (errno != ENOENT && errno != ENOTDIR)
e = errno;
@@ -427,9 +414,6 @@ loop:
goto success;
}
- /* We failed. If there was an entry for this command, delete it */
- if (cmdp && cmdp->cmdtype != CMDFUNCTION)
- delete_cmd_entry();
if (act & DO_ERR) {
if (e == ENOENT || e == ENOTDIR)
outfmt(out2, "%s: not found\n", name);
@@ -441,7 +425,8 @@ loop:
return;
success:
- cmdp->rehash = 0;
+ if (cd)
+ cmdtable_cd = 1;
entry->cmdtype = cmdp->cmdtype;
entry->u = cmdp->param;
entry->special = cmdp->special;
@@ -470,22 +455,15 @@ find_builtin(const char *name, int *special)
/*
- * Called when a cd is done. Marks all commands so the next time they
- * are executed they will be rehashed.
+ * Called when a cd is done. If any entry in cmdtable depends on the current
+ * directory, simply clear cmdtable completely.
*/
void
hashcd(void)
{
- struct tblentry **pp;
- struct tblentry *cmdp;
-
- for (pp = cmdtable ; pp < &cmdtable[CMDTABLESIZE] ; pp++) {
- for (cmdp = *pp ; cmdp ; cmdp = cmdp->next) {
- if (cmdp->cmdtype == CMDNORMAL)
- cmdp->rehash = 1;
- }
- }
+ if (cmdtable_cd)
+ clearcmdentry();
}
@@ -527,6 +505,7 @@ clearcmdentry(void)
}
}
}
+ cmdtable_cd = 0;
INTON;
}
@@ -563,11 +542,10 @@ cmdlookup(const char *name, int add)
}
if (add && cmdp == NULL) {
INTOFF;
- cmdp = *pp = ckmalloc(sizeof (struct tblentry) - ARB
+ cmdp = *pp = ckmalloc(sizeof (struct tblentry)
+ strlen(name) + 1);
cmdp->next = NULL;
cmdp->cmdtype = CMDUNKNOWN;
- cmdp->rehash = 0;
strcpy(cmdp->cmdname, name);
INTON;
}