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