logo

utils

~/.local/bin tools and git-hooks git clone https://hacktivis.me/git/utils.git
commit: 21dc297b514fc1b20c99184095c8625adf6c90a3
parent 28a9f56416c894f8cde3388eee111d2a79d8ea6b
Author: Haelwenn (lanodan) Monnier <contact@hacktivis.me>
Date:   Tue,  1 Mar 2022 19:19:18 +0100

bin/tty: Handle write errors

Diffstat:

Mbin/tty.c14+++++++++++---
Mtest-bin/tty32+++++++++++++++++++++++++++++---
2 files changed, 40 insertions(+), 6 deletions(-)

diff --git a/bin/tty.c b/bin/tty.c @@ -4,17 +4,22 @@ #include <errno.h> // errno #include <stdio.h> // puts() -#include <unistd.h> // ttyname(), isatty() +#include <unistd.h> // ttyname() int main(void) { char *name = ttyname(STDIN_FILENO); + if(!name) { if(errno == ENOTTY) { - puts("not a tty"); + if(puts("not a tty") <= 0) + { + return 2; + } + return 1; } else @@ -24,7 +29,10 @@ main(void) } } - puts(name); + if(puts(name) <= 0) + { + return 2; + } return 0; } diff --git a/test-bin/tty b/test-bin/tty @@ -1,16 +1,42 @@ #!/usr/bin/env atf-sh atf_test_case basic basic_body() { + tty >/dev/null || atf_expect_fail "test environment not in a tty" + atf_check -o 'not-inline:not a tty\n' ../bin/tty + } -atf_test_case devnull -devnull_body() { +atf_test_case in_devnull +in_devnull_body() { atf_check -s 'exit:1' -o 'inline:not a tty\n' ../bin/tty </dev/null } +atf_test_case out_devnull +out_devnull_body() { + tty >/dev/null || atf_expect_fail "test environment not in a tty" + + atf_check -o 'not-inline:not a tty\n' sh -c '../bin/tty >/dev/null' +} + +atf_test_case out_devfull +out_devfull_body() { + if test -f /usr/include/features.h && grep -q '#define\W__GLIBC__' /usr/include/features.h + then + atf_expect_fail "glibc ignoring write errors for puts()" + fi + [ "$(uname -s)" = "NetBSD" ] && atf_expect_fail "NetBSD ignoring write errors for puts()" + [ "$(uname -s)" = "FreeBSD" ] && atf_expect_fail "FreeBSD ignoring write errors for puts()" + + tty_out="$(tty)" + + atf_check -s "exit:2" sh -c '../bin/tty >/dev/full' +} + atf_init_test_cases() { cd "$(atf_get_srcdir)" || exit 1 atf_add_test_case basic - atf_add_test_case devnull + atf_add_test_case in_devnull + atf_add_test_case out_devnull + atf_add_test_case out_devfull }