]>
Commit | Line | Data |
---|---|---|
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) |
31 | static 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 | |
9385eb3d | 36 | #include "namespace.h" |
e9ce8d39 | 37 | #include <sys/types.h> |
9385eb3d | 38 | #include <sys/fcntl.h> |
e9ce8d39 | 39 | #include <sys/ioctl.h> |
e9ce8d39 | 40 | #include <sys/time.h> |
e9ce8d39 A |
41 | |
42 | #include <errno.h> | |
e9ce8d39 A |
43 | #include <termios.h> |
44 | #include <unistd.h> | |
9385eb3d | 45 | #include "un-namespace.h" |
e9ce8d39 A |
46 | |
47 | int | |
48 | tcgetattr(fd, t) | |
49 | int fd; | |
50 | struct termios *t; | |
51 | { | |
52 | ||
9385eb3d | 53 | return (_ioctl(fd, TIOCGETA, t)); |
e9ce8d39 A |
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: | |
9385eb3d | 70 | return (_ioctl(fd, TIOCSETA, t)); |
e9ce8d39 | 71 | case TCSADRAIN: |
9385eb3d | 72 | return (_ioctl(fd, TIOCSETAW, t)); |
e9ce8d39 | 73 | case TCSAFLUSH: |
9385eb3d | 74 | return (_ioctl(fd, TIOCSETAF, t)); |
e9ce8d39 A |
75 | default: |
76 | errno = EINVAL; | |
77 | return (-1); | |
78 | } | |
79 | } | |
80 | ||
81 | int | |
e9ce8d39 | 82 | tcsetpgrp(int fd, pid_t pgrp) |
e9ce8d39 A |
83 | { |
84 | int s; | |
85 | ||
86 | s = pgrp; | |
9385eb3d | 87 | return (_ioctl(fd, TIOCSPGRP, &s)); |
e9ce8d39 A |
88 | } |
89 | ||
90 | pid_t | |
91 | tcgetpgrp(fd) | |
9385eb3d | 92 | int fd; |
e9ce8d39 A |
93 | { |
94 | int s; | |
95 | ||
9385eb3d | 96 | if (_ioctl(fd, TIOCGPGRP, &s) < 0) |
e9ce8d39 A |
97 | return ((pid_t)-1); |
98 | ||
99 | return ((pid_t)s); | |
100 | } | |
101 | ||
1f2f436a A |
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 | ||
e9ce8d39 A |
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 | ||
9385eb3d A |
180 | t->c_iflag &= ~(IMAXBEL|IXOFF|INPCK|BRKINT|PARMRK|ISTRIP|INLCR|IGNCR|ICRNL|IXON|IGNPAR); |
181 | t->c_iflag |= IGNBRK; | |
e9ce8d39 | 182 | t->c_oflag &= ~OPOST; |
9385eb3d | 183 | t->c_lflag &= ~(ECHO|ECHOE|ECHOK|ECHONL|ICANON|ISIG|IEXTEN|NOFLSH|TOSTOP|PENDIN); |
e9ce8d39 | 184 | t->c_cflag &= ~(CSIZE|PARENB); |
9385eb3d A |
185 | t->c_cflag |= CS8|CREAD; |
186 | t->c_cc[VMIN] = 1; | |
187 | t->c_cc[VTIME] = 0; | |
e9ce8d39 A |
188 | } |
189 | ||
9385eb3d | 190 | int |
e9ce8d39 A |
191 | tcsendbreak(fd, len) |
192 | int fd, len; | |
193 | { | |
194 | struct timeval sleepytime; | |
195 | ||
196 | sleepytime.tv_sec = 0; | |
197 | sleepytime.tv_usec = 400000; | |
9385eb3d | 198 | if (_ioctl(fd, TIOCSBRK, 0) == -1) |
e9ce8d39 | 199 | return (-1); |
9385eb3d A |
200 | (void)_select(0, 0, 0, 0, &sleepytime); |
201 | if (_ioctl(fd, TIOCCBRK, 0) == -1) | |
e9ce8d39 A |
202 | return (-1); |
203 | return (0); | |
204 | } | |
205 | ||
9385eb3d A |
206 | int |
207 | __tcdrain(fd) | |
e9ce8d39 A |
208 | int fd; |
209 | { | |
9385eb3d | 210 | return (_ioctl(fd, TIOCDRAIN, 0)); |
e9ce8d39 A |
211 | } |
212 | ||
9385eb3d A |
213 | __weak_reference(__tcdrain, tcdrain); |
214 | __weak_reference(__tcdrain, _tcdrain); | |
215 | ||
216 | int | |
e9ce8d39 A |
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 | } | |
9385eb3d | 236 | return (_ioctl(fd, TIOCFLUSH, &com)); |
e9ce8d39 A |
237 | } |
238 | ||
9385eb3d | 239 | int |
e9ce8d39 A |
240 | tcflow(fd, action) |
241 | int fd, action; | |
242 | { | |
243 | struct termios term; | |
244 | u_char c; | |
245 | ||
246 | switch (action) { | |
247 | case TCOOFF: | |
9385eb3d | 248 | return (_ioctl(fd, TIOCSTOP, 0)); |
e9ce8d39 | 249 | case TCOON: |
9385eb3d | 250 | return (_ioctl(fd, TIOCSTART, 0)); |
e9ce8d39 A |
251 | case TCION: |
252 | case TCIOFF: | |
253 | if (tcgetattr(fd, &term) == -1) | |
254 | return (-1); | |
255 | c = term.c_cc[action == TCIOFF ? VSTOP : VSTART]; | |
9385eb3d | 256 | if (c != _POSIX_VDISABLE && _write(fd, &c, sizeof(c)) == -1) |
e9ce8d39 A |
257 | return (-1); |
258 | return (0); | |
259 | default: | |
260 | errno = EINVAL; | |
261 | return (-1); | |
262 | } | |
263 | /* NOTREACHED */ | |
264 | } |