blob: 1d88506ca482bd61a8ef303cf175674fefa49480 [file] [log] [blame]
Jari Aaltoccc6cda1996-12-23 17:02:34 +00001/* tty - return terminal name */
2
3/* See Makefile for compilation details. */
4
Jari Aalto31859422009-01-12 13:36:28 +00005/*
6 Copyright (C) 1999-2009 Free Software Foundation, Inc.
7
8 This file is part of GNU Bash.
9 Bash is free software: you can redistribute it and/or modify
10 it under the terms of the GNU General Public License as published by
11 the Free Software Foundation, either version 3 of the License, or
12 (at your option) any later version.
13
14 Bash is distributed in the hope that it will be useful,
15 but WITHOUT ANY WARRANTY; without even the implied warranty of
16 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 GNU General Public License for more details.
18
19 You should have received a copy of the GNU General Public License
20 along with Bash. If not, see <http://www.gnu.org/licenses/>.
21*/
22
Jari Aaltoccc6cda1996-12-23 17:02:34 +000023#include "config.h"
24
25#include <stdio.h>
26#include "builtins.h"
27#include "shell.h"
28#include "bashgetopt.h"
Jari Aalto31859422009-01-12 13:36:28 +000029#include "common.h"
Jari Aaltoccc6cda1996-12-23 17:02:34 +000030
31extern char *ttyname ();
32
33tty_builtin (list)
34 WORD_LIST *list;
35{
36 int opt, sflag;
37 char *t;
38
39 reset_internal_getopt ();
40 sflag = 0;
41 while ((opt = internal_getopt (list, "s")) != -1)
42 {
43 switch (opt)
44 {
45 case 's':
46 sflag = 1;
47 break;
48 default:
49 builtin_usage ();
50 return (EX_USAGE);
51 }
52 }
53 list = loptend;
54
55 t = ttyname (0);
56 if (sflag == 0)
57 puts (t ? t : "not a tty");
58 return (t ? EXECUTION_SUCCESS : EXECUTION_FAILURE);
59}
60
61char *tty_doc[] = {
Jari Aalto31859422009-01-12 13:36:28 +000062 "Display terminal name.",
63 "",
Jari Aaltoccc6cda1996-12-23 17:02:34 +000064 "tty writes the name of the terminal that is opened for standard",
65 "input to standard output. If the `-s' option is supplied, nothing",
66 "is written; the exit status determines whether or not the standard",
67 "input is connected to a tty.",
68 (char *)NULL
69};
70
71/* The standard structure describing a builtin command. bash keeps an array
72 of these structures. */
73struct builtin tty_struct = {
74 "tty", /* builtin name */
75 tty_builtin, /* function implementing the builtin */
76 BUILTIN_ENABLED, /* initial flags for builtin */
77 tty_doc, /* array of long documentation strings. */
78 "tty [-s]", /* usage synopsis; becomes short_doc */
79 0 /* reserved for internal use */
80};