Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
freebsd
GitHub Repository: freebsd/pkg
Path: blob/main/external/curl/src/terminal.c
2065 views
1
/***************************************************************************
2
* _ _ ____ _
3
* Project ___| | | | _ \| |
4
* / __| | | | |_) | |
5
* | (__| |_| | _ <| |___
6
* \___|\___/|_| \_\_____|
7
*
8
* Copyright (C) Daniel Stenberg, <[email protected]>, 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
#include <curlx.h>
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 big.
43
*/
44
45
unsigned int get_terminal_columns(void)
46
{
47
unsigned int width = 0;
48
char *colp = curl_getenv("COLUMNS");
49
if(colp) {
50
curl_off_t num;
51
const char *p = colp;
52
if(!curlx_str_number(&p, &num, 10000) && (num > 20))
53
width = (unsigned int)num;
54
curl_free(colp);
55
}
56
57
if(!width) {
58
int cols = 0;
59
60
#ifdef TIOCGSIZE
61
struct ttysize ts;
62
if(!ioctl(STDIN_FILENO, TIOCGSIZE, &ts))
63
cols = ts.ts_cols;
64
#elif defined(TIOCGWINSZ)
65
struct winsize ts;
66
if(!ioctl(STDIN_FILENO, TIOCGWINSZ, &ts))
67
cols = (int)ts.ws_col;
68
#elif defined(_WIN32) && !defined(CURL_WINDOWS_UWP) && !defined(UNDER_CE)
69
{
70
HANDLE stderr_hnd = GetStdHandle(STD_ERROR_HANDLE);
71
CONSOLE_SCREEN_BUFFER_INFO console_info;
72
73
if((stderr_hnd != INVALID_HANDLE_VALUE) &&
74
GetConsoleScreenBufferInfo(stderr_hnd, &console_info)) {
75
/*
76
* Do not use +1 to get the true screen-width since writing a
77
* character at the right edge will cause a line wrap.
78
*/
79
cols = (int)
80
(console_info.srWindow.Right - console_info.srWindow.Left);
81
}
82
}
83
#endif /* TIOCGSIZE */
84
if(cols >= 0 && cols < 10000)
85
width = (unsigned int)cols;
86
}
87
if(!width)
88
width = 79;
89
return width; /* 79 for unknown, might also be tiny or enormous */
90
}
91
92