.
[coreutils.git] / src / tty.c
blob26c293bb7b5826006090a62522dcccfd0dae2b4c
1 /* tty -- print the path of the terminal connected to standard input
2 Copyright (C) 90, 91, 92, 93, 94, 1995 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 2, or (at your option)
7 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, write to the Free Software
16 Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. */
18 /* Displays "not a tty" if stdin is not a terminal.
19 Displays nothing if -s option is given.
20 Exit status 0 if stdin is a tty, 1 if not, 2 if usage error,
21 3 if write error.
23 Written by David MacKenzie <djm@gnu.ai.mit.edu>. */
25 #include <config.h>
26 #include <stdio.h>
27 #include <getopt.h>
28 #include <sys/types.h>
30 #include "system.h"
31 #include "version.h"
32 #include "error.h"
34 static void usage __P ((int status));
36 /* The name under which this program was run. */
37 char *program_name;
39 /* If nonzero, return an exit status but produce no output. */
40 static int silent;
42 /* If nonzero, display usage information and exit. */
43 static int show_help;
45 /* If nonzero, print the version on standard output and exit. */
46 static int show_version;
48 static struct option const longopts[] =
50 {"help", no_argument, &show_help, 1},
51 {"silent", no_argument, NULL, 's'},
52 {"quiet", no_argument, NULL, 's'},
53 {"version", no_argument, &show_version, 1},
54 {NULL, 0, NULL, 0}
57 void
58 main (int argc, char **argv)
60 char *tty;
61 int optc;
63 program_name = argv[0];
64 setlocale (LC_ALL, "");
65 bindtextdomain (PACKAGE, LOCALEDIR);
66 textdomain (PACKAGE);
68 silent = 0;
70 while ((optc = getopt_long (argc, argv, "s", longopts, (int *) 0)) != EOF)
72 switch (optc)
74 case 0:
75 break;
77 case 's':
78 silent = 1;
79 break;
81 default:
82 usage (2);
86 if (show_version)
88 printf ("tty - %s\n", version_string);
89 exit (0);
92 if (show_help)
93 usage (0);
95 if (optind != argc)
96 usage (2);
98 tty = ttyname (0);
99 if (!silent)
101 if (tty)
102 puts (tty);
103 else
104 puts (_("not a tty"));
106 if (ferror (stdout) || fclose (stdout) == EOF)
107 error (3, errno, _("standard output"));
110 exit (isatty (0) ? 0 : 1);
113 static void
114 usage (int status)
116 if (status != 0)
117 fprintf (stderr, _("Try `%s --help' for more information.\n"),
118 program_name);
119 else
121 printf (_("Usage: %s [OPTION]...\n"), program_name);
122 printf (_("\
123 Print the file name of the terminal connected to standard input.\n\
125 -s, --silent, --quiet print nothing, only return an exit status\n\
126 --help display this help and exit\n\
127 --version output version information and exit\n\
128 "));
130 exit (status);