|
24 | 24 | #include <config.h> |
25 | 25 |
|
26 | 26 | #include <sys/ioctl.h> |
| 27 | +#include <sys/stat.h> |
27 | 28 | #include <stdio.h> |
28 | 29 | #include <stdlib.h> |
29 | 30 | #include <string.h> |
@@ -286,6 +287,9 @@ sudo_term_is_raw_v1(int fd) |
286 | 287 | struct termios term = { 0 }; |
287 | 288 | debug_decl(sudo_term_is_raw, SUDO_DEBUG_UTIL); |
288 | 289 |
|
| 290 | + if (!sudo_isatty(fd, NULL)) |
| 291 | + debug_return_bool(false); |
| 292 | + |
289 | 293 | sudo_lock_file(fd, SUDO_LOCK); |
290 | 294 | if (tcgetattr(fd, &term) == -1) { |
291 | 295 | sudo_debug_printf(SUDO_DEBUG_ERROR|SUDO_DEBUG_ERRNO, |
@@ -457,3 +461,31 @@ sudo_term_copy_v1(int src, int dst) |
457 | 461 | sudo_lock_file(src, SUDO_UNLOCK); |
458 | 462 | debug_return_bool(ret); |
459 | 463 | } |
| 464 | + |
| 465 | +/* |
| 466 | + * Like isatty(3) but stats the fd and stores the result in sb. |
| 467 | + * Only calls isatty(3) if fd is a character special device. |
| 468 | + * Returns true if a tty, else returns false and sets errno. |
| 469 | + */ |
| 470 | +bool |
| 471 | +sudo_isatty_v1(int fd, struct stat *sbp) |
| 472 | +{ |
| 473 | + bool ret = false; |
| 474 | + struct stat sb; |
| 475 | + debug_decl(sudo_isatty, SUDO_DEBUG_EXEC); |
| 476 | + |
| 477 | + if (sbp == NULL) |
| 478 | + sbp = &sb; |
| 479 | + |
| 480 | + if (fstat(fd, sbp) == 0) { |
| 481 | + if (!S_ISCHR(sbp->st_mode)) { |
| 482 | + errno = ENOTTY; |
| 483 | + } else { |
| 484 | + ret = isatty(fd) == 1; |
| 485 | + } |
| 486 | + } else if (sbp != &sb) { |
| 487 | + /* Always initialize sbp. */ |
| 488 | + memset(sbp, 0, sizeof(*sbp)); |
| 489 | + } |
| 490 | + debug_return_bool(ret); |
| 491 | +} |
0 commit comments