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)
35 static 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/fcntl.h>
42 #include <termios.h>
43 #include <stdio.h>
44 #include <unistd.h>
45
46 int
tcgetattr(fd,t)47 tcgetattr(fd, t)
48 int fd;
49 struct termios *t;
50 {
51
52 return(ioctl(fd, TIOCGETA, t));
53 }
54
55 int
tcsetattr(fd,opt,t)56 tcsetattr(fd, opt, t)
57 int fd, opt;
58 const struct termios *t;
59 {
60 struct termios localterm;
61
62 if (opt & TCSASOFT) {
63 localterm = *t;
64 localterm.c_cflag |= CIGNORE;
65 t = &localterm;
66 opt &= ~TCSASOFT;
67 }
68 if (opt == TCSANOW)
69 return (ioctl(fd, TIOCSETA, t));
70 else if (opt == TCSADRAIN)
71 return (ioctl(fd, TIOCSETAW, t));
72 return (ioctl(fd, TIOCSETAF, t));
73 }
74
75 int
76 #if __STDC__
tcsetpgrp(int fd,pid_t pgrp)77 tcsetpgrp(int fd, pid_t pgrp)
78 #else
79 tcsetpgrp(fd, pgrp)
80 int fd;
81 pid_t pgrp;
82 #endif
83 {
84 int s;
85
86 s = pgrp;
87 return(ioctl(fd, TIOCSPGRP, &s));
88 }
89
90 pid_t
tcgetpgrp(fd)91 tcgetpgrp(fd)
92 {
93 int s;
94
95 if (ioctl(fd, TIOCGPGRP, &s) < 0)
96 return((pid_t)-1);
97
98 return((pid_t)s);
99 }
100
101 speed_t
cfgetospeed(t)102 cfgetospeed(t)
103 const struct termios *t;
104 {
105
106 return(t->c_ospeed);
107 }
108
109 speed_t
cfgetispeed(t)110 cfgetispeed(t)
111 const struct termios *t;
112 {
113
114 return(t->c_ispeed);
115 }
116
117 int
cfsetospeed(t,speed)118 cfsetospeed(t, speed)
119 struct termios *t;
120 speed_t speed;
121 {
122 t->c_ospeed = speed;
123
124 return (0);
125 }
126
127 int
cfsetispeed(t,speed)128 cfsetispeed(t, speed)
129 struct termios *t;
130 speed_t speed;
131 {
132 t->c_ispeed = speed;
133
134 return (0);
135 }
136
137 void
cfsetspeed(t,speed)138 cfsetspeed(t, speed)
139 struct termios *t;
140 speed_t speed;
141 {
142 t->c_ispeed = t->c_ospeed = speed;
143 }
144
145 /*
146 * Make a pre-existing termios structure into "raw" mode:
147 * character-at-a-time mode with no characters interpreted,
148 * 8-bit data path.
149 */
150 void
cfmakeraw(t)151 cfmakeraw(t)
152 struct termios *t;
153 {
154 t->c_iflag &= ~(IGNBRK|BRKINT|PARMRK|ISTRIP|INLCR|IGNCR|ICRNL|IXON);
155 t->c_oflag &= ~OPOST;
156 t->c_lflag &= ~(ECHO|ECHONL|ICANON|ISIG|IEXTEN);
157 t->c_cflag &= ~(CSIZE|PARENB);
158 t->c_cflag |= CS8;
159 /* set MIN/TIME */
160 }
161
tcsendbreak(fd,len)162 tcsendbreak(fd, len)
163 int fd, len;
164 {
165 struct timeval sleepytime;
166
167 sleepytime.tv_sec = 0;
168 sleepytime.tv_usec = 400000;
169 if (ioctl(fd, TIOCSBRK, 0) == -1)
170 return (-1);
171 select(0, 0, 0, 0, &sleepytime);
172 if (ioctl(fd, TIOCCBRK, 0) == -1)
173 return (-1);
174
175 return (0);
176 }
177
tcdrain(fd)178 tcdrain(fd)
179 int fd;
180 {
181 if (ioctl(fd, TIOCDRAIN, 0) == -1)
182 return (-1);
183
184 return (0);
185 }
186
tcflush(fd,which)187 tcflush(fd, which)
188 int fd, which;
189 {
190 int com;
191
192 switch (which) {
193 case TCIFLUSH:
194 com = FREAD;
195 break;
196 case TCOFLUSH:
197 com = FWRITE;
198 break;
199 case TCIOFLUSH:
200 com = FREAD | FWRITE;
201 break;
202 default:
203 errno = EINVAL;
204 return (-1);
205 }
206 if (ioctl(fd, TIOCFLUSH, &com) == -1)
207 return (-1);
208
209 return (0);
210 }
211
tcflow(fd,action)212 tcflow(fd, action)
213 int fd, action;
214 {
215 switch (action) {
216 case TCOOFF:
217 return (ioctl(fd, TIOCSTOP, 0));
218 break;
219 case TCOON:
220 return (ioctl(fd, TIOCSTART, 0));
221 break;
222 case TCIOFF:
223 case TCION: { /* these posix functions are STUPID */
224 struct termios term;
225 unsigned char c;
226
227 if (tcgetattr(fd, &term) == -1)
228 return (-1);
229 c = term.c_cc[action == TCIOFF ? VSTOP : VSTART];
230 if (c != _POSIX_VDISABLE && write(fd, &c, 1) == -1)
231 return (-1);
232 break;
233 }
234 default:
235 errno = EINVAL;
236 return (-1);
237 }
238
239 return (0);
240 }
241