summaryrefslogtreecommitdiffstats
path: root/include/ttyutils.h
diff options
context:
space:
mode:
authorKarel Zak2012-05-15 11:58:20 +0200
committerKarel Zak2012-05-15 11:58:20 +0200
commit7e9a9af14fef3ad555a287a367165e4a66b8482d (patch)
tree7c8faeec3718acc38cc4669a86abd398bd4b7939 /include/ttyutils.h
parentblkid: use get_terminal_width() from ttyutils.h (diff)
downloadkernel-qcow2-util-linux-7e9a9af14fef3ad555a287a367165e4a66b8482d.tar.gz
kernel-qcow2-util-linux-7e9a9af14fef3ad555a287a367165e4a66b8482d.tar.xz
kernel-qcow2-util-linux-7e9a9af14fef3ad555a287a367165e4a66b8482d.zip
include/ttyutils: more robust get_terminal_width()
Signed-off-by: Karel Zak <kzak@redhat.com>
Diffstat (limited to 'include/ttyutils.h')
-rw-r--r--include/ttyutils.h14
1 files changed, 12 insertions, 2 deletions
diff --git a/include/ttyutils.h b/include/ttyutils.h
index 15809e85a..3c40d72a4 100644
--- a/include/ttyutils.h
+++ b/include/ttyutils.h
@@ -3,6 +3,7 @@
#include <stdlib.h>
#include <termios.h>
+#include <limits.h>
#ifdef HAVE_SYS_IOCTL_H
#include <sys/ioctl.h>
#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;
}