dd: synchronize output after write errors
[coreutils.git] / src / tty.c
blob562c4e7080e2d3b350655d18691c4083e7391d1c
1 /* tty -- print the name of the terminal connected to standard input
2 Copyright (C) 1990-2022 Free Software Foundation, Inc.
4 This program is free software: you can redistribute it and/or modify
5 it under the terms of the GNU General Public License as published by
6 the Free Software Foundation, either version 3 of the License, or
7 (at your option) any later version.
9 This program is distributed in the hope that it will be useful,
10 but WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 GNU General Public License for more details.
14 You should have received a copy of the GNU General Public License
15 along with this program. If not, see <https://www.gnu.org/licenses/>. */
17 /* Displays "not a tty" if stdin is not a terminal.
18 Displays nothing if -s option is given.
19 Exit status 0 if stdin is a tty, 1 if not a tty, 2 if usage error,
20 3 if write error.
22 Written by David MacKenzie <djm@gnu.ai.mit.edu>. */
24 #include <config.h>
25 #include <stdio.h>
26 #include <getopt.h>
27 #include <sys/types.h>
29 #include "system.h"
30 #include "error.h"
31 #include "quote.h"
33 /* Exit statuses. */
34 enum
36 TTY_STDIN_NOTTY = 1,
37 TTY_FAILURE = 2,
38 TTY_WRITE_ERROR = 3
41 /* The official name of this program (e.g., no 'g' prefix). */
42 #define PROGRAM_NAME "tty"
44 #define AUTHORS proper_name ("David MacKenzie")
46 /* If true, return an exit status but produce no output. */
47 static bool silent;
49 static struct option const longopts[] =
51 {"silent", no_argument, NULL, 's'},
52 {"quiet", no_argument, NULL, 's'},
53 {GETOPT_HELP_OPTION_DECL},
54 {GETOPT_VERSION_OPTION_DECL},
55 {NULL, 0, NULL, 0}
58 void
59 usage (int status)
61 if (status != EXIT_SUCCESS)
62 emit_try_help ();
63 else
65 printf (_("Usage: %s [OPTION]...\n"), program_name);
66 fputs (_("\
67 Print the file name of the terminal connected to standard input.\n\
68 \n\
69 -s, --silent, --quiet print nothing, only return an exit status\n\
70 "), stdout);
71 fputs (HELP_OPTION_DESCRIPTION, stdout);
72 fputs (VERSION_OPTION_DESCRIPTION, stdout);
73 emit_ancillary_info (PROGRAM_NAME);
75 exit (status);
78 int
79 main (int argc, char **argv)
81 int optc;
83 initialize_main (&argc, &argv);
84 set_program_name (argv[0]);
85 setlocale (LC_ALL, "");
86 bindtextdomain (PACKAGE, LOCALEDIR);
87 textdomain (PACKAGE);
89 initialize_exit_failure (TTY_WRITE_ERROR);
90 atexit (close_stdout);
92 silent = false;
94 while ((optc = getopt_long (argc, argv, "s", longopts, NULL)) != -1)
96 switch (optc)
98 case 's':
99 silent = true;
100 break;
102 case_GETOPT_HELP_CHAR;
104 case_GETOPT_VERSION_CHAR (PROGRAM_NAME, AUTHORS);
106 default:
107 usage (TTY_FAILURE);
111 if (optind < argc)
113 error (0, 0, _("extra operand %s"), quote (argv[optind]));
114 usage (TTY_FAILURE);
117 errno = ENOENT;
119 if (silent)
120 return isatty (STDIN_FILENO) ? EXIT_SUCCESS : TTY_STDIN_NOTTY;
122 int status = EXIT_SUCCESS;
123 char const *tty = ttyname (STDIN_FILENO);
125 if (! tty)
127 tty = _("not a tty");
128 status = TTY_STDIN_NOTTY;
131 puts (tty);
132 return status;