Hi, Mike Frysinger, le Sat 11 Aug 2007 22:48:43 -0400, a écrit : > > - #include <termio.h> turned into #include <termios.h>: termio.h is > > deprecated since a lot time, we should really use termios.h instead. > > - ioctl(0, TCFLSH, x) is turned into tcflush(0, x). These are really > > equivalents (see the glibc implementation of tcflush). TCSA &such > > ioctls should most probably also be replaced by tcsetattr & such, but > > since glibc's implementation is not that straightforward and according > > to comments there are some kludges, I preferred to do this change for > > GNU/Hurd only. > > i think everything should use termios functions. these are a POSIX standard > and the underlying hacks and what not are the libc's problem, not util-linux. Ok, here is a patch. Samuel
diff --git a/login-utils/agetty.c b/login-utils/agetty.c index 6640cf2..9c81c0f 100644 --- a/login-utils/agetty.c +++ b/login-utils/agetty.c @@ -18,7 +18,7 @@ #include <stdlib.h> #include <string.h> #include <sys/ioctl.h> -#include <termio.h> +#include <termios.h> #include <signal.h> #include <errno.h> #include <sys/types.h> @@ -96,27 +96,6 @@ #define DEF_SWITCH 0 /* default switch char */ /* - * SunOS 4.1.1 termio is broken. We must use the termios stuff instead, - * because the termio -> termios translation does not clear the termios - * CIBAUD bits. Therefore, the tty driver would sometimes report that input - * baud rate != output baud rate. I did not notice that problem with SunOS - * 4.1. We will use termios where available, and termio otherwise. - */ - -/* linux 0.12 termio is broken too, if we use it c_cc[VERASE] isn't set - properly, but all is well if we use termios?! */ - -#ifdef TCGETS -#undef TCGETA -#undef TCSETA -#undef TCSETAW -#define termio termios -#define TCGETA TCGETS -#define TCSETA TCSETS -#define TCSETAW TCSETSW -#endif - - /* * This program tries to not use the standard-i/o library. This keeps the * executable small on systems that do not have shared libraries (System V * Release <3). @@ -234,13 +213,13 @@ int main P_((int argc, char **argv)); void parse_args P_((int argc, char **argv, struct options *op)); void parse_speeds P_((struct options *op, char *arg)); void update_utmp P_((char *line)); -void open_tty P_((char *tty, struct termio *tp, int local)); -void termio_init P_((struct termio *tp, int speed, struct options *op)); -void auto_baud P_((struct termio *tp)); -void do_prompt P_((struct options *op, struct termio *tp)); -void next_speed P_((struct termio *tp, struct options *op)); -char *get_logname P_((struct options *op, struct chardata *cp, struct termio *tp)); -void termio_final P_((struct options *op, struct termio *tp, struct chardata *cp)); +void open_tty P_((char *tty, struct termios *tp, int local)); +void termio_init P_((struct termios *tp, int speed, struct options *op)); +void auto_baud P_((struct termios *tp)); +void do_prompt P_((struct options *op, struct termios *tp)); +void next_speed P_((struct termios *tp, struct options *op)); +char *get_logname P_((struct options *op, struct chardata *cp, struct termios *tp)); +void termio_final P_((struct options *op, struct termios *tp, struct chardata *cp)); int caps_lock P_((char *s)); int bcode P_((char *s)); void usage P_((void)); @@ -269,7 +248,7 @@ main(argc, argv) { char *logname = NULL; /* login name, given to /bin/login */ struct chardata chardata; /* set by get_logname() */ - struct termio termio; /* terminal mode bits */ + struct termios termios; /* terminal mode bits */ static struct options options = { F_ISSUE, /* show /etc/issue (SYSV_STYLE) */ 0, /* no timeout */ @@ -324,19 +303,12 @@ main(argc, argv) debug("calling open_tty\n"); /* Open the tty as standard { input, output, error }. */ - open_tty(options.tty, &termio, options.flags & F_LOCAL); + open_tty(options.tty, &termios, options.flags & F_LOCAL); -#ifdef __linux__ - { - int iv; - - iv = getpid(); - (void) ioctl(0, TIOCSPGRP, &iv); - } -#endif - /* Initialize the termio settings (raw mode, eight-bit, blocking i/o). */ + tcsetpgrp(0, getpid()); + /* Initialize the termios settings (raw mode, eight-bit, blocking i/o). */ debug("calling termio_init\n"); - termio_init(&termio, options.speeds[FIRST_SPEED], &options); + termio_init(&termios, options.speeds[FIRST_SPEED], &options); /* write the modem init string and DON'T flush the buffers */ if (options.flags & F_INITSTRING) { @@ -352,7 +324,7 @@ main(argc, argv) /* Optionally detect the baud rate from the modem status message. */ debug("before autobaud\n"); if (options.flags & F_PARSE) - auto_baud(&termio); + auto_baud(&termios); /* Set the optional timer. */ if (options.timeout) @@ -376,8 +348,8 @@ main(argc, argv) if (!(options.flags & F_NOPROMPT)) { /* Read the login name. */ debug("reading login name\n"); - while ((logname = get_logname(&options, &chardata, &termio)) == 0) - next_speed(&termio, &options); + while ((logname = get_logname(&options, &chardata, &termios)) == 0) + next_speed(&termios, &options); } /* Disable timer. */ @@ -385,9 +357,9 @@ main(argc, argv) if (options.timeout) (void) alarm(0); - /* Finalize the termio settings. */ + /* Finalize the termios settings. */ - termio_final(&options, &termio, &chardata); + termio_final(&options, &termios, &chardata); /* Now the newline character should be properly written. */ @@ -645,7 +617,7 @@ update_utmp(line) void open_tty(tty, tp, local) char *tty; - struct termio *tp; + struct termios *tp; int local; { /* Get rid of the present standard { output, error} if any. */ @@ -702,8 +674,8 @@ open_tty(tty, tp, local) * 5 seconds seems to be a good value. */ - if (ioctl(0, TCGETA, tp) < 0) - error("%s: ioctl: %m", tty); + if (tcgetattr(0, tp) < 0) + error("%s: tcgetattr: %m", tty); /* * It seems to be a terminal. Set proper protections and ownership. Mode @@ -721,35 +693,38 @@ open_tty(tty, tp, local) errno = 0; /* ignore above errors */ } -/* termio_init - initialize termio settings */ +/* termio_init - initialize termios settings */ char gbuf[1024]; char area[1024]; void termio_init(tp, speed, op) - struct termio *tp; + struct termios *tp; int speed; struct options *op; { /* - * Initial termio settings: 8-bit characters, raw-mode, blocking i/o. + * Initial termios settings: 8-bit characters, raw-mode, blocking i/o. * Special characters are set after we have read the login name; all * reads will be done in raw mode anyway. Errors will be dealt with * lateron. */ -#ifdef __linux__ /* flush input and output queues, important for modems! */ - (void) ioctl(0, TCFLSH, TCIOFLUSH); -#endif + (void) tcflush(0, TCIOFLUSH); - tp->c_cflag = CS8 | HUPCL | CREAD | speed; + tp->c_cflag = CS8 | HUPCL | CREAD; + cfsetispeed(tp, speed); + cfsetospeed(tp, speed); if (op->flags & F_LOCAL) { tp->c_cflag |= CLOCAL; } - tp->c_iflag = tp->c_lflag = tp->c_oflag = tp->c_line = 0; + tp->c_iflag = tp->c_lflag = tp->c_oflag = 0; +#ifndef __GNU__ + tp->c_line = 0; +#endif tp->c_cc[VMIN] = 1; tp->c_cc[VTIME] = 0; @@ -760,7 +735,7 @@ termio_init(tp, speed, op) tp->c_cflag |= CRTSCTS; #endif - (void) ioctl(0, TCSETA, tp); + (void) tcsetattr(0, TCSANOW, tp); /* go to blocking input even in local mode */ fcntl(0, F_SETFL, fcntl(0, F_GETFL, 0) & ~O_NONBLOCK); @@ -771,7 +746,7 @@ termio_init(tp, speed, op) /* auto_baud - extract baud rate from modem status message */ void auto_baud(tp) - struct termio *tp; + struct termios *tp; { int speed; int vmin; @@ -804,7 +779,7 @@ auto_baud(tp) tp->c_iflag |= ISTRIP; /* enable 8th-bit stripping */ vmin = tp->c_cc[VMIN]; tp->c_cc[VMIN] = 0; /* don't block if queue empty */ - (void) ioctl(0, TCSETA, tp); + tcsetattr(0, TCSANOW, tp); /* * Wait for a while, then read everything the modem has said so far and @@ -817,8 +792,8 @@ auto_baud(tp) for (bp = buf; bp < buf + nread; bp++) { if (isascii(*bp) && isdigit(*bp)) { if ((speed = bcode(bp))) { - tp->c_cflag &= ~CBAUD; - tp->c_cflag |= speed; + cfsetispeed(tp, speed); + cfsetospeed(tp, speed); } break; } @@ -828,14 +803,14 @@ auto_baud(tp) tp->c_iflag = iflag; tp->c_cc[VMIN] = vmin; - (void) ioctl(0, TCSETA, tp); + (void) tcsetattr(0, TCSANOW, tp); } /* do_prompt - show login prompt, optionally preceded by /etc/issue contents */ void do_prompt(op, tp) struct options *op; - struct termio *tp; + struct termios *tp; { #ifdef ISSUE FILE *fd; @@ -851,7 +826,7 @@ do_prompt(op, tp) if ((op->flags & F_ISSUE) && (fd = fopen(op->issue, "r"))) { oflag = tp->c_oflag; /* save current setting */ tp->c_oflag |= (ONLCR | OPOST); /* map NL in output to CR-NL */ - (void) ioctl(0, TCSETAW, tp); + (void) tcsetattr(0, TCSADRAIN, tp); while ((c = getc(fd)) != EOF) @@ -955,7 +930,7 @@ do_prompt(op, tp) int i; for (i = 0; speedtab[i].speed; i++) { - if (speedtab[i].code == (tp->c_cflag & CBAUD)) { + if (speedtab[i].code == cfgetispeed(tp)) { printf("%ld", speedtab[i].speed); break; } @@ -987,7 +962,7 @@ do_prompt(op, tp) fflush(stdout); tp->c_oflag = oflag; /* restore settings */ - (void) ioctl(0, TCSETAW, tp); /* wait till output is gone */ + (void) tcsetattr(0, TCSADRAIN, tp); /* wait till output is gone */ (void) fclose(fd); } #endif @@ -1002,15 +977,15 @@ do_prompt(op, tp) /* next_speed - select next baud rate */ void next_speed(tp, op) - struct termio *tp; + struct termios *tp; struct options *op; { static int baud_index = FIRST_SPEED;/* current speed index */ baud_index = (baud_index + 1) % op->numspeed; - tp->c_cflag &= ~CBAUD; - tp->c_cflag |= op->speeds[baud_index]; - (void) ioctl(0, TCSETA, tp); + cfsetispeed(tp, op->speeds[baud_index]); + cfsetospeed(tp, op->speeds[baud_index]); + (void) tcsetattr(0, TCSANOW, tp); } /* get_logname - get user name, establish parity, speed, erase, kill, eol */ @@ -1018,7 +993,7 @@ next_speed(tp, op) char *get_logname(op, cp, tp) struct options *op; struct chardata *cp; - struct termio *tp; + struct termios *tp; { static char logname[BUFSIZ]; char *bp; @@ -1040,7 +1015,7 @@ char *get_logname(op, cp, tp) /* Flush pending input (esp. after parsing or switching the baud rate). */ (void) sleep(1); - (void) ioctl(0, TCFLSH, TCIFLUSH); + (void) tcflush(0, TCIFLUSH); /* Prompt for and read a login name. */ @@ -1129,7 +1104,7 @@ char *get_logname(op, cp, tp) void termio_final(op, tp, cp) struct options *op; - struct termio *tp; + struct termios *tp; struct chardata *cp; { /* General terminal-independent stuff. */ @@ -1145,7 +1120,7 @@ termio_final(op, tp, cp) tp->c_cc[VEOL] = DEF_EOL; #ifdef __linux__ tp->c_cc[VSWTC] = DEF_SWITCH; /* default switch character */ -#else +#elif defined(VSWTCH) tp->c_cc[VSWTCH] = DEF_SWITCH; /* default switch character */ #endif @@ -1179,7 +1154,9 @@ termio_final(op, tp, cp) if (cp->capslock) { tp->c_iflag |= IUCLC; +#ifdef XCASE tp->c_lflag |= XCASE; +#endif tp->c_oflag |= OLCUC; } /* Optionally enable hardware flow control */ @@ -1191,8 +1168,8 @@ termio_final(op, tp, cp) /* Finally, make the new settings effective */ - if (ioctl(0, TCSETA, tp) < 0) - error("%s: ioctl: TCSETA: %m", op->tty); + if (tcsetattr(0, TCSANOW, tp) < 0) + error("%s: tcsetattr: TCSANOW: %m", op->tty); } /* caps_lock - string contains upper case without lower case */ diff --git a/login-utils/login.c b/login-utils/login.c index 1af8792..629b062 100644 --- a/login-utils/login.c +++ b/login-utils/login.c @@ -1308,12 +1308,12 @@ getloginname(void) { static void timedout2(int sig) { - struct termio ti; + struct termios ti; /* reset echo */ - ioctl(0, TCGETA, &ti); + tcgetattr(0, &ti); ti.c_lflag |= ECHO; - ioctl(0, TCSETA, &ti); + tcsetattr(0, TCSANOW, &ti); exit(0); /* %% */ } diff --git a/misc-utils/setterm.c b/misc-utils/setterm.c index ebf7f0a..eda99a3 100644 --- a/misc-utils/setterm.c +++ b/misc-utils/setterm.c @@ -114,11 +114,6 @@ #include <sys/time.h> #include "nls.h" -#ifndef TCGETS -/* TCGETS is either defined in termios.h, or here: */ -#include <asm/ioctls.h> -#endif - #if __GNU_LIBRARY__ < 5 #ifndef __alpha__ # include <linux/unistd.h> diff --git a/text-utils/more.c b/text-utils/more.c index 5dcae7b..14af765 100644 --- a/text-utils/more.c +++ b/text-utils/more.c @@ -1817,7 +1817,7 @@ retry: no_intty = tcgetattr(fileno(stdin), &otty); tcgetattr(fileno(stderr), &otty); savetty0 = otty; - slow_tty = (otty.c_cflag & CBAUD) < B1200; + slow_tty = cfgetispeed(&otty) < B1200; hardtabs = (otty.c_oflag & TABDLY) != XTABS; if (!no_tty) { otty.c_lflag &= ~(ICANON|ECHO);