From 7e9a9af14fef3ad555a287a367165e4a66b8482d Mon Sep 17 00:00:00 2001 From: Karel Zak Date: Tue, 15 May 2012 11:58:20 +0200 Subject: [PATCH] include/ttyutils: more robust get_terminal_width() Signed-off-by: Karel Zak --- include/ttyutils.h | 14 ++++++++++++-- misc-utils/blkid.c | 6 ++++-- 2 files changed, 16 insertions(+), 4 deletions(-) diff --git a/include/ttyutils.h b/include/ttyutils.h index 15809e85a4..3c40d72a40 100644 --- a/include/ttyutils.h +++ b/include/ttyutils.h @@ -3,6 +3,7 @@ #include #include +#include #ifdef HAVE_SYS_IOCTL_H #include #endif @@ -97,8 +98,17 @@ static inline int get_terminal_width(void) return w_win.ws_col; #endif cp = getenv("COLUMNS"); - if (cp) - return strtol(cp, NULL, 10); + if (cp) { + char *end = NULL; + long c; + + errno = 0; + c = strtol(cp, &end, 10); + + if (errno == 0 && end && *end == '\0' && end > cp && + c > 0 && c <= INT_MAX) + return c; + } return 0; } diff --git a/misc-utils/blkid.c b/misc-utils/blkid.c index aec3528b47..5268aba1e1 100644 --- a/misc-utils/blkid.c +++ b/misc-utils/blkid.c @@ -148,9 +148,11 @@ static void pretty_print_line(const char *device, const char *fs_type, static int term_width = -1; int len, w; - if (term_width < 0) + if (term_width < 0) { term_width = get_terminal_width(); - + if (term_width <= 0) + term_width = 80; + } if (term_width > 80) { term_width -= 80; w = term_width / 10; -- 2.47.3