.
[coreutils.git] / src / tty.c
blob68332a7c2e1554167af2ed9fd47b19652ecc5c88
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 ();
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 non-zero, display usage information and exit. */
43 static int show_help;
45 /* If non-zero, 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 (argc, argv)
59 int argc;
60 char **argv;
62 char *tty;
63 int optc;
65 program_name = argv[0];
66 silent = 0;
68 while ((optc = getopt_long (argc, argv, "s", longopts, (int *) 0)) != EOF)
70 switch (optc)
72 case 0:
73 break;
75 case 's':
76 silent = 1;
77 break;
79 default:
80 usage (2);
84 if (show_version)
86 printf ("tty - %s\n", version_string);
87 exit (0);
90 if (show_help)
91 usage (0);
93 if (optind != argc)
94 usage (2);
96 tty = ttyname (0);
97 if (!silent)
99 if (tty)
100 puts (tty);
101 else
102 puts ("not a tty");
104 if (ferror (stdout) || fclose (stdout) == EOF)
105 error (3, errno, "standard output");
108 exit (isatty (0) ? 0 : 1);
111 static void
112 usage (status)
113 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\
129 exit (status);