Added documentation for few missing options.
[git.git] / git.c
diff --git a/git.c b/git.c
index b9b8c62..c26cac6 100644 (file)
--- a/git.c
+++ b/git.c
@@ -8,6 +8,7 @@
 #include <errno.h>
 #include <limits.h>
 #include <stdarg.h>
+#include "git-compat-util.h"
 
 #ifndef PATH_MAX
 # define PATH_MAX 4096
@@ -59,7 +60,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 +134,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;
@@ -149,10 +153,10 @@ static void list_commands(const char *exec_path, const char *pattern)
 }
 
 #ifdef __GNUC__
-static void usage(const char *exec_path, const char *fmt, ...)
+static void cmd_usage(const char *exec_path, const char *fmt, ...)
        __attribute__((__format__(__printf__, 2, 3), __noreturn__));
 #endif
-static void usage(const char *exec_path, const char *fmt, ...)
+static void cmd_usage(const char *exec_path, const char *fmt, ...)
 {
        if (fmt) {
                va_list ap;
@@ -185,7 +189,6 @@ static void prepend_to_path(const char *dir, int len)
        path_len = len + strlen(old_path) + 1;
 
        path = malloc(path_len + 1);
-       path[path_len + 1] = '\0';
 
        memcpy(path, dir, len);
        path[len] = ':';
@@ -248,12 +251,12 @@ int main(int argc, char **argv, char **envp)
                else if (!strcmp(arg, "help"))
                        show_help = 1;
                else if (!show_help)
-                       usage(NULL, NULL);
+                       cmd_usage(NULL, NULL);
        }
 
        if (i >= argc || show_help) {
                if (i >= argc)
-                       usage(exec_path, NULL);
+                       cmd_usage(exec_path, NULL);
 
                show_man_page(argv[i]);
        }
@@ -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.\n");
+               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)
+               cmd_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;
 }