Use number of tty columns that is passed in user_info instead of

getting it directly in the lbuf code.
This commit is contained in:
Todd C. Miller
2010-03-17 21:25:32 -04:00
parent d5ae4c7d87
commit 038ec569de
7 changed files with 26 additions and 42 deletions

View 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 *, ...);

View 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;

View 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

View 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

View File

@@ -53,39 +53,16 @@
#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));

View File

@@ -20,8 +20,6 @@
#include <config.h>
/* XXX - prune this */
#include <sys/types.h>
#include <sys/param.h>
@@ -52,12 +50,14 @@
#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);

View 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);