.
[coreutils.git] / src / tty.c
blobcc67bdcf444b72b12a402b86ed6ce1c05637f41e
1 /* tty -- print the path of the terminal connected to standard input
2 Copyright (C) 90, 91, 92, 93, 94, 95, 1996 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 Foundation,
16 Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, 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 "error.h"
33 static void usage __P ((int status));
35 /* The name under which this program was run. */
36 char *program_name;
38 /* If nonzero, return an exit status but produce no output. */
39 static int silent;
41 /* If nonzero, display usage information and exit. */
42 static int show_help;
44 /* If nonzero, print the version on standard output and exit. */
45 static int show_version;
47 static struct option const longopts[] =
49 {"help", no_argument, &show_help, 1},
50 {"silent", no_argument, NULL, 's'},
51 {"quiet", no_argument, NULL, 's'},
52 {"version", no_argument, &show_version, 1},
53 {NULL, 0, NULL, 0}
56 int
57 main (int argc, char **argv)
59 char *tty;
60 int optc;
62 program_name = argv[0];
63 setlocale (LC_ALL, "");
64 bindtextdomain (PACKAGE, LOCALEDIR);
65 textdomain (PACKAGE);
67 silent = 0;
69 while ((optc = getopt_long (argc, argv, "s", longopts, (int *) 0)) != EOF)
71 switch (optc)
73 case 0:
74 break;
76 case 's':
77 silent = 1;
78 break;
80 default:
81 usage (2);
85 if (show_version)
87 printf ("tty (%s) %s\n", GNU_PACKAGE, VERSION);
88 exit (0);
91 if (show_help)
92 usage (0);
94 if (optind != argc)
95 usage (2);
97 tty = ttyname (0);
98 if (!silent)
100 if (tty)
101 puts (tty);
102 else
103 puts (_("not a tty"));
105 if (ferror (stdout) || fclose (stdout) == EOF)
106 error (3, errno, _("standard output"));
109 exit (isatty (0) ? 0 : 1);
112 static void
113 usage (int status)
115 if (status != 0)
116 fprintf (stderr, _("Try `%s --help' for more information.\n"),
117 program_name);
118 else
120 printf (_("Usage: %s [OPTION]...\n"), program_name);
121 printf (_("\
122 Print the file name of the terminal connected to standard input.\n\
124 -s, --silent, --quiet print nothing, only return an exit status\n\
125 --help display this help and exit\n\
126 --version output version information and exit\n\
127 "));
128 puts (_("\nReport bugs to sh-utils-bugs@gnu.ai.mit.edu"));
130 exit (status);