Skip to content

Commit

Permalink
list_commands(): simplify code by using chdir()
Browse files Browse the repository at this point in the history
The current code builds absolute path strings for each file to
stat(), this can easily be avoided by chdir()ing into the directory.

Signed-off-by: Scott R Parish <srp@srparish.net>
Signed-off-by: Junio C Hamano <gitster@pobox.com>
  • Loading branch information
srp authored and gitster committed Oct 30, 2007
1 parent 384df83 commit 0966003
Showing 1 changed file with 3 additions and 15 deletions.
18 changes: 3 additions & 15 deletions help.c
Expand Up @@ -96,36 +96,24 @@ static void pretty_print_string_list(struct cmdname **cmdname, int longest)
static void list_commands(const char *exec_path)
{
unsigned int longest = 0;
char path[PATH_MAX];
const char *prefix = "git-";
int prefix_len = strlen(prefix);
int dirlen;
DIR *dir = opendir(exec_path);
struct dirent *de;

if (!dir) {
if (!dir || chdir(exec_path)) {
fprintf(stderr, "git: '%s': %s\n", exec_path, strerror(errno));
exit(1);
}

dirlen = strlen(exec_path);
if (PATH_MAX - 20 < dirlen) {
fprintf(stderr, "git: insanely long exec-path '%s'\n",
exec_path);
exit(1);
}

memcpy(path, exec_path, dirlen);
path[dirlen++] = '/';

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

if (prefixcmp(de->d_name, prefix))
continue;
strcpy(path+dirlen, de->d_name);
if (stat(path, &st) || /* stat, not lstat */

if (stat(de->d_name, &st) || /* stat, not lstat */
!S_ISREG(st.st_mode) ||
!(st.st_mode & S_IXUSR))
continue;
Expand Down

0 comments on commit 0966003

Please sign in to comment.