2 * Copyright (c) 2009 Todd C. Miller <Todd.Miller@courtesan.com>
4 * Permission to use, copy, modify, and distribute this software for any
5 * purpose with or without fee is hereby granted, provided that the above
6 * copyright notice and this permission notice appear in all copies.
8 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
9 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
10 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
11 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
12 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
13 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
14 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
19 #include <sys/types.h>
20 #include <sys/param.h>
29 #endif /* STDC_HEADERS */
31 # if defined(HAVE_MEMORY_H) && !defined(STDC_HEADERS)
36 # ifdef HAVE_STRINGS_H
39 #endif /* HAVE_STRING_H */
47 # include <sys/ioctl.h>
48 # endif /* HAVE_TERMIO_H */
49 #endif /* HAVE_TERMIOS_H */
63 #ifndef _POSIX_VDISABLE
65 # define _POSIX_VDISABLE VDISABLE
67 # define _POSIX_VDISABLE 0
72 * Compat macros for non-termios systems.
74 #ifndef HAVE_TERMIOS_H
77 # define termios termio
78 # define tcgetattr(f, t) ioctl(f, TCGETA, t)
79 # define tcsetattr(f, a, t) ioctl(f, a, t)
81 # define TCSAFLUSH TCSETAF
84 # define termios sgttyb
85 # define c_lflag sg_flags
86 # define tcgetattr(f, t) ioctl(f, TIOCGETP, t)
87 # define tcsetattr(f, a, t) ioctl(f, a, t)
89 # define TCSAFLUSH TIOCSETP
90 # endif /* HAVE_TERMIO_H */
91 #endif /* HAVE_TERMIOS_H */
93 typedef struct termios sudo_term_t;
95 static sudo_term_t term, oterm;
105 if (tcsetattr(fd, TCSAFLUSH|TCSASOFT, &oterm) != 0)
116 if (!changed && tcgetattr(fd, &oterm) != 0)
118 (void) memcpy(&term, &oterm, sizeof(term));
119 CLR(term.c_lflag, ECHO|ECHONL);
121 term.c_cc[VSTATUS] = _POSIX_VDISABLE;
123 if (tcsetattr(fd, TCSAFLUSH|TCSASOFT, &term) == 0) {
130 #if defined(HAVE_TERMIOS_H) || defined(HAVE_TERMIO_H)
135 if (!changed && tcgetattr(fd, &oterm) != 0)
137 (void) memcpy(&term, &oterm, sizeof(term));
138 /* Set terminal to half-cooked mode */
140 term.c_cc[VTIME] = 0;
141 CLR(term.c_lflag, ECHO | ECHONL | ICANON | IEXTEN);
142 SET(term.c_lflag, ISIG);
144 term.c_cc[VSTATUS] = _POSIX_VDISABLE;
146 if (tcsetattr(fd, TCSAFLUSH|TCSASOFT, &term) == 0) {
147 term_erase = term.c_cc[VERASE];
148 term_kill = term.c_cc[VKILL];
161 if (!changed && ioctl(fd, TIOCGETP, &oterm) != 0)
163 (void) memcpy(&term, &oterm, sizeof(term));
164 /* Set terminal to half-cooked mode */
165 CLR(term.c_lflag, ECHO);
166 SET(term.sg_flags, CBREAK);
167 if (ioctl(fd, TIOCSETP, &term) == 0) {
168 term_erase = term.sg_erase;
169 term_kill = term.sg_kill;