xref: /onnv-gate/usr/src/lib/libcmd/common/tty.c (revision 12068:08a39a083754)
14887Schin /***********************************************************************
24887Schin *                                                                      *
34887Schin *               This software is part of the ast package               *
4*12068SRoger.Faulkner@Oracle.COM *          Copyright (c) 1992-2010 AT&T Intellectual Property          *
54887Schin *                      and is licensed under the                       *
64887Schin *                  Common Public License, Version 1.0                  *
78462SApril.Chin@Sun.COM *                    by AT&T Intellectual Property                     *
84887Schin *                                                                      *
94887Schin *                A copy of the License is available at                 *
104887Schin *            http://www.opensource.org/licenses/cpl1.0.txt             *
114887Schin *         (with md5 checksum 059e8cd6165cb4c31e351f2b69388fd9)         *
124887Schin *                                                                      *
134887Schin *              Information and Software Systems Research               *
144887Schin *                            AT&T Research                             *
154887Schin *                           Florham Park NJ                            *
164887Schin *                                                                      *
174887Schin *                 Glenn Fowler <gsf@research.att.com>                  *
184887Schin *                  David Korn <dgk@research.att.com>                   *
194887Schin *                                                                      *
204887Schin ***********************************************************************/
214887Schin #pragma prototyped
224887Schin /*
234887Schin  * David Korn
244887Schin  * AT&T Bell Laboratories
254887Schin  *
264887Schin  * tty
274887Schin  */
284887Schin 
294887Schin static const char usage[] =
308462SApril.Chin@Sun.COM "[-?\n@(#)$Id: tty (AT&T Research) 2008-03-13 $\n]"
314887Schin USAGE_LICENSE
324887Schin "[+NAME?tty - write the name of the terminal to standard output]"
334887Schin "[+DESCRIPTION?\btty\b writes the name of the terminal that is connected "
344887Schin 	"to standard input onto standard output.  If the standard input is not "
354887Schin 	"a terminal, \"\bnot a tty\b\" will be written to standard output.]"
364887Schin "[l:line-number?Write the synchronous line number of the terminal on a "
374887Schin 	"separate line following the terminal name line. If the standard "
384887Schin 	"input is not a synchronous  terminal then "
394887Schin 	"\"\bnot on an active synchronous line\b\" is written.]"
404887Schin "[s:silent|quiet?Disable the terminal name line. Use \b[[ -t 0 ]]]]\b instead.]"
414887Schin "[+EXIT STATUS?]{"
424887Schin         "[+0?Standard input is a tty.]"
434887Schin         "[+1?Standard input is not a tty.]"
444887Schin         "[+2?Invalid arguments.]"
454887Schin         "[+3?A an error occurred.]"
464887Schin "}"
474887Schin ;
484887Schin 
494887Schin 
504887Schin #include <cmd.h>
514887Schin 
524887Schin #if _mac_STWLINE
534887Schin #include <sys/stermio.h>
544887Schin #endif
554887Schin 
564887Schin int
b_tty(int argc,char * argv[],void * context)574887Schin b_tty(int argc, char *argv[], void* context)
584887Schin {
594887Schin 	register int n,sflag=0,lflag=0;
604887Schin 	register char *tty;
614887Schin 
624887Schin 	cmdinit(argc, argv, context, ERROR_CATALOG, 0);
634887Schin 	while (n = optget(argv, usage)) switch (n)
644887Schin 	{
654887Schin 	case 'l':
664887Schin 		lflag++;
674887Schin 		break;
684887Schin 	case 's':
694887Schin 		sflag++;
704887Schin 		break;
714887Schin 	case ':':
724887Schin 		error(2, "%s", opt_info.arg);
734887Schin 		break;
744887Schin 	case '?':
754887Schin 		error(ERROR_usage(2), "%s", opt_info.arg);
764887Schin 		break;
774887Schin 	}
784887Schin 	if(error_info.errors)
794887Schin 		error(ERROR_usage(2), "%s", optusage(NiL));
804887Schin 	if(!(tty=ttyname(0)))
814887Schin 	{
824887Schin 		tty = ERROR_translate(0, 0, 0, "not a tty");
834887Schin 		error_info.errors++;
844887Schin 	}
854887Schin 	if(!sflag)
864887Schin 		sfputr(sfstdout,tty,'\n');
878462SApril.Chin@Sun.COM 	if(lflag)
888462SApril.Chin@Sun.COM 	{
894887Schin #if _mac_STWLINE
908462SApril.Chin@Sun.COM 		if (n = ioctl(0, STWLINE, 0)) >= 0)
918462SApril.Chin@Sun.COM 			error(ERROR_OUTPUT, 1, "synchronous line %d", n);
928462SApril.Chin@Sun.COM 		else
934887Schin #endif
948462SApril.Chin@Sun.COM 			error(ERROR_OUTPUT, 1, "not on an active synchronous line");
958462SApril.Chin@Sun.COM 	}
964887Schin 	return(error_info.errors);
974887Schin }
98