/* tty -- print the name of the terminal connected to standard input This is the tty utility
Copyright (C) 1990-2018 Free Software Foundation, Inc.
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <https://www.gnu.org/licenses/>. */ The GNUv3 license
/* Displays "not a tty" if stdin is not a terminal.
Displays nothing if -s option is given.
Exit status 0 if stdin is a tty, 1 if not a tty, 2 if usage error,
3 if write error.
Written by David MacKenzie <djm@gnu.ai.mit.edu>. */
#include <config.h> Provides system specific information
#include <stdio.h> Provides standard I/O capability
#include <getopt.h> ...!includes auto-comment...
#include <sys/types.h> Provides system data types
#include "system.h" ...!includes auto-comment...
#include "error.h" ...!includes auto-comment...
#include "quote.h" ...!includes auto-comment...
/* Exit statuses. */
enum Line 34
{
TTY_STDIN_NOTTY = 1, Line 36
TTY_FAILURE = 2, Line 37
TTY_WRITE_ERROR = 3 Line 38
}; Block 1
/* The official name of this program (e.g., no 'g' prefix). */
#define PROGRAM_NAME "tty" Line 42
#define AUTHORS proper_name ("David MacKenzie") Line 44
/* If true, return an exit status but produce no output. */
static bool silent; Line 47
static struct option const longopts[] = Line 49
{
{"silent", no_argument, NULL, 's'}, Line 51
{"quiet", no_argument, NULL, 's'}, Line 52
{GETOPT_HELP_OPTION_DECL}, Line 53
{GETOPT_VERSION_OPTION_DECL}, Line 54
{NULL, 0, NULL, 0} Line 55
}; Block 2
void Line 58
usage (int status) Line 59
{
if (status != EXIT_SUCCESS) Line 61
emit_try_help (); ...!common auto-comment...
else Line 63
{
printf (_("Usage: %s [OPTION]...\n"), program_name); Line 65
fputs (_("\ Line 66
Print the file name of the terminal connected to standard input.\n\ Line 67
\n\
-s, --silent, --quiet print nothing, only return an exit status\n\ Line 69
"), stdout); Line 70
fputs (HELP_OPTION_DESCRIPTION, stdout); Line 71
fputs (VERSION_OPTION_DESCRIPTION, stdout); Line 72
emit_ancillary_info (PROGRAM_NAME); Line 73
}
exit (status); Line 75
} Block 3
int
main (int argc, char **argv) Line 79
{
int optc; Line 81
initialize_main (&argc, &argv); VMS-specific entry point handling wildcard expansion
set_program_name (argv[0]); Retains program name and discards path
setlocale (LC_ALL, ""); Sets up internationalization (i18n)
bindtextdomain (PACKAGE, LOCALEDIR); Assigns i18n directorySets text domain for _() [gettext()] function
textdomain (PACKAGE); Sets text domain for _() [gettext()] function
initialize_exit_failure (TTY_WRITE_ERROR); Line 89
atexit (close_stdout); Close stdout on exit (see gnulib)
silent = false; Line 92
while ((optc = getopt_long (argc, argv, "s", longopts, NULL)) != -1) Line 94
{
switch (optc) Line 96
{
case 's': Line 98
silent = true; Line 99
break; Line 100
case_GETOPT_HELP_CHAR; Line 102
case_GETOPT_VERSION_CHAR (PROGRAM_NAME, AUTHORS); Line 104
default: Line 106
usage (TTY_FAILURE); Line 107
}
}
if (optind < argc) Line 111
{
error (0, 0, _("extra operand %s"), quote (argv[optind])); Line 113
usage (TTY_FAILURE); Line 114
}
errno = ENOENT; Line 117
if (silent) Line 119
return isatty (STDIN_FILENO) ? EXIT_SUCCESS : TTY_STDIN_NOTTY; Line 120
int status = EXIT_SUCCESS; Line 122
char const *tty = ttyname (STDIN_FILENO); Line 123
if (! tty) Line 125
{
tty = _("not a tty"); Line 127
status = TTY_STDIN_NOTTY; Line 128
}
puts (tty); Line 131
return status; Line 132
} Block 4