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