]> git.saurik.com Git - apple/libc.git/blob - gen/FreeBSD/termios.c
Libc-763.12.tar.gz
[apple/libc.git] / gen / FreeBSD / termios.c
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: src/lib/libc/gen/termios.c,v 1.16 2009/05/07 13:49:48 ed Exp $");
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 int
114 tcsetsid(int fd, pid_t pid)
115 {
116
117 if (pid != getsid(0)) {
118 errno = EINVAL;
119 return (-1);
120 }
121
122 return (_ioctl(fd, TIOCSCTTY, NULL));
123 }
124
125 speed_t
126 cfgetospeed(t)
127 const struct termios *t;
128 {
129
130 return (t->c_ospeed);
131 }
132
133 speed_t
134 cfgetispeed(t)
135 const struct termios *t;
136 {
137
138 return (t->c_ispeed);
139 }
140
141 int
142 cfsetospeed(t, speed)
143 struct termios *t;
144 speed_t speed;
145 {
146
147 t->c_ospeed = speed;
148 return (0);
149 }
150
151 int
152 cfsetispeed(t, speed)
153 struct termios *t;
154 speed_t speed;
155 {
156
157 t->c_ispeed = speed;
158 return (0);
159 }
160
161 int
162 cfsetspeed(t, speed)
163 struct termios *t;
164 speed_t speed;
165 {
166
167 t->c_ispeed = t->c_ospeed = speed;
168 return (0);
169 }
170
171 /*
172 * Make a pre-existing termios structure into "raw" mode: character-at-a-time
173 * mode with no characters interpreted, 8-bit data path.
174 */
175 void
176 cfmakeraw(t)
177 struct termios *t;
178 {
179
180 t->c_iflag &= ~(IMAXBEL|IXOFF|INPCK|BRKINT|PARMRK|ISTRIP|INLCR|IGNCR|ICRNL|IXON|IGNPAR);
181 t->c_iflag |= IGNBRK;
182 t->c_oflag &= ~OPOST;
183 t->c_lflag &= ~(ECHO|ECHOE|ECHOK|ECHONL|ICANON|ISIG|IEXTEN|NOFLSH|TOSTOP|PENDIN);
184 t->c_cflag &= ~(CSIZE|PARENB);
185 t->c_cflag |= CS8|CREAD;
186 t->c_cc[VMIN] = 1;
187 t->c_cc[VTIME] = 0;
188 }
189
190 int
191 tcsendbreak(fd, len)
192 int fd, len;
193 {
194 struct timeval sleepytime;
195
196 sleepytime.tv_sec = 0;
197 sleepytime.tv_usec = 400000;
198 if (_ioctl(fd, TIOCSBRK, 0) == -1)
199 return (-1);
200 (void)_select(0, 0, 0, 0, &sleepytime);
201 if (_ioctl(fd, TIOCCBRK, 0) == -1)
202 return (-1);
203 return (0);
204 }
205
206 int
207 __tcdrain(fd)
208 int fd;
209 {
210 return (_ioctl(fd, TIOCDRAIN, 0));
211 }
212
213 __weak_reference(__tcdrain, tcdrain);
214 __weak_reference(__tcdrain, _tcdrain);
215
216 int
217 tcflush(fd, which)
218 int fd, which;
219 {
220 int com;
221
222 switch (which) {
223 case TCIFLUSH:
224 com = FREAD;
225 break;
226 case TCOFLUSH:
227 com = FWRITE;
228 break;
229 case TCIOFLUSH:
230 com = FREAD | FWRITE;
231 break;
232 default:
233 errno = EINVAL;
234 return (-1);
235 }
236 return (_ioctl(fd, TIOCFLUSH, &com));
237 }
238
239 int
240 tcflow(fd, action)
241 int fd, action;
242 {
243 struct termios term;
244 u_char c;
245
246 switch (action) {
247 case TCOOFF:
248 return (_ioctl(fd, TIOCSTOP, 0));
249 case TCOON:
250 return (_ioctl(fd, TIOCSTART, 0));
251 case TCION:
252 case TCIOFF:
253 if (tcgetattr(fd, &term) == -1)
254 return (-1);
255 c = term.c_cc[action == TCIOFF ? VSTOP : VSTART];
256 if (c != _POSIX_VDISABLE && _write(fd, &c, sizeof(c)) == -1)
257 return (-1);
258 return (0);
259 default:
260 errno = EINVAL;
261 return (-1);
262 }
263 /* NOTREACHED */
264 }