git-init-db: initialize shared repositories with --shared
[git.git] / git.c
diff --git a/git.c b/git.c
index 619f25a..e795ddb 100644 (file)
--- a/git.c
+++ b/git.c
@@ -8,15 +8,13 @@
 #include <errno.h>
 #include <limits.h>
 #include <stdarg.h>
+#include <sys/ioctl.h>
+#include "git-compat-util.h"
 
 #ifndef PATH_MAX
 # define PATH_MAX 4096
 #endif
 
-#ifdef NO_SETENV
-extern int gitsetenv(char *name, char *value, int overwrite);
-#endif
-
 static const char git_usage[] =
        "Usage: git [--version] [--exec-path[=GIT_EXEC_PATH]] [--help] COMMAND [ ARGS ]";
 
@@ -29,6 +27,16 @@ static int term_columns(void)
        if (col_string && (n_cols = atoi(col_string)) > 0)
                return n_cols;
 
+#ifdef TIOCGWINSZ
+       {
+               struct winsize ws;
+               if (!ioctl(1, TIOCGWINSZ, &ws)) {
+                       if (ws.ws_col)
+                               return ws.ws_col;
+               }
+       }
+#endif
+
        return 80;
 }
 
@@ -77,25 +85,28 @@ static int cmdname_compare(const void *a_, const void *b_)
 
 static void pretty_print_string_list(struct cmdname **cmdname, int longest)
 {
-       int cols = 1;
+       int cols = 1, rows;
        int space = longest + 1; /* min 1 SP between words */
        int max_cols = term_columns() - 1; /* don't print *on* the edge */
-       int i;
+       int i, j;
 
        if (space < max_cols)
                cols = max_cols / space;
+       rows = (cmdname_cnt + cols - 1) / cols;
 
        qsort(cmdname, cmdname_cnt, sizeof(*cmdname), cmdname_compare);
 
-       for (i = 0; i < cmdname_cnt; ) {
-               int c;
+       for (i = 0; i < rows; i++) {
                printf("  ");
 
-               for (c = cols; c && i < cmdname_cnt; i++) {
-                       printf("%s", cmdname[i]->name);
-
-                       if (--c)
-                               mput_char(' ', space - cmdname[i]->len);
+               for (j = 0; j < cols; j++) {
+                       int n = j * rows + i;
+                       int size = space;
+                       if (n >= cmdname_cnt)
+                               break;
+                       if (j == cols-1 || n + rows >= cmdname_cnt)
+                               size = 1;
+                       printf("%-*s", size, cmdname[n]->name);
                }
                putchar('\n');
        }
@@ -156,10 +167,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;
@@ -192,7 +203,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] = ':';
@@ -255,12 +265,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]);
        }
@@ -268,7 +278,7 @@ int main(int argc, char **argv, char **envp)
        if (*exec_path != '/') {
                if (!getcwd(git_command, sizeof(git_command))) {
                        fprintf(stderr,
-                               "git: cannot determine current directory");
+                               "git: cannot determine current directory\n");
                        exit(1);
                }
                len = strlen(git_command);
@@ -290,7 +300,7 @@ int main(int argc, char **argv, char **envp)
        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);
+               fprintf(stderr, "git: command name given is too long.\n");
                exit(1);
        }
 
@@ -298,7 +308,7 @@ int main(int argc, char **argv, char **envp)
        execve(git_command, &argv[i], envp);
 
        if (errno == ENOENT)
-               usage(exec_path, "'%s' is not a git-command", argv[i]);
+               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));