From c1f742d7750a14b680d2ceb2c75d952ccb53585d Mon Sep 17 00:00:00 2001 From: Dan McGee Date: Wed, 1 Jun 2011 13:28:00 -0500 Subject: Ensure list_display works on outputs of unknown width If getcols() returns 0, we were getting stuck before in a loop of no return. Teach getcols() to take a default value to return if the width is unknown, and use this everywhere as appropriate. Also make a few other cleanups while diagnosing this issue, such as const-ifying some variables. Noticed-by: Dave Reisner Signed-off-by: Dan McGee --- src/pacman/util.c | 20 +++++++++----------- 1 file changed, 9 insertions(+), 11 deletions(-) (limited to 'src/pacman/util.c') diff --git a/src/pacman/util.c b/src/pacman/util.c index 3d268031..508cc89f 100644 --- a/src/pacman/util.c +++ b/src/pacman/util.c @@ -101,7 +101,7 @@ int needs_root(void) } /* gets the current screen column width */ -int getcols(void) +int getcols(int def) { #ifdef TIOCGSIZE struct ttysize win; @@ -114,7 +114,7 @@ int getcols(void) return win.ws_col; } #endif - return 0; + return def; } /* does the same thing as 'rm -rf' */ @@ -209,14 +209,13 @@ void indentprint(const char *str, int indent) { wchar_t *wcstr; const wchar_t *p; - int len, cidx, cols; + int len, cidx; + const int cols = getcols(0); if(!str) { return; } - cols = getcols(); - /* if we're not a tty, print without indenting */ if(cols == 0) { printf("%s", str); @@ -425,8 +424,6 @@ static int string_length(const char *s) void string_display(const char *title, const char *string) { - int len = 0; - if(title) { printf("%s ", title); } @@ -434,7 +431,7 @@ void string_display(const char *title, const char *string) printf(_("None")); } else { /* compute the length of title + a space */ - len = string_length(title) + 1; + int len = string_length(title) + 1; indentprint(string, len); } printf("\n"); @@ -443,7 +440,7 @@ void string_display(const char *title, const char *string) void list_display(const char *title, const alpm_list_t *list) { const alpm_list_t *i; - int cols, len = 0; + int len = 0; if(title) { len = string_length(title) + 1; @@ -453,11 +450,12 @@ void list_display(const char *title, const alpm_list_t *list) if(!list) { printf("%s\n", _("None")); } else { + int cols; + const int maxcols = getcols(80); for(i = list, cols = len; i; i = alpm_list_next(i)) { char *str = alpm_list_getdata(i); int s = string_length(str); - int maxcols = getcols(); - if(maxcols > 0 && (cols + s + 2) >= maxcols) { + if(cols + s + 2 >= maxcols) { int j; cols = len; printf("\n"); -- cgit v1.2.3