@matheusbsbs / matheus-bagliano / commits / 0966003c8e
Fork 0

list_commands(): simplify code by using chdir()

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>

Scott R Parish committed Oct 27, 2007 at 01:36 UTC 0966003c8e8d1528912b10667b903cd981e3a7f6
1 file changed +3 -15
help.c
+3 -15
@@ -96,36 +96,24 @@ static void pretty_print_string_list(struct cmdname **cmdname, int longest)
96 static void list_commands(const char *exec_path)
97 {
98 unsigned int longest = 0;
99 - char path[PATH_MAX];
99 const char *prefix = "git-";
100 int prefix_len = strlen(prefix);
102 - int dirlen;
101 DIR *dir = opendir(exec_path);
102 struct dirent *de;
103
106 - if (!dir) {
104 + if (!dir || chdir(exec_path)) {
105 fprintf(stderr, "git: '%s': %s\n", exec_path, strerror(errno));
106 exit(1);
107 }
108
111 - dirlen = strlen(exec_path);
112 - if (PATH_MAX - 20 < dirlen) {
113 - fprintf(stderr, "git: insanely long exec-path '%s'\n",
114 - exec_path);
115 - exit(1);
116 - }
117 -
118 - memcpy(path, exec_path, dirlen);
119 - path[dirlen++] = '/';
120 -
109 while ((de = readdir(dir)) != NULL) {
110 struct stat st;
111 int entlen;
112
113 if (prefixcmp(de->d_name, prefix))
114 continue;
127 - strcpy(path+dirlen, de->d_name);
128 - if (stat(path, &st) || /* stat, not lstat */
115 +
116 + if (stat(de->d_name, &st) || /* stat, not lstat */
117 !S_ISREG(st.st_mode) ||
118 !(st.st_mode & S_IXUSR))
119 continue;