Replace FSF snail mail address with URLs.
[glibc.git] / sysdeps / unix / sysv / tcsetattr.c
blobca173b2e25cc2b0f678dd334f1d69320fdc49671
1 /* Copyright (C) 1992, 1995, 1996, 1997, 2002 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 Lesser General Public
6 License as published by the Free Software Foundation; either
7 version 2.1 of the 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 Lesser General Public License for more details.
14 You should have received a copy of the GNU Lesser General Public
15 License along with the GNU C Library; if not, see
16 <http://www.gnu.org/licenses/>. */
18 #include <errno.h>
19 #include <stddef.h>
20 #include <termios.h>
21 #include <sys/ioctl.h>
23 #include <sysv_termio.h>
26 const speed_t __unix_speeds[] =
29 50,
30 75,
31 110,
32 134,
33 150,
34 200,
35 300,
36 600,
37 1200,
38 1800,
39 2400,
40 4800,
41 9600,
42 19200,
43 38400,
47 /* Set the state of FD to *TERMIOS_P. */
48 int
49 tcsetattr (fd, optional_actions, termios_p)
50 int fd;
51 int optional_actions;
52 const struct termios *termios_p;
54 struct __sysv_termio buf;
55 int ioctl_function;
56 size_t i;
58 if (termios_p == NULL)
60 __set_errno (EINVAL);
61 return -1;
63 switch (optional_actions)
65 case TCSANOW:
66 ioctl_function = _TCSETA;
67 break;
68 case TCSADRAIN:
69 ioctl_function = _TCSETAW;
70 break;
71 case TCSAFLUSH:
72 ioctl_function = _TCSETAF;
73 break;
74 default:
75 __set_errno (EINVAL);
76 return -1;
79 if (termios_p->__ispeed != termios_p->__ospeed)
81 __set_errno (EINVAL);
82 return -1;
84 buf.c_cflag = -1;
85 for (i = 0; i <= sizeof (__unix_speeds) / sizeof (__unix_speeds[0]); ++i)
87 if (__unix_speeds[i] == termios_p->__ispeed)
88 buf.c_cflag = i;
90 if (buf.c_cflag == -1)
92 __set_errno (EINVAL);
93 return -1;
96 buf.c_iflag = 0;
97 if (termios_p->c_iflag & IGNBRK)
98 buf.c_iflag |= _SYSV_IGNBRK;
99 if (termios_p->c_iflag & BRKINT)
100 buf.c_iflag |= _SYSV_BRKINT;
101 if (termios_p->c_iflag & IGNPAR)
102 buf.c_iflag |= _SYSV_IGNPAR;
103 if (termios_p->c_iflag & PARMRK)
104 buf.c_iflag |= _SYSV_PARMRK;
105 if (termios_p->c_iflag & INPCK)
106 buf.c_iflag |= _SYSV_INPCK;
107 if (termios_p->c_iflag & ISTRIP)
108 buf.c_iflag |= _SYSV_ISTRIP;
109 if (termios_p->c_iflag & INLCR)
110 buf.c_iflag |= _SYSV_INLCR;
111 if (termios_p->c_iflag & IGNCR)
112 buf.c_iflag |= _SYSV_IGNCR;
113 if (termios_p->c_iflag & ICRNL)
114 buf.c_iflag |= _SYSV_ICRNL;
115 if (termios_p->c_iflag & IXON)
116 buf.c_iflag |= _SYSV_IXON;
117 if (termios_p->c_iflag & IXOFF)
118 buf.c_iflag |= _SYSV_IXOFF;
119 if (termios_p->c_iflag & IXANY)
120 buf.c_iflag |= _SYSV_IXANY;
121 if (termios_p->c_iflag & IMAXBEL)
122 buf.c_iflag |= _SYSV_IMAXBEL;
124 buf.c_oflag = 0;
125 if (termios_p->c_oflag & OPOST)
126 buf.c_oflag |= _SYSV_OPOST;
127 if (termios_p->c_oflag & ONLCR)
128 buf.c_oflag |= _SYSV_ONLCR;
130 /* So far, buf.c_cflag contains the speed in CBAUD. */
131 if (termios_p->c_cflag & CSTOPB)
132 buf.c_cflag |= _SYSV_CSTOPB;
133 if (termios_p->c_cflag & CREAD)
134 buf.c_cflag |= _SYSV_CREAD;
135 if (termios_p->c_cflag & PARENB)
136 buf.c_cflag |= _SYSV_PARENB;
137 if (termios_p->c_cflag & PARODD)
138 buf.c_cflag |= _SYSV_PARODD;
139 if (termios_p->c_cflag & HUPCL)
140 buf.c_cflag |= _SYSV_HUPCL;
141 if (termios_p->c_cflag & CLOCAL)
142 buf.c_cflag |= _SYSV_CLOCAL;
143 switch (termios_p->c_cflag & CSIZE)
145 case CS5:
146 buf.c_cflag |= _SYSV_CS5;
147 break;
148 case CS6:
149 buf.c_cflag |= _SYSV_CS6;
150 break;
151 case CS7:
152 buf.c_cflag |= _SYSV_CS7;
153 break;
154 case CS8:
155 buf.c_cflag |= _SYSV_CS8;
156 break;
159 buf.c_lflag = 0;
160 if (termios_p->c_lflag & ISIG)
161 buf.c_lflag |= _SYSV_ISIG;
162 if (termios_p->c_lflag & ICANON)
163 buf.c_lflag |= _SYSV_ICANON;
164 if (termios_p->c_lflag & ECHO)
165 buf.c_lflag |= _SYSV_ECHO;
166 if (termios_p->c_lflag & ECHOE)
167 buf.c_lflag |= _SYSV_ECHOE;
168 if (termios_p->c_lflag & ECHOK)
169 buf.c_lflag |= _SYSV_ECHOK;
170 if (termios_p->c_lflag & ECHONL)
171 buf.c_lflag |= _SYSV_ECHONL;
172 if (termios_p->c_lflag & NOFLSH)
173 buf.c_lflag |= _SYSV_NOFLSH;
174 if (termios_p->c_lflag & TOSTOP)
175 buf.c_lflag |= _SYSV_TOSTOP;
176 if (termios_p->c_lflag & ECHOCTL)
177 buf.c_lflag |= _SYSV_ECHOCTL;
178 if (termios_p->c_lflag & ECHOPRT)
179 buf.c_lflag |= _SYSV_ECHOPRT;
180 if (termios_p->c_lflag & ECHOKE)
181 buf.c_lflag |= _SYSV_ECHOKE;
182 if (termios_p->c_lflag & FLUSHO)
183 buf.c_lflag |= _SYSV_FLUSHO;
184 if (termios_p->c_lflag & PENDIN)
185 buf.c_lflag |= _SYSV_PENDIN;
186 if (termios_p->c_lflag & IEXTEN)
187 buf.c_lflag |= _SYSV_IEXTEN;
189 buf.c_cc[_SYSV_VINTR] = termios_p->c_cc[VINTR];
190 buf.c_cc[_SYSV_VQUIT] = termios_p->c_cc[VQUIT];
191 buf.c_cc[_SYSV_VERASE] = termios_p->c_cc[VERASE];
192 buf.c_cc[_SYSV_VKILL] = termios_p->c_cc[VKILL];
193 if (buf.c_lflag & _SYSV_ICANON)
195 buf.c_cc[_SYSV_VEOF] = termios_p->c_cc[VEOF];
196 buf.c_cc[_SYSV_VEOL] = termios_p->c_cc[VEOL];
198 else
200 buf.c_cc[_SYSV_VMIN] = termios_p->c_cc[VMIN];
201 buf.c_cc[_SYSV_VTIME] = termios_p->c_cc[VTIME];
203 buf.c_cc[_SYSV_VEOL2] = termios_p->c_cc[VEOL2];
205 if (__ioctl (fd, ioctl_function, &buf) < 0)
206 return -1;
207 return 0;
209 libc_hidden_def (tcsetattr)