]> git.saurik.com Git - apple/libc.git/blame - gen/FreeBSD/termios.c
Libc-825.40.1.tar.gz
[apple/libc.git] / gen / FreeBSD / termios.c
CommitLineData
9385eb3d 1/*-
e9ce8d39
A
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.
e9ce8d39
A
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
9385eb3d
A
30#if defined(LIBC_SCCS) && !defined(lint)
31static char sccsid[] = "@(#)termios.c 8.2 (Berkeley) 2/21/94";
32#endif /* LIBC_SCCS and not lint */
33#include <sys/cdefs.h>
1f2f436a 34__FBSDID("$FreeBSD: src/lib/libc/gen/termios.c,v 1.16 2009/05/07 13:49:48 ed Exp $");
e9ce8d39 35
ad3c9f2a
A
36#if __DARWIN_UNIX03
37#ifdef VARIANT_CANCELABLE
38#include <pthread.h>
39
40extern void _pthread_testcancel(pthread_t thread, int isconforming);
41#endif /* VARIANT_CANCELABLE */
42#endif /* __DARWIN_UNIX03 */
43
9385eb3d 44#include "namespace.h"
e9ce8d39 45#include <sys/types.h>
9385eb3d 46#include <sys/fcntl.h>
e9ce8d39 47#include <sys/ioctl.h>
e9ce8d39 48#include <sys/time.h>
e9ce8d39
A
49
50#include <errno.h>
e9ce8d39
A
51#include <termios.h>
52#include <unistd.h>
9385eb3d 53#include "un-namespace.h"
e9ce8d39 54
ad3c9f2a 55#ifndef BUILDING_VARIANT
e9ce8d39
A
56int
57tcgetattr(fd, t)
58 int fd;
59 struct termios *t;
60{
61
9385eb3d 62 return (_ioctl(fd, TIOCGETA, t));
e9ce8d39
A
63}
64
65int
66tcsetattr(fd, opt, t)
67 int fd, opt;
68 const struct termios *t;
69{
70 struct termios localterm;
71
72 if (opt & TCSASOFT) {
73 localterm = *t;
74 localterm.c_cflag |= CIGNORE;
75 t = &localterm;
76 }
77 switch (opt & ~TCSASOFT) {
78 case TCSANOW:
9385eb3d 79 return (_ioctl(fd, TIOCSETA, t));
e9ce8d39 80 case TCSADRAIN:
9385eb3d 81 return (_ioctl(fd, TIOCSETAW, t));
e9ce8d39 82 case TCSAFLUSH:
9385eb3d 83 return (_ioctl(fd, TIOCSETAF, t));
e9ce8d39
A
84 default:
85 errno = EINVAL;
86 return (-1);
87 }
88}
89
90int
e9ce8d39 91tcsetpgrp(int fd, pid_t pgrp)
e9ce8d39
A
92{
93 int s;
94
ad3c9f2a
A
95 if (isatty(fd) == 0)
96 return (-1);
97
e9ce8d39 98 s = pgrp;
9385eb3d 99 return (_ioctl(fd, TIOCSPGRP, &s));
e9ce8d39
A
100}
101
102pid_t
103tcgetpgrp(fd)
9385eb3d 104 int fd;
e9ce8d39
A
105{
106 int s;
107
ad3c9f2a
A
108 if (isatty(fd) == 0)
109 return ((pid_t)-1);
110
9385eb3d 111 if (_ioctl(fd, TIOCGPGRP, &s) < 0)
e9ce8d39
A
112 return ((pid_t)-1);
113
114 return ((pid_t)s);
115}
116
ad3c9f2a 117#if 0 // Needs API review first
1f2f436a
A
118pid_t
119tcgetsid(int fd)
120{
121 int s;
122
123 if (_ioctl(fd, TIOCGSID, &s) < 0)
124 return ((pid_t)-1);
125
126 return ((pid_t)s);
127}
128
129int
130tcsetsid(int fd, pid_t pid)
131{
132
133 if (pid != getsid(0)) {
134 errno = EINVAL;
135 return (-1);
136 }
137
138 return (_ioctl(fd, TIOCSCTTY, NULL));
139}
ad3c9f2a 140#endif
1f2f436a 141
e9ce8d39
A
142speed_t
143cfgetospeed(t)
144 const struct termios *t;
145{
146
147 return (t->c_ospeed);
148}
149
150speed_t
151cfgetispeed(t)
152 const struct termios *t;
153{
154
155 return (t->c_ispeed);
156}
157
158int
159cfsetospeed(t, speed)
160 struct termios *t;
161 speed_t speed;
162{
163
164 t->c_ospeed = speed;
165 return (0);
166}
167
168int
169cfsetispeed(t, speed)
170 struct termios *t;
171 speed_t speed;
172{
173
174 t->c_ispeed = speed;
175 return (0);
176}
177
178int
179cfsetspeed(t, speed)
180 struct termios *t;
181 speed_t speed;
182{
183
184 t->c_ispeed = t->c_ospeed = speed;
185 return (0);
186}
187
188/*
189 * Make a pre-existing termios structure into "raw" mode: character-at-a-time
190 * mode with no characters interpreted, 8-bit data path.
191 */
192void
193cfmakeraw(t)
194 struct termios *t;
195{
196
9385eb3d
A
197 t->c_iflag &= ~(IMAXBEL|IXOFF|INPCK|BRKINT|PARMRK|ISTRIP|INLCR|IGNCR|ICRNL|IXON|IGNPAR);
198 t->c_iflag |= IGNBRK;
e9ce8d39 199 t->c_oflag &= ~OPOST;
9385eb3d 200 t->c_lflag &= ~(ECHO|ECHOE|ECHOK|ECHONL|ICANON|ISIG|IEXTEN|NOFLSH|TOSTOP|PENDIN);
e9ce8d39 201 t->c_cflag &= ~(CSIZE|PARENB);
9385eb3d
A
202 t->c_cflag |= CS8|CREAD;
203 t->c_cc[VMIN] = 1;
204 t->c_cc[VTIME] = 0;
e9ce8d39
A
205}
206
9385eb3d 207int
e9ce8d39
A
208tcsendbreak(fd, len)
209 int fd, len;
210{
211 struct timeval sleepytime;
212
213 sleepytime.tv_sec = 0;
214 sleepytime.tv_usec = 400000;
9385eb3d 215 if (_ioctl(fd, TIOCSBRK, 0) == -1)
e9ce8d39 216 return (-1);
9385eb3d
A
217 (void)_select(0, 0, 0, 0, &sleepytime);
218 if (_ioctl(fd, TIOCCBRK, 0) == -1)
e9ce8d39
A
219 return (-1);
220 return (0);
221}
ad3c9f2a 222#endif /* BUILDING_VARIANT */
e9ce8d39 223
9385eb3d
A
224int
225__tcdrain(fd)
e9ce8d39
A
226 int fd;
227{
ad3c9f2a
A
228#if __DARWIN_UNIX03
229#ifdef VARIANT_CANCELABLE
230 _pthread_testcancel(pthread_self(), 1);
231#endif /* VARIANT_CANCELABLE */
232#endif /* __DARWIN_UNIX03 */
9385eb3d 233 return (_ioctl(fd, TIOCDRAIN, 0));
e9ce8d39
A
234}
235
9385eb3d
A
236__weak_reference(__tcdrain, tcdrain);
237__weak_reference(__tcdrain, _tcdrain);
238
ad3c9f2a 239#ifndef BUILDING_VARIANT
9385eb3d 240int
e9ce8d39
A
241tcflush(fd, which)
242 int fd, which;
243{
244 int com;
245
246 switch (which) {
247 case TCIFLUSH:
248 com = FREAD;
249 break;
250 case TCOFLUSH:
251 com = FWRITE;
252 break;
253 case TCIOFLUSH:
254 com = FREAD | FWRITE;
255 break;
256 default:
257 errno = EINVAL;
258 return (-1);
259 }
9385eb3d 260 return (_ioctl(fd, TIOCFLUSH, &com));
e9ce8d39
A
261}
262
9385eb3d 263int
e9ce8d39
A
264tcflow(fd, action)
265 int fd, action;
266{
267 struct termios term;
268 u_char c;
269
270 switch (action) {
271 case TCOOFF:
9385eb3d 272 return (_ioctl(fd, TIOCSTOP, 0));
e9ce8d39 273 case TCOON:
9385eb3d 274 return (_ioctl(fd, TIOCSTART, 0));
e9ce8d39 275 case TCION:
ad3c9f2a 276 return (_ioctl(fd, TIOCIXON, 0));
e9ce8d39 277 case TCIOFF:
ad3c9f2a 278 return (_ioctl(fd, TIOCIXOFF, 0));
e9ce8d39
A
279 default:
280 errno = EINVAL;
281 return (-1);
282 }
283 /* NOTREACHED */
284}
ad3c9f2a 285#endif /* BUILDING_VARIANT */