1 /* tty -- print the name of the terminal connected to standard input
2 Copyright (C) 1990-2005 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 <http://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, 2 if usage error,
22 Written by David MacKenzie <djm@gnu.ai.mit.edu>. */
27 #include <sys/types.h>
40 /* The official name of this program (e.g., no `g' prefix). */
41 #define PROGRAM_NAME "tty"
43 #define AUTHORS "David MacKenzie"
45 /* The name under which this program was run. */
48 /* If true, return an exit status but produce no output. */
51 static struct option const longopts[] =
53 {"silent", no_argument, NULL, 's'},
54 {"quiet", no_argument, NULL, 's'},
55 {GETOPT_HELP_OPTION_DECL},
56 {GETOPT_VERSION_OPTION_DECL},
63 if (status != EXIT_SUCCESS)
64 fprintf (stderr, _("Try `%s --help' for more information.\n"),
68 printf (_("Usage: %s [OPTION]...\n"), program_name);
70 Print the file name of the terminal connected to standard input.\n\
72 -s, --silent, --quiet print nothing, only return an exit status\n\
74 fputs (HELP_OPTION_DESCRIPTION, stdout);
75 fputs (VERSION_OPTION_DESCRIPTION, stdout);
76 emit_bug_reporting_address ();
82 main (int argc, char **argv)
87 initialize_main (&argc, &argv);
88 program_name = argv[0];
89 setlocale (LC_ALL, "");
90 bindtextdomain (PACKAGE, LOCALEDIR);
93 initialize_exit_failure (TTY_WRITE_ERROR);
94 atexit (close_stdout);
98 while ((optc = getopt_long (argc, argv, "s", longopts, NULL)) != -1)
106 case_GETOPT_HELP_CHAR;
108 case_GETOPT_VERSION_CHAR (PROGRAM_NAME, AUTHORS);
116 error (0, 0, _("extra operand %s"), quote (argv[optind]));
118 tty = ttyname (STDIN_FILENO);
124 puts (_("not a tty"));
127 exit (isatty (STDIN_FILENO) ? EXIT_SUCCESS : EXIT_FAILURE);