initial import
[glibc.git] / sysdeps / unix / bsd / tcsetattr.c
blobe731d830f607de5666c9445eabd9fdc9ecb06f26
1 /* Copyright (C) 1991, 1993 Free Software Foundation, Inc.
2 This file is part of the GNU C Library.
4 The GNU C Library is free software; you can redistribute it and/or
5 modify it under the terms of the GNU Library General Public License as
6 published by the Free Software Foundation; either version 2 of the
7 License, or (at your option) any later version.
9 The GNU C Library is distributed in the hope that it will be useful,
10 but WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 Library General Public License for more details.
14 You should have received a copy of the GNU Library General Public
15 License along with the GNU C Library; see the file COPYING.LIB. If
16 not, write to the Free Software Foundation, Inc., 675 Mass Ave,
17 Cambridge, MA 02139, USA. */
19 #include <ansidecl.h>
20 #include <errno.h>
21 #include <stddef.h>
22 #include <termios.h>
24 #include "bsdtty.h"
27 CONST speed_t __bsd_speeds[] =
30 50,
31 75,
32 110,
33 134,
34 150,
35 200,
36 300,
37 600,
38 1200,
39 1800,
40 2400,
41 4800,
42 9600,
43 19200,
44 38400,
48 /* Set the state of FD to *TERMIOS_P. */
49 int
50 DEFUN(tcsetattr, (fd, optional_actions, termios_p),
51 int fd AND int optional_actions AND CONST struct termios *termios_p)
53 struct sgttyb buf;
54 struct tchars tchars;
55 struct ltchars ltchars;
56 int local;
57 #ifdef TIOCGETX
58 int extra;
59 #endif
60 size_t i;
62 if (__ioctl(fd, TIOCGETP, &buf) < 0 ||
63 __ioctl(fd, TIOCGETC, &tchars) < 0 ||
64 __ioctl(fd, TIOCGLTC, &ltchars) < 0 ||
65 #ifdef TIOCGETX
66 __ioctl(fd, TIOCGETX, &extra) < 0 ||
67 #endif
68 __ioctl(fd, TIOCLGET, &local) < 0)
69 return -1;
71 if (termios_p == NULL)
73 errno = EINVAL;
74 return -1;
76 switch (optional_actions)
78 case TCSANOW:
79 break;
80 case TCSADRAIN:
81 if (tcdrain(fd) < 0)
82 return -1;
83 break;
84 case TCSAFLUSH:
85 if (tcflush(fd, TCIFLUSH) < 0)
86 return -1;
87 break;
88 default:
89 errno = EINVAL;
90 return -1;
93 buf.sg_ispeed = buf.sg_ospeed = -1;
94 for (i = 0; i <= sizeof (__bsd_speeds) / sizeof (__bsd_speeds[0]); ++i)
96 if (__bsd_speeds[i] == termios_p->__ispeed)
97 buf.sg_ispeed = i;
98 if (__bsd_speeds[i] == termios_p->__ospeed)
99 buf.sg_ospeed = i;
101 if (buf.sg_ispeed == -1 || buf.sg_ospeed == -1)
103 errno = EINVAL;
104 return -1;
107 buf.sg_flags &= ~(CBREAK|RAW);
108 if (!(termios_p->c_lflag & ICANON))
109 buf.sg_flags |= (termios_p->c_cflag & ISIG) ? CBREAK : RAW;
110 #ifdef LPASS8
111 if (termios_p->c_oflag & CS8)
112 local |= LPASS8;
113 else
114 local &= ~LPASS8;
115 #endif
116 if (termios_p->c_lflag & _NOFLSH)
117 local |= LNOFLSH;
118 else
119 local &= ~LNOFLSH;
120 if (termios_p->c_oflag & OPOST)
121 local &= ~LLITOUT;
122 else
123 local |= LLITOUT;
124 #ifdef TIOCGETX
125 if (termios_p->c_lflag & ISIG)
126 extra &= ~NOISIG;
127 else
128 extra |= NOISIG;
129 if (termios_p->c_cflag & CSTOPB)
130 extra |= STOPB;
131 else
132 extra &= ~STOPB;
133 #endif
134 if (termios_p->c_iflag & ICRNL)
135 buf.sg_flags |= CRMOD;
136 else
137 buf.sg_flags &= ~CRMOD;
138 if (termios_p->c_iflag & IXOFF)
139 buf.sg_flags |= TANDEM;
140 else
141 buf.sg_flags &= ~TANDEM;
143 buf.sg_flags &= ~(ODDP|EVENP);
144 if (!(termios_p->c_cflag & PARENB))
145 buf.sg_flags |= ODDP | EVENP;
146 else if (termios_p->c_cflag & PARODD)
147 buf.sg_flags |= ODDP;
148 else
149 buf.sg_flags |= EVENP;
151 if (termios_p->c_lflag & _ECHO)
152 buf.sg_flags |= ECHO;
153 else
154 buf.sg_flags &= ~ECHO;
155 if (termios_p->c_lflag & ECHOE)
156 local |= LCRTERA;
157 else
158 local &= ~LCRTERA;
159 if (termios_p->c_lflag & ECHOK)
160 local |= LCRTKIL;
161 else
162 local &= ~LCRTKIL;
163 if (termios_p->c_lflag & _TOSTOP)
164 local |= LTOSTOP;
165 else
166 local &= ~LTOSTOP;
168 buf.sg_erase = termios_p->c_cc[VERASE];
169 buf.sg_kill = termios_p->c_cc[VKILL];
170 tchars.t_eofc = termios_p->c_cc[VEOF];
171 tchars.t_intrc = termios_p->c_cc[VINTR];
172 tchars.t_quitc = termios_p->c_cc[VQUIT];
173 ltchars.t_suspc = termios_p->c_cc[VSUSP];
174 tchars.t_startc = termios_p->c_cc[VSTART];
175 tchars.t_stopc = termios_p->c_cc[VSTOP];
177 if (__ioctl(fd, TIOCSETP, &buf) < 0 ||
178 __ioctl(fd, TIOCSETC, &tchars) < 0 ||
179 __ioctl(fd, TIOCSLTC, &ltchars) < 0 ||
180 #ifdef TIOCGETX
181 __ioctl(fd, TIOCSETX, &extra) < 0 ||
182 #endif
183 __ioctl(fd, TIOCLSET, &local) < 0)
184 return -1;
185 return 0;