2010-03-17 10:36:02 -04:00
|
|
|
/*
|
2019-04-29 07:21:51 -06:00
|
|
|
* SPDX-License-Identifier: ISC
|
|
|
|
*
|
2023-04-16 15:45:19 -06:00
|
|
|
* Copyright (c) 2010-2012, 2014-2015, 2023 Todd C. Miller <Todd.Miller@sudo.ws>
|
2010-03-17 10:36:02 -04:00
|
|
|
*
|
|
|
|
* Permission to use, copy, modify, and distribute this software for any
|
|
|
|
* purpose with or without fee is hereby granted, provided that the above
|
|
|
|
* copyright notice and this permission notice appear in all copies.
|
|
|
|
*
|
|
|
|
* THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
|
|
|
|
* WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
|
|
|
|
* MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
|
|
|
|
* ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
|
|
|
|
* WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
|
|
|
|
* ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
|
|
|
|
* OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
|
|
|
|
*/
|
|
|
|
|
2018-10-26 08:39:09 -06:00
|
|
|
/*
|
|
|
|
* This is an open source non-commercial project. Dear PVS-Studio, please check it.
|
|
|
|
* PVS-Studio Static Code Analyzer for C, C++ and C#: http://www.viva64.com
|
|
|
|
*/
|
2018-10-21 08:46:05 -06:00
|
|
|
|
2010-03-17 10:36:02 -04:00
|
|
|
#include <config.h>
|
|
|
|
|
|
|
|
#include <sys/ioctl.h>
|
2015-06-19 14:29:27 -06:00
|
|
|
#include <stdlib.h>
|
2015-07-02 09:08:28 -06:00
|
|
|
#include <unistd.h>
|
2017-05-15 09:37:58 -06:00
|
|
|
#include <termios.h> /* for struct winsize on HP-UX */
|
2013-12-16 14:52:31 -07:00
|
|
|
#include <limits.h>
|
2010-03-17 10:36:02 -04:00
|
|
|
|
2023-09-25 10:13:28 -06:00
|
|
|
#include <sudo_compat.h>
|
|
|
|
#include <sudo_debug.h>
|
|
|
|
#include <sudo_util.h>
|
2010-03-17 10:36:02 -04:00
|
|
|
|
2011-10-22 14:40:21 -04:00
|
|
|
static int
|
2023-04-16 15:45:19 -06:00
|
|
|
get_ttysize_ioctl(int fd, int *rowp, int *colp)
|
2011-10-22 14:40:21 -04:00
|
|
|
{
|
2010-10-02 17:11:07 -04:00
|
|
|
struct winsize wsize;
|
2019-12-22 08:48:16 -07:00
|
|
|
debug_decl(get_ttysize_ioctl, SUDO_DEBUG_UTIL);
|
2010-03-17 10:36:02 -04:00
|
|
|
|
2023-12-09 12:54:56 -07:00
|
|
|
if (fd != -1 && sudo_isatty(fd, NULL)) {
|
|
|
|
if (ioctl(fd, TIOCGWINSZ, &wsize) == 0) {
|
|
|
|
if (wsize.ws_row != 0 && wsize.ws_col != 0) {
|
|
|
|
*rowp = wsize.ws_row;
|
|
|
|
*colp = wsize.ws_col;
|
|
|
|
debug_return_int(0);
|
|
|
|
}
|
2023-04-16 15:31:14 -06:00
|
|
|
}
|
2011-10-22 14:40:21 -04:00
|
|
|
}
|
|
|
|
debug_return_int(-1);
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2014-07-22 11:26:17 -06:00
|
|
|
sudo_get_ttysize_v1(int *rowp, int *colp)
|
2023-04-16 15:45:19 -06:00
|
|
|
{
|
|
|
|
sudo_get_ttysize_v2(STDERR_FILENO, rowp, colp);
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
sudo_get_ttysize_v2(int fd, int *rowp, int *colp)
|
2011-10-22 14:40:21 -04:00
|
|
|
{
|
2019-12-22 08:48:16 -07:00
|
|
|
debug_decl(sudo_get_ttysize, SUDO_DEBUG_UTIL);
|
2011-10-22 14:40:21 -04:00
|
|
|
|
2023-04-16 15:45:19 -06:00
|
|
|
if (get_ttysize_ioctl(fd, rowp, colp) == -1) {
|
2011-10-22 14:40:21 -04:00
|
|
|
char *p;
|
|
|
|
|
|
|
|
/* Fall back on $LINES and $COLUMNS. */
|
2013-12-10 16:23:21 -07:00
|
|
|
if ((p = getenv("LINES")) == NULL ||
|
2023-07-07 15:07:04 -06:00
|
|
|
(*rowp = (int)sudo_strtonum(p, 1, INT_MAX, NULL)) <= 0) {
|
2011-10-22 14:40:21 -04:00
|
|
|
*rowp = 24;
|
2013-12-10 16:23:21 -07:00
|
|
|
}
|
|
|
|
if ((p = getenv("COLUMNS")) == NULL ||
|
2023-07-07 15:07:04 -06:00
|
|
|
(*colp = (int)sudo_strtonum(p, 1, INT_MAX, NULL)) <= 0) {
|
2011-10-22 14:40:21 -04:00
|
|
|
*colp = 80;
|
2013-12-10 16:23:21 -07:00
|
|
|
}
|
2010-03-17 10:36:02 -04:00
|
|
|
}
|
|
|
|
|
2011-10-22 14:40:21 -04:00
|
|
|
debug_return;
|
2010-03-17 10:36:02 -04:00
|
|
|
}
|