test-lib.sh: Add new function, test_expect_code
[gitweb.git] / git.c
diff --git a/git.c b/git.c
index b9b8c62f47cc54ef03161e61569dcd0d7b259828..878c359704dca8591755a68ff5a826503c017b3e 100644 (file)
--- a/git.c
+++ b/git.c
@@ -59,7 +59,8 @@ static void add_cmdname(const char *name, int len)
        if (!ent)
                oom();
        ent->len = len;
-       memcpy(ent->name, name, len+1);
+       memcpy(ent->name, name, len);
+       ent->name[len] = 0;
        cmdname[cmdname_cnt++] = ent;
 }
 
@@ -132,6 +133,8 @@ static void list_commands(const char *exec_path, const char *pattern)
                        continue;
 
                entlen = strlen(de->d_name);
+               if (4 < entlen && !strcmp(de->d_name + entlen - 4, ".exe"))
+                       entlen -= 4;
 
                if (longest < entlen)
                        longest = entlen;
@@ -270,7 +273,7 @@ int main(int argc, char **argv, char **envp)
                while (!strncmp(exec_path, "./", 2)) {
                        exec_path += 2;
                        while (*exec_path == '/')
-                               *exec_path++;
+                               exec_path++;
                }
                snprintf(git_command + len, sizeof(git_command) - len,
                         "/%s", exec_path);
@@ -280,16 +283,21 @@ int main(int argc, char **argv, char **envp)
        len = strlen(git_command);
        prepend_to_path(git_command, len);
 
-       strncat(&git_command[len], "/git-", sizeof(git_command) - len);
-       len += 5;
-       strncat(&git_command[len], argv[i], sizeof(git_command) - len);
-
-       if (access(git_command, X_OK))
-               usage(exec_path, "'%s' is not a git-command", argv[i]);
+       len += snprintf(git_command + len, sizeof(git_command) - len,
+                       "/git-%s", argv[i]);
+       if (sizeof(git_command) <= len) {
+               fprintf(stderr, "git: command name given is too long (%d)\n", len);
+               exit(1);
+       }
 
        /* execve() can only ever return if it fails */
        execve(git_command, &argv[i], envp);
-       printf("Failed to run command '%s': %s\n", git_command, strerror(errno));
+
+       if (errno == ENOENT)
+               usage(exec_path, "'%s' is not a git-command", argv[i]);
+
+       fprintf(stderr, "Failed to run command '%s': %s\n",
+               git_command, strerror(errno));
 
        return 1;
 }