From a927c39c0604c24daaddbc67e3c4c6ffdc925e2e Mon Sep 17 00:00:00 2001 From: DTB Date: Fri, 26 Jul 2024 07:42:05 -0600 Subject: [PATCH] peek(1): update style --- src/peek.c | 91 ++++++++++++++++++++++++++++++------------------------ 1 file changed, 50 insertions(+), 41 deletions(-) diff --git a/src/peek.c b/src/peek.c index a769e90..cde3ac4 100644 --- a/src/peek.c +++ b/src/peek.c @@ -20,60 +20,67 @@ #include /* fprintf(3), fgetc(3), perror(3), fputc(3), stderr, stdin, * stdout, EOF, NULL */ #include /* exit(3), EXIT_FAILURE */ -#if !defined EX_IOERR || !defined EX_OK || !defined EX_USAGE -# include -#endif +#include /* EX_IOERR, EX_OK, EX_USAGE */ #include /* tcgetattr(3), tcsetattr(3), struct termios, ECHO */ #include /* getopt(3), isatty(3), STDIN_FILENO */ -static char *program_name = "peek"; +char *program_name = "peek"; /* Restores terminal echo; otherwise when a user ^Cs the terminal would * continue to not display typed text. If sig isn't zero, this will terminate * the program. */ -static void restore_echo(int sig){ +static void +restore_echo(int sig) { static struct termios t; - tcgetattr(STDIN_FILENO, &t); - t.c_lflag |= ECHO; - tcsetattr(STDIN_FILENO, TCSAFLUSH, &t); + /* Failure isn't reported because this is the termination routine anyway; + * errors will be obvious. */ + if (tcgetattr(STDIN_FILENO, &t) == 0) { + t.c_lflag |= ECHO; + (void)tcsetattr(STDIN_FILENO, TCSAFLUSH, &t); + } - /* If, for whatever ungodly reason, exit(3) returns, the user will notice - * their typed characters on the screen. */ - if(sig != 0) - exit(EXIT_FAILURE); + if (sig != 0) { exit(EXIT_FAILURE); } /* Terminated by signal. */ return; } -static int ioerr(char *s){ perror(s); restore_echo(0); return EX_IOERR; } -static int usage(char *s){ - fprintf(stderr, "Usage: %s (-1enot) (-p [program [arguments...]])\n", s); +static int +ioerr(char *argv0) { + perror(argv0); + restore_echo(0); + return EX_IOERR; +} + +static int +usage(char *argv0) { + (void)fprintf(stderr, "Usage: %s [-i]\n", argv0); return EX_USAGE; } int main(int argc, char *argv[]){ - - if(argc < 1) - return usage(program_name); - - { /* option parsing */ + if (argc > 0) { /* option parsing */ char allow_nonterminals; int c; + program_name = argv[0]; + allow_nonterminals = 0; - while((c = getopt(argc, argv, "i")) != -1) - switch(c){ - case 'i': allow_nonterminals = 1; break; - default: return usage(argv[0]); + while ((c = getopt(argc, argv, "i")) != -1) { + switch (c) { + case 'i': allow_nonterminals = 1; break; + default: return usage(argv[0]); } + } - if(argc > optind) - return usage(argv[0]); + if (argc > optind) { return usage(argv[0]); } - if(!allow_nonterminals && isatty(STDIN_FILENO) != 1){ - fprintf(stderr, "%s: Must be run in a terminal" - " (option -i skips this check)\n", argv[0]); + if (!allow_nonterminals && isatty(STDIN_FILENO) != 1) { + (void)fprintf( + stderr, + "%s: Must be run in a terminal (option -i skips this check)\n", + argv[0] + ); return EX_USAGE; } } @@ -88,28 +95,30 @@ int main(int argc, char *argv[]){ struct sigaction act = { 0 }; act.sa_handler = restore_echo; - if(sigaction(SIGINT, &act, NULL) != 0) - perror(argv[0]); + if(sigaction(SIGINT, &act, NULL) != 0) { perror(program_name); } #else - if(signal(SIGINT, restore_echo) == SIG_ERR) - perror(argv[0]); + if(signal(SIGINT, restore_echo) == SIG_ERR) { perror(program_name); } #endif } - - { /* banish terminal echo */ + + /* Banish terminal echo; this terminates when it fails, because this is the + * whole point of the program. */ + { struct termios t; - tcgetattr(STDIN_FILENO, &t); + if (tcgetattr(STDIN_FILENO, &t) != 0) { return ioerror(program_name); } t.c_lflag ^= ECHO; - tcsetattr(STDIN_FILENO, TCSAFLUSH, &t); + if (tcsetattr(STDIN_FILENO, TCSAFLUSH, &t) != 0) { + return ioerror(program_name); + } } - { /* actual input loop */ + { /* Input loop */ int c; - while((c = fgetc(stdin)) != EOF) - if(fputc(c, stdout) == EOF) - return ioerr(argv[0]); + while ((c = fgetc(stdin)) != EOF) { + if(fputc(c, stdout) == EOF) { return ioerr(program_name); } + } } restore_echo(0);