commit 40244f6eac457069e60611709cda1851aae1eb08 killruana <killruana@gmail.com> 2017-11-11 13:29:36 +0100 Proper rewrite of function termsize (renamed getTerminalHeight): - add documentation - use constant instead of litteral integer value for the fd - handle ioctl failure - use better return type
extension.c | 17 +++++++++++++---- 1 file changed, 13 insertions(+), 4 deletions(-)
diff --git a/extension.c b/extension.c index 9c4c57a..498529d 100644 --- a/extension.c +++ b/extension.c @@ -1,8 +1,17 @@ +#include <limits.h> #include <sys/ioctl.h> +#include <unistd.h> -int termsize() +/** + * @brief Get the height of the terminal in term of visible lines + * @return the height of the terminal or UINT_MAX in case of error + */ +unsigned int getTerminalHeight() { - struct winsize w; - ioctl(0,TIOCGWINSZ, &w); - return(w.ws_row); + struct winsize w; + if (ioctl(STDOUT_FILENO, TIOCGWINSZ, &w) < 0) { + return UINT_MAX; + } + + return w.ws_row; }