merge with 1.8.1i
[platform/upstream/coreutils.git] / src / tty.c
1 /* tty -- print the path of the terminal connected to standard input
2    Copyright (C) 1990, 1991 Free Software Foundation, Inc.
3
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.
8
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.
13
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.  */
17
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.
22
23    Written by David MacKenzie <djm@gnu.ai.mit.edu>.  */
24
25 #ifdef HAVE_CONFIG_H
26 #if defined (CONFIG_BROKETS)
27 /* We use <config.h> instead of "config.h" so that a compilation
28    using -I. -I$srcdir will use ./config.h rather than $srcdir/config.h
29    (which it would do because it found this file in $srcdir).  */
30 #include <config.h>
31 #else
32 #include "config.h"
33 #endif
34 #endif
35
36 #include <stdio.h>
37 #include <getopt.h>
38 #include <sys/types.h>
39
40 #include "system.h"
41 #include "version.h"
42
43 void error ();
44
45 static void usage ();
46
47 /* The name under which this program was run. */
48 char *program_name;
49
50 /* If nonzero, return an exit status but produce no output. */
51 static int silent;
52
53 /* If non-zero, display usage information and exit.  */
54 static int show_help;
55
56 /* If non-zero, print the version on standard output and exit.  */
57 static int show_version;
58
59 static struct option const longopts[] =
60 {
61   {"help", no_argument, &show_help, 1},
62   {"silent", no_argument, NULL, 's'},
63   {"quiet", no_argument, NULL, 's'},
64   {"version", no_argument, &show_version, 1},
65   {NULL, 0, NULL, 0}
66 };
67
68 void
69 main (argc, argv)
70      int argc;
71      char **argv;
72 {
73   char *tty;
74   int optc;
75
76   program_name = argv[0];
77   silent = 0;
78
79   while ((optc = getopt_long (argc, argv, "s", longopts, (int *) 0)) != EOF)
80     {
81       switch (optc)
82         {
83         case 0:
84           break;
85
86         case 's':
87           silent = 1;
88           break;
89
90         default:
91           usage (2);
92         }
93     }
94
95   if (show_version)
96     {
97       printf ("%s\n", version_string);
98       exit (0);
99     }
100
101   if (show_help)
102     usage (0);
103
104   if (optind != argc)
105     usage (2);
106
107   tty = ttyname (0);
108   if (!silent)
109     {
110       if (tty)
111         puts (tty);
112       else
113         puts ("not a tty");
114
115       if (ferror (stdout) || fclose (stdout) == EOF)
116         error (3, errno, "standard output");
117     }
118
119   exit (isatty (0) ? 0 : 1);
120 }
121
122 static void
123 usage (status)
124      int status;
125 {
126   if (status != 0)
127     fprintf (stderr, "Try `%s --help' for more information.\n",
128              program_name);
129   else
130     {
131       printf ("Usage: %s [OPTION]...\n", program_name);
132       printf ("\
133 \n\
134   -s, --silent, --quiet   print nothing, only return an exit status\n\
135       --help              display this help and exit\n\
136       --version           output version information and exit\n\
137 ");
138     }
139   exit (status);
140 }