xref: /freebsd/lib/libc/gen/tcsetattr.3 (revision 4cf49a43559ed9fdad601bdcccd2c55963008675)
1.\" Copyright (c) 1991, 1993
2.\"	The Regents of the University of California.  All rights reserved.
3.\"
4.\" Redistribution and use in source and binary forms, with or without
5.\" modification, are permitted provided that the following conditions
6.\" are met:
7.\" 1. Redistributions of source code must retain the above copyright
8.\"    notice, this list of conditions and the following disclaimer.
9.\" 2. Redistributions in binary form must reproduce the above copyright
10.\"    notice, this list of conditions and the following disclaimer in the
11.\"    documentation and/or other materials provided with the distribution.
12.\" 3. All advertising materials mentioning features or use of this software
13.\"    must display the following acknowledgement:
14.\"	This product includes software developed by the University of
15.\"	California, Berkeley and its contributors.
16.\" 4. Neither the name of the University nor the names of its contributors
17.\"    may be used to endorse or promote products derived from this software
18.\"    without specific prior written permission.
19.\"
20.\" THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
21.\" ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
22.\" IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
23.\" ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
24.\" FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
25.\" DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
26.\" OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
27.\" HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
28.\" LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
29.\" OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
30.\" SUCH DAMAGE.
31.\"
32.\"	@(#)tcsetattr.3	8.3 (Berkeley) 1/2/94
33.\" $FreeBSD$
34.\"
35.Dd January 2, 1994
36.Dt TCSETATTR 3
37.Os
38.Sh NAME
39.Nm cfgetispeed ,
40.Nm cfsetispeed ,
41.Nm cfgetospeed ,
42.Nm cfsetospeed ,
43.Nm cfsetspeed ,
44.Nm cfmakeraw ,
45.Nm tcgetattr ,
46.Nm tcsetattr
47.Nd manipulating the termios structure
48.Sh SYNOPSIS
49.Fd #include <termios.h>
50.Ft speed_t
51.Fn cfgetispeed "const struct termios *t"
52.Ft int
53.Fn cfsetispeed "struct termios *t" "speed_t speed"
54.Ft speed_t
55.Fn cfgetospeed "const struct termios *t"
56.Ft int
57.Fn cfsetospeed "struct termios *t" "speed_t speed"
58.Ft int
59.Fn cfsetspeed "struct termios *t" "speed_t speed"
60.Ft void
61.Fn cfmakeraw "struct termios *t"
62.Ft int
63.Fn tcgetattr "int fd" "struct termios *t"
64.Ft int
65.Fn tcsetattr "int fd" "int action" "const struct termios *t"
66.Sh DESCRIPTION
67The
68.Fn cfmakeraw ,
69.Fn tcgetattr
70and
71.Fn tcsetattr
72functions are provided for getting and setting the termios structure.
73.Pp
74The
75.Fn cfgetispeed ,
76.Fn cfsetispeed ,
77.Fn cfgetospeed ,
78.Fn cfsetospeed
79and
80.Fn cfsetspeed
81functions are provided for getting and setting the baud rate values in
82the termios structure.
83The effects of the functions on the terminal as described below
84do not become effective, nor are all errors detected, until the
85.Fn tcsetattr
86function is called.
87Certain values for baud rates set in the termios structure and passed to
88.Fn tcsetattr
89have special meanings.
90These are discussed in the portion of the manual page that describes the
91.Fn tcsetattr
92function.
93.Sh GETTING AND SETTING THE BAUD RATE
94The input and output baud rates are found in the termios structure.
95The unsigned integer
96.Li speed_t
97is typdef'd in the include file
98.Aq Pa termios.h .
99The value of the integer corresponds directly to the baud rate being
100represented, however, the following symbolic values are defined.
101.Bd -literal
102#define B0	0
103#define B50	50
104#define B75	75
105#define B110	110
106#define B134	134
107#define B150	150
108#define B200	200
109#define B300	300
110#define B600	600
111#define B1200	1200
112#define	B1800	1800
113#define B2400	2400
114#define B4800	4800
115#define B9600	9600
116#define B19200	19200
117#define B38400	38400
118#ifndef _POSIX_SOURCE
119#define EXTA	19200
120#define EXTB	38400
121#endif  /*_POSIX_SOURCE */
122.Ed
123.Pp
124The
125.Fn cfgetispeed
126function returns the input baud rate in the termios structure referenced by
127.Fa tp .
128.Pp
129The
130.Fn cfsetispeed
131function sets the input baud rate in the termios structure referenced by
132.Fa tp
133to
134.Fa speed .
135.Pp
136The
137.Fn cfgetospeed
138function returns the output baud rate in the termios structure referenced by
139.Fa tp .
140.Pp
141The
142.Fn cfsetospeed
143function sets the output baud rate in the termios structure referenced by
144.Fa tp
145to
146.Fa speed .
147.Pp
148The
149.Fn cfsetspeed
150function sets both the input and output baud rate in the termios structure
151referenced by
152.Fa tp
153to
154.Fa speed .
155.Pp
156Upon successful completion, the functions
157.Fn cfsetispeed ,
158.Fn cfsetospeed ,
159and
160.Fn cfsetspeed
161return a value of 0.
162Otherwise, a value of -1 is returned and the global variable
163.Va errno
164is set to indicate the error.
165.Sh GETTING AND SETTING THE TERMIOS STATE
166This section describes the functions that are used to control the general
167terminal interface.
168Unless otherwise noted for a specific command, these functions are restricted
169from use by background processes.
170Attempts to perform these operations shall cause the process group to be sent
171a SIGTTOU signal.
172If the calling process is blocking or ignoring SIGTTOU signals, the process
173is allowed to perform the operation and the SIGTTOU signal is not sent.
174.Pp
175In all the functions, although
176.Fa fd
177is an open file descriptor, the functions affect the underlying terminal
178file, not just the open file description associated with the particular
179file descriptor.
180.Pp
181The
182.Fn cfmakeraw
183function sets the flags stored in the termios structure to a state disabling
184all input and output processing, giving a
185.Dq raw I/O path.
186It should be noted that there is no function to reverse this effect.
187This is because there are a variety of processing options that could be
188re-enabled and the correct method is for an application to snapshot the
189current terminal state using the function
190.Fn tcgetattr ,
191setting raw mode with
192.Fn cfmakeraw
193and the subsequent
194.Fn tcsetattr ,
195and then using another
196.Fn tcsetattr
197with the saved state to revert to the previous terminal state.
198.Pp
199The
200.Fn tcgetattr
201function copies the parameters associated with the terminal referenced
202by
203.Fa fd
204in the termios structure referenced by
205.Fa tp .
206This function is allowed from a background process, however, the terminal
207attributes may be subsequently changed by a foreground process.
208.Pp
209The
210.Fn tcsetattr
211function sets the parameters associated with the terminal from the
212termios structure referenced by
213.Fa tp .
214The
215.Fa action
216field is created by
217.Em or Ns 'ing
218the following values, as specified in the include file
219.Aq Pa termios.h .
220.Bl -tag -width "TCSADRAIN"
221.It Fa TCSANOW
222The change occurs immediately.
223.It Fa TCSADRAIN
224The change occurs after all output written to
225.Fa fd
226has been transmitted to the terminal.
227This value of
228.Fa action
229should be used when changing parameters that affect output.
230.It Fa TCSAFLUSH
231The change occurs after all output written to
232.Fa fd
233has been transmitted to the terminal.
234Additionally, any input that has been received but not read is discarded.
235.It Fa TCSASOFT
236If this value is
237.Em or Ns 'ed
238into the
239.Fa action
240value, the values of the
241.Em c_cflag ,
242.Em c_ispeed ,
243and
244.Em c_ospeed
245fields are ignored.
246.El
247.Pp
248The 0 baud rate is used to terminate the connection.
249If 0 is specified as the output speed to the function
250.Fn tcsetattr ,
251modem control will no longer be asserted on the terminal, disconnecting
252the terminal.
253.Pp
254If zero is specified as the input speed to the function
255.Fn tcsetattr ,
256the input baud rate will be set to the same value as that specified by
257the output baud rate.
258.Pp
259If
260.Fn tcsetattr
261is unable to make any of the requested changes, it returns -1 and
262sets errno.
263Otherwise, it makes all of the requested changes it can.
264If the specified input and output baud rates differ and are a combination
265that is not supported, neither baud rate is changed.
266.Pp
267Upon successful completion, the functions
268.Fn tcgetattr
269and
270.Fn tcsetattr
271return a value of 0.
272Otherwise, they
273return -1 and the global variable
274.Va errno
275is set to indicate the error, as follows:
276.Bl -tag -width Er
277.It Bq Er EBADF
278The
279.Fa fd
280argument to
281.Fn tcgetattr
282or
283.Fn tcsetattr
284was not a valid file descriptor.
285.It Bq Er EINTR
286The
287.Fn tcsetattr
288function was interrupted by a signal.
289.It Bq Er EINVAL
290The
291.Fa action
292argument to the
293.Fn tcsetattr
294function was not valid, or an attempt was made to change an attribute
295represented in the termios structure to an unsupported value.
296.It Bq Er ENOTTY
297The file associated with the
298.Fa fd
299argument to
300.Fn tcgetattr
301or
302.Fn tcsetattr
303is not a terminal.
304.El
305.Sh SEE ALSO
306.Xr tcsendbreak 3 ,
307.Xr termios 4
308.Sh STANDARDS
309The
310.Fn cfgetispeed ,
311.Fn cfsetispeed ,
312.Fn cfgetospeed ,
313.Fn cfsetospeed ,
314.Fn tcgetattr
315and
316.Fn tcsetattr
317functions are expected to be compliant with the
318.St -p1003.1-88
319specification.
320The
321.Fn cfmakeraw
322and
323.Fn cfsetspeed
324functions,
325as well as the
326.Li TCSASOFT
327option to the
328.Fn tcsetattr
329function are extensions to the
330.St -p1003.1-88
331specification.
332