Avoid chdir() in list_commands_in_dir()

The function list_commands_in_dir() tried to be lazy and just chdir()
to the directory which entries it listed, so that the check if the
file is executable could be done on dir->d_name.

However, there is no good reason to jump around wildly just to find
all Git commands.

Instead, have a strbuf and construct the full path dynamically.

Signed-off-by: Johannes Schindelin <johannes.schindelin@gmx.de>
Signed-off-by: Junio C Hamano <gitster@pobox.com>
maint
Johannes Schindelin 2008-07-27 22:34:14 +02:00 committed by Junio C Hamano
parent b6f637d199
commit f5d600e2dd
1 changed files with 10 additions and 2 deletions

12
help.c
View File

@ -425,17 +425,24 @@ static unsigned int list_commands_in_dir(struct cmdnames *cmds,
int prefix_len = strlen(prefix); int prefix_len = strlen(prefix);
DIR *dir = opendir(path); DIR *dir = opendir(path);
struct dirent *de; struct dirent *de;
struct strbuf buf = STRBUF_INIT;
int len;


if (!dir || chdir(path)) if (!dir)
return 0; return 0;


strbuf_addf(&buf, "%s/", path);
len = buf.len;

while ((de = readdir(dir)) != NULL) { while ((de = readdir(dir)) != NULL) {
int entlen; int entlen;


if (prefixcmp(de->d_name, prefix)) if (prefixcmp(de->d_name, prefix))
continue; continue;


if (!is_executable(de->d_name)) strbuf_setlen(&buf, len);
strbuf_addstr(&buf, de->d_name);
if (!is_executable(buf.buf))
continue; continue;


entlen = strlen(de->d_name) - prefix_len; entlen = strlen(de->d_name) - prefix_len;
@ -448,6 +455,7 @@ static unsigned int list_commands_in_dir(struct cmdnames *cmds,
add_cmdname(cmds, de->d_name + prefix_len, entlen); add_cmdname(cmds, de->d_name + prefix_len, entlen);
} }
closedir(dir); closedir(dir);
strbuf_release(&buf);


return longest; return longest;
} }