1 /*- 2 * Copyright (c) 1989, 1993 3 * The Regents of the University of California. All rights reserved. 4 * 5 * Redistribution and use in source and binary forms, with or without 6 * modification, are permitted provided that the following conditions 7 * are met: 8 * 1. Redistributions of source code must retain the above copyright 9 * notice, this list of conditions and the following disclaimer. 10 * 2. Redistributions in binary form must reproduce the above copyright 11 * notice, this list of conditions and the following disclaimer in the 12 * documentation and/or other materials provided with the distribution. 13 * 4. Neither the name of the University nor the names of its contributors 14 * may be used to endorse or promote products derived from this software 15 * without specific prior written permission. 16 * 17 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 18 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 19 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 20 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 21 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 22 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 23 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 24 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 25 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 26 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 27 * SUCH DAMAGE. 28 */ 29 30 #if defined(LIBC_SCCS) && !defined(lint) 31 static char sccsid[] = "@(#)termios.c 8.2 (Berkeley) 2/21/94"; 32 #endif /* LIBC_SCCS and not lint */ 33 #include <sys/cdefs.h> 34 __FBSDID("$FreeBSD$"); 35 36 #include "namespace.h" 37 #include <sys/types.h> 38 #include <sys/fcntl.h> 39 #include <sys/ioctl.h> 40 #include <sys/time.h> 41 42 #include <errno.h> 43 #include <termios.h> 44 #include <unistd.h> 45 #include "un-namespace.h" 46 47 int 48 tcgetattr(fd, t) 49 int fd; 50 struct termios *t; 51 { 52 53 return (_ioctl(fd, TIOCGETA, t)); 54 } 55 56 int 57 tcsetattr(fd, opt, t) 58 int fd, opt; 59 const struct termios *t; 60 { 61 struct termios localterm; 62 63 if (opt & TCSASOFT) { 64 localterm = *t; 65 localterm.c_cflag |= CIGNORE; 66 t = &localterm; 67 } 68 switch (opt & ~TCSASOFT) { 69 case TCSANOW: 70 return (_ioctl(fd, TIOCSETA, t)); 71 case TCSADRAIN: 72 return (_ioctl(fd, TIOCSETAW, t)); 73 case TCSAFLUSH: 74 return (_ioctl(fd, TIOCSETAF, t)); 75 default: 76 errno = EINVAL; 77 return (-1); 78 } 79 } 80 81 int 82 tcsetpgrp(int fd, pid_t pgrp) 83 { 84 int s; 85 86 s = pgrp; 87 return (_ioctl(fd, TIOCSPGRP, &s)); 88 } 89 90 pid_t 91 tcgetpgrp(fd) 92 int fd; 93 { 94 int s; 95 96 if (_ioctl(fd, TIOCGPGRP, &s) < 0) 97 return ((pid_t)-1); 98 99 return ((pid_t)s); 100 } 101 102 pid_t 103 tcgetsid(int fd) 104 { 105 int s; 106 107 if (_ioctl(fd, TIOCGSID, &s) < 0) 108 return ((pid_t)-1); 109 110 return ((pid_t)s); 111 } 112 113 speed_t 114 cfgetospeed(t) 115 const struct termios *t; 116 { 117 118 return (t->c_ospeed); 119 } 120 121 speed_t 122 cfgetispeed(t) 123 const struct termios *t; 124 { 125 126 return (t->c_ispeed); 127 } 128 129 int 130 cfsetospeed(t, speed) 131 struct termios *t; 132 speed_t speed; 133 { 134 135 t->c_ospeed = speed; 136 return (0); 137 } 138 139 int 140 cfsetispeed(t, speed) 141 struct termios *t; 142 speed_t speed; 143 { 144 145 t->c_ispeed = speed; 146 return (0); 147 } 148 149 int 150 cfsetspeed(t, speed) 151 struct termios *t; 152 speed_t speed; 153 { 154 155 t->c_ispeed = t->c_ospeed = speed; 156 return (0); 157 } 158 159 /* 160 * Make a pre-existing termios structure into "raw" mode: character-at-a-time 161 * mode with no characters interpreted, 8-bit data path. 162 */ 163 void 164 cfmakeraw(t) 165 struct termios *t; 166 { 167 168 t->c_iflag &= ~(IMAXBEL|IXOFF|INPCK|BRKINT|PARMRK|ISTRIP|INLCR|IGNCR|ICRNL|IXON|IGNPAR); 169 t->c_iflag |= IGNBRK; 170 t->c_oflag &= ~OPOST; 171 t->c_lflag &= ~(ECHO|ECHOE|ECHOK|ECHONL|ICANON|ISIG|IEXTEN|NOFLSH|TOSTOP|PENDIN); 172 t->c_cflag &= ~(CSIZE|PARENB); 173 t->c_cflag |= CS8|CREAD; 174 t->c_cc[VMIN] = 1; 175 t->c_cc[VTIME] = 0; 176 } 177 178 int 179 tcsendbreak(fd, len) 180 int fd, len; 181 { 182 struct timeval sleepytime; 183 184 sleepytime.tv_sec = 0; 185 sleepytime.tv_usec = 400000; 186 if (_ioctl(fd, TIOCSBRK, 0) == -1) 187 return (-1); 188 (void)_select(0, 0, 0, 0, &sleepytime); 189 if (_ioctl(fd, TIOCCBRK, 0) == -1) 190 return (-1); 191 return (0); 192 } 193 194 int 195 __tcdrain(fd) 196 int fd; 197 { 198 return (_ioctl(fd, TIOCDRAIN, 0)); 199 } 200 201 __weak_reference(__tcdrain, tcdrain); 202 __weak_reference(__tcdrain, _tcdrain); 203 204 int 205 tcflush(fd, which) 206 int fd, which; 207 { 208 int com; 209 210 switch (which) { 211 case TCIFLUSH: 212 com = FREAD; 213 break; 214 case TCOFLUSH: 215 com = FWRITE; 216 break; 217 case TCIOFLUSH: 218 com = FREAD | FWRITE; 219 break; 220 default: 221 errno = EINVAL; 222 return (-1); 223 } 224 return (_ioctl(fd, TIOCFLUSH, &com)); 225 } 226 227 int 228 tcflow(fd, action) 229 int fd, action; 230 { 231 struct termios term; 232 u_char c; 233 234 switch (action) { 235 case TCOOFF: 236 return (_ioctl(fd, TIOCSTOP, 0)); 237 case TCOON: 238 return (_ioctl(fd, TIOCSTART, 0)); 239 case TCION: 240 case TCIOFF: 241 if (tcgetattr(fd, &term) == -1) 242 return (-1); 243 c = term.c_cc[action == TCIOFF ? VSTOP : VSTART]; 244 if (c != _POSIX_VDISABLE && _write(fd, &c, sizeof(c)) == -1) 245 return (-1); 246 return (0); 247 default: 248 errno = EINVAL; 249 return (-1); 250 } 251 /* NOTREACHED */ 252 } 253