1/***********************************************************************
2*                                                                      *
3*               This software is part of the ast package               *
4*          Copyright (c) 1992-2010 AT&T Intellectual Property          *
5*                      and is licensed under the                       *
6*                  Common Public License, Version 1.0                  *
7*                    by AT&T Intellectual Property                     *
8*                                                                      *
9*                A copy of the License is available at                 *
10*            http://www.opensource.org/licenses/cpl1.0.txt             *
11*         (with md5 checksum 059e8cd6165cb4c31e351f2b69388fd9)         *
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
29static const char usage[] =
30"[-?\n@(#)$Id: tty (AT&T Research) 2008-03-13 $\n]"
31USAGE_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
56int
57b_tty(int argc, char *argv[], void* context)
58{
59	register int n,sflag=0,lflag=0;
60	register char *tty;
61
62	cmdinit(argc, argv, context, ERROR_CATALOG, 0);
63	while (n = optget(argv, usage)) switch (n)
64	{
65	case 'l':
66		lflag++;
67		break;
68	case 's':
69		sflag++;
70		break;
71	case ':':
72		error(2, "%s", opt_info.arg);
73		break;
74	case '?':
75		error(ERROR_usage(2), "%s", opt_info.arg);
76		break;
77	}
78	if(error_info.errors)
79		error(ERROR_usage(2), "%s", optusage(NiL));
80	if(!(tty=ttyname(0)))
81	{
82		tty = ERROR_translate(0, 0, 0, "not a tty");
83		error_info.errors++;
84	}
85	if(!sflag)
86		sfputr(sfstdout,tty,'\n');
87	if(lflag)
88	{
89#if _mac_STWLINE
90		if (n = ioctl(0, STWLINE, 0)) >= 0)
91			error(ERROR_OUTPUT, 1, "synchronous line %d", n);
92		else
93#endif
94			error(ERROR_OUTPUT, 1, "not on an active synchronous line");
95	}
96	return(error_info.errors);
97}
98