(usage): Add one- and two-line descriptions to --help output.
[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 "version.h"
32 #include "error.h"
33
34 static void usage ();
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 non-zero, display usage information and exit.  */
43 static int show_help;
44
45 /* If non-zero, 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 void
58 main (argc, argv)
59      int argc;
60      char **argv;
61 {
62   char *tty;
63   int optc;
64
65   program_name = argv[0];
66   silent = 0;
67
68   while ((optc = getopt_long (argc, argv, "s", longopts, (int *) 0)) != EOF)
69     {
70       switch (optc)
71         {
72         case 0:
73           break;
74
75         case 's':
76           silent = 1;
77           break;
78
79         default:
80           usage (2);
81         }
82     }
83
84   if (show_version)
85     {
86       printf ("tty - %s\n", version_string);
87       exit (0);
88     }
89
90   if (show_help)
91     usage (0);
92
93   if (optind != argc)
94     usage (2);
95
96   tty = ttyname (0);
97   if (!silent)
98     {
99       if (tty)
100         puts (tty);
101       else
102         puts ("not a tty");
103
104       if (ferror (stdout) || fclose (stdout) == EOF)
105         error (3, errno, "standard output");
106     }
107
108   exit (isatty (0) ? 0 : 1);
109 }
110
111 static void
112 usage (status)
113      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 }