Include closeout.h. (main): Use close_stdout_status.
[platform/upstream/coreutils.git] / src / tty.c
1 /* tty -- print the path of the terminal connected to standard input
2    Copyright (C) 1990-1999 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 Foundation,
16    Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, 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 #include <config.h>
26 #include <stdio.h>
27 #include <getopt.h>
28 #include <sys/types.h>
29
30 #include "system.h"
31 #include "closeout.h"
32 #include "error.h"
33
34 static void usage PARAMS ((int status));
35
36 /* The name under which this program was run. */
37 char *program_name;
38
39 /* If nonzero, return an exit status but produce no output. */
40 static int silent;
41
42 /* If nonzero, display usage information and exit.  */
43 static int show_help;
44
45 /* If nonzero, print the version on standard output and exit.  */
46 static int show_version;
47
48 static struct option const longopts[] =
49 {
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}
55 };
56
57 int
58 main (int argc, char **argv)
59 {
60   char *tty;
61   int optc;
62
63   program_name = argv[0];
64   setlocale (LC_ALL, "");
65   bindtextdomain (PACKAGE, LOCALEDIR);
66   textdomain (PACKAGE);
67
68   silent = 0;
69
70   while ((optc = getopt_long (argc, argv, "s", longopts, NULL)) != -1)
71     {
72       switch (optc)
73         {
74         case 0:
75           break;
76
77         case 's':
78           silent = 1;
79           break;
80
81         default:
82           usage (2);
83         }
84     }
85
86   if (show_version)
87     {
88       printf ("tty (%s) %s\n", GNU_PACKAGE, VERSION);
89       exit (0);
90     }
91
92   if (show_help)
93     usage (0);
94
95   if (optind != argc)
96     usage (2);
97
98   tty = ttyname (0);
99   if (!silent)
100     {
101       if (tty)
102         puts (tty);
103       else
104         puts (_("not a tty"));
105
106       close_stdout_status (3);
107     }
108
109   exit (isatty (0) ? 0 : 1);
110 }
111
112 static void
113 usage (int status)
114 {
115   if (status != 0)
116     fprintf (stderr, _("Try `%s --help' for more information.\n"),
117              program_name);
118   else
119     {
120       printf (_("Usage: %s [OPTION]...\n"), program_name);
121       printf (_("\
122 Print the file name of the terminal connected to standard input.\n\
123 \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 <bug-sh-utils@gnu.org>."));
129     }
130   exit (status);
131 }