xref: /curl/src/terminal.c (revision bc2f72b9)
1 /***************************************************************************
2  *                                  _   _ ____  _
3  *  Project                     ___| | | |  _ \| |
4  *                             / __| | | | |_) | |
5  *                            | (__| |_| |  _ <| |___
6  *                             \___|\___/|_| \_\_____|
7  *
8  * Copyright (C) Daniel Stenberg, <daniel@haxx.se>, et al.
9  *
10  * This software is licensed as described in the file COPYING, which
11  * you should have received as part of this distribution. The terms
12  * are also available at https://curl.se/docs/copyright.html.
13  *
14  * You may opt to use, copy, modify, merge, publish, distribute and/or sell
15  * copies of the Software, and permit persons to whom the Software is
16  * furnished to do so, under the terms of the COPYING file.
17  *
18  * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
19  * KIND, either express or implied.
20  *
21  * SPDX-License-Identifier: curl
22  *
23  ***************************************************************************/
24 #include "tool_setup.h"
25 
26 #ifdef HAVE_SYS_IOCTL_H
27 #include <sys/ioctl.h>
28 #endif
29 
30 #include "terminal.h"
31 
32 #include "memdebug.h" /* keep this as LAST include */
33 
34 #ifdef HAVE_TERMIOS_H
35 #  include <termios.h>
36 #elif defined(HAVE_TERMIO_H)
37 #  include <termio.h>
38 #endif
39 
40 /*
41  * get_terminal_columns() returns the number of columns in the current
42  * terminal. It will return 79 on failure. Also, the number can be very big.
43  */
44 
get_terminal_columns(void)45 unsigned int get_terminal_columns(void)
46 {
47   unsigned int width = 0;
48   char *colp = curl_getenv("COLUMNS");
49   if(colp) {
50     char *endptr;
51     long num = strtol(colp, &endptr, 10);
52     if((endptr != colp) && (endptr == colp + strlen(colp)) && (num > 20) &&
53        (num < 10000))
54       width = (unsigned int)num;
55     curl_free(colp);
56   }
57 
58   if(!width) {
59     int cols = 0;
60 
61 #ifdef TIOCGSIZE
62     struct ttysize ts;
63     if(!ioctl(STDIN_FILENO, TIOCGSIZE, &ts))
64       cols = ts.ts_cols;
65 #elif defined(TIOCGWINSZ)
66     struct winsize ts;
67     if(!ioctl(STDIN_FILENO, TIOCGWINSZ, &ts))
68       cols = (int)ts.ws_col;
69 #elif defined(_WIN32) && !defined(CURL_WINDOWS_UWP)
70     {
71       HANDLE  stderr_hnd = GetStdHandle(STD_ERROR_HANDLE);
72       CONSOLE_SCREEN_BUFFER_INFO console_info;
73 
74       if((stderr_hnd != INVALID_HANDLE_VALUE) &&
75          GetConsoleScreenBufferInfo(stderr_hnd, &console_info)) {
76         /*
77          * Do not use +1 to get the true screen-width since writing a
78          * character at the right edge will cause a line wrap.
79          */
80         cols = (int)
81           (console_info.srWindow.Right - console_info.srWindow.Left);
82       }
83     }
84 #endif /* TIOCGSIZE */
85     if(cols >= 0 && cols < 10000)
86       width = (unsigned int)cols;
87   }
88   if(!width)
89     width = 79;
90   return width; /* 79 for unknown, might also be very small or very big */
91 }
92