(WRITTEN_BY): Rename from AUTHORS.
[platform/upstream/coreutils.git] / src / tty.c
1 /* tty -- print the path of the terminal connected to standard input
2    Copyright (C) 1990-2003 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 "exitfail.h"
32 #include "error.h"
33
34 /* The official name of this program (e.g., no `g' prefix).  */
35 #define PROGRAM_NAME "tty"
36
37 #define WRITTEN_BY _("Written by David MacKenzie.")
38
39 /* The name under which this program was run. */
40 char *program_name;
41
42 /* If nonzero, return an exit status but produce no output. */
43 static int silent;
44
45 static struct option const longopts[] =
46 {
47   {"silent", no_argument, NULL, 's'},
48   {"quiet", no_argument, NULL, 's'},
49   {GETOPT_HELP_OPTION_DECL},
50   {GETOPT_VERSION_OPTION_DECL},
51   {NULL, 0, NULL, 0}
52 };
53
54 void
55 usage (int status)
56 {
57   if (status != 0)
58     fprintf (stderr, _("Try `%s --help' for more information.\n"),
59              program_name);
60   else
61     {
62       printf (_("Usage: %s [OPTION]...\n"), program_name);
63       fputs (_("\
64 Print the file name of the terminal connected to standard input.\n\
65 \n\
66   -s, --silent, --quiet   print nothing, only return an exit status\n\
67 "), stdout);
68       fputs (HELP_OPTION_DESCRIPTION, stdout);
69       fputs (VERSION_OPTION_DESCRIPTION, stdout);
70       printf (_("\nReport bugs to <%s>.\n"), PACKAGE_BUGREPORT);
71     }
72   exit (status);
73 }
74
75 int
76 main (int argc, char **argv)
77 {
78   char *tty;
79   int optc;
80
81   initialize_main (&argc, &argv);
82   program_name = argv[0];
83   setlocale (LC_ALL, "");
84   bindtextdomain (PACKAGE, LOCALEDIR);
85   textdomain (PACKAGE);
86
87   exit_failure = 3;
88   atexit (close_stdout);
89
90   silent = 0;
91
92   while ((optc = getopt_long (argc, argv, "s", longopts, NULL)) != -1)
93     {
94       switch (optc)
95         {
96         case 0:
97           break;
98
99         case 's':
100           silent = 1;
101           break;
102
103         case_GETOPT_HELP_CHAR;
104
105         case_GETOPT_VERSION_CHAR (PROGRAM_NAME, WRITTEN_BY);
106
107         default:
108           usage (2);
109         }
110     }
111
112   if (optind < argc)
113     error (0, 0, _("ignoring all arguments"));
114
115   tty = ttyname (0);
116   if (!silent)
117     {
118       if (tty)
119         puts (tty);
120       else
121         puts (_("not a tty"));
122     }
123
124   exit (isatty (0) ? 0 : 1);
125 }