xref: /dragonfly/libexec/getty/main.c (revision 7bcb6caf)
1 /*-
2  * SPDX-License-Identifier: BSD-3-Clause
3  *
4  * Copyright (c) 1980, 1993
5  *	The Regents of the University of California.  All rights reserved.
6  *
7  * Redistribution and use in source and binary forms, with or without
8  * modification, are permitted provided that the following conditions
9  * are met:
10  * 1. Redistributions of source code must retain the above copyright
11  *    notice, this list of conditions and the following disclaimer.
12  * 2. Redistributions in binary form must reproduce the above copyright
13  *    notice, this list of conditions and the following disclaimer in the
14  *    documentation and/or other materials provided with the distribution.
15  * 3. Neither the name of the University nor the names of its contributors
16  *    may be used to endorse or promote products derived from this software
17  *    without specific prior written permission.
18  *
19  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
20  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
21  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
22  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
23  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
24  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
25  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
26  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
27  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
28  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
29  * SUCH DAMAGE.
30  *
31  * @(#)from: main.c	8.1 (Berkeley) 6/20/93
32  * $FreeBSD: head/libexec/getty/main.c 329992 2018-02-25 20:15:06Z trasz $
33  */
34 
35 #include <sys/param.h>
36 #include <sys/ioctl.h>
37 #include <sys/time.h>
38 #include <sys/resource.h>
39 #include <sys/stat.h>
40 #include <sys/ttydefaults.h>
41 #include <sys/utsname.h>
42 
43 #include <ctype.h>
44 #include <errno.h>
45 #include <fcntl.h>
46 #include <locale.h>
47 #include <libutil.h>
48 #include <setjmp.h>
49 #include <signal.h>
50 #include <stdlib.h>
51 #include <string.h>
52 #include <syslog.h>
53 #include <termios.h>
54 #include <time.h>
55 #include <unistd.h>
56 
57 #include "gettytab.h"
58 #include "extern.h"
59 #include "pathnames.h"
60 
61 /*
62  * Set the amount of running time that getty should accumulate
63  * before deciding that something is wrong and exit.
64  */
65 #define GETTY_TIMEOUT	60 /* seconds */
66 
67 #undef CTRL
68 #define CTRL(x)  (x&037)
69 
70 /* defines for auto detection of incoming PPP calls (->PAP/CHAP) */
71 
72 #define PPP_FRAME           0x7e  /* PPP Framing character */
73 #define PPP_STATION         0xff  /* "All Station" character */
74 #define PPP_ESCAPE          0x7d  /* Escape Character */
75 #define PPP_CONTROL         0x03  /* PPP Control Field */
76 #define PPP_CONTROL_ESCAPED 0x23  /* PPP Control Field, escaped */
77 #define PPP_LCP_HI          0xc0  /* LCP protocol - high byte */
78 #define PPP_LCP_LOW         0x21  /* LCP protocol - low byte */
79 
80 /* original mode; flags've been reset using values from <sys/ttydefaults.h> */
81 struct termios omode;
82 /* current mode */
83 struct termios tmode;
84 
85 static int crmod, digit, lower, upper;
86 
87 char	hostname[MAXHOSTNAMELEN];
88 static char	name[MAXLOGNAME*3];
89 static char	dev[] = _PATH_DEV;
90 static char	ttyn[32];
91 
92 #define	OBUFSIZ		128
93 #define	TABBUFSIZ	512
94 
95 static char	defent[TABBUFSIZ];
96 static char	tabent[TABBUFSIZ];
97 static const char	*tname;
98 
99 static char	*env[128];
100 
101 static char partab[] = {
102 	0001,0201,0201,0001,0201,0001,0001,0201,
103 	0202,0004,0003,0205,0005,0206,0201,0001,
104 	0201,0001,0001,0201,0001,0201,0201,0001,
105 	0001,0201,0201,0001,0201,0001,0001,0201,
106 	0200,0000,0000,0200,0000,0200,0200,0000,
107 	0000,0200,0200,0000,0200,0000,0000,0200,
108 	0000,0200,0200,0000,0200,0000,0000,0200,
109 	0200,0000,0000,0200,0000,0200,0200,0000,
110 	0200,0000,0000,0200,0000,0200,0200,0000,
111 	0000,0200,0200,0000,0200,0000,0000,0200,
112 	0000,0200,0200,0000,0200,0000,0000,0200,
113 	0200,0000,0000,0200,0000,0200,0200,0000,
114 	0000,0200,0200,0000,0200,0000,0000,0200,
115 	0200,0000,0000,0200,0000,0200,0200,0000,
116 	0200,0000,0000,0200,0000,0200,0200,0000,
117 	0000,0200,0200,0000,0200,0000,0000,0201
118 };
119 
120 #define	ERASE	tmode.c_cc[VERASE]
121 #define	KILL	tmode.c_cc[VKILL]
122 #define	EOT	tmode.c_cc[VEOF]
123 
124 #define	puts	Gputs
125 
126 static void	defttymode(void);
127 static void	dingdong(int);
128 static void	dogettytab(void);
129 static int	getname(void);
130 static void	interrupt(int);
131 static void	oflush(void);
132 static void	prompt(void);
133 static void	putchr(int);
134 static void	putf(const char *);
135 static void	putpad(const char *);
136 static void	puts(const char *);
137 static void	timeoverrun(int);
138 static char	*get_line(int);
139 static void	setttymode(int);
140 static int	opentty(const char *, int);
141 
142 static jmp_buf timeout;
143 
144 static void
145 dingdong(int signo __unused)
146 {
147 	alarm(0);
148 	longjmp(timeout, 1);
149 }
150 
151 static jmp_buf	intrupt;
152 
153 static void
154 interrupt(int signo __unused)
155 {
156 	longjmp(intrupt, 1);
157 }
158 
159 /*
160  * Action to take when getty is running too long.
161  */
162 static void
163 timeoverrun(int signo __unused)
164 {
165 
166 	syslog(LOG_ERR, "getty exiting due to excessive running time");
167 	exit(1);
168 }
169 
170 int
171 main(int argc, char *argv[])
172 {
173 	int first_sleep = 1, first_time = 1;
174 	struct rlimit limit;
175 	int rval;
176 
177 	signal(SIGINT, SIG_IGN);
178 	signal(SIGQUIT, SIG_IGN);
179 
180 	openlog("getty", LOG_CONS|LOG_PID, LOG_AUTH);
181 	gethostname(hostname, sizeof(hostname) - 1);
182 	hostname[sizeof(hostname) - 1] = '\0';
183 	if (hostname[0] == '\0')
184 		strcpy(hostname, "Amnesiac");
185 
186 	/*
187 	 * Limit running time to deal with broken or dead lines.
188 	 */
189 	(void)signal(SIGXCPU, timeoverrun);
190 	limit.rlim_max = RLIM_INFINITY;
191 	limit.rlim_cur = GETTY_TIMEOUT;
192 	(void)setrlimit(RLIMIT_CPU, &limit);
193 
194 	gettable("default", defent);
195 	gendefaults();
196 	tname = "default";
197 	if (argc > 1)
198 		tname = argv[1];
199 
200 	/*
201 	 * The following is a work around for vhangup interactions
202 	 * which cause great problems getting window systems started.
203 	 * If the tty line is "-", we do the old style getty presuming
204 	 * that the file descriptors are already set up for us.
205 	 * J. Gettys - MIT Project Athena.
206 	 */
207 	if (argc <= 2 || strcmp(argv[2], "-") == 0)
208 	    strcpy(ttyn, ttyname(STDIN_FILENO));
209 	else {
210 	    strcpy(ttyn, dev);
211 	    strncat(ttyn, argv[2], sizeof(ttyn)-sizeof(dev));
212 	    if (strcmp(argv[0], "+") != 0) {
213 		chown(ttyn, 0, 0);
214 		chmod(ttyn, 0600);
215 		revoke(ttyn);
216 
217 		/*
218 		 * Do the first scan through gettytab.
219 		 * Terminal mode parameters will be wrong until
220 		 * defttymode() called, but they're irrelevant for
221 		 * the initial setup of the terminal device.
222 		 */
223 		dogettytab();
224 
225 		/*
226 		 * Init or answer modem sequence has been specified.
227 		 */
228 		if (IC || AC) {
229 			if (!opentty(ttyn, O_RDWR|O_NONBLOCK))
230 				exit(1);
231 			defttymode();
232 			setttymode(1);
233 		}
234 
235 		if (IC) {
236 			if (getty_chat(IC, CT, DC) > 0) {
237 				syslog(LOG_ERR, "modem init problem on %s", ttyn);
238 				(void)tcsetattr(STDIN_FILENO, TCSANOW, &tmode);
239 				exit(1);
240 			}
241 		}
242 
243 		if (AC) {
244 			fd_set rfds;
245 			struct timeval to;
246 			int i;
247 
248 			FD_ZERO(&rfds);
249 			FD_SET(0, &rfds);
250 			to.tv_sec = RT;
251 			to.tv_usec = 0;
252 			i = select(32, &rfds, NULL, NULL, RT ? &to : NULL);
253 			if (i < 0) {
254 				syslog(LOG_ERR, "select %s: %m", ttyn);
255 			} else if (i == 0) {
256 				syslog(LOG_NOTICE, "recycle tty %s", ttyn);
257 				(void)tcsetattr(STDIN_FILENO, TCSANOW, &tmode);
258 				exit(0);  /* recycle for init */
259 			}
260 			i = getty_chat(AC, CT, DC);
261 			if (i > 0) {
262 				syslog(LOG_ERR, "modem answer problem on %s", ttyn);
263 				(void)tcsetattr(STDIN_FILENO, TCSANOW, &tmode);
264 				exit(1);
265 			}
266 		} else { /* maybe blocking open */
267 			if (!opentty(ttyn, O_RDWR | (NC ? O_NONBLOCK : 0 )))
268 				exit(1);
269 		}
270 	    }
271 	}
272 
273 	defttymode();
274 	for (;;) {
275 
276 		/*
277 		 * if a delay was specified then sleep for that
278 		 * number of seconds before writing the initial prompt
279 		 */
280 		if (first_sleep && DE) {
281 		    sleep(DE);
282 		    /* remove any noise */
283 		    (void)tcflush(STDIN_FILENO, TCIOFLUSH);
284 		}
285 		first_sleep = 0;
286 
287 		setttymode(0);
288 		if (AB) {
289 			tname = autobaud();
290 			dogettytab();
291 			continue;
292 		}
293 		if (PS) {
294 			tname = portselector();
295 			dogettytab();
296 			continue;
297 		}
298 		if (CL && *CL)
299 			putpad(CL);
300 		edithost(HE);
301 
302 		/* if this is the first time through this, and an
303 		   issue file has been given, then send it */
304 		if (first_time && IF) {
305 			int fd;
306 
307 			if ((fd = open(IF, O_RDONLY)) != -1) {
308 				char * cp;
309 
310 				while ((cp = get_line(fd)) != NULL) {
311 					  putf(cp);
312 				}
313 				close(fd);
314 			}
315 		}
316 		first_time = 0;
317 
318 		if (IMP && *IMP && !(PL && PP))
319 			system(IMP);
320 		if (IM && *IM && !(PL && PP))
321 			putf(IM);
322 		if (setjmp(timeout)) {
323 			cfsetispeed(&tmode, B0);
324 			cfsetospeed(&tmode, B0);
325 			(void)tcsetattr(STDIN_FILENO, TCSANOW, &tmode);
326 			exit(1);
327 		}
328 		if (TO) {
329 			signal(SIGALRM, dingdong);
330 			alarm(TO);
331 		}
332 
333 		rval = 0;
334 		if (AL) {
335 			const char *p = AL;
336 			char *q = name;
337 
338 			while (*p && q < &name[sizeof name - 1]) {
339 				if (isupper(*p))
340 					upper = 1;
341 				else if (islower(*p))
342 					lower = 1;
343 				else if (isdigit(*p))
344 					digit = 1;
345 				*q++ = *p++;
346 			}
347 		} else if (!(PL && PP))
348 			rval = getname();
349 		if (rval == 2 || (PL && PP)) {
350 			oflush();
351 			alarm(0);
352 			limit.rlim_max = RLIM_INFINITY;
353 			limit.rlim_cur = RLIM_INFINITY;
354 			(void)setrlimit(RLIMIT_CPU, &limit);
355 			execle(PP, "ppplogin", ttyn, NULL, env);
356 			syslog(LOG_ERR, "%s: %m", PP);
357 			exit(1);
358 		} else if (rval || AL) {
359 			int i;
360 
361 			oflush();
362 			alarm(0);
363 			signal(SIGALRM, SIG_DFL);
364 			if (name[0] == '\0')
365 				continue;
366 			if (name[0] == '-') {
367 				puts("user names may not start with '-'.");
368 				continue;
369 			}
370 			if (!(upper || lower || digit)) {
371 				if (AL) {
372 					syslog(LOG_ERR,
373 					    "invalid auto-login name: %s", AL);
374 					exit(1);
375 				} else
376 					continue;
377 			}
378 			set_flags(2);
379 			if (crmod) {
380 				tmode.c_iflag |= ICRNL;
381 				tmode.c_oflag |= ONLCR;
382 			}
383 #if REALLY_OLD_TTYS
384 			if (upper || UC)
385 				tmode.sg_flags |= LCASE;
386 			if (lower || LC)
387 				tmode.sg_flags &= ~LCASE;
388 #endif
389 			if (tcsetattr(STDIN_FILENO, TCSANOW, &tmode) < 0) {
390 				syslog(LOG_ERR, "tcsetattr %s: %m", ttyn);
391 				exit(1);
392 			}
393 			signal(SIGINT, SIG_DFL);
394 			for (i = 0; environ[i] != NULL; i++)
395 				env[i] = environ[i];
396 			makeenv(&env[i]);
397 
398 			limit.rlim_max = RLIM_INFINITY;
399 			limit.rlim_cur = RLIM_INFINITY;
400 			(void)setrlimit(RLIMIT_CPU, &limit);
401 			execle(LO, "login", AL ? "-fp" : "-p", name,
402 			    NULL, env);
403 			syslog(LOG_ERR, "%s: %m", LO);
404 			exit(1);
405 		}
406 		alarm(0);
407 		signal(SIGALRM, SIG_DFL);
408 		signal(SIGINT, SIG_IGN);
409 		if (NX && *NX) {
410 			tname = NX;
411 			dogettytab();
412 		}
413 	}
414 }
415 
416 static int
417 opentty(const char *tty, int flags)
418 {
419 	int failopenlogged = 0, i, saved_errno;
420 
421 	while ((i = open(tty, flags)) == -1)
422 	{
423 		saved_errno = errno;
424 		if (!failopenlogged) {
425 			syslog(LOG_ERR, "open %s: %m", tty);
426 			failopenlogged = 1;
427 		}
428 		if (saved_errno == ENOENT)
429 			return 0;
430 		sleep(60);
431 	}
432 	if (login_tty(i) < 0) {
433 		if (daemon(0,0) < 0) {
434 			syslog(LOG_ERR,"daemon: %m");
435 			close(i);
436 			return 0;
437 		}
438 		if (login_tty(i) < 0) {
439 			syslog(LOG_ERR, "login_tty %s: %m", tty);
440 			close(i);
441 			return 0;
442 		}
443 	}
444 	return 1;
445 }
446 
447 static void
448 defttymode(void)
449 {
450 	struct termios def;
451 
452 	/* Start with default tty settings. */
453 	if (tcgetattr(STDIN_FILENO, &tmode) < 0) {
454 		syslog(LOG_ERR, "tcgetattr %s: %m", ttyn);
455 		exit(1);
456 	}
457 	omode = tmode; /* fill c_cc for dogettytab() */
458 	dogettytab();
459 	/*
460 	 * Don't rely on the driver too much, and initialize crucial
461 	 * things according to <sys/ttydefaults.h>.  Avoid clobbering
462 	 * the c_cc[] settings however, the console drivers might wish
463 	 * to leave their idea of the preferred VERASE key value
464 	 * there.
465 	 */
466 	cfmakesane(&def);
467 	tmode.c_iflag = def.c_iflag;
468 	tmode.c_oflag = def.c_oflag;
469 	tmode.c_lflag = def.c_lflag;
470 	tmode.c_cflag = def.c_cflag;
471 	if (NC)
472 		tmode.c_cflag |= CLOCAL;
473 	omode = tmode;
474 }
475 
476 static void
477 setttymode(int raw)
478 {
479 	int off = 0;
480 
481 	(void)tcflush(STDIN_FILENO, TCIOFLUSH);	/* clear out the crap */
482 	ioctl(STDIN_FILENO, FIONBIO, &off);	/* turn off non-blocking mode */
483 	ioctl(STDIN_FILENO, FIOASYNC, &off);	/* ditto for async mode */
484 
485 	if (IS)
486 		cfsetispeed(&tmode, speed(IS));
487 	else if (SP)
488 		cfsetispeed(&tmode, speed(SP));
489 	if (OS)
490 		cfsetospeed(&tmode, speed(OS));
491 	else if (SP)
492 		cfsetospeed(&tmode, speed(SP));
493 	set_flags(0);
494 	setchars();
495 	if (raw)
496 		cfmakeraw(&tmode);
497 	if (tcsetattr(STDIN_FILENO, TCSANOW, &tmode) < 0) {
498 		syslog(LOG_ERR, "tcsetattr %s: %m", ttyn);
499 		exit(1);
500 	}
501 }
502 
503 
504 static int
505 getname(void)
506 {
507 	int c;
508 	char *np;
509 	unsigned char cs;
510 	int ppp_state = 0;
511 	int ppp_connection = 0;
512 
513 	/*
514 	 * Interrupt may happen if we use CBREAK mode
515 	 */
516 	if (setjmp(intrupt)) {
517 		signal(SIGINT, SIG_IGN);
518 		return (0);
519 	}
520 	signal(SIGINT, interrupt);
521 	set_flags(1);
522 	prompt();
523 	oflush();
524 	if (PF > 0) {
525 		sleep(PF);
526 		PF = 0;
527 	}
528 	if (tcsetattr(STDIN_FILENO, TCSANOW, &tmode) < 0) {
529 		syslog(LOG_ERR, "%s: %m", ttyn);
530 		exit(1);
531 	}
532 	crmod = digit = lower = upper = 0;
533 	np = name;
534 	for (;;) {
535 		oflush();
536 		if (read(STDIN_FILENO, &cs, 1) <= 0)
537 			exit(0);
538 		if ((c = cs&0177) == 0)
539 			return (0);
540 
541 		/* PPP detection state machine..
542 		   Look for sequences:
543 		   PPP_FRAME, PPP_STATION, PPP_ESCAPE, PPP_CONTROL_ESCAPED or
544 		   PPP_FRAME, PPP_STATION, PPP_CONTROL (deviant from RFC)
545 		   See RFC1662.
546 		   Derived from code from Michael Hancock, <michaelh@cet.co.jp>
547 		   and Erik 'PPP' Olson, <eriko@wrq.com>
548 		 */
549 
550 		if (PP && (cs == PPP_FRAME)) {
551 			ppp_state = 1;
552 		} else if (ppp_state == 1 && cs == PPP_STATION) {
553 			ppp_state = 2;
554 		} else if (ppp_state == 2 && cs == PPP_ESCAPE) {
555 			ppp_state = 3;
556 		} else if ((ppp_state == 2 && cs == PPP_CONTROL)
557 			|| (ppp_state == 3 && cs == PPP_CONTROL_ESCAPED)) {
558 			ppp_state = 4;
559 		} else if (ppp_state == 4 && cs == PPP_LCP_HI) {
560 			ppp_state = 5;
561 		} else if (ppp_state == 5 && cs == PPP_LCP_LOW) {
562 			ppp_connection = 1;
563 			break;
564 		} else {
565 			ppp_state = 0;
566 		}
567 
568 		if (c == EOT || c == CTRL('d'))
569 			exit(0);
570 		if (c == '\r' || c == '\n' || np >= &name[sizeof name-1]) {
571 			putf("\r\n");
572 			break;
573 		}
574 		if (islower(c))
575 			lower = 1;
576 		else if (isupper(c))
577 			upper = 1;
578 		else if (c == ERASE || c == '\b' || c == 0177) {
579 			if (np > name) {
580 				np--;
581 				if (cfgetospeed(&tmode) >= 1200)
582 					puts("\b \b");
583 				else
584 					putchr(cs);
585 			}
586 			continue;
587 		} else if (c == KILL || c == CTRL('u')) {
588 			putchr('\r');
589 			if (cfgetospeed(&tmode) < 1200)
590 				putchr('\n');
591 			/* this is the way they do it down under ... */
592 			else if (np > name)
593 				puts("                                     \r");
594 			prompt();
595 			digit = lower = upper = 0;
596 			np = name;
597 			continue;
598 		} else if (isdigit(c))
599 			digit = 1;
600 		if (IG && (c <= ' ' || c > 0176))
601 			continue;
602 		*np++ = c;
603 		putchr(cs);
604 	}
605 	signal(SIGINT, SIG_IGN);
606 	*np = 0;
607 	if (c == '\r')
608 		crmod = 1;
609 	if ((upper && !lower && !LC) || UC)
610 		for (np = name; *np; np++)
611 			if (isupper(*np))
612 				*np = tolower(*np);
613 	return (1 + ppp_connection);
614 }
615 
616 static void
617 putpad(const char *s)
618 {
619 	int pad = 0;
620 	speed_t ospeed = cfgetospeed(&tmode);
621 
622 	if (isdigit(*s)) {
623 		while (isdigit(*s)) {
624 			pad *= 10;
625 			pad += *s++ - '0';
626 		}
627 		pad *= 10;
628 		if (*s == '.' && isdigit(s[1])) {
629 			pad += s[1] - '0';
630 			s += 2;
631 		}
632 	}
633 
634 	puts(s);
635 	/*
636 	 * If no delay needed, or output speed is
637 	 * not comprehensible, then don't try to delay.
638 	 */
639 	if (pad == 0 || ospeed <= 0)
640 		return;
641 
642 	/*
643 	 * Round up by a half a character frame, and then do the delay.
644 	 * Too bad there are no user program accessible programmed delays.
645 	 * Transmitting pad characters slows many terminals down and also
646 	 * loads the system.
647 	 */
648 	pad = (pad * ospeed + 50000) / 100000;
649 	while (pad--)
650 		putchr(*PC);
651 }
652 
653 static void
654 puts(const char *s)
655 {
656 	while (*s)
657 		putchr(*s++);
658 }
659 
660 static char	outbuf[OBUFSIZ];
661 static int	obufcnt = 0;
662 
663 static void
664 putchr(int cc)
665 {
666 	char c;
667 
668 	c = cc;
669 	if (!NP) {
670 		c |= partab[c&0177] & 0200;
671 		if (OP)
672 			c ^= 0200;
673 	}
674 	if (!UB) {
675 		outbuf[obufcnt++] = c;
676 		if (obufcnt >= OBUFSIZ)
677 			oflush();
678 	} else
679 		write(STDOUT_FILENO, &c, 1);
680 }
681 
682 static void
683 oflush(void)
684 {
685 	if (obufcnt)
686 		write(STDOUT_FILENO, outbuf, obufcnt);
687 	obufcnt = 0;
688 }
689 
690 static void
691 prompt(void)
692 {
693 
694 	putf(LM);
695 	if (CO)
696 		putchr('\n');
697 }
698 
699 
700 static char *
701 get_line(int fd)
702 {
703 	size_t i = 0;
704 	static char linebuf[512];
705 
706 	/*
707 	 * This is certainly slow, but it avoids having to include
708 	 * stdio.h unnecessarily. Issue files should be small anyway.
709 	 */
710 	while (i < (sizeof linebuf - 3) && read(fd, linebuf+i, 1)==1) {
711 		if (linebuf[i] == '\n') {
712 			/* Don't rely on newline mode, assume raw */
713 			linebuf[i++] = '\r';
714 			linebuf[i++] = '\n';
715 			linebuf[i] = '\0';
716 			return linebuf;
717 		}
718 		++i;
719 	}
720 	linebuf[i] = '\0';
721 	return i ? linebuf : 0;
722 }
723 
724 static void
725 putf(const char *cp)
726 {
727 	time_t t;
728 	char *slash, db[100];
729 
730 	static struct utsname kerninfo;
731 
732 	if (!*kerninfo.sysname)
733 		uname(&kerninfo);
734 
735 	while (*cp) {
736 		if (*cp != '%') {
737 			putchr(*cp++);
738 			continue;
739 		}
740 		switch (*++cp) {
741 
742 		case 't':
743 			slash = strrchr(ttyn, '/');
744 			if (slash == NULL)
745 				puts(ttyn);
746 			else
747 				puts(&slash[1]);
748 			break;
749 
750 		case 'h':
751 			puts(editedhost);
752 			break;
753 
754 		case 'd': {
755 			t = (time_t)0;
756 			(void)time(&t);
757 			if (Lo)
758 				(void)setlocale(LC_TIME, Lo);
759 			(void)strftime(db, sizeof(db), DF, localtime(&t));
760 			puts(db);
761 			break;
762 
763 		case 's':
764 			puts(kerninfo.sysname);
765 			break;
766 
767 		case 'm':
768 			puts(kerninfo.machine);
769 			break;
770 
771 		case 'r':
772 			puts(kerninfo.release);
773 			break;
774 
775 		case 'v':
776 			puts(kerninfo.version);
777 			break;
778 		}
779 
780 		case '%':
781 			putchr('%');
782 			break;
783 		}
784 		cp++;
785 	}
786 }
787 
788 /*
789  * Read a gettytab database entry and perform necessary quirks.
790  */
791 static void
792 dogettytab(void)
793 {
794 	/* Read the database entry. */
795 	gettable(tname, tabent);
796 
797 	/*
798 	 * Avoid inheriting the parity values from the default entry
799 	 * if any of them is set in the current entry.
800 	 * Mixing different parity settings is unreasonable.
801 	 */
802 	if (OPset || EPset || APset || NPset)
803 		OPset = EPset = APset = NPset = 1;
804 
805 	/* Fill in default values for unset capabilities. */
806 	setdefaults();
807 }
808