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