maint: avoid new syntax-check failure
[coreutils/ericb.git] / src / tty.c
blobfb75950483ac1ebbbb7382cc4f4dc72349bd6091
1 /* tty -- print the name of the terminal connected to standard input
2 Copyright (C) 1990-2005, 2008-2011 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,
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_FAILURE = 2,
37 TTY_WRITE_ERROR = 3
40 /* The official name of this program (e.g., no `g' prefix). */
41 #define PROGRAM_NAME "tty"
43 #define AUTHORS proper_name ("David MacKenzie")
45 /* If true, return an exit status but produce no output. */
46 static bool silent;
48 static struct option const longopts[] =
50 {"silent", no_argument, NULL, 's'},
51 {"quiet", no_argument, NULL, 's'},
52 {GETOPT_HELP_OPTION_DECL},
53 {GETOPT_VERSION_OPTION_DECL},
54 {NULL, 0, NULL, 0}
57 void
58 usage (int status)
60 if (status != EXIT_SUCCESS)
61 fprintf (stderr, _("Try `%s --help' for more information.\n"),
62 program_name);
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 ();
75 exit (status);
78 int
79 main (int argc, char **argv)
81 char *tty;
82 int optc;
84 initialize_main (&argc, &argv);
85 set_program_name (argv[0]);
86 setlocale (LC_ALL, "");
87 bindtextdomain (PACKAGE, LOCALEDIR);
88 textdomain (PACKAGE);
90 initialize_exit_failure (TTY_WRITE_ERROR);
91 atexit (close_stdout);
93 silent = false;
95 while ((optc = getopt_long (argc, argv, "s", longopts, NULL)) != -1)
97 switch (optc)
99 case 's':
100 silent = true;
101 break;
103 case_GETOPT_HELP_CHAR;
105 case_GETOPT_VERSION_CHAR (PROGRAM_NAME, AUTHORS);
107 default:
108 usage (TTY_FAILURE);
112 if (optind < argc)
113 error (0, 0, _("extra operand %s"), quote (argv[optind]));
115 tty = ttyname (STDIN_FILENO);
116 if (!silent)
118 if (tty)
119 puts (tty);
120 else
121 puts (_("not a tty"));
124 exit (isatty (STDIN_FILENO) ? EXIT_SUCCESS : EXIT_FAILURE);