Patch-ID: bash32-032
[bash.git] / examples / loadables / tty.c
blob2183123f2f6820e63b8f123956c08307181d2bf8
1 /* tty - return terminal name */
3 /* See Makefile for compilation details. */
5 #include "config.h"
7 #include <stdio.h>
8 #include "builtins.h"
9 #include "shell.h"
10 #include "bashgetopt.h"
12 extern char *ttyname ();
14 tty_builtin (list)
15 WORD_LIST *list;
17 int opt, sflag;
18 char *t;
20 reset_internal_getopt ();
21 sflag = 0;
22 while ((opt = internal_getopt (list, "s")) != -1)
24 switch (opt)
26 case 's':
27 sflag = 1;
28 break;
29 default:
30 builtin_usage ();
31 return (EX_USAGE);
34 list = loptend;
36 t = ttyname (0);
37 if (sflag == 0)
38 puts (t ? t : "not a tty");
39 return (t ? EXECUTION_SUCCESS : EXECUTION_FAILURE);
42 char *tty_doc[] = {
43 "tty writes the name of the terminal that is opened for standard",
44 "input to standard output. If the `-s' option is supplied, nothing",
45 "is written; the exit status determines whether or not the standard",
46 "input is connected to a tty.",
47 (char *)NULL
50 /* The standard structure describing a builtin command. bash keeps an array
51 of these structures. */
52 struct builtin tty_struct = {
53 "tty", /* builtin name */
54 tty_builtin, /* function implementing the builtin */
55 BUILTIN_ENABLED, /* initial flags for builtin */
56 tty_doc, /* array of long documentation strings. */
57 "tty [-s]", /* usage synopsis; becomes short_doc */
58 0 /* reserved for internal use */