minor improvement from torek
[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)
1909d51b 9static char sccsid[] = "@(#)termios.c 5.11 (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>
d365c391 16#define KERNEL /* XXX - FREAD and FWRITE ifdef'd KERNEL*/
f18741dc
MT
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 28
d365c391 29 return (ioctl(fd, TIOCGETA, t));
816ee792
MT
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;
816ee792 43 }
1909d51b 44 switch (opt & ~TCSASOFT) {
d365c391 45 case TCSANOW:
7c1f1e23 46 return (ioctl(fd, TIOCSETA, t));
d365c391 47 case TCSADRAIN:
7c1f1e23 48 return (ioctl(fd, TIOCSETAW, t));
1909d51b 49 case TCSAFLUSH:
d365c391
KB
50 return (ioctl(fd, TIOCSETAF, t));
51 default:
52 errno = EINVAL;
53 return (-1);
54 }
816ee792
MT
55}
56
c5980113
DS
57int
58#if __STDC__
59tcsetpgrp(int fd, pid_t pgrp)
60#else
816ee792 61tcsetpgrp(fd, pgrp)
c5980113
DS
62 int fd;
63 pid_t pgrp;
64#endif
816ee792 65{
023abf7f 66 int s;
f18741dc 67
023abf7f 68 s = pgrp;
d365c391 69 return (ioctl(fd, TIOCSPGRP, &s));
816ee792
MT
70}
71
c5980113 72pid_t
816ee792
MT
73tcgetpgrp(fd)
74{
7dbb5ba6 75 int s;
816ee792 76
7dbb5ba6 77 if (ioctl(fd, TIOCGPGRP, &s) < 0)
d365c391 78 return ((pid_t)-1);
f18741dc 79
d365c391 80 return ((pid_t)s);
816ee792
MT
81}
82
c5980113 83speed_t
816ee792 84cfgetospeed(t)
c5980113 85 const struct termios *t;
816ee792 86{
f18741dc 87
d365c391 88 return (t->c_ospeed);
816ee792
MT
89}
90
c5980113 91speed_t
816ee792 92cfgetispeed(t)
c5980113 93 const struct termios *t;
816ee792 94{
f18741dc 95
d365c391 96 return (t->c_ispeed);
816ee792
MT
97}
98
c5980113 99int
816ee792
MT
100cfsetospeed(t, speed)
101 struct termios *t;
c5980113 102 speed_t speed;
816ee792
MT
103{
104 t->c_ospeed = speed;
f18741dc 105 return (0);
816ee792
MT
106}
107
c5980113 108int
816ee792
MT
109cfsetispeed(t, speed)
110 struct termios *t;
c5980113 111 speed_t speed;
816ee792
MT
112{
113 t->c_ispeed = speed;
f18741dc 114 return (0);
816ee792
MT
115}
116
d365c391 117int
816ee792
MT
118cfsetspeed(t, speed)
119 struct termios *t;
c5980113 120 speed_t speed;
816ee792
MT
121{
122 t->c_ispeed = t->c_ospeed = speed;
d365c391 123 return (0);
816ee792
MT
124}
125
5f7a2531 126/*
d365c391
KB
127 * Make a pre-existing termios structure into "raw" mode: character-at-a-time
128 * mode with no characters interpreted, 8-bit data path.
5f7a2531 129 */
c5980113 130void
816ee792
MT
131cfmakeraw(t)
132 struct termios *t;
133{
5f7a2531
MK
134 t->c_iflag &= ~(IGNBRK|BRKINT|PARMRK|ISTRIP|INLCR|IGNCR|ICRNL|IXON);
135 t->c_oflag &= ~OPOST;
9de4492f 136 t->c_lflag &= ~(ECHO|ECHONL|ICANON|ISIG|IEXTEN);
5f7a2531
MK
137 t->c_cflag &= ~(CSIZE|PARENB);
138 t->c_cflag |= CS8;
d365c391 139 /* XXX set MIN/TIME */
816ee792 140}
f18741dc
MT
141
142tcsendbreak(fd, len)
143 int fd, len;
144{
145 struct timeval sleepytime;
146
147 sleepytime.tv_sec = 0;
148 sleepytime.tv_usec = 400000;
149 if (ioctl(fd, TIOCSBRK, 0) == -1)
150 return (-1);
d365c391 151 (void)select(0, 0, 0, 0, &sleepytime);
f18741dc
MT
152 if (ioctl(fd, TIOCCBRK, 0) == -1)
153 return (-1);
f18741dc
MT
154 return (0);
155}
156
157tcdrain(fd)
158 int fd;
159{
d365c391 160 return (ioctl(fd, TIOCDRAIN, 0) == -1 ? -1 : 0);
f18741dc
MT
161}
162
163tcflush(fd, which)
164 int fd, which;
165{
166 int com;
167
168 switch (which) {
169 case TCIFLUSH:
170 com = FREAD;
171 break;
172 case TCOFLUSH:
173 com = FWRITE;
174 break;
175 case TCIOFLUSH:
176 com = FREAD | FWRITE;
177 break;
178 default:
179 errno = EINVAL;
180 return (-1);
181 }
d365c391 182 return (ioctl(fd, TIOCFLUSH, &com) == -1 ? -1 : 0);
f18741dc
MT
183}
184
185tcflow(fd, action)
186 int fd, action;
187{
d365c391
KB
188 struct termios term;
189 u_char c;
190
f18741dc
MT
191 switch (action) {
192 case TCOOFF:
d365c391 193 return (ioctl(fd, TIOCSTOP, 0) == -1 ? -1 : 0);
f18741dc 194 case TCOON:
d365c391
KB
195 return (ioctl(fd, TIOCSTART, 0) == -1 ? -1 : 0);
196 case TCION:
f18741dc 197 case TCIOFF:
e800a1f5 198 if (tcgetattr(fd, &term) == -1)
f18741dc
MT
199 return (-1);
200 c = term.c_cc[action == TCIOFF ? VSTOP : VSTART];
d365c391 201 if (c != _POSIX_VDISABLE && write(fd, &c, sizeof(c)) == -1)
f18741dc 202 return (-1);
d365c391 203 return (0);
f18741dc
MT
204 default:
205 errno = EINVAL;
206 return (-1);
207 }
d365c391 208 /* NOTREACHED */
f18741dc 209}