summaryrefslogtreecommitdiff
path: root/help.c
diff options
context:
space:
mode:
authorJohannes Schindelin <Johannes.Schindelin@gmx.de>2008-07-27 20:34:14 (GMT)
committerJunio C Hamano <gitster@pobox.com>2008-07-27 21:14:38 (GMT)
commitf5d600e2dd8721d7b9177f1c82fe54822d9f1d62 (patch)
tree111721373283921eb86bc684b7f766057719d5fb /help.c
parentb6f637d199fed238179865a38f180fccc37ee8cc (diff)
downloadgit-f5d600e2dd8721d7b9177f1c82fe54822d9f1d62.zip
git-f5d600e2dd8721d7b9177f1c82fe54822d9f1d62.tar.gz
git-f5d600e2dd8721d7b9177f1c82fe54822d9f1d62.tar.bz2
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>
Diffstat (limited to 'help.c')
-rw-r--r--help.c12
1 files changed, 10 insertions, 2 deletions
diff --git a/help.c b/help.c
index bfc84ae..3cb1962 100644
--- a/help.c
+++ b/help.c
@@ -425,17 +425,24 @@ static unsigned int list_commands_in_dir(struct cmdnames *cmds,
int prefix_len = strlen(prefix);
DIR *dir = opendir(path);
struct dirent *de;
+ struct strbuf buf = STRBUF_INIT;
+ int len;
- if (!dir || chdir(path))
+ if (!dir)
return 0;
+ strbuf_addf(&buf, "%s/", path);
+ len = buf.len;
+
while ((de = readdir(dir)) != NULL) {
int entlen;
if (prefixcmp(de->d_name, prefix))
continue;
- if (!is_executable(de->d_name))
+ strbuf_setlen(&buf, len);
+ strbuf_addstr(&buf, de->d_name);
+ if (!is_executable(buf.buf))
continue;
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);
}
closedir(dir);
+ strbuf_release(&buf);
return longest;
}