1 /* vi: set sw=4 ts=4: */
2 /* agetty.c - another getty program for Linux. By W. Z. Venema 1989
3 Ported to Linux by Peter Orbaek <poe@daimi.aau.dk>
4 This program is freely distributable. The entire man-page used to
5 be here. Now read the real man-page agetty.8 instead.
7 -f option added by Eric Rasmussen <ear@usfirst.org> - 12/28/95
9 1999-02-22 Arkadiusz Mi¶kiewicz <misiek@misiek.eu.org>
10 - added Native Language Support
12 1999-05-05 Thorsten Kranzkowski <dl8bcu@gmx.net>
13 - enable hardware flow control before displaying /etc/issue
21 #include <sys/ioctl.h>
24 #include <sys/signal.h>
33 #define _PATH_LOGIN "/bin/login"
36 #include <sys/param.h>
40 extern void updwtmp(const char *filename, const struct utmp *ut);
42 /* If USE_SYSLOG is undefined all diagnostics go directly to /dev/console. */
49 * Some heuristics to find out what environment we are in: if it is not
50 * System V, assume it is SunOS 4.
53 #ifdef LOGIN_PROCESS /* defined in System V utmp.h */
54 #define SYSV_STYLE /* select System V style getty */
58 * Things you may want to modify.
60 * If ISSUE is not defined, agetty will never display the contents of the
61 * /etc/issue file. You will not want to spit out large "issue" files at the
62 * wrong baud rate. Relevant for System V only.
64 * You may disagree with the default line-editing etc. characters defined
65 * below. Note, however, that DEL cannot be used for interrupt generation
66 * and for line editing at the same time.
70 #define ISSUE "/etc/issue" /* displayed before the login prompt */
71 #include <sys/utsname.h>
75 #define LOGIN " login: " /* login prompt */
77 /* Some shorthands for control characters. */
79 #define CTL(x) (x ^ 0100) /* Assumes ASCII dialect */
80 #define CR CTL('M') /* carriage return */
81 #define NL CTL('J') /* line feed */
82 #define BS CTL('H') /* back space */
83 #define DEL CTL('?') /* delete */
85 /* Defaults for line-editing etc. characters; you may want to change this. */
87 #define DEF_ERASE DEL /* default erase character */
88 #define DEF_INTR CTL('C') /* default interrupt character */
89 #define DEF_QUIT CTL('\\') /* default quit char */
90 #define DEF_KILL CTL('U') /* default kill char */
91 #define DEF_EOF CTL('D') /* default EOF char */
93 #define DEF_SWITCH 0 /* default switch char */
96 * SunOS 4.1.1 termio is broken. We must use the termios stuff instead,
97 * because the termio -> termios translation does not clear the termios
98 * CIBAUD bits. Therefore, the tty driver would sometimes report that input
99 * baud rate != output baud rate. I did not notice that problem with SunOS
100 * 4.1. We will use termios where available, and termio otherwise.
103 /* linux 0.12 termio is broken too, if we use it c_cc[VERASE] isn't set
104 properly, but all is well if we use termios?! */
110 #define termio termios
111 #define TCGETA TCGETS
112 #define TCSETA TCSETS
113 #define TCSETAW TCSETSW
117 * This program tries to not use the standard-i/o library. This keeps the
118 * executable small on systems that do not have shared libraries (System V
126 * When multiple baud rates are specified on the command line, the first one
127 * we will try is the first one specified.
130 #define FIRST_SPEED 0
132 /* Storage for command-line options. */
134 #define MAX_SPEED 10 /* max. nr. of baud rates */
137 int flags; /* toggle switches, see below */
138 int timeout; /* time-out period */
139 char *login; /* login program */
140 char *tty; /* name of tty */
141 char *initstring; /* modem init string */
142 char *issue; /* alternative issue file */
143 int numspeed; /* number of baud rates to try */
144 int speeds[MAX_SPEED]; /* baud rates to be tried */
147 #define F_PARSE (1<<0) /* process modem status messages */
148 #define F_ISSUE (1<<1) /* display /etc/issue */
149 #define F_RTSCTS (1<<2) /* enable RTS/CTS flow control */
150 #define F_LOCAL (1<<3) /* force local */
151 #define F_INITSTRING (1<<4) /* initstring is set */
152 #define F_WAITCRLF (1<<5) /* wait for CR or LF */
153 #define F_CUSTISSUE (1<<6) /* give alternative issue file */
154 #define F_NOPROMPT (1<<7) /* don't ask for login name! */
156 /* Storage for things detected while the login name was read. */
159 int erase; /* erase character */
160 int kill; /* kill character */
161 int eol; /* end-of-line character */
162 int parity; /* what parity did we see */
163 int capslock; /* upper case without lower case */
166 /* Initial values for the above. */
168 struct chardata init_chardata = {
169 DEF_ERASE, /* default erase character */
170 DEF_KILL, /* default kill character */
171 13, /* default eol char */
172 0, /* space parity */
181 static struct Speedtab speedtab[] = {
219 void parse_args(int argc, char **argv, struct options *op);
220 void parse_speeds(struct options *op, char *arg);
221 void update_utmp(char *line);
222 void open_tty(char *tty, struct termio *tp, int local);
223 void termio_init(struct termio *tp, int speed, struct options *op);
224 void auto_baud(struct termio *tp);
225 void do_prompt(struct options *op, struct termio *tp);
226 void next_speed(struct termio *tp, struct options *op);
227 char *get_logname(struct options *op, struct chardata *cp,
230 void termio_final(struct options *op, struct termio *tp,
232 struct chardata *cp);
233 int caps_lock(char *s);
235 static void error(const char *fmt, ...);
237 /* The following is used for understandable diagnostics. */
241 /* Fake hostname for ut_host specified on command line. */
242 char *fakehost = NULL;
246 #define debug(s) fprintf(dbf,s); fflush(dbf)
247 #define DEBUGTERM "/dev/ttyp0"
250 #define debug(s) /* nothing */
253 int getty_main(int argc, char **argv)
255 char *logname = NULL; /* login name, given to /bin/login */
256 struct chardata chardata; /* set by get_logname() */
257 struct termio termio; /* terminal mode bits */
258 static struct options options = {
259 F_ISSUE, /* show /etc/issue (SYSV_STYLE) */
261 _PATH_LOGIN, /* default login program */
262 "tty1", /* default tty line */
263 "", /* modem init string */
264 ISSUE, /* default issue file */
265 0, /* no baud rates known yet */
268 /* The BSD-style init command passes us a useless process name. */
277 dbf = xfopen(DEBUGTERM, "w");
282 for (i = 1; i < argc; i++) {
289 /* Parse command-line arguments. */
291 parse_args(argc, argv, &options);
297 /* Update the utmp file. */
300 update_utmp(options.tty);
303 debug("calling open_tty\n");
304 /* Open the tty as standard { input, output, error }. */
305 open_tty(options.tty, &termio, options.flags & F_LOCAL);
312 if (ioctl(0, TIOCSPGRP, &iv) < 0)
313 perror_msg("ioctl() TIOCSPGRP call failed");
316 /* Initialize the termio settings (raw mode, eight-bit, blocking i/o). */
317 debug("calling termio_init\n");
318 termio_init(&termio, options.speeds[FIRST_SPEED], &options);
320 /* write the modem init string and DON'T flush the buffers */
321 if (options.flags & F_INITSTRING) {
322 debug("writing init string\n");
323 write(1, options.initstring, strlen(options.initstring));
326 if (!(options.flags & F_LOCAL)) {
327 /* go to blocking write mode unless -L is specified */
328 fcntl(1, F_SETFL, fcntl(1, F_GETFL, 0) & ~O_NONBLOCK);
331 /* Optionally detect the baud rate from the modem status message. */
332 debug("before autobaud\n");
333 if (options.flags & F_PARSE)
336 /* Set the optional timer. */
338 (void) alarm((unsigned) options.timeout);
340 /* optionally wait for CR or LF before writing /etc/issue */
341 if (options.flags & F_WAITCRLF) {
344 debug("waiting for cr-lf\n");
345 while (read(0, &ch, 1) == 1) {
346 ch &= 0x7f; /* strip "parity bit" */
348 fprintf(dbf, "read %c\n", ch);
350 if (ch == '\n' || ch == '\r')
355 chardata = init_chardata;
356 if (!(options.flags & F_NOPROMPT)) {
357 /* Read the login name. */
358 debug("reading login name\n");
359 /* while ((logname = get_logname(&options, &chardata, &termio)) == 0) */
360 while ((logname = get_logname(&options, &chardata, &termio)) ==
361 NULL) next_speed(&termio, &options);
369 /* Finalize the termio settings. */
371 termio_final(&options, &termio, &chardata);
373 /* Now the newline character should be properly written. */
375 (void) write(1, "\n", 1);
377 /* Let the login program take care of password validation. */
379 (void) execl(options.login, options.login, "--", logname, (char *) 0);
380 error("%s: can't exec %s: %m", options.tty, options.login);
381 return (0); /* quiet GCC */
384 /* parse-args - parse command-line arguments */
386 void parse_args(argc, argv, op)
391 extern char *optarg; /* getopt */
392 extern int optind; /* getopt */
395 while (isascii(c = getopt(argc, argv, "I:LH:f:hil:mt:wn"))) {
398 if (!(op->initstring = malloc(strlen(optarg)))) {
399 error("can't malloc initstring");
406 /* copy optarg into op->initstring decoding \ddd
407 octal codes into chars */
411 if (*p == '\\') { /* know \\ means \ */
416 } else { /* handle \000 - \177 */
418 for (i = 1; i <= 3; i++) {
419 if (*p >= '0' && *p <= '7') {
434 op->flags |= F_INITSTRING;
437 case 'L': /* force local */
438 op->flags |= F_LOCAL;
440 case 'H': /* fake login host */
443 case 'f': /* custom issue file */
444 op->flags |= F_CUSTISSUE;
447 case 'h': /* enable h/w flow control */
448 op->flags |= F_RTSCTS;
450 case 'i': /* do not show /etc/issue */
451 op->flags &= ~F_ISSUE;
454 op->login = optarg; /* non-default login program */
456 case 'm': /* parse modem status message */
457 op->flags |= F_PARSE;
460 op->flags |= F_NOPROMPT;
462 case 't': /* time out */
463 if ((op->timeout = atoi(optarg)) <= 0)
464 error("bad timeout value: %s", optarg);
467 op->flags |= F_WAITCRLF;
473 debug("after getopt loop\n");
474 if (argc < optind + 2) /* check parameter count */
477 /* we loosen up a bit and accept both "baudrate tty" and "tty baudrate" */
478 if ('0' <= argv[optind][0] && argv[optind][0] <= '9') {
479 /* a number first, assume it's a speed (BSD style) */
480 parse_speeds(op, argv[optind++]); /* baud rate(s) */
481 op->tty = argv[optind]; /* tty name */
483 op->tty = argv[optind++]; /* tty name */
484 parse_speeds(op, argv[optind]); /* baud rate(s) */
488 if (argc > optind && argv[optind])
489 setenv("TERM", argv[optind], 1);
491 debug("exiting parseargs\n");
494 /* parse_speeds - parse alternate baud rates */
496 void parse_speeds(op, arg)
503 debug("entered parse_speeds\n");
504 for (cp = strtok(arg, ","); cp != 0; cp = strtok((char *) 0, ",")) {
505 if ((op->speeds[op->numspeed++] = bcode(cp)) <= 0)
506 error("bad speed: %s", cp);
507 if (op->numspeed > MAX_SPEED)
508 error("too many alternate speeds");
510 debug("exiting parsespeeds\n");
515 /* update_utmp - update our utmp entry */
516 void update_utmp(line)
521 int mypid = getpid();
526 #if ! (__GLIBC__ > 2 || (__GLIBC__ == 2 && __GLIBC_MINOR__ >= 1))
531 * The utmp file holds miscellaneous information about things started by
532 * /sbin/init and other system-related events. Our purpose is to update
533 * the utmp entry for the current process, in particular the process type
534 * and the tty line we are listening to. Return successfully only if the
535 * utmp file can be opened for update, and if we are able to find our
536 * entry in the utmp file.
540 utmpname(_PATH_UTMP);
542 while ((utp = getutent())
543 && !(utp->ut_type == INIT_PROCESS && utp->ut_pid == mypid)) /* nothing */
547 memcpy(&ut, utp, sizeof(ut));
549 /* some inits don't initialize utmp... */
550 memset(&ut, 0, sizeof(ut));
551 strncpy(ut.ut_id, line + 3, sizeof(ut.ut_id));
555 strncpy(ut.ut_user, "LOGIN", sizeof(ut.ut_user));
556 strncpy(ut.ut_line, line, sizeof(ut.ut_line));
558 strncpy(ut.ut_host, fakehost, sizeof(ut.ut_host));
561 ut.ut_type = LOGIN_PROCESS;
568 updwtmp(_PATH_WTMP, &ut);
570 #else /* not __linux__ */
574 if ((ut_fd = open(UTMP_FILE, 2)) < 0) {
575 error("%s: open for update: %m"), UTMP_FILE;
577 long ut_size = sizeof(ut); /* avoid nonsense */
579 while (read(ut_fd, (char *) &ut, sizeof(ut)) == sizeof(ut)) {
580 if (ut.ut_type == INIT_PROCESS && ut.ut_pid == mypid) {
581 ut.ut_type = LOGIN_PROCESS;
582 ut.ut_time = time((long *) 0);
583 (void) strncpy(ut.ut_name, "LOGIN", sizeof(ut.ut_name));
584 (void) strncpy(ut.ut_line, line, sizeof(ut.ut_line));
586 (void) strncpy(ut.ut_host, fakehost, sizeof(ut.ut_host));
587 (void) lseek(ut_fd, -ut_size, 1);
588 (void) write(ut_fd, (char *) &ut, sizeof(ut));
593 error("%s: no utmp entry", line);
596 #endif /* __linux__ */
601 /* open_tty - set up tty as standard { input, output, error } */
602 void open_tty(tty, tp, local)
607 /* Get rid of the present standard { output, error} if any. */
611 errno = 0; /* ignore above errors */
613 /* Set up new standard input, unless we are given an already opened port. */
615 if (strcmp(tty, "-")) {
618 /* Sanity checks... */
621 error("/dev: chdir() failed: %m");
622 if (stat(tty, &st) < 0)
623 error("/dev/%s: %m", tty);
624 if ((st.st_mode & S_IFMT) != S_IFCHR)
625 error("/dev/%s: not a character device", tty);
627 /* Open the tty as standard input. */
630 errno = 0; /* ignore close(2) errors */
633 if (open(tty, O_RDWR | O_NONBLOCK, 0) != 0)
634 error("/dev/%s: cannot open as standard input: %m", tty);
639 * Standard input should already be connected to an open port. Make
640 * sure it is open for read/write.
643 if ((fcntl(0, F_GETFL, 0) & O_RDWR) != O_RDWR)
644 error("%s: not open for read/write", tty);
647 /* Set up standard output and standard error file descriptors. */
649 if (dup(0) != 1 || dup(0) != 2) /* set up stdout and stderr */
650 error("%s: dup problem: %m", tty); /* we have a problem */
653 * The following ioctl will fail if stdin is not a tty, but also when
654 * there is noise on the modem control lines. In the latter case, the
655 * common course of action is (1) fix your cables (2) give the modem more
656 * time to properly reset after hanging up. SunOS users can achieve (2)
657 * by patching the SunOS kernel variable "zsadtrlow" to a larger value;
658 * 5 seconds seems to be a good value.
661 if (ioctl(0, TCGETA, tp) < 0)
662 error("%s: ioctl: %m", tty);
665 * It seems to be a terminal. Set proper protections and ownership. Mode
666 * 0622 is suitable for SYSV <4 because /bin/login does not change
667 * protections. SunOS 4 login will change the protections to 0620 (write
668 * access for group tty) after the login has succeeded.
673 /* tty to root.dialout 660 */
677 id = (gr = getgrnam("dialout")) ? gr->gr_gid : 0;
681 /* vcs,vcsa to root.sys 600 */
682 if (!strncmp(tty, "tty", 3) && isdigit(tty[3])) {
685 if (!(vcs = malloc(strlen(tty))))
686 error("Can't malloc for vcs");
687 if (!(vcsa = malloc(strlen(tty) + 1)))
688 error("Can't malloc for vcsa");
690 strcpy(vcs + 3, tty + 3);
691 strcpy(vcsa, "vcsa");
692 strcpy(vcsa + 4, tty + 3);
694 id = (gr = getgrnam("sys")) ? gr->gr_gid : 0;
705 (void) chown(tty, 0, 0); /* root, sys */
706 (void) chmod(tty, 0622); /* crw--w--w- */
707 errno = 0; /* ignore above errors */
711 /* termio_init - initialize termio settings */
716 void termio_init(tp, speed, op)
723 * Initial termio settings: 8-bit characters, raw-mode, blocking i/o.
724 * Special characters are set after we have read the login name; all
725 * reads will be done in raw mode anyway. Errors will be dealt with
729 /* flush input and output queues, important for modems! */
730 (void) ioctl(0, TCFLSH, TCIOFLUSH);
733 tp->c_cflag = CS8 | HUPCL | CREAD | speed;
734 if (op->flags & F_LOCAL) {
735 tp->c_cflag |= CLOCAL;
738 tp->c_iflag = tp->c_lflag = tp->c_oflag = tp->c_line = 0;
742 /* Optionally enable hardware flow control */
745 if (op->flags & F_RTSCTS)
746 tp->c_cflag |= CRTSCTS;
749 (void) ioctl(0, TCSETA, tp);
751 /* go to blocking input even in local mode */
752 fcntl(0, F_SETFL, fcntl(0, F_GETFL, 0) & ~O_NONBLOCK);
754 debug("term_io 2\n");
757 /* auto_baud - extract baud rate from modem status message */
769 * This works only if the modem produces its status code AFTER raising
770 * the DCD line, and if the computer is fast enough to set the proper
771 * baud rate before the message has gone by. We expect a message of the
774 * <junk><number><junk>
776 * The number is interpreted as the baud rate of the incoming call. If the
777 * modem does not tell us the baud rate within one second, we will keep
778 * using the current baud rate. It is advisable to enable BREAK
779 * processing (comma-separated list of baud rates) if the processing of
780 * modem status messages is enabled.
784 * Use 7-bit characters, don't block if input queue is empty. Errors will
785 * be dealt with lateron.
789 tp->c_iflag |= ISTRIP; /* enable 8th-bit stripping */
790 vmin = tp->c_cc[VMIN];
791 tp->c_cc[VMIN] = 0; /* don't block if queue empty */
792 (void) ioctl(0, TCSETA, tp);
795 * Wait for a while, then read everything the modem has said so far and
796 * try to extract the speed of the dial-in call.
800 if ((nread = read(0, buf, sizeof(buf) - 1)) > 0) {
802 for (bp = buf; bp < buf + nread; bp++) {
803 if (isascii(*bp) && isdigit(*bp)) {
804 if ((speed = bcode(bp))) {
805 tp->c_cflag &= ~CBAUD;
806 tp->c_cflag |= speed;
812 /* Restore terminal settings. Errors will be dealt with lateron. */
815 tp->c_cc[VMIN] = vmin;
816 (void) ioctl(0, TCSETA, tp);
819 /* do_prompt - show login prompt, optionally preceded by /etc/issue contents */
820 void do_prompt(op, tp)
833 (void) write(1, "\r\n", 2); /* start a new line */
834 #ifdef ISSUE /* optional: show /etc/issue */
835 if ((op->flags & F_ISSUE) && (fd = fopen(op->issue, "r"))) {
836 oflag = tp->c_oflag; /* save current setting */
837 tp->c_oflag |= (ONLCR | OPOST); /* map NL in output to CR-NL */
838 (void) ioctl(0, TCSETAW, tp);
841 while ((c = getc(fd)) != EOF) {
847 (void) printf("%s", uts.sysname);
851 (void) printf("%s", uts.nodename);
855 (void) printf("%s", uts.release);
859 (void) printf("%s", uts.version);
863 (void) printf("%s", uts.machine);
868 char domainname[256];
870 getdomainname(domainname, sizeof(domainname));
871 domainname[sizeof(domainname) - 1] = '\0';
872 printf("%s", domainname);
879 char *weekday[] = { "Sun", "Mon", "Tue", "Wed", "Thu",
882 char *month[] = { "Jan", "Feb", "Mar", "Apr", "May",
883 "Jun", "Jul", "Aug", "Sep", "Oct",
890 tm = localtime(&now);
893 (void) printf("%s %s %d %d",
894 weekday[tm->tm_wday],
895 month[tm->tm_mon], tm->tm_mday,
898 2000 : tm->tm_year + 1900);
900 (void) printf("%02d:%02d:%02d", tm->tm_hour,
901 tm->tm_min, tm->tm_sec);
907 (void) printf("%s", op->tty);
914 for (i = 0; speedtab[i].speed; i++) {
915 if (speedtab[i].code == (tp->c_cflag & CBAUD)) {
916 printf("%ld", speedtab[i].speed);
929 while ((ut = getutent()))
930 if (ut->ut_type == USER_PROCESS)
933 printf("%d ", users);
935 printf((users == 1) ? "user" : "users");
946 tp->c_oflag = oflag; /* restore settings */
947 (void) ioctl(0, TCSETAW, tp); /* wait till output is gone */
953 char hn[MAXHOSTNAMELEN + 1];
955 (void) gethostname(hn, MAXHOSTNAMELEN);
956 write(1, hn, strlen(hn));
959 (void) write(1, LOGIN, sizeof(LOGIN) - 1); /* always show login prompt */
962 /* next_speed - select next baud rate */
963 void next_speed(tp, op)
967 static int baud_index = FIRST_SPEED; /* current speed index */
969 baud_index = (baud_index + 1) % op->numspeed;
970 tp->c_cflag &= ~CBAUD;
971 tp->c_cflag |= op->speeds[baud_index];
972 (void) ioctl(0, TCSETA, tp);
975 /* get_logname - get user name, establish parity, speed, erase, kill, eol */
976 /* return NULL on failure, logname on success */
977 char *get_logname(op, cp, tp)
982 static char logname[BUFSIZ];
984 char c; /* input character, full eight bits */
985 char ascval; /* low 7 bits of input character */
986 int bits; /* # of "1" bits per character */
987 int mask; /* mask with 1 bit up */
988 static char *erase[] = { /* backspace-space-backspace */
989 "\010\040\010", /* space parity */
990 "\010\040\010", /* odd parity */
991 "\210\240\210", /* even parity */
992 "\210\240\210", /* no parity */
995 /* Initialize kill, erase, parity etc. (also after switching speeds). */
999 /* Flush pending input (esp. after parsing or switching the baud rate). */
1002 (void) ioctl(0, TCFLSH, TCIFLUSH);
1004 /* Prompt for and read a login name. */
1006 for (*logname = 0; *logname == 0; /* void */ ) {
1008 /* Write issue file and prompt, with "parity" bit == 0. */
1012 /* Read name, watch for break, parity, erase, kill, end-of-line. */
1014 for (bp = logname, cp->eol = 0; cp->eol == 0; /* void */ ) {
1016 /* Do not report trivial EINTR/EIO errors. */
1018 if (read(0, &c, 1) < 1) {
1019 if (errno == EINTR || errno == EIO)
1021 error("%s: read: %m", op->tty);
1023 /* Do BREAK handling elsewhere. */
1025 if ((c == 0) && op->numspeed > 1)
1029 /* Do parity bit handling. */
1031 if (c != (ascval = (c & 0177))) { /* "parity" bit on ? */
1032 for (bits = 1, mask = 1; mask & 0177; mask <<= 1)
1034 bits++; /* count "1" bits */
1035 cp->parity |= ((bits & 1) ? 1 : 2);
1037 /* Do erase, kill and end-of-line processing. */
1042 *bp = 0; /* terminate logname */
1043 cp->eol = ascval; /* set end-of-line char */
1048 cp->erase = ascval; /* set erase character */
1050 (void) write(1, erase[cp->parity], 3);
1056 cp->kill = ascval; /* set kill character */
1057 while (bp > logname) {
1058 (void) write(1, erase[cp->parity], 3);
1065 if (!isascii(ascval) || !isprint(ascval)) {
1066 /* ignore garbage characters */ ;
1067 } else if (bp - logname >= sizeof(logname) - 1) {
1068 error("%s: input overrun", op->tty);
1070 (void) write(1, &c, 1); /* echo the character */
1071 *bp++ = ascval; /* and store it */
1077 /* Handle names with upper case and no lower case. */
1079 if ((cp->capslock = caps_lock(logname))) {
1080 for (bp = logname; *bp; bp++)
1082 *bp = tolower(*bp); /* map name to lower case */
1087 /* termio_final - set the final tty mode bits */
1088 void termio_final(op, tp, cp)
1091 struct chardata *cp;
1093 /* General terminal-independent stuff. */
1095 tp->c_iflag |= IXON | IXOFF; /* 2-way flow control */
1096 tp->c_lflag |= ICANON | ISIG | ECHO | ECHOE | ECHOK | ECHOKE;
1097 /* no longer| ECHOCTL | ECHOPRT */
1098 tp->c_oflag |= OPOST;
1099 /* tp->c_cflag = 0; */
1100 tp->c_cc[VINTR] = DEF_INTR; /* default interrupt */
1101 tp->c_cc[VQUIT] = DEF_QUIT; /* default quit */
1102 tp->c_cc[VEOF] = DEF_EOF; /* default EOF character */
1103 tp->c_cc[VEOL] = DEF_EOL;
1105 tp->c_cc[VSWTC] = DEF_SWITCH; /* default switch character */
1107 tp->c_cc[VSWTCH] = DEF_SWITCH; /* default switch character */
1110 /* Account for special characters seen in input. */
1112 if (cp->eol == CR) {
1113 tp->c_iflag |= ICRNL; /* map CR in input to NL */
1114 tp->c_oflag |= ONLCR; /* map NL in output to CR-NL */
1116 tp->c_cc[VERASE] = cp->erase; /* set erase character */
1117 tp->c_cc[VKILL] = cp->kill; /* set kill character */
1119 /* Account for the presence or absence of parity bits in input. */
1121 switch (cp->parity) {
1122 case 0: /* space (always 0) parity */
1124 case 1: /* odd parity */
1125 tp->c_cflag |= PARODD;
1127 case 2: /* even parity */
1128 tp->c_cflag |= PARENB;
1129 tp->c_iflag |= INPCK | ISTRIP;
1131 case (1 | 2): /* no parity bit */
1132 tp->c_cflag &= ~CSIZE;
1136 /* Account for upper case without lower case. */
1139 tp->c_iflag |= IUCLC;
1140 tp->c_lflag |= XCASE;
1141 tp->c_oflag |= OLCUC;
1143 /* Optionally enable hardware flow control */
1146 if (op->flags & F_RTSCTS)
1147 tp->c_cflag |= CRTSCTS;
1150 /* Finally, make the new settings effective */
1152 if (ioctl(0, TCSETA, tp) < 0)
1153 error("%s: ioctl: TCSETA: %m", op->tty);
1156 /* caps_lock - string contains upper case without lower case */
1157 /* returns 1 if true, 0 if false */
1163 for (capslock = 0; *s; s++) {
1167 capslock = isupper(*s);
1172 /* bcode - convert speed string to speed code; return 0 on failure */
1176 struct Speedtab *sp;
1177 long speed = atol(s);
1179 for (sp = speedtab; sp->speed; sp++)
1180 if (sp->speed == speed)
1185 /* error - report errors to console or syslog; only understands %s and %m */
1187 #define str2cpy(b,s1,s2) strcat(strcpy(b,s1),s2)
1190 * output error messages
1192 static void error(const char *fmt, ...)
1205 strncpy(buf, progname, 256);
1206 strncat(buf, ": ", 256);
1208 bp = buf + strlen(buf);
1211 va_start(va_alist, fmt);
1212 vsnprintf(bp, 256 - strlen(buf), fmt, va_alist);
1217 openlog(progname, LOG_PID, LOG_AUTH);
1218 syslog(LOG_ERR, buf);
1221 strncat(bp, "\r\n", 256 - strlen(buf));
1223 if ((fd = open("/dev/console", 1)) >= 0) {
1224 write(fd, buf, strlen(buf));
1228 (void) sleep((unsigned) 10); /* be kind to init(8) */