LCOV - code coverage report
Current view: top level - src - tty.c (source / functions) Hit Total Coverage
Test: coreutils.info Lines: 32 32 100.0 %
Date: 2018-01-30 Functions: 2 2 100.0 %

          Line data    Source code
       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 3 of the License, or
       7             :    (at your option) 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, see <http://www.gnu.org/licenses/>.  */
      16             : 
      17             : /* Displays "not a tty" if stdin is not a terminal.
      18             :    Displays nothing if -s option is given.
      19             :    Exit status 0 if stdin is a tty, 1 if not, 2 if usage error,
      20             :    3 if write error.
      21             : 
      22             :    Written by David MacKenzie <djm@gnu.ai.mit.edu>.  */
      23             : 
      24             : #include <config.h>
      25             : #include <stdio.h>
      26             : #include <getopt.h>
      27             : #include <sys/types.h>
      28             : 
      29             : #include "system.h"
      30             : #include "error.h"
      31             : #include "quote.h"
      32             : 
      33             : /* Exit statuses.  */
      34             : enum
      35             :   {
      36             :     TTY_FAILURE = 2,
      37             :     TTY_WRITE_ERROR = 3
      38             :   };
      39             : 
      40             : /* The official name of this program (e.g., no `g' prefix).  */
      41             : #define PROGRAM_NAME "tty"
      42             : 
      43             : #define AUTHORS "David MacKenzie"
      44             : 
      45             : /* The name under which this program was run. */
      46             : char *program_name;
      47             : 
      48             : /* If true, return an exit status but produce no output. */
      49             : static bool silent;
      50             : 
      51             : static struct option const longopts[] =
      52             : {
      53             :   {"silent", no_argument, NULL, 's'},
      54             :   {"quiet", no_argument, NULL, 's'},
      55             :   {GETOPT_HELP_OPTION_DECL},
      56             :   {GETOPT_VERSION_OPTION_DECL},
      57             :   {NULL, 0, NULL, 0}
      58             : };
      59             : 
      60             : void
      61           7 : usage (int status)
      62             : {
      63           7 :   if (status != EXIT_SUCCESS)
      64           6 :     fprintf (stderr, _("Try `%s --help' for more information.\n"),
      65             :              program_name);
      66             :   else
      67             :     {
      68           1 :       printf (_("Usage: %s [OPTION]...\n"), program_name);
      69           1 :       fputs (_("\
      70             : Print the file name of the terminal connected to standard input.\n\
      71             : \n\
      72             :   -s, --silent, --quiet   print nothing, only return an exit status\n\
      73             : "), stdout);
      74           1 :       fputs (HELP_OPTION_DESCRIPTION, stdout);
      75           1 :       fputs (VERSION_OPTION_DESCRIPTION, stdout);
      76           1 :       emit_bug_reporting_address ();
      77             :     }
      78           7 :   exit (status);
      79             : }
      80             : 
      81             : int
      82          41 : main (int argc, char **argv)
      83             : {
      84             :   char *tty;
      85             :   int optc;
      86             : 
      87             :   initialize_main (&argc, &argv);
      88          41 :   program_name = argv[0];
      89          41 :   setlocale (LC_ALL, "");
      90             :   bindtextdomain (PACKAGE, LOCALEDIR);
      91             :   textdomain (PACKAGE);
      92             : 
      93          41 :   initialize_exit_failure (TTY_WRITE_ERROR);
      94          41 :   atexit (close_stdout);
      95             : 
      96          41 :   silent = false;
      97             : 
      98          90 :   while ((optc = getopt_long (argc, argv, "s", longopts, NULL)) != -1)
      99             :     {
     100          16 :       switch (optc)
     101             :         {
     102           8 :         case 's':
     103           8 :           silent = true;
     104           8 :           break;
     105             : 
     106           1 :         case_GETOPT_HELP_CHAR;
     107             : 
     108           1 :         case_GETOPT_VERSION_CHAR (PROGRAM_NAME, AUTHORS);
     109             : 
     110           6 :         default:
     111           6 :           usage (TTY_FAILURE);
     112             :         }
     113             :     }
     114             : 
     115          33 :   if (optind < argc)
     116          27 :     error (0, 0, _("extra operand %s"), quote (argv[optind]));
     117             : 
     118          33 :   tty = ttyname (STDIN_FILENO);
     119          33 :   if (!silent)
     120             :     {
     121          26 :       if (tty)
     122          24 :         puts (tty);
     123             :       else
     124           2 :         puts (_("not a tty"));
     125             :     }
     126             : 
     127          33 :   exit (isatty (STDIN_FILENO) ? EXIT_SUCCESS : EXIT_FAILURE);
     128             : }

Generated by: LCOV version 1.10