]> granicus.if.org Git - sudo/commitdiff
Fix line wrapping in usage() and use the actual tty width instead of
authorTodd C. Miller <Todd.Miller@courtesan.com>
Sat, 18 Aug 2007 12:22:16 +0000 (12:22 +0000)
committerTodd C. Miller <Todd.Miller@courtesan.com>
Sat, 18 Aug 2007 12:22:16 +0000 (12:22 +0000)
assuming 80.

parse.c
sudo.c
sudo.h

diff --git a/parse.c b/parse.c
index 98a38a1c8e92e1cb0bc3cf583d5ec4c782dd107d..6db0d15bd7c7ddede60dfa9ba8be06461cab1f7f 100644 (file)
--- a/parse.c
+++ b/parse.c
@@ -64,7 +64,6 @@ extern struct defaults *defaults;
 static void print_member       __P((char *, int, int, int));
 static void display_defaults   __P((struct passwd *));
 static void display_bound_defaults __P((int));
-static int  get_ttycols                __P((void));
 static void print_wrap         __P((int, int, int, ...));
 
 #define        print_def(a)            print_wrap(4, 0, 1, a);
@@ -491,7 +490,7 @@ print_member(name, type, negated, alias_type)
 # define ts_cols       ws_col
 #endif
 
-static int
+int
 get_ttycols()
 {
     char *p;
diff --git a/sudo.c b/sudo.c
index de730ea4ff9bd1ad35e8cdbd1c1c3c2f40531a99..2b0c626f379e2a83bef2b0bd33be75fc4822834c 100644 (file)
--- a/sudo.c
+++ b/sudo.c
@@ -1318,13 +1318,21 @@ usage(exit_val)
     int exit_val;
 {
     char **p, **uvec[5];
-    int i, linelen, linemax, ulen;
+    int i, linelen, linemax, ulen, plen;
     static char *uvec1[] = {
-       " -h | -K | -k | -L | -V | -v",
+       " -h |",
+       " -K |",
+       " -k |",
+       " -L |",
+       " -V |",
+       " -v",
        NULL
     };
     static char *uvec2[] = {
-       " -l [-U username] [-u username|#uid] [command]",
+       " -l",
+       " [-U username]",
+       " [-u username|#uid]",
+       " [command]",
        NULL
     };
     static char *uvec3[] = {
@@ -1373,20 +1381,22 @@ usage(exit_val)
     }
 
     /*
-     * Print usage and wrap lines as needed.
-     * Assumes an 80-character wide terminal, which is kind of bogus...
+     * Print usage and wrap lines as needed, depending on the
+     * tty width.
      */
     ulen = (int)strlen(getprogname()) + 7;
-    linemax = 80;
+    linemax = get_ttycols();
     for (i = 0; uvec[i] != NULL; i++) {
-       linelen = linemax - ulen;
        printf("usage: %s", getprogname());
+       linelen = linemax - ulen;
        for (p = uvec[i]; *p != NULL; p++) {
-           if (linelen == linemax || (linelen -= strlen(*p)) >= 0) {
+           plen = (int)strlen(*p);
+           if (linelen >= plen || linelen == linemax - ulen) {
                fputs(*p, stdout);
+               linelen -= plen;
            } else {
                p--;
-               linelen = linemax;
+               linelen = linemax - ulen;
                printf("\n%*s", ulen, "");
            }
        }
diff --git a/sudo.h b/sudo.h
index 31791f6dcb7bface1afb5be16cdf1e8addb87d19..659da3a74bafd0a96d8fa5688f6ea94e1605359c 100644 (file)
--- a/sudo.h
+++ b/sudo.h
@@ -264,6 +264,7 @@ int gettime         __P((struct timespec *));
 FILE *open_sudoers     __P((const char *, int *));
 void display_privs     __P((VOID *, struct passwd *));
 int display_cmnd       __P((VOID *, struct passwd *));
+int get_ttycols                __P((void));
 void sudo_setenv       __P((const char *, const char *, int));
 void sudo_unsetenv     __P((const char *));
 void sudo_setgrent     __P((void));