]> git.ipfire.org Git - thirdparty/bash.git/blob - examples/loadables/tty.c
bash-4.4 beta release
[thirdparty/bash.git] / examples / loadables / tty.c
1 /* tty - return terminal name */
2
3 /* See Makefile for compilation details. */
4
5 /*
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
23 #include "config.h"
24
25 #include <stdio.h>
26 #include "builtins.h"
27 #include "shell.h"
28 #include "bashgetopt.h"
29 #include "common.h"
30
31 extern char *ttyname ();
32
33 int
34 tty_builtin (list)
35 WORD_LIST *list;
36 {
37 int opt, sflag;
38 char *t;
39
40 reset_internal_getopt ();
41 sflag = 0;
42 while ((opt = internal_getopt (list, "s")) != -1)
43 {
44 switch (opt)
45 {
46 case 's':
47 sflag = 1;
48 break;
49 default:
50 builtin_usage ();
51 return (EX_USAGE);
52 }
53 }
54 list = loptend;
55
56 t = ttyname (0);
57 if (sflag == 0)
58 puts (t ? t : "not a tty");
59 return (t ? EXECUTION_SUCCESS : EXECUTION_FAILURE);
60 }
61
62 char *tty_doc[] = {
63 "Display terminal name.",
64 "",
65 "tty writes the name of the terminal that is opened for standard",
66 "input to standard output. If the `-s' option is supplied, nothing",
67 "is written; the exit status determines whether or not the standard",
68 "input is connected to a tty.",
69 (char *)NULL
70 };
71
72 /* The standard structure describing a builtin command. bash keeps an array
73 of these structures. */
74 struct builtin tty_struct = {
75 "tty", /* builtin name */
76 tty_builtin, /* function implementing the builtin */
77 BUILTIN_ENABLED, /* initial flags for builtin */
78 tty_doc, /* array of long documentation strings. */
79 "tty [-s]", /* usage synopsis; becomes short_doc */
80 0 /* reserved for internal use */
81 };