/***************************************************************************1* _ _ ____ _2* Project ___| | | | _ \| |3* / __| | | | |_) | |4* | (__| |_| | _ <| |___5* \___|\___/|_| \_\_____|6*7* Copyright (C) Daniel Stenberg, <[email protected]>, et al.8*9* This software is licensed as described in the file COPYING, which10* you should have received as part of this distribution. The terms11* are also available at https://curl.se/docs/copyright.html.12*13* You may opt to use, copy, modify, merge, publish, distribute and/or sell14* copies of the Software, and permit persons to whom the Software is15* furnished to do so, under the terms of the COPYING file.16*17* This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY18* KIND, either express or implied.19*20* SPDX-License-Identifier: curl21*22***************************************************************************/23#include "tool_setup.h"2425#ifdef HAVE_SYS_IOCTL_H26#include <sys/ioctl.h>27#endif2829#include "terminal.h"30#include <curlx.h>31#include <memdebug.h> /* keep this as LAST include */3233#ifdef HAVE_TERMIOS_H34# include <termios.h>35#elif defined(HAVE_TERMIO_H)36# include <termio.h>37#endif3839/*40* get_terminal_columns() returns the number of columns in the current41* terminal. It will return 79 on failure. Also, the number can be big.42*/4344unsigned int get_terminal_columns(void)45{46unsigned int width = 0;47char *colp = curl_getenv("COLUMNS");48if(colp) {49curl_off_t num;50const char *p = colp;51if(!curlx_str_number(&p, &num, 10000) && (num > 20))52width = (unsigned int)num;53curl_free(colp);54}5556if(!width) {57int cols = 0;5859#ifdef TIOCGSIZE60struct ttysize ts;61if(!ioctl(STDIN_FILENO, TIOCGSIZE, &ts))62cols = ts.ts_cols;63#elif defined(TIOCGWINSZ)64struct winsize ts;65if(!ioctl(STDIN_FILENO, TIOCGWINSZ, &ts))66cols = (int)ts.ws_col;67#elif defined(_WIN32) && !defined(CURL_WINDOWS_UWP) && !defined(UNDER_CE)68{69HANDLE stderr_hnd = GetStdHandle(STD_ERROR_HANDLE);70CONSOLE_SCREEN_BUFFER_INFO console_info;7172if((stderr_hnd != INVALID_HANDLE_VALUE) &&73GetConsoleScreenBufferInfo(stderr_hnd, &console_info)) {74/*75* Do not use +1 to get the true screen-width since writing a76* character at the right edge will cause a line wrap.77*/78cols = (int)79(console_info.srWindow.Right - console_info.srWindow.Left);80}81}82#endif /* TIOCGSIZE */83if(cols >= 0 && cols < 10000)84width = (unsigned int)cols;85}86if(!width)87width = 79;88return width; /* 79 for unknown, might also be tiny or enormous */89}909192