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