Don't include version.h.
[platform/upstream/coreutils.git] / src / tty.c
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.
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 #include <config.h>
26 #include <stdio.h>
27 #include <getopt.h>
28 #include <sys/types.h>
29
30 #include "system.h"
31 #include "error.h"
32
33 static void usage __P ((int status));
34
35 /* The name under which this program was run. */
36 char *program_name;
37
38 /* If nonzero, return an exit status but produce no output. */
39 static int silent;
40
41 /* If nonzero, display usage information and exit.  */
42 static int show_help;
43
44 /* If nonzero, print the version on standard output and exit.  */
45 static int show_version;
46
47 static struct option const longopts[] =
48 {
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}
54 };
55
56 void
57 main (int argc, char **argv)
58 {
59   char *tty;
60   int optc;
61
62   program_name = argv[0];
63   setlocale (LC_ALL, "");
64   bindtextdomain (PACKAGE, LOCALEDIR);
65   textdomain (PACKAGE);
66
67   silent = 0;
68
69   while ((optc = getopt_long (argc, argv, "s", longopts, (int *) 0)) != EOF)
70     {
71       switch (optc)
72         {
73         case 0:
74           break;
75
76         case 's':
77           silent = 1;
78           break;
79
80         default:
81           usage (2);
82         }
83     }
84
85   if (show_version)
86     {
87       printf ("tty - %s\n", PACKAGE_VERSION);
88       exit (0);
89     }
90
91   if (show_help)
92     usage (0);
93
94   if (optind != argc)
95     usage (2);
96
97   tty = ttyname (0);
98   if (!silent)
99     {
100       if (tty)
101         puts (tty);
102       else
103         puts (_("not a tty"));
104
105       if (ferror (stdout) || fclose (stdout) == EOF)
106         error (3, errno, _("standard output"));
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     }
129   exit (status);
130 }