xref: /illumos-gate/usr/src/contrib/ast/src/lib/libcmd/tty.c (revision b30d1939)
1 /***********************************************************************
2 *                                                                      *
3 *               This software is part of the ast package               *
4 *          Copyright (c) 1992-2012 AT&T Intellectual Property          *
5 *                      and is licensed under the                       *
6 *                 Eclipse Public License, Version 1.0                  *
7 *                    by AT&T Intellectual Property                     *
8 *                                                                      *
9 *                A copy of the License is available at                 *
10 *          http://www.eclipse.org/org/documents/epl-v10.html           *
11 *         (with md5 checksum b35adb5213ca9657e911e9befb180842)         *
12 *                                                                      *
13 *              Information and Software Systems Research               *
14 *                            AT&T Research                             *
15 *                           Florham Park NJ                            *
16 *                                                                      *
17 *                 Glenn Fowler <gsf@research.att.com>                  *
18 *                  David Korn <dgk@research.att.com>                   *
19 *                                                                      *
20 ***********************************************************************/
21 #pragma prototyped
22 /*
23  * David Korn
24  * AT&T Bell Laboratories
25  *
26  * tty
27  */
28 
29 static const char usage[] =
30 "[-?\n@(#)$Id: tty (AT&T Research) 2008-03-13 $\n]"
31 USAGE_LICENSE
32 "[+NAME?tty - write the name of the terminal to standard output]"
33 "[+DESCRIPTION?\btty\b writes the name of the terminal that is connected "
34 	"to standard input onto standard output.  If the standard input is not "
35 	"a terminal, \"\bnot a tty\b\" will be written to standard output.]"
36 "[l:line-number?Write the synchronous line number of the terminal on a "
37 	"separate line following the terminal name line. If the standard "
38 	"input is not a synchronous  terminal then "
39 	"\"\bnot on an active synchronous line\b\" is written.]"
40 "[s:silent|quiet?Disable the terminal name line. Use \b[[ -t 0 ]]]]\b instead.]"
41 "[+EXIT STATUS?]{"
42         "[+0?Standard input is a tty.]"
43         "[+1?Standard input is not a tty.]"
44         "[+2?Invalid arguments.]"
45         "[+3?A an error occurred.]"
46 "}"
47 ;
48 
49 
50 #include <cmd.h>
51 
52 #if _mac_STWLINE
53 #include <sys/stermio.h>
54 #endif
55 
56 int
b_tty(int argc,char ** argv,Shbltin_t * context)57 b_tty(int argc, char** argv, Shbltin_t* context)
58 {
59 	register int	sflag = 0;
60 	register int	lflag = 0;
61 	register char*	tty;
62 #if _mac_STWLINE
63 	int		n;
64 #endif
65 
66 	cmdinit(argc, argv, context, ERROR_CATALOG, 0);
67 	for (;;)
68 	{
69 		switch (optget(argv, usage))
70 		{
71 		case 'l':
72 			lflag++;
73 			continue;
74 		case 's':
75 			sflag++;
76 			continue;
77 		case ':':
78 			error(2, "%s", opt_info.arg);
79 			break;
80 		case '?':
81 			error(ERROR_usage(2), "%s", opt_info.arg);
82 			break;
83 		}
84 		break;
85 	}
86 	if(error_info.errors)
87 		error(ERROR_usage(2), "%s", optusage(NiL));
88 	if(!(tty=ttyname(0)))
89 	{
90 		tty = ERROR_translate(0, 0, 0, "not a tty");
91 		error_info.errors++;
92 	}
93 	if(!sflag)
94 		sfputr(sfstdout,tty,'\n');
95 	if(lflag)
96 	{
97 #if _mac_STWLINE
98 		if ((n = ioctl(0, STWLINE, 0)) >= 0)
99 			error(ERROR_OUTPUT, 1, "synchronous line %d", n);
100 		else
101 #endif
102 			error(ERROR_OUTPUT, 1, "not on an active synchronous line");
103 	}
104 	return(error_info.errors);
105 }
106