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>
Johannes Schindelin committed
Jul 27, 2008 at 22:34 UTC
f5d600e2dd8721d7b9177f1c82fe54822d9f1d62
1 file changed
+10
-2
help.c
+10
-2
@@ -425,17 +425,24 @@ static unsigned int list_commands_in_dir(struct cmdnames *cmds,
425
int prefix_len = strlen(prefix);
426
DIR *dir = opendir(path);
427
struct dirent *de;
428
+ struct strbuf buf = STRBUF_INIT;
429
+ int len;
430
429
- if (!dir || chdir(path))
431
+ if (!dir)
432
return 0;
433
434
+ strbuf_addf(&buf, "%s/", path);
435
+ len = buf.len;
436
+
437
while ((de = readdir(dir)) != NULL) {
438
int entlen;
439
440
if (prefixcmp(de->d_name, prefix))
441
continue;
442
438
- if (!is_executable(de->d_name))
443
+ strbuf_setlen(&buf, len);
444
+ strbuf_addstr(&buf, de->d_name);
445
+ if (!is_executable(buf.buf))
446
continue;
447
448
entlen = strlen(de->d_name) - prefix_len;
@@ -448,6 +455,7 @@ static unsigned int list_commands_in_dir(struct cmdnames *cmds,
455
add_cmdname(cmds, de->d_name + prefix_len, entlen);
456
}
457
closedir(dir);
458
+ strbuf_release(&buf);
459
460
return longest;
461
}