Fixed BADSIG to be SIG_ERR per POSIX. /sys/sys/signal.h was updated earlier,
[unix-history] / lib / libc / gen / termios.c
CommitLineData
15637ed4
RG
1/*-
2 * Copyright (c) 1989 The Regents of the University of California.
3 * 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. All advertising materials mentioning features or use of this software
14 * must display the following acknowledgement:
15 * This product includes software developed by the University of
16 * California, Berkeley and its contributors.
17 * 4. Neither the name of the University nor the names of its contributors
18 * may be used to endorse or promote products derived from this software
19 * without specific prior written permission.
20 *
21 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
22 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
23 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
24 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
25 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
26 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
27 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
28 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
29 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
30 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
31 * SUCH DAMAGE.
32 */
33
34#if defined(LIBC_SCCS) && !defined(lint)
35static char sccsid[] = "@(#)termios.c 5.9 (Berkeley) 5/20/91";
36#endif /* LIBC_SCCS and not lint */
37
38#include <sys/types.h>
39#include <sys/errno.h>
40#include <sys/ioctl.h>
41#include <sys/tty.h>
42#define KERNEL /* XXX - FREAD and FWRITE was ifdef'd KERNEL*/
43#include <sys/fcntl.h>
44#undef KERNEL
45#include <termios.h>
46#include <stdio.h>
47#include <unistd.h>
48
49int
50tcgetattr(fd, t)
51 int fd;
52 struct termios *t;
53{
54
55 return(ioctl(fd, TIOCGETA, t));
56}
57
58int
59tcsetattr(fd, opt, t)
60 int fd, opt;
61 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 opt &= ~TCSASOFT;
70 }
71 if (opt == TCSANOW)
72 return (ioctl(fd, TIOCSETA, t));
73 else if (opt == TCSADRAIN)
74 return (ioctl(fd, TIOCSETAW, t));
75 return (ioctl(fd, TIOCSETAF, t));
76}
77
78int
79#if __STDC__
80tcsetpgrp(int fd, pid_t pgrp)
81#else
82tcsetpgrp(fd, pgrp)
83 int fd;
84 pid_t pgrp;
85#endif
86{
87 int s;
88
89 s = pgrp;
90 return(ioctl(fd, TIOCSPGRP, &s));
91}
92
93pid_t
94tcgetpgrp(fd)
95{
96 int s;
97
98 if (ioctl(fd, TIOCGPGRP, &s) < 0)
99 return((pid_t)-1);
100
101 return((pid_t)s);
102}
103
104speed_t
105cfgetospeed(t)
106 const struct termios *t;
107{
108
109 return(t->c_ospeed);
110}
111
112speed_t
113cfgetispeed(t)
114 const struct termios *t;
115{
116
117 return(t->c_ispeed);
118}
119
120int
121cfsetospeed(t, speed)
122 struct termios *t;
123 speed_t speed;
124{
125 t->c_ospeed = speed;
126
127 return (0);
128}
129
130int
131cfsetispeed(t, speed)
132 struct termios *t;
133 speed_t speed;
134{
135 t->c_ispeed = speed;
136
137 return (0);
138}
139
140void
141cfsetspeed(t, speed)
142 struct termios *t;
143 speed_t speed;
144{
145 t->c_ispeed = t->c_ospeed = speed;
146}
147
148/*
149 * Make a pre-existing termios structure into "raw" mode:
150 * character-at-a-time mode with no characters interpreted,
151 * 8-bit data path.
152 */
153void
154cfmakeraw(t)
155 struct termios *t;
156{
157 t->c_iflag &= ~(IGNBRK|BRKINT|PARMRK|ISTRIP|INLCR|IGNCR|ICRNL|IXON);
158 t->c_oflag &= ~OPOST;
159 t->c_lflag &= ~(ECHO|ECHONL|ICANON|ISIG|IEXTEN);
160 t->c_cflag &= ~(CSIZE|PARENB);
161 t->c_cflag |= CS8;
162 /* set MIN/TIME */
163}
164
165tcsendbreak(fd, len)
166 int fd, len;
167{
168 struct timeval sleepytime;
169
170 sleepytime.tv_sec = 0;
171 sleepytime.tv_usec = 400000;
172 if (ioctl(fd, TIOCSBRK, 0) == -1)
173 return (-1);
174 select(0, 0, 0, 0, &sleepytime);
175 if (ioctl(fd, TIOCCBRK, 0) == -1)
176 return (-1);
177
178 return (0);
179}
180
181tcdrain(fd)
182 int fd;
183{
184 if (ioctl(fd, TIOCDRAIN, 0) == -1)
185 return (-1);
186
187 return (0);
188}
189
190tcflush(fd, which)
191 int fd, which;
192{
193 int com;
194
195 switch (which) {
196 case TCIFLUSH:
197 com = FREAD;
198 break;
199 case TCOFLUSH:
200 com = FWRITE;
201 break;
202 case TCIOFLUSH:
203 com = FREAD | FWRITE;
204 break;
205 default:
206 errno = EINVAL;
207 return (-1);
208 }
209 if (ioctl(fd, TIOCFLUSH, &com) == -1)
210 return (-1);
211
212 return (0);
213}
214
215tcflow(fd, action)
216 int fd, action;
217{
218 switch (action) {
219 case TCOOFF:
220 return (ioctl(fd, TIOCSTOP, 0));
221 break;
222 case TCOON:
223 return (ioctl(fd, TIOCSTART, 0));
224 break;
225 case TCIOFF:
226 case TCION: { /* these posix functions are STUPID */
227 struct termios term;
228 unsigned char c;
229
230 if (tcgetattr(fd, &term) == -1)
231 return (-1);
232 c = term.c_cc[action == TCIOFF ? VSTOP : VSTART];
233 if (c != _POSIX_VDISABLE && write(fd, &c, 1) == -1)
234 return (-1);
235 break;
236 }
237 default:
238 errno = EINVAL;
239 return (-1);
240 }
241
242 return (0);
243}