macro and text revision (-mdoc version 3)
[unix-history] / usr / src / lib / libc / gen / termios.c
CommitLineData
7c1f1e23 1/*-
f1fd7d4e
MK
2 * Copyright (c) 1989 The Regents of the University of California.
3 * All rights reserved.
4 *
7c1f1e23 5 * %sccs.include.redist.c%
f1fd7d4e
MK
6 */
7
8#if defined(LIBC_SCCS) && !defined(lint)
f18741dc 9static char sccsid[] = "@(#)termios.c 5.6 (Berkeley) %G%";
f1fd7d4e
MK
10#endif /* LIBC_SCCS and not lint */
11
816ee792
MT
12#include <sys/types.h>
13#include <sys/errno.h>
7c1f1e23 14#include <sys/ioctl.h>
816ee792 15#include <sys/tty.h>
f18741dc
MT
16#define KERNEL /* XXX - FREAD and FWRITE was ifdef'd KERNEL*/
17#include <sys/fcntl.h>
18#undef KERNEL
c5980113 19#include <termios.h>
816ee792 20#include <stdio.h>
c5980113 21#include <unistd.h>
816ee792 22
c5980113 23int
816ee792
MT
24tcgetattr(fd, t)
25 int fd;
26 struct termios *t;
27{
816ee792
MT
28
29 return(ioctl(fd, TIOCGETA, t));
30}
31
c5980113 32int
816ee792
MT
33tcsetattr(fd, opt, t)
34 int fd, opt;
c5980113 35 const struct termios *t;
816ee792 36{
7c1f1e23 37 struct termios localterm;
816ee792 38
7c1f1e23
MT
39 if (opt & TCSASOFT) {
40 localterm = *t;
41 localterm.c_cflag |= CIGNORE;
42 t = &localterm;
43 opt &= TCSASOFT;
816ee792 44 }
7c1f1e23
MT
45 if (opt == TCSANOW)
46 return (ioctl(fd, TIOCSETA, t));
47 else if (opt == TCSADRAIN)
48 return (ioctl(fd, TIOCSETAW, t));
c5980113 49 return (ioctl(fd, TIOCSETAF, t));
816ee792
MT
50}
51
c5980113
DS
52int
53#if __STDC__
54tcsetpgrp(int fd, pid_t pgrp)
55#else
816ee792 56tcsetpgrp(fd, pgrp)
c5980113
DS
57 int fd;
58 pid_t pgrp;
59#endif
816ee792 60{
f18741dc 61
816ee792
MT
62 return(ioctl(fd, TIOCSPGRP, &pgrp));
63}
64
c5980113 65pid_t
816ee792
MT
66tcgetpgrp(fd)
67{
7dbb5ba6 68 int s;
816ee792 69
7dbb5ba6
KB
70 if (ioctl(fd, TIOCGPGRP, &s) < 0)
71 return((pid_t)-1);
f18741dc 72
7dbb5ba6 73 return((pid_t)s);
816ee792
MT
74}
75
c5980113 76speed_t
816ee792 77cfgetospeed(t)
c5980113 78 const struct termios *t;
816ee792 79{
f18741dc 80
816ee792
MT
81 return(t->c_ospeed);
82}
83
c5980113 84speed_t
816ee792 85cfgetispeed(t)
c5980113 86 const struct termios *t;
816ee792 87{
f18741dc 88
816ee792
MT
89 return(t->c_ispeed);
90}
91
c5980113 92int
816ee792
MT
93cfsetospeed(t, speed)
94 struct termios *t;
c5980113 95 speed_t speed;
816ee792
MT
96{
97 t->c_ospeed = speed;
f18741dc
MT
98
99 return (0);
816ee792
MT
100}
101
c5980113 102int
816ee792
MT
103cfsetispeed(t, speed)
104 struct termios *t;
c5980113 105 speed_t speed;
816ee792
MT
106{
107 t->c_ispeed = speed;
f18741dc
MT
108
109 return (0);
816ee792
MT
110}
111
c5980113 112void
816ee792
MT
113cfsetspeed(t, speed)
114 struct termios *t;
c5980113 115 speed_t speed;
816ee792
MT
116{
117 t->c_ispeed = t->c_ospeed = speed;
f18741dc
MT
118
119 return (0)
816ee792
MT
120}
121
5f7a2531
MK
122/*
123 * Make a pre-existing termios structure into "raw" mode:
124 * character-at-a-time mode with no characters interpreted,
125 * 8-bit data path.
126 */
c5980113 127void
816ee792
MT
128cfmakeraw(t)
129 struct termios *t;
130{
5f7a2531
MK
131 t->c_iflag &= ~(IGNBRK|BRKINT|PARMRK|ISTRIP|INLCR|IGNCR|ICRNL|IXON);
132 t->c_oflag &= ~OPOST;
9de4492f 133 t->c_lflag &= ~(ECHO|ECHONL|ICANON|ISIG|IEXTEN);
5f7a2531
MK
134 t->c_cflag &= ~(CSIZE|PARENB);
135 t->c_cflag |= CS8;
816ee792
MT
136 /* set MIN/TIME */
137}
f18741dc
MT
138
139tcsendbreak(fd, len)
140 int fd, len;
141{
142 struct timeval sleepytime;
143
144 sleepytime.tv_sec = 0;
145 sleepytime.tv_usec = 400000;
146 if (ioctl(fd, TIOCSBRK, 0) == -1)
147 return (-1);
148 select(0, 0, 0, 0, &sleepytime);
149 if (ioctl(fd, TIOCCBRK, 0) == -1)
150 return (-1);
151
152 return (0);
153}
154
155tcdrain(fd)
156 int fd;
157{
158 if (ioctl(fd, TIOCDRAIN, 0) == -1)
159 return (-1);
160
161 return (0);
162}
163
164tcflush(fd, which)
165 int fd, which;
166{
167 int com;
168
169 switch (which) {
170 case TCIFLUSH:
171 com = FREAD;
172 break;
173 case TCOFLUSH:
174 com = FWRITE;
175 break;
176 case TCIOFLUSH:
177 com = FREAD | FWRITE;
178 break;
179 default:
180 errno = EINVAL;
181 return (-1);
182 }
183 if (ioctl(fd, TIOCFLUSH, &com) == -1)
184 return (-1);
185
186 return (0);
187}
188
189tcflow(fd, action)
190 int fd, action;
191{
192 switch (action) {
193 case TCOOFF:
194 return (ioctl(fd, TIOCSTOP, 0));
195 break;
196 case TCOON:
197 return (ioctl(fd, TIOCSTART, 0));
198 break;
199 case TCIOFF:
200 case TCION: { /* these posix functions are STUPID */
201 struct termios term;
202 char c;
203
204 if (tcgetattr(fd, &term) == -1);
205 return (-1);
206 c = term.c_cc[action == TCIOFF ? VSTOP : VSTART];
207 if (c != _POSIX_VDISABLE && write(fd, &c, 1) == -1)
208 return (-1);
209 break;
210 }
211 default:
212 errno = EINVAL;
213 return (-1);
214 }
215
216 return (0);
217}