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