logo

utils

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

bin/tty: Reduce syscalls

Diffstat:

Mbin/tty.c19+++++++++++--------
1 file changed, 11 insertions(+), 8 deletions(-)

diff --git a/bin/tty.c b/bin/tty.c @@ -2,23 +2,26 @@ // Copyright 2017-2022 Haelwenn (lanodan) Monnier <contact+utils@hacktivis.me> // SPDX-License-Identifier: GPL-2.0-only OR GPL-3.0-only +#include <errno.h> // errno #include <stdio.h> // puts() #include <unistd.h> // ttyname(), isatty() int main(void) { - if(!isatty(STDIN_FILENO)) - { - puts("not a tty"); - return 1; - } - char *name = ttyname(STDIN_FILENO); if(!name) { - perror("ttyname"); - return 2; + if(errno == ENOTTY) + { + puts("not a tty"); + return 1; + } + else + { + perror("ttyname"); + return 2; + } } puts(name);