]> granicus.if.org Git - sudo/commitdiff
Use number of tty columns that is passed in user_info instead of
authorTodd C. Miller <Todd.Miller@courtesan.com>
Thu, 18 Mar 2010 01:25:32 +0000 (21:25 -0400)
committerTodd C. Miller <Todd.Miller@courtesan.com>
Thu, 18 Mar 2010 01:25:32 +0000 (21:25 -0400)
getting it directly in the lbuf code.

include/lbuf.h
plugins/sudoers/sudo_nss.c
plugins/sudoers/sudoers.c
plugins/sudoers/sudoers.h
src/lbuf.c
src/parse_args.c
src/sudo.c

index 76ca0f2239e00545eb1abd50cc2d8fc637d0ace0..302918aad94359924b3ebeaa8782286907c752d4 100644 (file)
@@ -28,9 +28,10 @@ struct lbuf {
     int indent;
     int len;
     int size;
+    int cols;
 };
 
-void lbuf_init         (struct lbuf *, char *, int, int);
+void lbuf_init         (struct lbuf *, char *, int, int, int);
 void lbuf_destroy      (struct lbuf *);
 void lbuf_append       (struct lbuf *, ...);
 void lbuf_append_quoted        (struct lbuf *, const char *, ...);
index c474e9a42972dca876a535df8bf5c696c605e7e1..c6e905a4e040b0c375461405f3f35b6378fa1992 100644 (file)
@@ -240,7 +240,7 @@ display_privs(snl, pw)
     /* Reset group vector so group matching works correctly. */
     reset_groups(pw);
 
-    lbuf_init(&lbuf, NULL, 4, 0);
+    lbuf_init(&lbuf, NULL, 4, 0, sudo_user.cols);
 
     /* Display defaults from all sources. */
     count = 0;
index b86775dc8d2a5b1c27d3fd67cbcc18a67c924a67..7820135a2ab7d85940ce7b3a8fac8a945f6d628c 100644 (file)
@@ -1280,6 +1280,14 @@ deserialize_info(char * const settings[], char * const user_info[])
                user_shost = estrndup(user_host, (size_t)(p - user_host));
            continue;
        }
+       if (MATCHES(*cur, "lines=")) {
+           sudo_user.lines = atoi(*cur + sizeof("lines=") - 1);
+           continue;
+       }
+       if (MATCHES(*cur, "cols=")) {
+           sudo_user.cols = atoi(*cur + sizeof("cols=") - 1);
+           continue;
+       }
     }
 
 #undef MATCHES
index 147323000bdfd21f88a856a3570236be93ffc8fa..46a3c78473d0190eca566f069212984bbe3ca76d 100644 (file)
@@ -65,6 +65,8 @@ struct sudo_user {
     int   ngroups;
     uid_t uid;
     uid_t gid;
+    int   lines;
+    int   cols;
     GETGROUPS_T *groups;
     struct list_member *env_vars;
 #ifdef HAVE_SELINUX
index 8f46876332112893db14d0b7f0e6e7ffd50f3c4b..148278ded66ddc3114928aa947b3f25d27588982 100644 (file)
 #include "missing.h"
 #include "lbuf.h"
 
-#if !defined(TIOCGSIZE) && defined(TIOCGWINSZ)
-# define TIOCGSIZE     TIOCGWINSZ
-# define ttysize       winsize
-# define ts_cols       ws_col
-#endif
-
-int
-get_ttycols(void)
-{
-    char *p;
-    int cols;
-#ifdef TIOCGSIZE
-    struct ttysize win;
-
-    if (ioctl(STDERR_FILENO, TIOCGSIZE, &win) == 0 && win.ts_cols != 0)
-       return((int)win.ts_cols);
-#endif
-
-    /* Fall back on $COLUMNS. */
-    if ((p = getenv("COLUMNS")) == NULL || (cols = atoi(p)) <= 0)
-       cols = 80;
-    return(cols);
-}
-
 /*
  * TODO: add support for embedded newlines in lbufs
  */
 
 void
-lbuf_init(struct lbuf *lbuf, char *buf, int indent, int continuation)
+lbuf_init(struct lbuf *lbuf, char *buf, int indent, int continuation, int cols)
 {
     lbuf->continuation = continuation;
     lbuf->indent = indent;
+    lbuf->cols = cols;
     lbuf->len = 0;
     lbuf->size = 0;
     lbuf->buf = NULL;
@@ -188,14 +165,11 @@ lbuf_print(struct lbuf *lbuf)
 {
     char *cp;
     int i, have, contlen;
-    static int cols = -1;
 
-    if (cols == -1)
-       cols = get_ttycols();
     contlen = lbuf->continuation ? 2 : 0;
 
     /* For very small widths just give up... */
-    if (cols <= lbuf->indent + contlen + 20) {
+    if (lbuf->cols <= lbuf->indent + contlen + 20) {
        puts(lbuf->buf);
        goto done;
     }
@@ -205,7 +179,7 @@ lbuf_print(struct lbuf *lbuf)
      * boundary.
      */
     cp = lbuf->buf;
-    have = cols;
+    have = lbuf->cols;
     while (cp != NULL && *cp != '\0') {
        char *ep;
        int need = lbuf->len - (int)(cp - lbuf->buf);
@@ -234,7 +208,7 @@ lbuf_print(struct lbuf *lbuf)
         * the whitespace, and print a line continuaton char if needed.
         */
        if (cp != NULL) {
-           have = cols - lbuf->indent;
+           have = lbuf->cols - lbuf->indent;
            do {
                cp++;
            } while (isspace((unsigned char)*cp));
index cbcb11e05afa63ae5527ce698bd0b4ba08b86f29..41d0ed5cd31e34a26aeb76c1948e38ec6a5eba78 100644 (file)
@@ -20,8 +20,6 @@
 
 #include <config.h>
 
-/* XXX - prune this */
-
 #include <sys/types.h>
 #include <sys/param.h>
 
 
 #include <sudo_usage.h>
 #include "sudo.h"
-#include "lbuf.h" /* XXX */
+#include "lbuf.h"
 
 /* For getopt(3) */
 extern char *optarg;
 extern int optind;
 
+extern struct user_details user_details;
+
 /* XXX - better home for these and extern in header file */
 int tgetpass_flags;
 int user_closefrom = -1;
@@ -376,7 +376,6 @@ parse_args(int argc, char **argv, int *nargc, char ***nargv, char ***settingsp,
 /*
  * Give usage message and exit.
  * The actual usage strings are in sudo_usage.h for configure substitution.
- * XXX - avoid lbuf usage
  */
 static void
 usage(int exit_val)
@@ -405,7 +404,7 @@ usage(int exit_val)
      * tty width.
      */
     ulen = (int)strlen(getprogname()) + 8;
-    lbuf_init(&lbuf, NULL, ulen, 0);
+    lbuf_init(&lbuf, NULL, ulen, 0, user_details.ts_cols);
     for (i = 0; uvec[i] != NULL; i++) {
        lbuf_append(&lbuf, "usage: ", getprogname(), uvec[i], NULL);
        lbuf_print(&lbuf);
index 43501978b96b54c18febb1becbad88eb7431ab0a..86b0eac2d009b8c1ec3967d09b115763d968d8b3 100644 (file)
@@ -154,16 +154,16 @@ main(int argc, char *argv[], char *envp[])
     disable_coredumps();
     fix_fds();
 
+    /* Fill in user_info with user name, uid, cwd, etc. */
+    memset(&user_details, 0, sizeof(user_details));
+    user_info = get_user_info(&user_details);
+
     /* Parse command line arguments. */
     sudo_mode = parse_args(Argc, Argv, &nargc, &nargv, &settings, &env_add);
 
     /* Read sudo.conf and load plugins. */
     sudo_load_plugins(_PATH_SUDO_CONF, &policy_plugin, &io_plugins);
 
-    /* Fill in user_info with user name, uid, cwd, etc. */
-    memset(&user_details, 0, sizeof(user_details));
-    user_info = get_user_info(&user_details);
-
     /* Open each plugin. */
     ok = policy_plugin.u.policy->open(SUDO_API_VERSION, sudo_conversation,
        settings, user_info, envp);