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