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 * 3. 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 * $FreeBSD: src/lib/libc/gen/termios.c,v 1.9.2.1 2000/03/18 23:13:25 jasone Exp $ 30 * $DragonFly: src/lib/libc/gen/termios.c,v 1.5 2005/11/19 22:32:53 swildner Exp $ 31 * 32 * @(#)termios.c 8.2 (Berkeley) 2/21/94 33 */ 34 35 #include "namespace.h" 36 #include <sys/types.h> 37 #include <sys/fcntl.h> 38 #include <sys/ioctl.h> 39 #include <sys/time.h> 40 41 #include <errno.h> 42 #include <termios.h> 43 #include <unistd.h> 44 #include "un-namespace.h" 45 46 int 47 tcgetattr(int fd, struct termios *t) 48 { 49 50 return (_ioctl(fd, TIOCGETA, t)); 51 } 52 53 int 54 tcsetattr(int fd, int opt, const struct termios *t) 55 { 56 struct termios localterm; 57 58 if (opt & TCSASOFT) { 59 localterm = *t; 60 localterm.c_cflag |= CIGNORE; 61 t = &localterm; 62 } 63 switch (opt & ~TCSASOFT) { 64 case TCSANOW: 65 return (_ioctl(fd, TIOCSETA, t)); 66 case TCSADRAIN: 67 return (_ioctl(fd, TIOCSETAW, t)); 68 case TCSAFLUSH: 69 return (_ioctl(fd, TIOCSETAF, t)); 70 default: 71 errno = EINVAL; 72 return (-1); 73 } 74 } 75 76 int 77 tcsetpgrp(int fd, pid_t pgrp) 78 { 79 int s; 80 81 s = pgrp; 82 return (_ioctl(fd, TIOCSPGRP, &s)); 83 } 84 85 pid_t 86 tcgetpgrp(int fd) 87 { 88 int s; 89 90 if (_ioctl(fd, TIOCGPGRP, &s) < 0) 91 return ((pid_t)-1); 92 93 return ((pid_t)s); 94 } 95 96 pid_t 97 tcgetsid(int fd) 98 { 99 int s; 100 101 if (_ioctl(fd, TIOCGSID, &s) < 0) 102 return ((pid_t)-1); 103 104 return ((pid_t)s); 105 } 106 107 speed_t 108 cfgetospeed(const struct termios *t) 109 { 110 111 return (t->c_ospeed); 112 } 113 114 speed_t 115 cfgetispeed(const struct termios *t) 116 { 117 118 return (t->c_ispeed); 119 } 120 121 int 122 cfsetospeed(struct termios *t, speed_t speed) 123 { 124 125 t->c_ospeed = speed; 126 return (0); 127 } 128 129 int 130 cfsetispeed(struct termios *t, speed_t speed) 131 { 132 133 t->c_ispeed = speed; 134 return (0); 135 } 136 137 int 138 cfsetspeed(struct termios *t, speed_t speed) 139 { 140 141 t->c_ispeed = t->c_ospeed = speed; 142 return (0); 143 } 144 145 /* 146 * Make a pre-existing termios structure into "raw" mode: character-at-a-time 147 * mode with no characters interpreted, 8-bit data path. 148 */ 149 void 150 cfmakeraw(struct termios *t) 151 { 152 153 t->c_iflag &= ~(IMAXBEL|IXOFF|INPCK|BRKINT|PARMRK|ISTRIP|INLCR|IGNCR|ICRNL|IXON|IGNPAR); 154 t->c_iflag |= IGNBRK; 155 t->c_oflag &= ~OPOST; 156 t->c_lflag &= ~(ECHO|ECHOE|ECHOK|ECHONL|ICANON|ISIG|IEXTEN|NOFLSH|TOSTOP|PENDIN); 157 t->c_cflag &= ~(CSIZE|PARENB); 158 t->c_cflag |= CS8|CREAD; 159 t->c_cc[VMIN] = 1; 160 t->c_cc[VTIME] = 0; 161 } 162 163 int 164 tcsendbreak(int fd, int len __unused) 165 { 166 struct timeval sleepytime; 167 168 sleepytime.tv_sec = 0; 169 sleepytime.tv_usec = 400000; 170 if (_ioctl(fd, TIOCSBRK, 0) == -1) 171 return (-1); 172 _select(0, 0, 0, 0, &sleepytime); 173 if (_ioctl(fd, TIOCCBRK, 0) == -1) 174 return (-1); 175 return (0); 176 } 177 178 int 179 __tcdrain(int fd) 180 { 181 return (_ioctl(fd, TIOCDRAIN, 0)); 182 } 183 184 #ifndef _THREAD_SAFE 185 __weak_reference(__tcdrain, tcdrain); 186 #endif 187 188 int 189 tcflush(int fd, int which) 190 { 191 int com; 192 193 switch (which) { 194 case TCIFLUSH: 195 com = FREAD; 196 break; 197 case TCOFLUSH: 198 com = FWRITE; 199 break; 200 case TCIOFLUSH: 201 com = FREAD | FWRITE; 202 break; 203 default: 204 errno = EINVAL; 205 return (-1); 206 } 207 return (_ioctl(fd, TIOCFLUSH, &com)); 208 } 209 210 int 211 tcflow(int fd, int action) 212 { 213 struct termios term; 214 u_char c; 215 216 switch (action) { 217 case TCOOFF: 218 return (_ioctl(fd, TIOCSTOP, 0)); 219 case TCOON: 220 return (_ioctl(fd, TIOCSTART, 0)); 221 case TCION: 222 case TCIOFF: 223 if (tcgetattr(fd, &term) == -1) 224 return (-1); 225 c = term.c_cc[action == TCIOFF ? VSTOP : VSTART]; 226 if (c != _POSIX_VDISABLE && _write(fd, &c, sizeof(c)) == -1) 227 return (-1); 228 return (0); 229 default: 230 errno = EINVAL; 231 return (-1); 232 } 233 /* NOTREACHED */ 234 } 235