Home | History | Annotate | Line # | Download | only in getty
main.c revision 1.3
      1  1.1      cgd /*-
      2  1.1      cgd  * Copyright (c) 1980 The Regents of the University of California.
      3  1.1      cgd  * All rights reserved.
      4  1.1      cgd  *
      5  1.1      cgd  * Redistribution and use in source and binary forms, with or without
      6  1.1      cgd  * modification, are permitted provided that the following conditions
      7  1.1      cgd  * are met:
      8  1.1      cgd  * 1. Redistributions of source code must retain the above copyright
      9  1.1      cgd  *    notice, this list of conditions and the following disclaimer.
     10  1.1      cgd  * 2. Redistributions in binary form must reproduce the above copyright
     11  1.1      cgd  *    notice, this list of conditions and the following disclaimer in the
     12  1.1      cgd  *    documentation and/or other materials provided with the distribution.
     13  1.1      cgd  * 3. All advertising materials mentioning features or use of this software
     14  1.1      cgd  *    must display the following acknowledgement:
     15  1.1      cgd  *	This product includes software developed by the University of
     16  1.1      cgd  *	California, Berkeley and its contributors.
     17  1.1      cgd  * 4. Neither the name of the University nor the names of its contributors
     18  1.1      cgd  *    may be used to endorse or promote products derived from this software
     19  1.1      cgd  *    without specific prior written permission.
     20  1.1      cgd  *
     21  1.1      cgd  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
     22  1.1      cgd  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     23  1.1      cgd  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     24  1.1      cgd  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
     25  1.1      cgd  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     26  1.1      cgd  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
     27  1.1      cgd  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     28  1.1      cgd  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     29  1.1      cgd  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     30  1.1      cgd  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     31  1.1      cgd  * SUCH DAMAGE.
     32  1.1      cgd  */
     33  1.1      cgd 
     34  1.1      cgd #ifndef lint
     35  1.1      cgd char copyright[] =
     36  1.1      cgd "@(#) Copyright (c) 1980 The Regents of the University of California.\n\
     37  1.1      cgd  All rights reserved.\n";
     38  1.1      cgd #endif /* not lint */
     39  1.1      cgd 
     40  1.1      cgd #ifndef lint
     41  1.2  mycroft /*static char sccsid[] = "from: @(#)main.c	5.16 (Berkeley) 3/27/91";*/
     42  1.3  mycroft static char rcsid[] = "$Id: main.c,v 1.3 1993/08/29 22:47:03 mycroft Exp $";
     43  1.1      cgd #endif /* not lint */
     44  1.1      cgd 
     45  1.1      cgd #define USE_OLD_TTY
     46  1.1      cgd 
     47  1.1      cgd #include <sys/param.h>
     48  1.1      cgd #include <sys/stat.h>
     49  1.3  mycroft #include <sys/utsname.h>
     50  1.1      cgd #include <signal.h>
     51  1.1      cgd #include <fcntl.h>
     52  1.1      cgd #include <sgtty.h>
     53  1.1      cgd #include <time.h>
     54  1.1      cgd #include <ctype.h>
     55  1.1      cgd #include <setjmp.h>
     56  1.1      cgd #include <syslog.h>
     57  1.1      cgd #include <unistd.h>
     58  1.1      cgd #include <ctype.h>
     59  1.1      cgd #include <stdlib.h>
     60  1.1      cgd #include <string.h>
     61  1.1      cgd #include "gettytab.h"
     62  1.1      cgd #include "pathnames.h"
     63  1.1      cgd 
     64  1.1      cgd struct	sgttyb tmode = {
     65  1.1      cgd 	0, 0, CERASE, CKILL, 0
     66  1.1      cgd };
     67  1.1      cgd struct	tchars tc = {
     68  1.1      cgd 	CINTR, CQUIT, CSTART,
     69  1.1      cgd 	CSTOP, CEOF, CBRK,
     70  1.1      cgd };
     71  1.1      cgd struct	ltchars ltc = {
     72  1.1      cgd 	CSUSP, CDSUSP, CRPRNT,
     73  1.1      cgd 	CFLUSH, CWERASE, CLNEXT
     74  1.1      cgd };
     75  1.1      cgd 
     76  1.1      cgd int crmod, digit, lower, upper;
     77  1.1      cgd 
     78  1.1      cgd char	hostname[MAXHOSTNAMELEN];
     79  1.3  mycroft struct	utsname kerninfo;
     80  1.1      cgd char	name[16];
     81  1.1      cgd char	dev[] = _PATH_DEV;
     82  1.1      cgd char	ttyn[32];
     83  1.1      cgd char	*portselector();
     84  1.1      cgd char	*ttyname();
     85  1.1      cgd 
     86  1.1      cgd #define	OBUFSIZ		128
     87  1.1      cgd #define	TABBUFSIZ	512
     88  1.1      cgd 
     89  1.1      cgd char	defent[TABBUFSIZ];
     90  1.1      cgd char	defstrs[TABBUFSIZ];
     91  1.1      cgd char	tabent[TABBUFSIZ];
     92  1.1      cgd char	tabstrs[TABBUFSIZ];
     93  1.1      cgd 
     94  1.1      cgd char	*env[128];
     95  1.1      cgd 
     96  1.1      cgd char partab[] = {
     97  1.1      cgd 	0001,0201,0201,0001,0201,0001,0001,0201,
     98  1.1      cgd 	0202,0004,0003,0205,0005,0206,0201,0001,
     99  1.1      cgd 	0201,0001,0001,0201,0001,0201,0201,0001,
    100  1.1      cgd 	0001,0201,0201,0001,0201,0001,0001,0201,
    101  1.1      cgd 	0200,0000,0000,0200,0000,0200,0200,0000,
    102  1.1      cgd 	0000,0200,0200,0000,0200,0000,0000,0200,
    103  1.1      cgd 	0000,0200,0200,0000,0200,0000,0000,0200,
    104  1.1      cgd 	0200,0000,0000,0200,0000,0200,0200,0000,
    105  1.1      cgd 	0200,0000,0000,0200,0000,0200,0200,0000,
    106  1.1      cgd 	0000,0200,0200,0000,0200,0000,0000,0200,
    107  1.1      cgd 	0000,0200,0200,0000,0200,0000,0000,0200,
    108  1.1      cgd 	0200,0000,0000,0200,0000,0200,0200,0000,
    109  1.1      cgd 	0000,0200,0200,0000,0200,0000,0000,0200,
    110  1.1      cgd 	0200,0000,0000,0200,0000,0200,0200,0000,
    111  1.1      cgd 	0200,0000,0000,0200,0000,0200,0200,0000,
    112  1.1      cgd 	0000,0200,0200,0000,0200,0000,0000,0201
    113  1.1      cgd };
    114  1.1      cgd 
    115  1.1      cgd #define	ERASE	tmode.sg_erase
    116  1.1      cgd #define	KILL	tmode.sg_kill
    117  1.1      cgd #define	EOT	tc.t_eofc
    118  1.1      cgd 
    119  1.1      cgd jmp_buf timeout;
    120  1.1      cgd 
    121  1.1      cgd static void
    122  1.1      cgd dingdong()
    123  1.1      cgd {
    124  1.1      cgd 
    125  1.1      cgd 	alarm(0);
    126  1.1      cgd 	signal(SIGALRM, SIG_DFL);
    127  1.1      cgd 	longjmp(timeout, 1);
    128  1.1      cgd }
    129  1.1      cgd 
    130  1.1      cgd jmp_buf	intrupt;
    131  1.1      cgd 
    132  1.1      cgd static void
    133  1.1      cgd interrupt()
    134  1.1      cgd {
    135  1.1      cgd 
    136  1.1      cgd 	signal(SIGINT, interrupt);
    137  1.1      cgd 	longjmp(intrupt, 1);
    138  1.1      cgd }
    139  1.1      cgd 
    140  1.1      cgd main(argc, argv)
    141  1.1      cgd 	int argc;
    142  1.1      cgd 	char **argv;
    143  1.1      cgd {
    144  1.1      cgd 	extern	char **environ;
    145  1.1      cgd 	char *tname;
    146  1.1      cgd 	long allflags;
    147  1.1      cgd 	int repcnt = 0;
    148  1.1      cgd 
    149  1.1      cgd 	signal(SIGINT, SIG_IGN);
    150  1.1      cgd /*
    151  1.1      cgd 	signal(SIGQUIT, SIG_DFL);
    152  1.1      cgd */
    153  1.1      cgd 	openlog("getty", LOG_ODELAY|LOG_CONS, LOG_AUTH);
    154  1.1      cgd 	gethostname(hostname, sizeof(hostname));
    155  1.1      cgd 	if (hostname[0] == '\0')
    156  1.1      cgd 		strcpy(hostname, "Amnesiac");
    157  1.3  mycroft 	uname(&kerninfo);
    158  1.1      cgd 	/*
    159  1.1      cgd 	 * The following is a work around for vhangup interactions
    160  1.1      cgd 	 * which cause great problems getting window systems started.
    161  1.1      cgd 	 * If the tty line is "-", we do the old style getty presuming
    162  1.1      cgd 	 * that the file descriptors are already set up for us.
    163  1.1      cgd 	 * J. Gettys - MIT Project Athena.
    164  1.1      cgd 	 */
    165  1.1      cgd 	if (argc <= 2 || strcmp(argv[2], "-") == 0)
    166  1.1      cgd 	    strcpy(ttyn, ttyname(0));
    167  1.1      cgd 	else {
    168  1.1      cgd 	    int i;
    169  1.1      cgd 
    170  1.1      cgd 	    strcpy(ttyn, dev);
    171  1.1      cgd 	    strncat(ttyn, argv[2], sizeof(ttyn)-sizeof(dev));
    172  1.1      cgd 	    if (strcmp(argv[0], "+") != 0) {
    173  1.1      cgd 		chown(ttyn, 0, 0);
    174  1.1      cgd 		chmod(ttyn, 0600);
    175  1.1      cgd 		revoke(ttyn);
    176  1.1      cgd 		/*
    177  1.1      cgd 		 * Delay the open so DTR stays down long enough to be detected.
    178  1.1      cgd 		 */
    179  1.1      cgd 		sleep(2);
    180  1.1      cgd 		while ((i = open(ttyn, O_RDWR)) == -1) {
    181  1.1      cgd 			if (repcnt % 10 == 0) {
    182  1.1      cgd 				syslog(LOG_ERR, "%s: %m", ttyn);
    183  1.1      cgd 				closelog();
    184  1.1      cgd 			}
    185  1.1      cgd 			repcnt++;
    186  1.1      cgd 			sleep(60);
    187  1.1      cgd 		}
    188  1.1      cgd 		login_tty(i);
    189  1.1      cgd 	    }
    190  1.1      cgd 	}
    191  1.1      cgd 
    192  1.1      cgd 	gettable("default", defent, defstrs);
    193  1.1      cgd 	gendefaults();
    194  1.1      cgd 	tname = "default";
    195  1.1      cgd 	if (argc > 1)
    196  1.1      cgd 		tname = argv[1];
    197  1.1      cgd 	for (;;) {
    198  1.1      cgd 		int ldisp = OTTYDISC;
    199  1.1      cgd 		int off = 0;
    200  1.1      cgd 
    201  1.1      cgd 		gettable(tname, tabent, tabstrs);
    202  1.1      cgd 		if (OPset || EPset || APset)
    203  1.1      cgd 			APset++, OPset++, EPset++;
    204  1.1      cgd 		setdefaults();
    205  1.1      cgd 		ioctl(0, TIOCFLUSH, 0);		/* clear out the crap */
    206  1.1      cgd 		ioctl(0, FIONBIO, &off);	/* turn off non-blocking mode */
    207  1.1      cgd 		ioctl(0, FIOASYNC, &off);	/* ditto for async mode */
    208  1.1      cgd 		if (IS)
    209  1.1      cgd 			tmode.sg_ispeed = speed(IS);
    210  1.1      cgd 		else if (SP)
    211  1.1      cgd 			tmode.sg_ispeed = speed(SP);
    212  1.1      cgd 		if (OS)
    213  1.1      cgd 			tmode.sg_ospeed = speed(OS);
    214  1.1      cgd 		else if (SP)
    215  1.1      cgd 			tmode.sg_ospeed = speed(SP);
    216  1.1      cgd 		tmode.sg_flags = setflags(0);
    217  1.1      cgd 		ioctl(0, TIOCSETP, &tmode);
    218  1.1      cgd 		setchars();
    219  1.1      cgd 		ioctl(0, TIOCSETC, &tc);
    220  1.1      cgd 		if (HC)
    221  1.1      cgd 			ioctl(0, TIOCHPCL, 0);
    222  1.1      cgd 		if (AB) {
    223  1.1      cgd 			extern char *autobaud();
    224  1.1      cgd 
    225  1.1      cgd 			tname = autobaud();
    226  1.1      cgd 			continue;
    227  1.1      cgd 		}
    228  1.1      cgd 		if (PS) {
    229  1.1      cgd 			tname = portselector();
    230  1.1      cgd 			continue;
    231  1.1      cgd 		}
    232  1.1      cgd 		if (CL && *CL)
    233  1.1      cgd 			putpad(CL);
    234  1.1      cgd 		edithost(HE);
    235  1.1      cgd 		if (IM && *IM)
    236  1.1      cgd 			putf(IM);
    237  1.1      cgd 		if (setjmp(timeout)) {
    238  1.1      cgd 			tmode.sg_ispeed = tmode.sg_ospeed = 0;
    239  1.1      cgd 			ioctl(0, TIOCSETP, &tmode);
    240  1.1      cgd 			exit(1);
    241  1.1      cgd 		}
    242  1.1      cgd 		if (TO) {
    243  1.1      cgd 			signal(SIGALRM, dingdong);
    244  1.1      cgd 			alarm(TO);
    245  1.1      cgd 		}
    246  1.1      cgd 		if (getname()) {
    247  1.1      cgd 			register int i;
    248  1.1      cgd 
    249  1.1      cgd 			oflush();
    250  1.1      cgd 			alarm(0);
    251  1.1      cgd 			signal(SIGALRM, SIG_DFL);
    252  1.1      cgd 			if (name[0] == '-') {
    253  1.1      cgd 				puts("user names may not start with '-'.");
    254  1.1      cgd 				continue;
    255  1.1      cgd 			}
    256  1.1      cgd 			if (!(upper || lower || digit))
    257  1.1      cgd 				continue;
    258  1.1      cgd 			allflags = setflags(2);
    259  1.1      cgd 			tmode.sg_flags = allflags & 0xffff;
    260  1.1      cgd 			allflags >>= 16;
    261  1.1      cgd 			if (crmod || NL)
    262  1.1      cgd 				tmode.sg_flags |= CRMOD;
    263  1.1      cgd 			if (upper || UC)
    264  1.1      cgd 				tmode.sg_flags |= LCASE;
    265  1.1      cgd 			if (lower || LC)
    266  1.1      cgd 				tmode.sg_flags &= ~LCASE;
    267  1.1      cgd 			ioctl(0, TIOCSETP, &tmode);
    268  1.1      cgd 			ioctl(0, TIOCSLTC, &ltc);
    269  1.1      cgd 			ioctl(0, TIOCLSET, &allflags);
    270  1.1      cgd 			signal(SIGINT, SIG_DFL);
    271  1.1      cgd 			for (i = 0; environ[i] != (char *)0; i++)
    272  1.1      cgd 				env[i] = environ[i];
    273  1.1      cgd 			makeenv(&env[i]);
    274  1.1      cgd 
    275  1.1      cgd 			/*
    276  1.1      cgd 			 * this is what login was doing anyway.
    277  1.1      cgd 			 * soon we rewrite getty completely.
    278  1.1      cgd 			 */
    279  1.1      cgd 			set_ttydefaults(0);
    280  1.1      cgd 			execle(LO, "login", "-p", name, (char *) 0, env);
    281  1.1      cgd 			syslog(LOG_ERR, "%s: %m", LO);
    282  1.1      cgd 			exit(1);
    283  1.1      cgd 		}
    284  1.1      cgd 		alarm(0);
    285  1.1      cgd 		signal(SIGALRM, SIG_DFL);
    286  1.1      cgd 		signal(SIGINT, SIG_IGN);
    287  1.1      cgd 		if (NX && *NX)
    288  1.1      cgd 			tname = NX;
    289  1.1      cgd 	}
    290  1.1      cgd }
    291  1.1      cgd 
    292  1.1      cgd getname()
    293  1.1      cgd {
    294  1.1      cgd 	register int c;
    295  1.1      cgd 	register char *np;
    296  1.1      cgd 	char cs;
    297  1.1      cgd 
    298  1.1      cgd 	/*
    299  1.1      cgd 	 * Interrupt may happen if we use CBREAK mode
    300  1.1      cgd 	 */
    301  1.1      cgd 	if (setjmp(intrupt)) {
    302  1.1      cgd 		signal(SIGINT, SIG_IGN);
    303  1.1      cgd 		return (0);
    304  1.1      cgd 	}
    305  1.1      cgd 	signal(SIGINT, interrupt);
    306  1.1      cgd 	tmode.sg_flags = setflags(0);
    307  1.1      cgd 	ioctl(0, TIOCSETP, &tmode);
    308  1.1      cgd 	tmode.sg_flags = setflags(1);
    309  1.1      cgd 	prompt();
    310  1.1      cgd 	if (PF > 0) {
    311  1.1      cgd 		oflush();
    312  1.1      cgd 		sleep(PF);
    313  1.1      cgd 		PF = 0;
    314  1.1      cgd 	}
    315  1.1      cgd 	ioctl(0, TIOCSETP, &tmode);
    316  1.1      cgd 	crmod = digit = lower = upper = 0;
    317  1.1      cgd 	np = name;
    318  1.1      cgd 	for (;;) {
    319  1.1      cgd 		oflush();
    320  1.1      cgd 		if (read(STDIN_FILENO, &cs, 1) <= 0)
    321  1.1      cgd 			exit(0);
    322  1.1      cgd 		if ((c = cs&0177) == 0)
    323  1.1      cgd 			return (0);
    324  1.1      cgd 		if (c == EOT)
    325  1.1      cgd 			exit(1);
    326  1.1      cgd 		if (c == '\r' || c == '\n' || np >= &name[sizeof name]) {
    327  1.1      cgd 			putf("\r\n");
    328  1.1      cgd 			break;
    329  1.1      cgd 		}
    330  1.1      cgd 		if (islower(c))
    331  1.1      cgd 			lower = 1;
    332  1.1      cgd 		else if (isupper(c))
    333  1.1      cgd 			upper = 1;
    334  1.1      cgd 		else if (c == ERASE || c == '#' || c == '\b') {
    335  1.1      cgd 			if (np > name) {
    336  1.1      cgd 				np--;
    337  1.1      cgd 				if (tmode.sg_ospeed >= B1200)
    338  1.1      cgd 					puts("\b \b");
    339  1.1      cgd 				else
    340  1.1      cgd 					putchr(cs);
    341  1.1      cgd 			}
    342  1.1      cgd 			continue;
    343  1.1      cgd 		} else if (c == KILL || c == '@') {
    344  1.1      cgd 			putchr(cs);
    345  1.1      cgd 			putchr('\r');
    346  1.1      cgd 			if (tmode.sg_ospeed < B1200)
    347  1.1      cgd 				putchr('\n');
    348  1.1      cgd 			/* this is the way they do it down under ... */
    349  1.1      cgd 			else if (np > name)
    350  1.1      cgd 				puts("                                     \r");
    351  1.1      cgd 			prompt();
    352  1.1      cgd 			np = name;
    353  1.1      cgd 			continue;
    354  1.1      cgd 		} else if (isdigit(c))
    355  1.1      cgd 			digit++;
    356  1.1      cgd 		if (IG && (c <= ' ' || c > 0176))
    357  1.1      cgd 			continue;
    358  1.1      cgd 		*np++ = c;
    359  1.1      cgd 		putchr(cs);
    360  1.1      cgd 	}
    361  1.1      cgd 	signal(SIGINT, SIG_IGN);
    362  1.1      cgd 	*np = 0;
    363  1.1      cgd 	if (c == '\r')
    364  1.1      cgd 		crmod = 1;
    365  1.1      cgd 	if (upper && !lower && !LC || UC)
    366  1.1      cgd 		for (np = name; *np; np++)
    367  1.1      cgd 			if (isupper(*np))
    368  1.1      cgd 				*np = tolower(*np);
    369  1.1      cgd 	return (1);
    370  1.1      cgd }
    371  1.1      cgd 
    372  1.1      cgd static
    373  1.1      cgd short	tmspc10[] = {
    374  1.1      cgd 	0, 2000, 1333, 909, 743, 666, 500, 333, 166, 83, 55, 41, 20, 10, 5, 15
    375  1.1      cgd };
    376  1.1      cgd 
    377  1.1      cgd putpad(s)
    378  1.1      cgd 	register char *s;
    379  1.1      cgd {
    380  1.1      cgd 	register pad = 0;
    381  1.1      cgd 	register mspc10;
    382  1.1      cgd 
    383  1.1      cgd 	if (isdigit(*s)) {
    384  1.1      cgd 		while (isdigit(*s)) {
    385  1.1      cgd 			pad *= 10;
    386  1.1      cgd 			pad += *s++ - '0';
    387  1.1      cgd 		}
    388  1.1      cgd 		pad *= 10;
    389  1.1      cgd 		if (*s == '.' && isdigit(s[1])) {
    390  1.1      cgd 			pad += s[1] - '0';
    391  1.1      cgd 			s += 2;
    392  1.1      cgd 		}
    393  1.1      cgd 	}
    394  1.1      cgd 
    395  1.1      cgd 	puts(s);
    396  1.1      cgd 	/*
    397  1.1      cgd 	 * If no delay needed, or output speed is
    398  1.1      cgd 	 * not comprehensible, then don't try to delay.
    399  1.1      cgd 	 */
    400  1.1      cgd 	if (pad == 0)
    401  1.1      cgd 		return;
    402  1.1      cgd 	if (tmode.sg_ospeed <= 0 ||
    403  1.1      cgd 	    tmode.sg_ospeed >= (sizeof tmspc10 / sizeof tmspc10[0]))
    404  1.1      cgd 		return;
    405  1.1      cgd 
    406  1.1      cgd 	/*
    407  1.1      cgd 	 * Round up by a half a character frame, and then do the delay.
    408  1.1      cgd 	 * Too bad there are no user program accessible programmed delays.
    409  1.1      cgd 	 * Transmitting pad characters slows many terminals down and also
    410  1.1      cgd 	 * loads the system.
    411  1.1      cgd 	 */
    412  1.1      cgd 	mspc10 = tmspc10[tmode.sg_ospeed];
    413  1.1      cgd 	pad += mspc10 / 2;
    414  1.1      cgd 	for (pad /= mspc10; pad > 0; pad--)
    415  1.1      cgd 		putchr(*PC);
    416  1.1      cgd }
    417  1.1      cgd 
    418  1.1      cgd puts(s)
    419  1.1      cgd 	register char *s;
    420  1.1      cgd {
    421  1.1      cgd 	while (*s)
    422  1.1      cgd 		putchr(*s++);
    423  1.1      cgd }
    424  1.1      cgd 
    425  1.1      cgd char	outbuf[OBUFSIZ];
    426  1.1      cgd int	obufcnt = 0;
    427  1.1      cgd 
    428  1.1      cgd putchr(cc)
    429  1.1      cgd {
    430  1.1      cgd 	char c;
    431  1.1      cgd 
    432  1.1      cgd 	c = cc;
    433  1.1      cgd 	if (!NP) {
    434  1.1      cgd 		c |= partab[c&0177] & 0200;
    435  1.1      cgd 		if (OP)
    436  1.1      cgd 			c ^= 0200;
    437  1.1      cgd 	}
    438  1.1      cgd 	if (!UB) {
    439  1.1      cgd 		outbuf[obufcnt++] = c;
    440  1.1      cgd 		if (obufcnt >= OBUFSIZ)
    441  1.1      cgd 			oflush();
    442  1.1      cgd 	} else
    443  1.1      cgd 		write(STDOUT_FILENO, &c, 1);
    444  1.1      cgd }
    445  1.1      cgd 
    446  1.1      cgd oflush()
    447  1.1      cgd {
    448  1.1      cgd 	if (obufcnt)
    449  1.1      cgd 		write(STDOUT_FILENO, outbuf, obufcnt);
    450  1.1      cgd 	obufcnt = 0;
    451  1.1      cgd }
    452  1.1      cgd 
    453  1.1      cgd prompt()
    454  1.1      cgd {
    455  1.1      cgd 
    456  1.1      cgd 	putf(LM);
    457  1.1      cgd 	if (CO)
    458  1.1      cgd 		putchr('\n');
    459  1.1      cgd }
    460  1.1      cgd 
    461  1.1      cgd putf(cp)
    462  1.1      cgd 	register char *cp;
    463  1.1      cgd {
    464  1.1      cgd 	extern char editedhost[];
    465  1.1      cgd 	time_t t;
    466  1.1      cgd 	char *slash, db[100];
    467  1.1      cgd 
    468  1.1      cgd 	while (*cp) {
    469  1.1      cgd 		if (*cp != '%') {
    470  1.1      cgd 			putchr(*cp++);
    471  1.1      cgd 			continue;
    472  1.1      cgd 		}
    473  1.1      cgd 		switch (*++cp) {
    474  1.1      cgd 
    475  1.1      cgd 		case 't':
    476  1.1      cgd 			slash = rindex(ttyn, '/');
    477  1.1      cgd 			if (slash == (char *) 0)
    478  1.1      cgd 				puts(ttyn);
    479  1.1      cgd 			else
    480  1.1      cgd 				puts(&slash[1]);
    481  1.1      cgd 			break;
    482  1.1      cgd 
    483  1.1      cgd 		case 'h':
    484  1.1      cgd 			puts(editedhost);
    485  1.1      cgd 			break;
    486  1.1      cgd 
    487  1.1      cgd 		case 'd': {
    488  1.1      cgd 			static char fmt[] = "%l:% %P on %A, %d %B %Y";
    489  1.1      cgd 
    490  1.1      cgd 			fmt[4] = 'M';		/* I *hate* SCCS... */
    491  1.1      cgd 			(void)time(&t);
    492  1.1      cgd 			(void)strftime(db, sizeof(db), fmt, localtime(&t));
    493  1.1      cgd 			puts(db);
    494  1.3  mycroft 			break;
    495  1.3  mycroft 
    496  1.3  mycroft 		case 's':
    497  1.3  mycroft 			puts(kerninfo.sysname);
    498  1.3  mycroft 			break;
    499  1.3  mycroft 
    500  1.3  mycroft 		case 'm':
    501  1.3  mycroft 			puts(kerninfo.machine);
    502  1.3  mycroft 			break;
    503  1.3  mycroft 
    504  1.3  mycroft 		case 'r':
    505  1.3  mycroft 			puts(kerninfo.release);
    506  1.3  mycroft 			break;
    507  1.3  mycroft 
    508  1.3  mycroft 		case 'v':
    509  1.3  mycroft 			puts(kerninfo.version);
    510  1.1      cgd 			break;
    511  1.1      cgd 		}
    512  1.1      cgd 
    513  1.1      cgd 		case '%':
    514  1.1      cgd 			putchr('%');
    515  1.1      cgd 			break;
    516  1.1      cgd 		}
    517  1.1      cgd 		cp++;
    518  1.1      cgd 	}
    519  1.1      cgd }
    520