X-Git-Url: https://ncurses.scripts.mit.edu/?p=ncurses.git;a=blobdiff_plain;f=progs%2Ftty_settings.c;h=274de9fecf6fe01c56bbf2bee1e6d3cbd93a5d12;hp=93d9e2abc405e9755a4f8c13fcd6d05ac2b22dfc;hb=3b3a6d094f484e799b1430782f1130a9af71a94e;hpb=62ca6190a9a8ddccb2c4d5ca7b2ef9f88432da65 diff --git a/progs/tty_settings.c b/progs/tty_settings.c index 93d9e2ab..274de9fe 100644 --- a/progs/tty_settings.c +++ b/progs/tty_settings.c @@ -1,5 +1,6 @@ /**************************************************************************** - * Copyright (c) 2016 Free Software Foundation, Inc. * + * Copyright 2020 Thomas E. Dickey * + * Copyright 2016,2017 Free Software Foundation, Inc. * * * * Permission is hereby granted, free of charge, to any person obtaining a * * copy of this software and associated documentation files (the * @@ -35,31 +36,21 @@ #include -MODULE_ID("$Id: tty_settings.c,v 1.2 2016/12/24 19:31:11 tom Exp $") +MODULE_ID("$Id: tty_settings.c,v 1.6 2020/02/02 23:34:34 tom Exp $") static int my_fd; static TTY original_settings; static bool can_restore = FALSE; -static void -exit_error(void) -{ - restore_tty_settings(); - (void) fprintf(stderr, "\n"); - ExitProgram(EXIT_FAILURE); - /* NOTREACHED */ -} - static void failed(const char *msg) { - char temp[BUFSIZ]; + int code = errno; - _nc_STRCPY(temp, _nc_progname, sizeof(temp)); - _nc_STRCAT(temp, ": ", sizeof(temp)); - _nc_STRNCAT(temp, msg, sizeof(temp), sizeof(temp) - strlen(temp) - 2); - perror(temp); - exit_error(); + (void) fprintf(stderr, "%s: %s: %s\n", _nc_progname, msg, strerror(code)); + restore_tty_settings(); + (void) fprintf(stderr, "\n"); + ExitProgram(ErrSystem(code)); /* NOTREACHED */ } @@ -79,16 +70,21 @@ get_tty_settings(int fd, TTY * tty_settings) * stderr is less likely to be redirected than stdout; try that first. */ int -save_tty_settings(TTY * tty_settings) +save_tty_settings(TTY * tty_settings, bool need_tty) { if (!get_tty_settings(STDERR_FILENO, tty_settings) && !get_tty_settings(STDOUT_FILENO, tty_settings) && !get_tty_settings(STDIN_FILENO, tty_settings) && !get_tty_settings(open("/dev/tty", O_RDWR), tty_settings)) { - failed("terminal attributes"); + if (need_tty) { + failed("terminal attributes"); + } else { + my_fd = fileno(stdout); + } + } else { + can_restore = TRUE; + original_settings = *tty_settings; } - can_restore = TRUE; - original_settings = *tty_settings; return my_fd; }