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