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