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 */
54 __unused static const char rcsid[] = "$Sudo: term.c,v 1.4 2009/02/25 10:47:12 millert Exp $";
67 #ifndef _POSIX_VDISABLE
69 # define _POSIX_VDISABLE VDISABLE
71 # define _POSIX_VDISABLE 0
76 * Compat macros for non-termios systems.
78 #ifndef HAVE_TERMIOS_H
81 # define termios termio
82 # define tcgetattr(f, t) ioctl(f, TCGETA, t)
83 # define tcsetattr(f, a, t) ioctl(f, a, t)
85 # define TCSAFLUSH TCSETAF
88 # define termios sgttyb
89 # define c_lflag sg_flags
90 # define tcgetattr(f, t) ioctl(f, TIOCGETP, t)
91 # define tcsetattr(f, a, t) ioctl(f, a, t)
93 # define TCSAFLUSH TIOCSETP
94 # endif /* HAVE_TERMIO_H */
95 #endif /* HAVE_TERMIOS_H */
97 typedef struct termios sudo_term_t;
99 static sudo_term_t term, oterm;
109 if (tcsetattr(fd, TCSAFLUSH|TCSASOFT, &oterm) != 0)
120 if (!changed && tcgetattr(fd, &oterm) != 0)
122 (void) memcpy(&term, &oterm, sizeof(term));
123 CLR(term.c_lflag, ECHO|ECHONL);
125 term.c_cc[VSTATUS] = _POSIX_VDISABLE;
127 if (tcsetattr(fd, TCSAFLUSH|TCSASOFT, &term) == 0) {
134 #if defined(HAVE_TERMIOS_H) || defined(HAVE_TERMIO_H)
139 if (!changed && tcgetattr(fd, &oterm) != 0)
141 (void) memcpy(&term, &oterm, sizeof(term));
142 /* Set terminal to half-cooked mode */
144 term.c_cc[VTIME] = 0;
145 CLR(term.c_lflag, ECHO | ECHONL | ICANON | IEXTEN);
146 SET(term.c_lflag, ISIG);
148 term.c_cc[VSTATUS] = _POSIX_VDISABLE;
150 if (tcsetattr(fd, TCSAFLUSH|TCSASOFT, &term) == 0) {
151 term_erase = term.c_cc[VERASE];
152 term_kill = term.c_cc[VKILL];
165 if (!changed && ioctl(fd, TIOCGETP, &oterm) != 0)
167 (void) memcpy(&term, &oterm, sizeof(term));
168 /* Set terminal to half-cooked mode */
169 CLR(term.c_lflag, ECHO);
170 SET(term.sg_flags, CBREAK);
171 if (ioctl(fd, TIOCSETP, &term) == 0) {
172 term_erase = term.sg_erase;
173 term_kill = term.sg_kill;