/***************************************************************************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 "memdebug.h" /* keep this as LAST include */3132#ifdef HAVE_TERMIOS_H33# include <termios.h>34#elif defined(HAVE_TERMIO_H)35# include <termio.h>36#endif3738/*39* get_terminal_columns() returns the number of columns in the current40* terminal. It will return 79 on failure. Also, the number can be big.41*/4243unsigned int get_terminal_columns(void)44{45unsigned int width = 0;46char *colp = curl_getenv("COLUMNS");47if(colp) {48curl_off_t num;49const char *p = colp;50if(!curlx_str_number(&p, &num, 10000) && (num > 20))51width = (unsigned int)num;52curl_free(colp);53}5455if(!width) {56int cols = 0;5758#ifdef TIOCGSIZE59struct ttysize ts;60if(!ioctl(STDIN_FILENO, TIOCGSIZE, &ts))61cols = ts.ts_cols;62#elif defined(TIOCGWINSZ)63struct winsize ts;64if(!ioctl(STDIN_FILENO, TIOCGWINSZ, &ts))65cols = (int)ts.ws_col;66#elif defined(_WIN32) && !defined(CURL_WINDOWS_UWP) && !defined(UNDER_CE)67{68HANDLE stderr_hnd = GetStdHandle(STD_ERROR_HANDLE);69CONSOLE_SCREEN_BUFFER_INFO console_info;7071if((stderr_hnd != INVALID_HANDLE_VALUE) &&72GetConsoleScreenBufferInfo(stderr_hnd, &console_info)) {73/*74* Do not use +1 to get the true screen-width since writing a75* character at the right edge will cause a line wrap.76*/77cols = (int)78(console_info.srWindow.Right - console_info.srWindow.Left);79}80}81#endif /* TIOCGSIZE */82if(cols >= 0 && cols < 10000)83width = (unsigned int)cols;84}85if(!width)86width = 79;87return width; /* 79 for unknown, might also be tiny or enormous */88}899091