Signed-off-by: Karel Zak <kzak@redhat.com>
#include <stdlib.h>
#include <termios.h>
+#include <limits.h>
#ifdef HAVE_SYS_IOCTL_H
#include <sys/ioctl.h>
#endif
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;
}
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;