1
0
mirror of https://github.com/moparisthebest/pacman synced 2024-08-13 17:03:46 -04:00

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 <d@falconindy.com>
Signed-off-by: Dan McGee <dan@archlinux.org>
This commit is contained in:
Dan McGee 2011-06-01 13:28:00 -05:00
parent aad57cc06a
commit c1f742d775
3 changed files with 21 additions and 19 deletions

View File

@ -351,7 +351,9 @@ void cb_trans_progress(pmtransprog_t event, const char *pkgname, int percent,
int len, wclen, wcwid, padwid; int len, wclen, wcwid, padwid;
wchar_t *wcstr; wchar_t *wcstr;
if(config->noprogressbar) { const int cols = getcols(0);
if(config->noprogressbar || cols == 0) {
return; return;
} }
@ -397,7 +399,7 @@ void cb_trans_progress(pmtransprog_t event, const char *pkgname, int percent,
return; return;
} }
infolen = getcols() * 6 / 10; infolen = cols * 6 / 10;
if (infolen < 50) { if (infolen < 50) {
infolen = 50; infolen = 50;
} }
@ -454,7 +456,7 @@ void cb_trans_progress(pmtransprog_t event, const char *pkgname, int percent,
free(wcstr); free(wcstr);
/* call refactored fill progress function */ /* call refactored fill progress function */
fill_progress(percent, percent, getcols() - infolen); fill_progress(percent, percent, cols - infolen);
if(percent == 100) { if(percent == 100) {
alpm_list_t *i = NULL; alpm_list_t *i = NULL;
@ -497,7 +499,9 @@ void cb_dl_progress(const char *filename, off_t file_xfered, off_t file_total)
int file_percent = 0, total_percent = 0; int file_percent = 0, total_percent = 0;
char rate_size = 'K', xfered_size = 'K'; char rate_size = 'K', xfered_size = 'K';
if(config->noprogressbar || file_total == -1) { const int cols = getcols(0);
if(config->noprogressbar || cols == 0 || file_total == -1) {
if(file_xfered == 0) { if(file_xfered == 0) {
printf(_("downloading %s...\n"), filename); printf(_("downloading %s...\n"), filename);
fflush(stdout); fflush(stdout);
@ -505,7 +509,7 @@ void cb_dl_progress(const char *filename, off_t file_xfered, off_t file_total)
return; return;
} }
infolen = getcols() * 6 / 10; infolen = cols * 6 / 10;
if (infolen < 50) { if (infolen < 50) {
infolen = 50; infolen = 50;
} }
@ -662,9 +666,9 @@ void cb_dl_progress(const char *filename, off_t file_xfered, off_t file_total)
free(wcfname); free(wcfname);
if(totaldownload) { if(totaldownload) {
fill_progress(file_percent, total_percent, getcols() - infolen); fill_progress(file_percent, total_percent, cols - infolen);
} else { } else {
fill_progress(file_percent, file_percent, getcols() - infolen); fill_progress(file_percent, file_percent, cols - infolen);
} }
return; return;
} }

View File

@ -101,7 +101,7 @@ int needs_root(void)
} }
/* gets the current screen column width */ /* gets the current screen column width */
int getcols(void) int getcols(int def)
{ {
#ifdef TIOCGSIZE #ifdef TIOCGSIZE
struct ttysize win; struct ttysize win;
@ -114,7 +114,7 @@ int getcols(void)
return win.ws_col; return win.ws_col;
} }
#endif #endif
return 0; return def;
} }
/* does the same thing as 'rm -rf' */ /* does the same thing as 'rm -rf' */
@ -209,14 +209,13 @@ void indentprint(const char *str, int indent)
{ {
wchar_t *wcstr; wchar_t *wcstr;
const wchar_t *p; const wchar_t *p;
int len, cidx, cols; int len, cidx;
const int cols = getcols(0);
if(!str) { if(!str) {
return; return;
} }
cols = getcols();
/* if we're not a tty, print without indenting */ /* if we're not a tty, print without indenting */
if(cols == 0) { if(cols == 0) {
printf("%s", str); printf("%s", str);
@ -425,8 +424,6 @@ static int string_length(const char *s)
void string_display(const char *title, const char *string) void string_display(const char *title, const char *string)
{ {
int len = 0;
if(title) { if(title) {
printf("%s ", title); printf("%s ", title);
} }
@ -434,7 +431,7 @@ void string_display(const char *title, const char *string)
printf(_("None")); printf(_("None"));
} else { } else {
/* compute the length of title + a space */ /* compute the length of title + a space */
len = string_length(title) + 1; int len = string_length(title) + 1;
indentprint(string, len); indentprint(string, len);
} }
printf("\n"); 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) void list_display(const char *title, const alpm_list_t *list)
{ {
const alpm_list_t *i; const alpm_list_t *i;
int cols, len = 0; int len = 0;
if(title) { if(title) {
len = string_length(title) + 1; len = string_length(title) + 1;
@ -453,11 +450,12 @@ void list_display(const char *title, const alpm_list_t *list)
if(!list) { if(!list) {
printf("%s\n", _("None")); printf("%s\n", _("None"));
} else { } else {
int cols;
const int maxcols = getcols(80);
for(i = list, cols = len; i; i = alpm_list_next(i)) { for(i = list, cols = len; i; i = alpm_list_next(i)) {
char *str = alpm_list_getdata(i); char *str = alpm_list_getdata(i);
int s = string_length(str); int s = string_length(str);
int maxcols = getcols(); if(cols + s + 2 >= maxcols) {
if(maxcols > 0 && (cols + s + 2) >= maxcols) {
int j; int j;
cols = len; cols = len;
printf("\n"); printf("\n");

View File

@ -42,7 +42,7 @@
int trans_init(pmtransflag_t flags); int trans_init(pmtransflag_t flags);
int trans_release(void); int trans_release(void);
int needs_root(void); int needs_root(void);
int getcols(void); int getcols(int def);
int rmrf(const char *path); int rmrf(const char *path);
const char *mbasename(const char *path); const char *mbasename(const char *path);
char *mdirname(const char *path); char *mdirname(const char *path);