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