Mention BZ #15674
[glibc.git] / manual / terminal.texi
blob9e9c0570954dbfa2c4e9e16cf85995aa4b3d8aff
1 @node Low-Level Terminal Interface, Syslog, Sockets, Top
2 @c %MENU% How to change the characteristics of a terminal device
3 @chapter Low-Level Terminal Interface
5 This chapter describes functions that are specific to terminal devices.
6 You can use these functions to do things like turn off input echoing;
7 set serial line characteristics such as line speed and flow control; and
8 change which characters are used for end-of-file, command-line editing,
9 sending signals, and similar control functions.
11 Most of the functions in this chapter operate on file descriptors.
12 @xref{Low-Level I/O}, for more information about what a file
13 descriptor is and how to open a file descriptor for a terminal device.
15 @menu
16 * Is It a Terminal::            How to determine if a file is a terminal
17                                  device, and what its name is.
18 * I/O Queues::                  About flow control and typeahead.
19 * Canonical or Not::            Two basic styles of input processing.
20 * Terminal Modes::              How to examine and modify flags controlling
21                                  details of terminal I/O: echoing,
22                                  signals, editing.  Posix.
23 * BSD Terminal Modes::          BSD compatible terminal mode setting
24 * Line Control::                Sending break sequences, clearing
25                                  terminal buffers @dots{}
26 * Noncanon Example::            How to read single characters without echo.
27 * Pseudo-Terminals::            How to open a pseudo-terminal.
28 @end menu
30 @node Is It a Terminal
31 @section Identifying Terminals
32 @cindex terminal identification
33 @cindex identifying terminals
35 The functions described in this chapter only work on files that
36 correspond to terminal devices.  You can find out whether a file
37 descriptor is associated with a terminal by using the @code{isatty}
38 function.
40 @pindex unistd.h
41 Prototypes for the functions in this section are declared in the header
42 file @file{unistd.h}.
44 @comment unistd.h
45 @comment POSIX.1
46 @deftypefun int isatty (int @var{filedes})
47 This function returns @code{1} if @var{filedes} is a file descriptor
48 associated with an open terminal device, and @math{0} otherwise.
49 @end deftypefun
51 If a file descriptor is associated with a terminal, you can get its
52 associated file name using the @code{ttyname} function.  See also the
53 @code{ctermid} function, described in @ref{Identifying the Terminal}.
55 @comment unistd.h
56 @comment POSIX.1
57 @deftypefun {char *} ttyname (int @var{filedes})
58 If the file descriptor @var{filedes} is associated with a terminal
59 device, the @code{ttyname} function returns a pointer to a
60 statically-allocated, null-terminated string containing the file name of
61 the terminal file.  The value is a null pointer if the file descriptor
62 isn't associated with a terminal, or the file name cannot be determined.
63 @end deftypefun
65 @comment unistd.h
66 @comment POSIX.1
67 @deftypefun int ttyname_r (int @var{filedes}, char *@var{buf}, size_t @var{len})
68 The @code{ttyname_r} function is similar to the @code{ttyname} function
69 except that it places its result into the user-specified buffer starting
70 at @var{buf} with length @var{len}.
72 The normal return value from @code{ttyname_r} is @math{0}.  Otherwise an
73 error number is returned to indicate the error.  The following
74 @code{errno} error conditions are defined for this function:
76 @table @code
77 @item EBADF
78 The @var{filedes} argument is not a valid file descriptor.
80 @item ENOTTY
81 The @var{filedes} is not associated with a terminal.
83 @item ERANGE
84 The buffer length @var{len} is too small to store the string to be
85 returned.
86 @end table
87 @end deftypefun
89 @node I/O Queues
90 @section I/O Queues
92 Many of the remaining functions in this section refer to the input and
93 output queues of a terminal device.  These queues implement a form of
94 buffering @emph{within the kernel} independent of the buffering
95 implemented by I/O streams (@pxref{I/O on Streams}).
97 @cindex terminal input queue
98 @cindex typeahead buffer
99 The @dfn{terminal input queue} is also sometimes referred to as its
100 @dfn{typeahead buffer}.  It holds the characters that have been received
101 from the terminal but not yet read by any process.
103 The size of the input queue is described by the @code{MAX_INPUT} and
104 @w{@code{_POSIX_MAX_INPUT}} parameters; see @ref{Limits for Files}.  You
105 are guaranteed a queue size of at least @code{MAX_INPUT}, but the queue
106 might be larger, and might even dynamically change size.  If input flow
107 control is enabled by setting the @code{IXOFF} input mode bit
108 (@pxref{Input Modes}), the terminal driver transmits STOP and START
109 characters to the terminal when necessary to prevent the queue from
110 overflowing.  Otherwise, input may be lost if it comes in too fast from
111 the terminal.  In canonical mode, all input stays in the queue until a
112 newline character is received, so the terminal input queue can fill up
113 when you type a very long line.  @xref{Canonical or Not}.
115 @cindex terminal output queue
116 The @dfn{terminal output queue} is like the input queue, but for output;
117 it contains characters that have been written by processes, but not yet
118 transmitted to the terminal.  If output flow control is enabled by
119 setting the @code{IXON} input mode bit (@pxref{Input Modes}), the
120 terminal driver obeys START and STOP characters sent by the terminal to
121 stop and restart transmission of output.
123 @dfn{Clearing} the terminal input queue means discarding any characters
124 that have been received but not yet read.  Similarly, clearing the
125 terminal output queue means discarding any characters that have been
126 written but not yet transmitted.
128 @node Canonical or Not
129 @section Two Styles of Input: Canonical or Not
131 POSIX systems support two basic modes of input: canonical and
132 noncanonical.
134 @cindex canonical input processing
135 In @dfn{canonical input processing} mode, terminal input is processed in
136 lines terminated by newline (@code{'\n'}), EOF, or EOL characters.  No
137 input can be read until an entire line has been typed by the user, and
138 the @code{read} function (@pxref{I/O Primitives}) returns at most a
139 single line of input, no matter how many bytes are requested.
141 In canonical input mode, the operating system provides input editing
142 facilities: some characters are interpreted specially to perform editing
143 operations within the current line of text, such as ERASE and KILL.
144 @xref{Editing Characters}.
146 The constants @code{_POSIX_MAX_CANON} and @code{MAX_CANON} parameterize
147 the maximum number of bytes which may appear in a single line of
148 canonical input.  @xref{Limits for Files}.  You are guaranteed a maximum
149 line length of at least @code{MAX_CANON} bytes, but the maximum might be
150 larger, and might even dynamically change size.
152 @cindex noncanonical input processing
153 In @dfn{noncanonical input processing} mode, characters are not grouped
154 into lines, and ERASE and KILL processing is not performed.  The
155 granularity with which bytes are read in noncanonical input mode is
156 controlled by the MIN and TIME settings.  @xref{Noncanonical Input}.
158 Most programs use canonical input mode, because this gives the user a
159 way to edit input line by line.  The usual reason to use noncanonical
160 mode is when the program accepts single-character commands or provides
161 its own editing facilities.
163 The choice of canonical or noncanonical input is controlled by the
164 @code{ICANON} flag in the @code{c_lflag} member of @code{struct termios}.
165 @xref{Local Modes}.
167 @node Terminal Modes
168 @section Terminal Modes
170 @pindex termios.h
171 This section describes the various terminal attributes that control how
172 input and output are done.  The functions, data structures, and symbolic
173 constants are all declared in the header file @file{termios.h}.
175 Don't confuse terminal attributes with file attributes.  A device special
176 file which is associated with a terminal has file attributes as described
177 in @ref{File Attributes}.  These are unrelated to the attributes of the
178 terminal device itself, which are discussed in this section.
180 @menu
181 * Mode Data Types::             The data type @code{struct termios} and
182                                  related types.
183 * Mode Functions::              Functions to read and set the terminal
184                                  attributes.
185 * Setting Modes::               The right way to set terminal attributes
186                                  reliably.
187 * Input Modes::                 Flags controlling low-level input handling.
188 * Output Modes::                Flags controlling low-level output handling.
189 * Control Modes::               Flags controlling serial port behavior.
190 * Local Modes::                 Flags controlling high-level input handling.
191 * Line Speed::                  How to read and set the terminal line speed.
192 * Special Characters::          Characters that have special effects,
193                                  and how to change them.
194 * Noncanonical Input::          Controlling how long to wait for input.
195 @end menu
197 @node Mode Data Types
198 @subsection Terminal Mode Data Types
199 @cindex terminal mode data types
201 The entire collection of attributes of a terminal is stored in a
202 structure of type @code{struct termios}.  This structure is used
203 with the functions @code{tcgetattr} and @code{tcsetattr} to read
204 and set the attributes.
206 @comment termios.h
207 @comment POSIX.1
208 @deftp {Data Type} {struct termios}
209 Structure that records all the I/O attributes of a terminal.  The
210 structure includes at least the following members:
212 @table @code
213 @item tcflag_t c_iflag
214 A bit mask specifying flags for input modes; see @ref{Input Modes}.
216 @item tcflag_t c_oflag
217 A bit mask specifying flags for output modes; see @ref{Output Modes}.
219 @item tcflag_t c_cflag
220 A bit mask specifying flags for control modes; see @ref{Control Modes}.
222 @item tcflag_t c_lflag
223 A bit mask specifying flags for local modes; see @ref{Local Modes}.
225 @item cc_t c_cc[NCCS]
226 An array specifying which characters are associated with various
227 control functions; see @ref{Special Characters}.
228 @end table
230 The @code{struct termios} structure also contains members which
231 encode input and output transmission speeds, but the representation is
232 not specified.  @xref{Line Speed}, for how to examine and store the
233 speed values.
234 @end deftp
236 The following sections describe the details of the members of the
237 @code{struct termios} structure.
239 @comment termios.h
240 @comment POSIX.1
241 @deftp {Data Type} tcflag_t
242 This is an unsigned integer type used to represent the various
243 bit masks for terminal flags.
244 @end deftp
246 @comment termios.h
247 @comment POSIX.1
248 @deftp {Data Type} cc_t
249 This is an unsigned integer type used to represent characters associated
250 with various terminal control functions.
251 @end deftp
253 @comment termios.h
254 @comment POSIX.1
255 @deftypevr Macro int NCCS
256 The value of this macro is the number of elements in the @code{c_cc}
257 array.
258 @end deftypevr
260 @node Mode Functions
261 @subsection Terminal Mode Functions
262 @cindex terminal mode functions
264 @comment termios.h
265 @comment POSIX.1
266 @deftypefun int tcgetattr (int @var{filedes}, struct termios *@var{termios-p})
267 This function is used to examine the attributes of the terminal
268 device with file descriptor @var{filedes}.  The attributes are returned
269 in the structure that @var{termios-p} points to.
271 If successful, @code{tcgetattr} returns @math{0}.  A return value of @math{-1}
272 indicates an error.  The following @code{errno} error conditions are
273 defined for this function:
275 @table @code
276 @item EBADF
277 The @var{filedes} argument is not a valid file descriptor.
279 @item ENOTTY
280 The @var{filedes} is not associated with a terminal.
281 @end table
282 @end deftypefun
284 @comment termios.h
285 @comment POSIX.1
286 @deftypefun int tcsetattr (int @var{filedes}, int @var{when}, const struct termios *@var{termios-p})
287 This function sets the attributes of the terminal device with file
288 descriptor @var{filedes}.  The new attributes are taken from the
289 structure that @var{termios-p} points to.
291 The @var{when} argument specifies how to deal with input and output
292 already queued.  It can be one of the following values:
294 @table @code
295 @comment termios.h
296 @comment POSIX.1
297 @item TCSANOW
298 @vindex TCSANOW
299 Make the change immediately.
301 @comment termios.h
302 @comment POSIX.1
303 @item TCSADRAIN
304 @vindex TCSADRAIN
305 Make the change after waiting until all queued output has been written.
306 You should usually use this option when changing parameters that affect
307 output.
309 @comment termios.h
310 @comment POSIX.1
311 @item TCSAFLUSH
312 @vindex TCSAFLUSH
313 This is like @code{TCSADRAIN}, but also discards any queued input.
315 @comment termios.h
316 @comment BSD
317 @item TCSASOFT
318 @vindex TCSASOFT
319 This is a flag bit that you can add to any of the above alternatives.
320 Its meaning is to inhibit alteration of the state of the terminal
321 hardware.  It is a BSD extension; it is only supported on BSD systems
322 and @gnuhurdsystems{}.
324 Using @code{TCSASOFT} is exactly the same as setting the @code{CIGNORE}
325 bit in the @code{c_cflag} member of the structure @var{termios-p} points
326 to.  @xref{Control Modes}, for a description of @code{CIGNORE}.
327 @end table
329 If this function is called from a background process on its controlling
330 terminal, normally all processes in the process group are sent a
331 @code{SIGTTOU} signal, in the same way as if the process were trying to
332 write to the terminal.  The exception is if the calling process itself
333 is ignoring or blocking @code{SIGTTOU} signals, in which case the
334 operation is performed and no signal is sent.  @xref{Job Control}.
336 If successful, @code{tcsetattr} returns @math{0}.  A return value of
337 @math{-1} indicates an error.  The following @code{errno} error
338 conditions are defined for this function:
340 @table @code
341 @item EBADF
342 The @var{filedes} argument is not a valid file descriptor.
344 @item ENOTTY
345 The @var{filedes} is not associated with a terminal.
347 @item EINVAL
348 Either the value of the @code{when} argument is not valid, or there is
349 something wrong with the data in the @var{termios-p} argument.
350 @end table
351 @end deftypefun
353 Although @code{tcgetattr} and @code{tcsetattr} specify the terminal
354 device with a file descriptor, the attributes are those of the terminal
355 device itself and not of the file descriptor.  This means that the
356 effects of changing terminal attributes are persistent; if another
357 process opens the terminal file later on, it will see the changed
358 attributes even though it doesn't have anything to do with the open file
359 descriptor you originally specified in changing the attributes.
361 Similarly, if a single process has multiple or duplicated file
362 descriptors for the same terminal device, changing the terminal
363 attributes affects input and output to all of these file
364 descriptors.  This means, for example, that you can't open one file
365 descriptor or stream to read from a terminal in the normal
366 line-buffered, echoed mode; and simultaneously have another file
367 descriptor for the same terminal that you use to read from it in
368 single-character, non-echoed mode.  Instead, you have to explicitly
369 switch the terminal back and forth between the two modes.
371 @node Setting Modes
372 @subsection Setting Terminal Modes Properly
374 When you set terminal modes, you should call @code{tcgetattr} first to
375 get the current modes of the particular terminal device, modify only
376 those modes that you are really interested in, and store the result with
377 @code{tcsetattr}.
379 It's a bad idea to simply initialize a @code{struct termios} structure
380 to a chosen set of attributes and pass it directly to @code{tcsetattr}.
381 Your program may be run years from now, on systems that support members
382 not documented in this manual.  The way to avoid setting these members
383 to unreasonable values is to avoid changing them.
385 What's more, different terminal devices may require different mode
386 settings in order to function properly.  So you should avoid blindly
387 copying attributes from one terminal device to another.
389 When a member contains a collection of independent flags, as the
390 @code{c_iflag}, @code{c_oflag} and @code{c_cflag} members do, even
391 setting the entire member is a bad idea, because particular operating
392 systems have their own flags.  Instead, you should start with the
393 current value of the member and alter only the flags whose values matter
394 in your program, leaving any other flags unchanged.
396 Here is an example of how to set one flag (@code{ISTRIP}) in the
397 @code{struct termios} structure while properly preserving all the other
398 data in the structure:
400 @smallexample
401 @group
403 set_istrip (int desc, int value)
405   struct termios settings;
406   int result;
407 @end group
409 @group
410   result = tcgetattr (desc, &settings);
411   if (result < 0)
412     @{
413       perror ("error in tcgetattr");
414       return 0;
415     @}
416 @end group
417 @group
418   settings.c_iflag &= ~ISTRIP;
419   if (value)
420     settings.c_iflag |= ISTRIP;
421 @end group
422 @group
423   result = tcsetattr (desc, TCSANOW, &settings);
424   if (result < 0)
425     @{
426       perror ("error in tcsetattr");
427       return 0;
428    @}
429   return 1;
431 @end group
432 @end smallexample
434 @node Input Modes
435 @subsection Input Modes
437 This section describes the terminal attribute flags that control
438 fairly low-level aspects of input processing: handling of parity errors,
439 break signals, flow control, and @key{RET} and @key{LFD} characters.
441 All of these flags are bits in the @code{c_iflag} member of the
442 @code{struct termios} structure.  The member is an integer, and you
443 change flags using the operators @code{&}, @code{|} and @code{^}.  Don't
444 try to specify the entire value for @code{c_iflag}---instead, change
445 only specific flags and leave the rest untouched (@pxref{Setting
446 Modes}).
448 @comment termios.h
449 @comment POSIX.1
450 @deftypevr Macro tcflag_t INPCK
451 @cindex parity checking
452 If this bit is set, input parity checking is enabled.  If it is not set,
453 no checking at all is done for parity errors on input; the
454 characters are simply passed through to the application.
456 Parity checking on input processing is independent of whether parity
457 detection and generation on the underlying terminal hardware is enabled;
458 see @ref{Control Modes}.  For example, you could clear the @code{INPCK}
459 input mode flag and set the @code{PARENB} control mode flag to ignore
460 parity errors on input, but still generate parity on output.
462 If this bit is set, what happens when a parity error is detected depends
463 on whether the @code{IGNPAR} or @code{PARMRK} bits are set.  If neither
464 of these bits are set, a byte with a parity error is passed to the
465 application as a @code{'\0'} character.
466 @end deftypevr
468 @comment termios.h
469 @comment POSIX.1
470 @deftypevr Macro tcflag_t IGNPAR
471 If this bit is set, any byte with a framing or parity error is ignored.
472 This is only useful if @code{INPCK} is also set.
473 @end deftypevr
475 @comment termios.h
476 @comment POSIX.1
477 @deftypevr Macro tcflag_t PARMRK
478 If this bit is set, input bytes with parity or framing errors are marked
479 when passed to the program.  This bit is meaningful only when
480 @code{INPCK} is set and @code{IGNPAR} is not set.
482 The way erroneous bytes are marked is with two preceding bytes,
483 @code{377} and @code{0}.  Thus, the program actually reads three bytes
484 for one erroneous byte received from the terminal.
486 If a valid byte has the value @code{0377}, and @code{ISTRIP} (see below)
487 is not set, the program might confuse it with the prefix that marks a
488 parity error.  So a valid byte @code{0377} is passed to the program as
489 two bytes, @code{0377} @code{0377}, in this case.
490 @end deftypevr
492 @comment termios.h
493 @comment POSIX.1
494 @deftypevr Macro tcflag_t ISTRIP
495 If this bit is set, valid input bytes are stripped to seven bits;
496 otherwise, all eight bits are available for programs to read.
497 @end deftypevr
499 @comment termios.h
500 @comment POSIX.1
501 @deftypevr Macro tcflag_t IGNBRK
502 If this bit is set, break conditions are ignored.
504 @cindex break condition, detecting
505 A @dfn{break condition} is defined in the context of asynchronous
506 serial data transmission as a series of zero-value bits longer than a
507 single byte.
508 @end deftypevr
510 @comment termios.h
511 @comment POSIX.1
512 @deftypevr Macro tcflag_t BRKINT
513 If this bit is set and @code{IGNBRK} is not set, a break condition
514 clears the terminal input and output queues and raises a @code{SIGINT}
515 signal for the foreground process group associated with the terminal.
517 If neither @code{BRKINT} nor @code{IGNBRK} are set, a break condition is
518 passed to the application as a single @code{'\0'} character if
519 @code{PARMRK} is not set, or otherwise as a three-character sequence
520 @code{'\377'}, @code{'\0'}, @code{'\0'}.
521 @end deftypevr
523 @comment termios.h
524 @comment POSIX.1
525 @deftypevr Macro tcflag_t IGNCR
526 If this bit is set, carriage return characters (@code{'\r'}) are
527 discarded on input.  Discarding carriage return may be useful on
528 terminals that send both carriage return and linefeed when you type the
529 @key{RET} key.
530 @end deftypevr
532 @comment termios.h
533 @comment POSIX.1
534 @deftypevr Macro tcflag_t ICRNL
535 If this bit is set and @code{IGNCR} is not set, carriage return characters
536 (@code{'\r'}) received as input are passed to the application as newline
537 characters (@code{'\n'}).
538 @end deftypevr
540 @comment termios.h
541 @comment POSIX.1
542 @deftypevr Macro tcflag_t INLCR
543 If this bit is set, newline characters (@code{'\n'}) received as input
544 are passed to the application as carriage return characters (@code{'\r'}).
545 @end deftypevr
547 @comment termios.h
548 @comment POSIX.1
549 @deftypevr Macro tcflag_t IXOFF
550 If this bit is set, start/stop control on input is enabled.  In other
551 words, the computer sends STOP and START characters as necessary to
552 prevent input from coming in faster than programs are reading it.  The
553 idea is that the actual terminal hardware that is generating the input
554 data responds to a STOP character by suspending transmission, and to a
555 START character by resuming transmission.  @xref{Start/Stop Characters}.
556 @end deftypevr
558 @comment termios.h
559 @comment POSIX.1
560 @deftypevr Macro tcflag_t IXON
561 If this bit is set, start/stop control on output is enabled.  In other
562 words, if the computer receives a STOP character, it suspends output
563 until a START character is received.  In this case, the STOP and START
564 characters are never passed to the application program.  If this bit is
565 not set, then START and STOP can be read as ordinary characters.
566 @xref{Start/Stop Characters}.
567 @c !!! mention this interferes with using C-s and C-q for programs like emacs
568 @end deftypevr
570 @comment termios.h
571 @comment BSD
572 @deftypevr Macro tcflag_t IXANY
573 If this bit is set, any input character restarts output when output has
574 been suspended with the STOP character.  Otherwise, only the START
575 character restarts output.
577 This is a BSD extension; it exists only on BSD systems and
578 @gnulinuxhurdsystems{}.
579 @end deftypevr
581 @comment termios.h
582 @comment BSD
583 @deftypevr Macro tcflag_t IMAXBEL
584 If this bit is set, then filling up the terminal input buffer sends a
585 BEL character (code @code{007}) to the terminal to ring the bell.
587 This is a BSD extension.
588 @end deftypevr
590 @node Output Modes
591 @subsection Output Modes
593 This section describes the terminal flags and fields that control how
594 output characters are translated and padded for display.  All of these
595 are contained in the @code{c_oflag} member of the @w{@code{struct termios}}
596 structure.
598 The @code{c_oflag} member itself is an integer, and you change the flags
599 and fields using the operators @code{&}, @code{|}, and @code{^}.  Don't
600 try to specify the entire value for @code{c_oflag}---instead, change
601 only specific flags and leave the rest untouched (@pxref{Setting
602 Modes}).
604 @comment termios.h
605 @comment POSIX.1
606 @deftypevr Macro tcflag_t OPOST
607 If this bit is set, output data is processed in some unspecified way so
608 that it is displayed appropriately on the terminal device.  This
609 typically includes mapping newline characters (@code{'\n'}) onto
610 carriage return and linefeed pairs.
612 If this bit isn't set, the characters are transmitted as-is.
613 @end deftypevr
615 The following three bits are effective only if @code{OPOST} is set.
617 @comment termios.h
618 @comment POSIX.1
619 @deftypevr Macro tcflag_t ONLCR
620 If this bit is set, convert the newline character on output into a pair
621 of characters, carriage return followed by linefeed.
622 @end deftypevr
624 @comment termios.h (optional)
625 @comment BSD
626 @deftypevr Macro tcflag_t OXTABS
627 If this bit is set, convert tab characters on output into the appropriate
628 number of spaces to emulate a tab stop every eight columns.  This bit
629 exists only on BSD systems and @gnuhurdsystems{}; on
630 @gnulinuxsystems{} it is available as @code{XTABS}.
631 @end deftypevr
633 @comment termios.h (optional)
634 @comment BSD
635 @deftypevr Macro tcflag_t ONOEOT
636 If this bit is set, discard @kbd{C-d} characters (code @code{004}) on
637 output.  These characters cause many dial-up terminals to disconnect.
638 This bit exists only on BSD systems and @gnuhurdsystems{}.
639 @end deftypevr
641 @node Control Modes
642 @subsection Control Modes
644 This section describes the terminal flags and fields that control
645 parameters usually associated with asynchronous serial data
646 transmission.  These flags may not make sense for other kinds of
647 terminal ports (such as a network connection pseudo-terminal).  All of
648 these are contained in the @code{c_cflag} member of the @code{struct
649 termios} structure.
651 The @code{c_cflag} member itself is an integer, and you change the flags
652 and fields using the operators @code{&}, @code{|}, and @code{^}.  Don't
653 try to specify the entire value for @code{c_cflag}---instead, change
654 only specific flags and leave the rest untouched (@pxref{Setting
655 Modes}).
657 @comment termios.h
658 @comment POSIX.1
659 @deftypevr Macro tcflag_t CLOCAL
660 If this bit is set, it indicates that the terminal is connected
661 ``locally'' and that the modem status lines (such as carrier detect)
662 should be ignored.
663 @cindex modem status lines
664 @cindex carrier detect
666 On many systems if this bit is not set and you call @code{open} without
667 the @code{O_NONBLOCK} flag set, @code{open} blocks until a modem
668 connection is established.
670 If this bit is not set and a modem disconnect is detected, a
671 @code{SIGHUP} signal is sent to the controlling process group for the
672 terminal (if it has one).  Normally, this causes the process to exit;
673 see @ref{Signal Handling}.  Reading from the terminal after a disconnect
674 causes an end-of-file condition, and writing causes an @code{EIO} error
675 to be returned.  The terminal device must be closed and reopened to
676 clear the condition.
677 @cindex modem disconnect
678 @end deftypevr
680 @comment termios.h
681 @comment POSIX.1
682 @deftypevr Macro tcflag_t HUPCL
683 If this bit is set, a modem disconnect is generated when all processes
684 that have the terminal device open have either closed the file or exited.
685 @end deftypevr
687 @comment termios.h
688 @comment POSIX.1
689 @deftypevr Macro tcflag_t CREAD
690 If this bit is set, input can be read from the terminal.  Otherwise,
691 input is discarded when it arrives.
692 @end deftypevr
694 @comment termios.h
695 @comment POSIX.1
696 @deftypevr Macro tcflag_t CSTOPB
697 If this bit is set, two stop bits are used.  Otherwise, only one stop bit
698 is used.
699 @end deftypevr
701 @comment termios.h
702 @comment POSIX.1
703 @deftypevr Macro tcflag_t PARENB
704 If this bit is set, generation and detection of a parity bit are enabled.
705 @xref{Input Modes}, for information on how input parity errors are handled.
707 If this bit is not set, no parity bit is added to output characters, and
708 input characters are not checked for correct parity.
709 @end deftypevr
711 @comment termios.h
712 @comment POSIX.1
713 @deftypevr Macro tcflag_t PARODD
714 This bit is only useful if @code{PARENB} is set.  If @code{PARODD} is set,
715 odd parity is used, otherwise even parity is used.
716 @end deftypevr
718 The control mode flags also includes a field for the number of bits per
719 character.  You can use the @code{CSIZE} macro as a mask to extract the
720 value, like this: @code{settings.c_cflag & CSIZE}.
722 @comment termios.h
723 @comment POSIX.1
724 @deftypevr Macro tcflag_t CSIZE
725 This is a mask for the number of bits per character.
726 @end deftypevr
728 @comment termios.h
729 @comment POSIX.1
730 @deftypevr Macro tcflag_t CS5
731 This specifies five bits per byte.
732 @end deftypevr
734 @comment termios.h
735 @comment POSIX.1
736 @deftypevr Macro tcflag_t CS6
737 This specifies six bits per byte.
738 @end deftypevr
740 @comment termios.h
741 @comment POSIX.1
742 @deftypevr Macro tcflag_t CS7
743 This specifies seven bits per byte.
744 @end deftypevr
746 @comment termios.h
747 @comment POSIX.1
748 @deftypevr Macro tcflag_t CS8
749 This specifies eight bits per byte.
750 @end deftypevr
752 The following four bits are BSD extensions; these exist only on BSD
753 systems and @gnuhurdsystems{}.
755 @comment termios.h
756 @comment BSD
757 @deftypevr Macro tcflag_t CCTS_OFLOW
758 If this bit is set, enable flow control of output based on the CTS wire
759 (RS232 protocol).
760 @end deftypevr
762 @comment termios.h
763 @comment BSD
764 @deftypevr Macro tcflag_t CRTS_IFLOW
765 If this bit is set, enable flow control of input based on the RTS wire
766 (RS232 protocol).
767 @end deftypevr
769 @comment termios.h
770 @comment BSD
771 @deftypevr Macro tcflag_t MDMBUF
772 If this bit is set, enable carrier-based flow control of output.
773 @end deftypevr
775 @comment termios.h
776 @comment BSD
777 @deftypevr Macro tcflag_t CIGNORE
778 If this bit is set, it says to ignore the control modes and line speed
779 values entirely.  This is only meaningful in a call to @code{tcsetattr}.
781 The @code{c_cflag} member and the line speed values returned by
782 @code{cfgetispeed} and @code{cfgetospeed} will be unaffected by the
783 call.  @code{CIGNORE} is useful if you want to set all the software
784 modes in the other members, but leave the hardware details in
785 @code{c_cflag} unchanged.  (This is how the @code{TCSASOFT} flag to
786 @code{tcsettattr} works.)
788 This bit is never set in the structure filled in by @code{tcgetattr}.
789 @end deftypevr
791 @node Local Modes
792 @subsection Local Modes
794 This section describes the flags for the @code{c_lflag} member of the
795 @code{struct termios} structure.  These flags generally control
796 higher-level aspects of input processing than the input modes flags
797 described in @ref{Input Modes}, such as echoing, signals, and the choice
798 of canonical or noncanonical input.
800 The @code{c_lflag} member itself is an integer, and you change the flags
801 and fields using the operators @code{&}, @code{|}, and @code{^}.  Don't
802 try to specify the entire value for @code{c_lflag}---instead, change
803 only specific flags and leave the rest untouched (@pxref{Setting
804 Modes}).
806 @comment termios.h
807 @comment POSIX.1
808 @deftypevr Macro tcflag_t ICANON
809 This bit, if set, enables canonical input processing mode.  Otherwise,
810 input is processed in noncanonical mode.  @xref{Canonical or Not}.
811 @end deftypevr
813 @comment termios.h
814 @comment POSIX.1
815 @deftypevr Macro tcflag_t ECHO
816 If this bit is set, echoing of input characters back to the terminal
817 is enabled.
818 @cindex echo of terminal input
819 @end deftypevr
821 @comment termios.h
822 @comment POSIX.1
823 @deftypevr Macro tcflag_t ECHOE
824 If this bit is set, echoing indicates erasure of input with the ERASE
825 character by erasing the last character in the current line from the
826 screen.  Otherwise, the character erased is re-echoed to show what has
827 happened (suitable for a printing terminal).
829 This bit only controls the display behavior; the @code{ICANON} bit by
830 itself controls actual recognition of the ERASE character and erasure of
831 input, without which @code{ECHOE} is simply irrelevant.
832 @end deftypevr
834 @comment termios.h
835 @comment BSD
836 @deftypevr Macro tcflag_t ECHOPRT
837 This bit is like @code{ECHOE}, enables display of the ERASE character in
838 a way that is geared to a hardcopy terminal.  When you type the ERASE
839 character, a @samp{\} character is printed followed by the first
840 character erased.  Typing the ERASE character again just prints the next
841 character erased.  Then, the next time you type a normal character, a
842 @samp{/} character is printed before the character echoes.
844 This is a BSD extension, and exists only in BSD systems and
845 @gnulinuxhurdsystems{}.
846 @end deftypevr
848 @comment termios.h
849 @comment POSIX.1
850 @deftypevr Macro tcflag_t ECHOK
851 This bit enables special display of the KILL character by moving to a
852 new line after echoing the KILL character normally.  The behavior of
853 @code{ECHOKE} (below) is nicer to look at.
855 If this bit is not set, the KILL character echoes just as it would if it
856 were not the KILL character.  Then it is up to the user to remember that
857 the KILL character has erased the preceding input; there is no
858 indication of this on the screen.
860 This bit only controls the display behavior; the @code{ICANON} bit by
861 itself controls actual recognition of the KILL character and erasure of
862 input, without which @code{ECHOK} is simply irrelevant.
863 @end deftypevr
865 @comment termios.h
866 @comment BSD
867 @deftypevr Macro tcflag_t ECHOKE
868 This bit is similar to @code{ECHOK}.  It enables special display of the
869 KILL character by erasing on the screen the entire line that has been
870 killed.  This is a BSD extension, and exists only in BSD systems and
871 @gnulinuxhurdsystems{}.
872 @end deftypevr
874 @comment termios.h
875 @comment POSIX.1
876 @deftypevr Macro tcflag_t ECHONL
877 If this bit is set and the @code{ICANON} bit is also set, then the
878 newline (@code{'\n'}) character is echoed even if the @code{ECHO} bit
879 is not set.
880 @end deftypevr
882 @comment termios.h
883 @comment BSD
884 @deftypevr Macro tcflag_t ECHOCTL
885 If this bit is set and the @code{ECHO} bit is also set, echo control
886 characters with @samp{^} followed by the corresponding text character.
887 Thus, control-A echoes as @samp{^A}.  This is usually the preferred mode
888 for interactive input, because echoing a control character back to the
889 terminal could have some undesired effect on the terminal.
891 This is a BSD extension, and exists only in BSD systems and
892 @gnulinuxhurdsystems{}.
893 @end deftypevr
895 @comment termios.h
896 @comment POSIX.1
897 @deftypevr Macro tcflag_t ISIG
898 This bit controls whether the INTR, QUIT, and SUSP characters are
899 recognized.  The functions associated with these characters are performed
900 if and only if this bit is set.  Being in canonical or noncanonical
901 input mode has no affect on the interpretation of these characters.
903 You should use caution when disabling recognition of these characters.
904 Programs that cannot be interrupted interactively are very
905 user-unfriendly.  If you clear this bit, your program should provide
906 some alternate interface that allows the user to interactively send the
907 signals associated with these characters, or to escape from the program.
908 @cindex interactive signals, from terminal
910 @xref{Signal Characters}.
911 @end deftypevr
913 @comment termios.h
914 @comment POSIX.1
915 @deftypevr Macro tcflag_t IEXTEN
916 POSIX.1 gives @code{IEXTEN} implementation-defined meaning,
917 so you cannot rely on this interpretation on all systems.
919 On BSD systems and @gnulinuxhurdsystems{}, it enables the LNEXT and
920 DISCARD characters.
921 @xref{Other Special}.
922 @end deftypevr
924 @comment termios.h
925 @comment POSIX.1
926 @deftypevr Macro tcflag_t NOFLSH
927 Normally, the INTR, QUIT, and SUSP characters cause input and output
928 queues for the terminal to be cleared.  If this bit is set, the queues
929 are not cleared.
930 @end deftypevr
932 @comment termios.h
933 @comment POSIX.1
934 @deftypevr Macro tcflag_t TOSTOP
935 If this bit is set and the system supports job control, then
936 @code{SIGTTOU} signals are generated by background processes that
937 attempt to write to the terminal.  @xref{Access to the Terminal}.
938 @end deftypevr
940 The following bits are BSD extensions; they exist only on BSD systems
941 and @gnuhurdsystems{}.
943 @comment termios.h
944 @comment BSD
945 @deftypevr Macro tcflag_t ALTWERASE
946 This bit determines how far the WERASE character should erase.  The
947 WERASE character erases back to the beginning of a word; the question
948 is, where do words begin?
950 If this bit is clear, then the beginning of a word is a nonwhitespace
951 character following a whitespace character.  If the bit is set, then the
952 beginning of a word is an alphanumeric character or underscore following
953 a character which is none of those.
955 @xref{Editing Characters}, for more information about the WERASE character.
956 @end deftypevr
958 @comment termios.h
959 @comment BSD
960 @deftypevr Macro tcflag_t FLUSHO
961 This is the bit that toggles when the user types the DISCARD character.
962 While this bit is set, all output is discarded.  @xref{Other Special}.
963 @end deftypevr
965 @comment termios.h (optional)
966 @comment BSD
967 @deftypevr Macro tcflag_t NOKERNINFO
968 Setting this bit disables handling of the STATUS character.
969 @xref{Other Special}.
970 @end deftypevr
972 @comment termios.h
973 @comment BSD
974 @deftypevr Macro tcflag_t PENDIN
975 If this bit is set, it indicates that there is a line of input that
976 needs to be reprinted.  Typing the REPRINT character sets this bit; the
977 bit remains set until reprinting is finished.  @xref{Editing Characters}.
978 @end deftypevr
980 @c EXTPROC is too obscure to document now.  --roland
982 @node Line Speed
983 @subsection Line Speed
984 @cindex line speed
985 @cindex baud rate
986 @cindex terminal line speed
987 @cindex terminal line speed
989 The terminal line speed tells the computer how fast to read and write
990 data on the terminal.
992 If the terminal is connected to a real serial line, the terminal speed
993 you specify actually controls the line---if it doesn't match the
994 terminal's own idea of the speed, communication does not work.  Real
995 serial ports accept only certain standard speeds.  Also, particular
996 hardware may not support even all the standard speeds.  Specifying a
997 speed of zero hangs up a dialup connection and turns off modem control
998 signals.
1000 If the terminal is not a real serial line (for example, if it is a
1001 network connection), then the line speed won't really affect data
1002 transmission speed, but some programs will use it to determine the
1003 amount of padding needed.  It's best to specify a line speed value that
1004 matches the actual speed of the actual terminal, but you can safely
1005 experiment with different values to vary the amount of padding.
1007 There are actually two line speeds for each terminal, one for input and
1008 one for output.  You can set them independently, but most often
1009 terminals use the same speed for both directions.
1011 The speed values are stored in the @code{struct termios} structure, but
1012 don't try to access them in the @code{struct termios} structure
1013 directly.  Instead, you should use the following functions to read and
1014 store them:
1016 @comment termios.h
1017 @comment POSIX.1
1018 @deftypefun speed_t cfgetospeed (const struct termios *@var{termios-p})
1019 This function returns the output line speed stored in the structure
1020 @code{*@var{termios-p}}.
1021 @end deftypefun
1023 @comment termios.h
1024 @comment POSIX.1
1025 @deftypefun speed_t cfgetispeed (const struct termios *@var{termios-p})
1026 This function returns the input line speed stored in the structure
1027 @code{*@var{termios-p}}.
1028 @end deftypefun
1030 @comment termios.h
1031 @comment POSIX.1
1032 @deftypefun int cfsetospeed (struct termios *@var{termios-p}, speed_t @var{speed})
1033 This function stores @var{speed} in @code{*@var{termios-p}} as the output
1034 speed.  The normal return value is @math{0}; a value of @math{-1}
1035 indicates an error.  If @var{speed} is not a speed, @code{cfsetospeed}
1036 returns @math{-1}.
1037 @end deftypefun
1039 @comment termios.h
1040 @comment POSIX.1
1041 @deftypefun int cfsetispeed (struct termios *@var{termios-p}, speed_t @var{speed})
1042 This function stores @var{speed} in @code{*@var{termios-p}} as the input
1043 speed.  The normal return value is @math{0}; a value of @math{-1}
1044 indicates an error.  If @var{speed} is not a speed, @code{cfsetospeed}
1045 returns @math{-1}.
1046 @end deftypefun
1048 @comment termios.h
1049 @comment BSD
1050 @deftypefun int cfsetspeed (struct termios *@var{termios-p}, speed_t @var{speed})
1051 This function stores @var{speed} in @code{*@var{termios-p}} as both the
1052 input and output speeds.  The normal return value is @math{0}; a value
1053 of @math{-1} indicates an error.  If @var{speed} is not a speed,
1054 @code{cfsetspeed} returns @math{-1}.  This function is an extension in
1055 4.4 BSD.
1056 @end deftypefun
1058 @comment termios.h
1059 @comment POSIX.1
1060 @deftp {Data Type} speed_t
1061 The @code{speed_t} type is an unsigned integer data type used to
1062 represent line speeds.
1063 @end deftp
1065 The functions @code{cfsetospeed} and @code{cfsetispeed} report errors
1066 only for speed values that the system simply cannot handle.  If you
1067 specify a speed value that is basically acceptable, then those functions
1068 will succeed.  But they do not check that a particular hardware device
1069 can actually support the specified speeds---in fact, they don't know
1070 which device you plan to set the speed for.  If you use @code{tcsetattr}
1071 to set the speed of a particular device to a value that it cannot
1072 handle, @code{tcsetattr} returns @math{-1}.
1074 @strong{Portability note:} In @theglibc{}, the functions above
1075 accept speeds measured in bits per second as input, and return speed
1076 values measured in bits per second.  Other libraries require speeds to
1077 be indicated by special codes.  For POSIX.1 portability, you must use
1078 one of the following symbols to represent the speed; their precise
1079 numeric values are system-dependent, but each name has a fixed meaning:
1080 @code{B110} stands for 110 bps, @code{B300} for 300 bps, and so on.
1081 There is no portable way to represent any speed but these, but these are
1082 the only speeds that typical serial lines can support.
1084 @comment termios.h
1085 @comment POSIX.1
1086 @vindex B0
1087 @comment termios.h
1088 @comment POSIX.1
1089 @vindex B50
1090 @comment termios.h
1091 @comment POSIX.1
1092 @vindex B75
1093 @comment termios.h
1094 @comment POSIX.1
1095 @vindex B110
1096 @comment termios.h
1097 @comment POSIX.1
1098 @vindex B134
1099 @comment termios.h
1100 @comment POSIX.1
1101 @vindex B150
1102 @comment termios.h
1103 @comment POSIX.1
1104 @vindex B200
1105 @comment termios.h
1106 @comment POSIX.1
1107 @vindex B300
1108 @comment termios.h
1109 @comment POSIX.1
1110 @vindex B600
1111 @comment termios.h
1112 @comment POSIX.1
1113 @vindex B1200
1114 @comment termios.h
1115 @comment POSIX.1
1116 @vindex B1800
1117 @comment termios.h
1118 @comment POSIX.1
1119 @vindex B2400
1120 @comment termios.h
1121 @comment POSIX.1
1122 @vindex B4800
1123 @comment termios.h
1124 @comment POSIX.1
1125 @vindex B9600
1126 @comment termios.h
1127 @comment POSIX.1
1128 @vindex B19200
1129 @comment termios.h
1130 @comment POSIX.1
1131 @vindex B38400
1132 @comment termios.h
1133 @comment GNU
1134 @vindex B57600
1135 @comment termios.h
1136 @comment GNU
1137 @vindex B115200
1138 @comment termios.h
1139 @comment GNU
1140 @vindex B230400
1141 @comment termios.h
1142 @comment GNU
1143 @vindex B460800
1144 @smallexample
1145 B0  B50  B75  B110  B134  B150  B200
1146 B300  B600  B1200  B1800  B2400  B4800
1147 B9600  B19200  B38400  B57600  B115200
1148 B230400  B460800
1149 @end smallexample
1151 @vindex EXTA
1152 @vindex EXTB
1153 BSD defines two additional speed symbols as aliases: @code{EXTA} is an
1154 alias for @code{B19200} and @code{EXTB} is an alias for @code{B38400}.
1155 These aliases are obsolete.
1157 @node Special Characters
1158 @subsection Special Characters
1160 In canonical input, the terminal driver recognizes a number of special
1161 characters which perform various control functions.  These include the
1162 ERASE character (usually @key{DEL}) for editing input, and other editing
1163 characters.  The INTR character (normally @kbd{C-c}) for sending a
1164 @code{SIGINT} signal, and other signal-raising characters, may be
1165 available in either canonical or noncanonical input mode.  All these
1166 characters are described in this section.
1168 The particular characters used are specified in the @code{c_cc} member
1169 of the @code{struct termios} structure.  This member is an array; each
1170 element specifies the character for a particular role.  Each element has
1171 a symbolic constant that stands for the index of that element---for
1172 example, @code{VINTR} is the index of the element that specifies the INTR
1173 character, so storing @code{'='} in @code{@var{termios}.c_cc[VINTR]}
1174 specifies @samp{=} as the INTR character.
1176 @vindex _POSIX_VDISABLE
1177 On some systems, you can disable a particular special character function
1178 by specifying the value @code{_POSIX_VDISABLE} for that role.  This
1179 value is unequal to any possible character code.  @xref{Options for
1180 Files}, for more information about how to tell whether the operating
1181 system you are using supports @code{_POSIX_VDISABLE}.
1183 @menu
1184 * Editing Characters::          Special characters that terminate lines and
1185                                   delete text, and other editing functions.
1186 * Signal Characters::           Special characters that send or raise signals
1187                                   to or for certain classes of processes.
1188 * Start/Stop Characters::       Special characters that suspend or resume
1189                                   suspended output.
1190 * Other Special::               Other special characters for BSD systems:
1191                                   they can discard output, and print status.
1192 @end menu
1194 @node Editing Characters
1195 @subsubsection Characters for Input Editing
1197 These special characters are active only in canonical input mode.
1198 @xref{Canonical or Not}.
1200 @comment termios.h
1201 @comment POSIX.1
1202 @deftypevr Macro int VEOF
1203 @cindex EOF character
1204 This is the subscript for the EOF character in the special control
1205 character array.  @code{@var{termios}.c_cc[VEOF]} holds the character
1206 itself.
1208 The EOF character is recognized only in canonical input mode.  It acts
1209 as a line terminator in the same way as a newline character, but if the
1210 EOF character is typed at the beginning of a line it causes @code{read}
1211 to return a byte count of zero, indicating end-of-file.  The EOF
1212 character itself is discarded.
1214 Usually, the EOF character is @kbd{C-d}.
1215 @end deftypevr
1217 @comment termios.h
1218 @comment POSIX.1
1219 @deftypevr Macro int VEOL
1220 @cindex EOL character
1221 This is the subscript for the EOL character in the special control
1222 character array.  @code{@var{termios}.c_cc[VEOL]} holds the character
1223 itself.
1225 The EOL character is recognized only in canonical input mode.  It acts
1226 as a line terminator, just like a newline character.  The EOL character
1227 is not discarded; it is read as the last character in the input line.
1229 @c !!! example: this is set to ESC by 4.3 csh with "set filec" so it can
1230 @c complete partial lines without using cbreak or raw mode.
1232 You don't need to use the EOL character to make @key{RET} end a line.
1233 Just set the ICRNL flag.  In fact, this is the default state of
1234 affairs.
1235 @end deftypevr
1237 @comment termios.h
1238 @comment BSD
1239 @deftypevr Macro int VEOL2
1240 @cindex EOL2 character
1241 This is the subscript for the EOL2 character in the special control
1242 character array.  @code{@var{termios}.c_cc[VEOL2]} holds the character
1243 itself.
1245 The EOL2 character works just like the EOL character (see above), but it
1246 can be a different character.  Thus, you can specify two characters to
1247 terminate an input line, by setting EOL to one of them and EOL2 to the
1248 other.
1250 The EOL2 character is a BSD extension; it exists only on BSD systems
1251 and @gnulinuxhurdsystems{}.
1252 @end deftypevr
1254 @comment termios.h
1255 @comment POSIX.1
1256 @deftypevr Macro int VERASE
1257 @cindex ERASE character
1258 This is the subscript for the ERASE character in the special control
1259 character array.  @code{@var{termios}.c_cc[VERASE]} holds the
1260 character itself.
1262 The ERASE character is recognized only in canonical input mode.  When
1263 the user types the erase character, the previous character typed is
1264 discarded.  (If the terminal generates multibyte character sequences,
1265 this may cause more than one byte of input to be discarded.)  This
1266 cannot be used to erase past the beginning of the current line of text.
1267 The ERASE character itself is discarded.
1268 @c !!! mention ECHOE here
1270 Usually, the ERASE character is @key{DEL}.
1271 @end deftypevr
1273 @comment termios.h
1274 @comment BSD
1275 @deftypevr Macro int VWERASE
1276 @cindex WERASE character
1277 This is the subscript for the WERASE character in the special control
1278 character array.  @code{@var{termios}.c_cc[VWERASE]} holds the character
1279 itself.
1281 The WERASE character is recognized only in canonical mode.  It erases an
1282 entire word of prior input, and any whitespace after it; whitespace
1283 characters before the word are not erased.
1285 The definition of a ``word'' depends on the setting of the
1286 @code{ALTWERASE} mode; @pxref{Local Modes}.
1288 If the @code{ALTWERASE} mode is not set, a word is defined as a sequence
1289 of any characters except space or tab.
1291 If the @code{ALTWERASE} mode is set, a word is defined as a sequence of
1292 characters containing only letters, numbers, and underscores, optionally
1293 followed by one character that is not a letter, number, or underscore.
1295 The WERASE character is usually @kbd{C-w}.
1297 This is a BSD extension.
1298 @end deftypevr
1300 @comment termios.h
1301 @comment POSIX.1
1302 @deftypevr Macro int VKILL
1303 @cindex KILL character
1304 This is the subscript for the KILL character in the special control
1305 character array.  @code{@var{termios}.c_cc[VKILL]} holds the character
1306 itself.
1308 The KILL character is recognized only in canonical input mode.  When the
1309 user types the kill character, the entire contents of the current line
1310 of input are discarded.  The kill character itself is discarded too.
1312 The KILL character is usually @kbd{C-u}.
1313 @end deftypevr
1315 @comment termios.h
1316 @comment BSD
1317 @deftypevr Macro int VREPRINT
1318 @cindex REPRINT character
1319 This is the subscript for the REPRINT character in the special control
1320 character array.  @code{@var{termios}.c_cc[VREPRINT]} holds the character
1321 itself.
1323 The REPRINT character is recognized only in canonical mode.  It reprints
1324 the current input line.  If some asynchronous output has come while you
1325 are typing, this lets you see the line you are typing clearly again.
1327 The REPRINT character is usually @kbd{C-r}.
1329 This is a BSD extension.
1330 @end deftypevr
1332 @node Signal Characters
1333 @subsubsection Characters that Cause Signals
1335 These special characters may be active in either canonical or noncanonical
1336 input mode, but only when the @code{ISIG} flag is set (@pxref{Local
1337 Modes}).
1339 @comment termios.h
1340 @comment POSIX.1
1341 @deftypevr Macro int VINTR
1342 @cindex INTR character
1343 @cindex interrupt character
1344 This is the subscript for the INTR character in the special control
1345 character array.  @code{@var{termios}.c_cc[VINTR]} holds the character
1346 itself.
1348 The INTR (interrupt) character raises a @code{SIGINT} signal for all
1349 processes in the foreground job associated with the terminal.  The INTR
1350 character itself is then discarded.  @xref{Signal Handling}, for more
1351 information about signals.
1353 Typically, the INTR character is @kbd{C-c}.
1354 @end deftypevr
1356 @comment termios.h
1357 @comment POSIX.1
1358 @deftypevr Macro int VQUIT
1359 @cindex QUIT character
1360 This is the subscript for the QUIT character in the special control
1361 character array.  @code{@var{termios}.c_cc[VQUIT]} holds the character
1362 itself.
1364 The QUIT character raises a @code{SIGQUIT} signal for all processes in
1365 the foreground job associated with the terminal.  The QUIT character
1366 itself is then discarded.  @xref{Signal Handling}, for more information
1367 about signals.
1369 Typically, the QUIT character is @kbd{C-\}.
1370 @end deftypevr
1372 @comment termios.h
1373 @comment POSIX.1
1374 @deftypevr Macro int VSUSP
1375 @cindex SUSP character
1376 @cindex suspend character
1377 This is the subscript for the SUSP character in the special control
1378 character array.  @code{@var{termios}.c_cc[VSUSP]} holds the character
1379 itself.
1381 The SUSP (suspend) character is recognized only if the implementation
1382 supports job control (@pxref{Job Control}).  It causes a @code{SIGTSTP}
1383 signal to be sent to all processes in the foreground job associated with
1384 the terminal.  The SUSP character itself is then discarded.
1385 @xref{Signal Handling}, for more information about signals.
1387 Typically, the SUSP character is @kbd{C-z}.
1388 @end deftypevr
1390 Few applications disable the normal interpretation of the SUSP
1391 character.  If your program does this, it should provide some other
1392 mechanism for the user to stop the job.  When the user invokes this
1393 mechanism, the program should send a @code{SIGTSTP} signal to the
1394 process group of the process, not just to the process itself.
1395 @xref{Signaling Another Process}.
1397 @comment termios.h
1398 @comment BSD
1399 @deftypevr Macro int VDSUSP
1400 @cindex DSUSP character
1401 @cindex delayed suspend character
1402 This is the subscript for the DSUSP character in the special control
1403 character array.  @code{@var{termios}.c_cc[VDSUSP]} holds the character
1404 itself.
1406 The DSUSP (suspend) character is recognized only if the implementation
1407 supports job control (@pxref{Job Control}).  It sends a @code{SIGTSTP}
1408 signal, like the SUSP character, but not right away---only when the
1409 program tries to read it as input.  Not all systems with job control
1410 support DSUSP; only BSD-compatible systems (including @gnuhurdsystems{}).
1412 @xref{Signal Handling}, for more information about signals.
1414 Typically, the DSUSP character is @kbd{C-y}.
1415 @end deftypevr
1417 @node Start/Stop Characters
1418 @subsubsection Special Characters for Flow Control
1420 These special characters may be active in either canonical or noncanonical
1421 input mode, but their use is controlled by the flags @code{IXON} and
1422 @code{IXOFF} (@pxref{Input Modes}).
1424 @comment termios.h
1425 @comment POSIX.1
1426 @deftypevr Macro int VSTART
1427 @cindex START character
1428 This is the subscript for the START character in the special control
1429 character array.  @code{@var{termios}.c_cc[VSTART]} holds the
1430 character itself.
1432 The START character is used to support the @code{IXON} and @code{IXOFF}
1433 input modes.  If @code{IXON} is set, receiving a START character resumes
1434 suspended output; the START character itself is discarded.  If
1435 @code{IXANY} is set, receiving any character at all resumes suspended
1436 output; the resuming character is not discarded unless it is the START
1437 character.  @code{IXOFF} is set, the system may also transmit START
1438 characters to the terminal.
1440 The usual value for the START character is @kbd{C-q}.  You may not be
1441 able to change this value---the hardware may insist on using @kbd{C-q}
1442 regardless of what you specify.
1443 @end deftypevr
1445 @comment termios.h
1446 @comment POSIX.1
1447 @deftypevr Macro int VSTOP
1448 @cindex STOP character
1449 This is the subscript for the STOP character in the special control
1450 character array.  @code{@var{termios}.c_cc[VSTOP]} holds the character
1451 itself.
1453 The STOP character is used to support the @code{IXON} and @code{IXOFF}
1454 input modes.  If @code{IXON} is set, receiving a STOP character causes
1455 output to be suspended; the STOP character itself is discarded.  If
1456 @code{IXOFF} is set, the system may also transmit STOP characters to the
1457 terminal, to prevent the input queue from overflowing.
1459 The usual value for the STOP character is @kbd{C-s}.  You may not be
1460 able to change this value---the hardware may insist on using @kbd{C-s}
1461 regardless of what you specify.
1462 @end deftypevr
1464 @node Other Special
1465 @subsubsection Other Special Characters
1467 @comment termios.h
1468 @comment BSD
1469 @deftypevr Macro int VLNEXT
1470 @cindex LNEXT character
1471 This is the subscript for the LNEXT character in the special control
1472 character array.  @code{@var{termios}.c_cc[VLNEXT]} holds the character
1473 itself.
1475 The LNEXT character is recognized only when @code{IEXTEN} is set, but in
1476 both canonical and noncanonical mode.  It disables any special
1477 significance of the next character the user types.  Even if the
1478 character would normally perform some editing function or generate a
1479 signal, it is read as a plain character.  This is the analogue of the
1480 @kbd{C-q} command in Emacs.  ``LNEXT'' stands for ``literal next.''
1482 The LNEXT character is usually @kbd{C-v}.
1484 This character is available on BSD systems and @gnulinuxhurdsystems{}.
1485 @end deftypevr
1487 @comment termios.h
1488 @comment BSD
1489 @deftypevr Macro int VDISCARD
1490 @cindex DISCARD character
1491 This is the subscript for the DISCARD character in the special control
1492 character array.  @code{@var{termios}.c_cc[VDISCARD]} holds the character
1493 itself.
1495 The DISCARD character is recognized only when @code{IEXTEN} is set, but
1496 in both canonical and noncanonical mode.  Its effect is to toggle the
1497 discard-output flag.  When this flag is set, all program output is
1498 discarded.  Setting the flag also discards all output currently in the
1499 output buffer.  Typing any other character resets the flag.
1501 This character is available on BSD systems and @gnulinuxhurdsystems{}.
1502 @end deftypevr
1504 @comment termios.h
1505 @comment BSD
1506 @deftypevr Macro int VSTATUS
1507 @cindex STATUS character
1508 This is the subscript for the STATUS character in the special control
1509 character array.  @code{@var{termios}.c_cc[VSTATUS]} holds the character
1510 itself.
1512 The STATUS character's effect is to print out a status message about how
1513 the current process is running.
1515 The STATUS character is recognized only in canonical mode, and only if
1516 @code{NOKERNINFO} is not set.
1518 This character is available only on BSD systems and @gnuhurdsystems{}.
1519 @end deftypevr
1521 @node Noncanonical Input
1522 @subsection Noncanonical Input
1524 In noncanonical input mode, the special editing characters such as
1525 ERASE and KILL are ignored.  The system facilities for the user to edit
1526 input are disabled in noncanonical mode, so that all input characters
1527 (unless they are special for signal or flow-control purposes) are passed
1528 to the application program exactly as typed.  It is up to the
1529 application program to give the user ways to edit the input, if
1530 appropriate.
1532 Noncanonical mode offers special parameters called MIN and TIME for
1533 controlling whether and how long to wait for input to be available.  You
1534 can even use them to avoid ever waiting---to return immediately with
1535 whatever input is available, or with no input.
1537 The MIN and TIME are stored in elements of the @code{c_cc} array, which
1538 is a member of the @w{@code{struct termios}} structure.  Each element of
1539 this array has a particular role, and each element has a symbolic
1540 constant that stands for the index of that element.  @code{VMIN} and
1541 @code{VMAX} are the names for the indices in the array of the MIN and
1542 TIME slots.
1544 @comment termios.h
1545 @comment POSIX.1
1546 @deftypevr Macro int VMIN
1547 @cindex MIN termios slot
1548 This is the subscript for the MIN slot in the @code{c_cc} array.  Thus,
1549 @code{@var{termios}.c_cc[VMIN]} is the value itself.
1551 The MIN slot is only meaningful in noncanonical input mode; it
1552 specifies the minimum number of bytes that must be available in the
1553 input queue in order for @code{read} to return.
1554 @end deftypevr
1556 @comment termios.h
1557 @comment POSIX.1
1558 @deftypevr Macro int VTIME
1559 @cindex TIME termios slot
1560 This is the subscript for the TIME slot in the @code{c_cc} array.  Thus,
1561 @code{@var{termios}.c_cc[VTIME]} is the value itself.
1563 The TIME slot is only meaningful in noncanonical input mode; it
1564 specifies how long to wait for input before returning, in units of 0.1
1565 seconds.
1566 @end deftypevr
1568 The MIN and TIME values interact to determine the criterion for when
1569 @code{read} should return; their precise meanings depend on which of
1570 them are nonzero.  There are four possible cases:
1572 @itemize @bullet
1573 @item
1574 Both TIME and MIN are nonzero.
1576 In this case, TIME specifies how long to wait after each input character
1577 to see if more input arrives.  After the first character received,
1578 @code{read} keeps waiting until either MIN bytes have arrived in all, or
1579 TIME elapses with no further input.
1581 @code{read} always blocks until the first character arrives, even if
1582 TIME elapses first.  @code{read} can return more than MIN characters if
1583 more than MIN happen to be in the queue.
1585 @item
1586 Both MIN and TIME are zero.
1588 In this case, @code{read} always returns immediately with as many
1589 characters as are available in the queue, up to the number requested.
1590 If no input is immediately available, @code{read} returns a value of
1591 zero.
1593 @item
1594 MIN is zero but TIME has a nonzero value.
1596 In this case, @code{read} waits for time TIME for input to become
1597 available; the availability of a single byte is enough to satisfy the
1598 read request and cause @code{read} to return.  When it returns, it
1599 returns as many characters as are available, up to the number requested.
1600 If no input is available before the timer expires, @code{read} returns a
1601 value of zero.
1603 @item
1604 TIME is zero but MIN has a nonzero value.
1606 In this case, @code{read} waits until at least MIN bytes are available
1607 in the queue.  At that time, @code{read} returns as many characters as
1608 are available, up to the number requested.  @code{read} can return more
1609 than MIN characters if more than MIN happen to be in the queue.
1610 @end itemize
1612 What happens if MIN is 50 and you ask to read just 10 bytes?
1613 Normally, @code{read} waits until there are 50 bytes in the buffer (or,
1614 more generally, the wait condition described above is satisfied), and
1615 then reads 10 of them, leaving the other 40 buffered in the operating
1616 system for a subsequent call to @code{read}.
1618 @strong{Portability note:} On some systems, the MIN and TIME slots are
1619 actually the same as the EOF and EOL slots.  This causes no serious
1620 problem because the MIN and TIME slots are used only in noncanonical
1621 input and the EOF and EOL slots are used only in canonical input, but it
1622 isn't very clean.  @Theglibc{} allocates separate slots for these
1623 uses.
1625 @comment termios.h
1626 @comment BSD
1627 @deftypefun void cfmakeraw (struct termios *@var{termios-p})
1628 This function provides an easy way to set up @code{*@var{termios-p}} for
1629 what has traditionally been called ``raw mode'' in BSD.  This uses
1630 noncanonical input, and turns off most processing to give an unmodified
1631 channel to the terminal.
1633 It does exactly this:
1634 @smallexample
1635   @var{termios-p}->c_iflag &= ~(IGNBRK|BRKINT|PARMRK|ISTRIP
1636                                 |INLCR|IGNCR|ICRNL|IXON);
1637   @var{termios-p}->c_oflag &= ~OPOST;
1638   @var{termios-p}->c_lflag &= ~(ECHO|ECHONL|ICANON|ISIG|IEXTEN);
1639   @var{termios-p}->c_cflag &= ~(CSIZE|PARENB);
1640   @var{termios-p}->c_cflag |= CS8;
1641 @end smallexample
1642 @end deftypefun
1645 @node BSD Terminal Modes
1646 @section BSD Terminal Modes
1647 @cindex terminal modes, BSD
1649 The usual way to get and set terminal modes is with the functions described
1650 in @ref{Terminal Modes}.  However, on some systems you can use the
1651 BSD-derived functions in this section to do some of the same thing.  On
1652 many systems, these functions do not exist.  Even with @theglibc{},
1653 the functions simply fail with @code{errno} = @code{ENOSYS} with many
1654 kernels, including Linux.
1656 The symbols used in this section are declared in @file{sgtty.h}.
1658 @comment termios.h
1659 @comment BSD
1660 @deftp {Data Type} {struct sgttyb}
1661 This structure is an input or output parameter list for @code{gtty} and
1662 @code{stty}.
1664 @table @code
1665 @item char sg_ispeed
1666 Line speed for input
1667 @item char sg_ospeed
1668 Line speed for output
1669 @item char sg_erase
1670 Erase character
1671 @item char sg_kill
1672 Kill character
1673 @item int sg_flags
1674 Various flags
1675 @end table
1676 @end deftp
1678 @comment sgtty.h
1679 @comment BSD
1680 @deftypefun int gtty (int @var{filedes}, struct sgttyb *@var{attributes})
1681 This function gets the attributes of a terminal.
1683 @code{gtty} sets *@var{attributes} to describe the terminal attributes
1684 of the terminal which is open with file descriptor @var{filedes}.
1685 @end deftypefun
1687 @comment sgtty.h
1688 @comment BSD
1689 @deftypefun int stty (int @var{filedes}, const struct sgttyb *@var{attributes})
1691 This function sets the attributes of a terminal.
1693 @code{stty} sets the terminal attributes of the terminal which is open with
1694 file descriptor @var{filedes} to those described by *@var{filedes}.
1695 @end deftypefun
1697 @node Line Control
1698 @section Line Control Functions
1699 @cindex terminal line control functions
1701 These functions perform miscellaneous control actions on terminal
1702 devices.  As regards terminal access, they are treated like doing
1703 output: if any of these functions is used by a background process on its
1704 controlling terminal, normally all processes in the process group are
1705 sent a @code{SIGTTOU} signal.  The exception is if the calling process
1706 itself is ignoring or blocking @code{SIGTTOU} signals, in which case the
1707 operation is performed and no signal is sent.  @xref{Job Control}.
1709 @cindex break condition, generating
1710 @comment termios.h
1711 @comment POSIX.1
1712 @deftypefun int tcsendbreak (int @var{filedes}, int @var{duration})
1713 This function generates a break condition by transmitting a stream of
1714 zero bits on the terminal associated with the file descriptor
1715 @var{filedes}.  The duration of the break is controlled by the
1716 @var{duration} argument.  If zero, the duration is between 0.25 and 0.5
1717 seconds.  The meaning of a nonzero value depends on the operating system.
1719 This function does nothing if the terminal is not an asynchronous serial
1720 data port.
1722 The return value is normally zero.  In the event of an error, a value
1723 of @math{-1} is returned.  The following @code{errno} error conditions
1724 are defined for this function:
1726 @table @code
1727 @item EBADF
1728 The @var{filedes} is not a valid file descriptor.
1730 @item ENOTTY
1731 The @var{filedes} is not associated with a terminal device.
1732 @end table
1733 @end deftypefun
1736 @cindex flushing terminal output queue
1737 @cindex terminal output queue, flushing
1738 @comment termios.h
1739 @comment POSIX.1
1740 @deftypefun int tcdrain (int @var{filedes})
1741 The @code{tcdrain} function waits until all queued
1742 output to the terminal @var{filedes} has been transmitted.
1744 This function is a cancellation point in multi-threaded programs.  This
1745 is a problem if the thread allocates some resources (like memory, file
1746 descriptors, semaphores or whatever) at the time @code{tcdrain} is
1747 called.  If the thread gets canceled these resources stay allocated
1748 until the program ends.  To avoid this calls to @code{tcdrain} should be
1749 protected using cancellation handlers.
1750 @c ref pthread_cleanup_push / pthread_cleanup_pop
1752 The return value is normally zero.  In the event of an error, a value
1753 of @math{-1} is returned.  The following @code{errno} error conditions
1754 are defined for this function:
1756 @table @code
1757 @item EBADF
1758 The @var{filedes} is not a valid file descriptor.
1760 @item ENOTTY
1761 The @var{filedes} is not associated with a terminal device.
1763 @item EINTR
1764 The operation was interrupted by delivery of a signal.
1765 @xref{Interrupted Primitives}.
1766 @end table
1767 @end deftypefun
1770 @cindex clearing terminal input queue
1771 @cindex terminal input queue, clearing
1772 @comment termios.h
1773 @comment POSIX.1
1774 @deftypefun int tcflush (int @var{filedes}, int @var{queue})
1775 The @code{tcflush} function is used to clear the input and/or output
1776 queues associated with the terminal file @var{filedes}.  The @var{queue}
1777 argument specifies which queue(s) to clear, and can be one of the
1778 following values:
1780 @c Extra blank lines here make it look better.
1781 @table @code
1782 @vindex TCIFLUSH
1783 @item TCIFLUSH
1785 Clear any input data received, but not yet read.
1787 @vindex TCOFLUSH
1788 @item TCOFLUSH
1790 Clear any output data written, but not yet transmitted.
1792 @vindex TCIOFLUSH
1793 @item TCIOFLUSH
1795 Clear both queued input and output.
1796 @end table
1798 The return value is normally zero.  In the event of an error, a value
1799 of @math{-1} is returned.  The following @code{errno} error conditions
1800 are defined for this function:
1802 @table @code
1803 @item EBADF
1804 The @var{filedes} is not a valid file descriptor.
1806 @item ENOTTY
1807 The @var{filedes} is not associated with a terminal device.
1809 @item EINVAL
1810 A bad value was supplied as the @var{queue} argument.
1811 @end table
1813 It is unfortunate that this function is named @code{tcflush}, because
1814 the term ``flush'' is normally used for quite another operation---waiting
1815 until all output is transmitted---and using it for discarding input or
1816 output would be confusing.  Unfortunately, the name @code{tcflush} comes
1817 from POSIX and we cannot change it.
1818 @end deftypefun
1820 @cindex flow control, terminal
1821 @cindex terminal flow control
1822 @comment termios.h
1823 @comment POSIX.1
1824 @deftypefun int tcflow (int @var{filedes}, int @var{action})
1825 The @code{tcflow} function is used to perform operations relating to
1826 XON/XOFF flow control on the terminal file specified by @var{filedes}.
1828 The @var{action} argument specifies what operation to perform, and can
1829 be one of the following values:
1831 @table @code
1832 @vindex TCOOFF
1833 @item TCOOFF
1834 Suspend transmission of output.
1836 @vindex TCOON
1837 @item TCOON
1838 Restart transmission of output.
1840 @vindex TCIOFF
1841 @item TCIOFF
1842 Transmit a STOP character.
1844 @vindex TCION
1845 @item TCION
1846 Transmit a START character.
1847 @end table
1849 For more information about the STOP and START characters, see @ref{Special
1850 Characters}.
1852 The return value is normally zero.  In the event of an error, a value
1853 of @math{-1} is returned.  The following @code{errno} error conditions
1854 are defined for this function:
1856 @table @code
1857 @vindex EBADF
1858 @item EBADF
1859 The @var{filedes} is not a valid file descriptor.
1861 @vindex ENOTTY
1862 @item ENOTTY
1863 The @var{filedes} is not associated with a terminal device.
1865 @vindex EINVAL
1866 @item EINVAL
1867 A bad value was supplied as the @var{action} argument.
1868 @end table
1869 @end deftypefun
1871 @node Noncanon Example
1872 @section Noncanonical Mode Example
1874 Here is an example program that shows how you can set up a terminal
1875 device to read single characters in noncanonical input mode, without
1876 echo.
1878 @smallexample
1879 @include termios.c.texi
1880 @end smallexample
1882 This program is careful to restore the original terminal modes before
1883 exiting or terminating with a signal.  It uses the @code{atexit}
1884 function (@pxref{Cleanups on Exit}) to make sure this is done
1885 by @code{exit}.
1887 @ignore
1888 @c !!!! the example doesn't handle any signals!
1889 The signals handled in the example are the ones that typically occur due
1890 to actions of the user.  It might be desirable to handle other signals
1891 such as SIGSEGV that can result from bugs in the program.
1892 @end ignore
1894 The shell is supposed to take care of resetting the terminal modes when
1895 a process is stopped or continued; see @ref{Job Control}.  But some
1896 existing shells do not actually do this, so you may wish to establish
1897 handlers for job control signals that reset terminal modes.  The above
1898 example does so.
1901 @node Pseudo-Terminals
1902 @section Pseudo-Terminals
1903 @cindex pseudo-terminals
1905 A @dfn{pseudo-terminal} is a special interprocess communication channel
1906 that acts like a terminal.  One end of the channel is called the
1907 @dfn{master} side or @dfn{master pseudo-terminal device}, the other side
1908 is called the @dfn{slave} side.  Data written to the master side is
1909 received by the slave side as if it was the result of a user typing at
1910 an ordinary terminal, and data written to the slave side is sent to the
1911 master side as if it was written on an ordinary terminal.
1913 Pseudo terminals are the way programs like @code{xterm} and @code{emacs}
1914 implement their terminal emulation functionality.
1916 @menu
1917 * Allocation::             Allocating a pseudo terminal.
1918 * Pseudo-Terminal Pairs::  How to open both sides of a
1919                             pseudo-terminal in a single operation.
1920 @end menu
1922 @node Allocation
1923 @subsection Allocating Pseudo-Terminals
1924 @cindex allocating pseudo-terminals
1926 @pindex stdlib.h
1927 This subsection describes functions for allocating a pseudo-terminal,
1928 and for making this pseudo-terminal available for actual use.  These
1929 functions are declared in the header file @file{stdlib.h}.
1931 @comment stdlib.h
1932 @comment GNU
1933 @deftypefun int getpt (void)
1934 The @code{getpt} function returns a new file descriptor for the next
1935 available master pseudo-terminal.  The normal return value from
1936 @code{getpt} is a non-negative integer file descriptor.  In the case of
1937 an error, a value of @math{-1} is returned instead.  The following
1938 @code{errno} conditions are defined for this function:
1940 @table @code
1941 @item ENOENT
1942 There are no free master pseudo-terminals available.
1943 @end table
1945 This function is a GNU extension.
1946 @end deftypefun
1948 @comment stdlib.h
1949 @comment SVID, XPG4.2
1950 @deftypefun int grantpt (int @var{filedes})
1951 The @code{grantpt} function changes the ownership and access permission
1952 of the slave pseudo-terminal device corresponding to the master
1953 pseudo-terminal device associated with the file descriptor
1954 @var{filedes}.  The owner is set from the real user ID of the calling
1955 process (@pxref{Process Persona}), and the group is set to a special
1956 group (typically @dfn{tty}) or from the real group ID of the calling
1957 process.  The access permission is set such that the file is both
1958 readable and writable by the owner and only writable by the group.
1960 On some systems this function is implemented by invoking a special
1961 @code{setuid} root program (@pxref{How Change Persona}).  As a
1962 consequence, installing a signal handler for the @code{SIGCHLD} signal
1963 (@pxref{Job Control Signals}) may interfere with a call to
1964 @code{grantpt}.
1966 The normal return value from @code{grantpt} is @math{0}; a value of
1967 @math{-1} is returned in case of failure.  The following @code{errno}
1968 error conditions are defined for this function:
1970 @table @code
1971 @item EBADF
1972 The @var{filedes} argument is not a valid file descriptor.
1974 @item EINVAL
1975 The @var{filedes} argument is not associated with a master pseudo-terminal
1976 device.
1978 @item EACCES
1979 The slave pseudo-terminal device corresponding to the master associated
1980 with @var{filedes} could not be accessed.
1981 @end table
1983 @end deftypefun
1985 @comment stdlib.h
1986 @comment SVID, XPG4.2
1987 @deftypefun int unlockpt (int @var{filedes})
1988 The @code{unlockpt} function unlocks the slave pseudo-terminal device
1989 corresponding to the master pseudo-terminal device associated with the
1990 file descriptor @var{filedes}.  On many systems, the slave can only be
1991 opened after unlocking, so portable applications should always call
1992 @code{unlockpt} before trying to open the slave.
1994 The normal return value from @code{unlockpt} is @math{0}; a value of
1995 @math{-1} is returned in case of failure.  The following @code{errno}
1996 error conditions are defined for this function:
1998 @table @code
1999 @item EBADF
2000 The @var{filedes} argument is not a valid file descriptor.
2002 @item EINVAL
2003 The @var{filedes} argument is not associated with a master pseudo-terminal
2004 device.
2005 @end table
2006 @end deftypefun
2008 @comment stdlib.h
2009 @comment SVID, XPG4.2
2010 @deftypefun {char *} ptsname (int @var{filedes})
2011 If the file descriptor @var{filedes} is associated with a
2012 master pseudo-terminal device, the @code{ptsname} function returns a
2013 pointer to a statically-allocated, null-terminated string containing the
2014 file name of the associated slave pseudo-terminal file.  This string
2015 might be overwritten by subsequent calls to @code{ptsname}.
2016 @end deftypefun
2018 @comment stdlib.h
2019 @comment GNU
2020 @deftypefun int ptsname_r (int @var{filedes}, char *@var{buf}, size_t @var{len})
2021 The @code{ptsname_r} function is similar to the @code{ptsname} function
2022 except that it places its result into the user-specified buffer starting
2023 at @var{buf} with length @var{len}.
2025 This function is a GNU extension.
2026 @end deftypefun
2028 @strong{Portability Note:} On @w{System V} derived systems, the file
2029 returned by the @code{ptsname} and @code{ptsname_r} functions may be
2030 STREAMS-based, and therefore require additional processing after opening
2031 before it actually behaves as a pseudo terminal.
2032 @c FIXME: xref STREAMS
2034 Typical usage of these functions is illustrated by the following example:
2035 @smallexample
2037 open_pty_pair (int *amaster, int *aslave)
2039   int master, slave;
2040   char *name;
2042   master = getpt ();
2043   if (master < 0)
2044     return 0;
2046   if (grantpt (master) < 0 || unlockpt (master) < 0)
2047     goto close_master;
2048   name = ptsname (master);
2049   if (name == NULL)
2050     goto close_master;
2052   slave = open (name, O_RDWR);
2053   if (slave == -1)
2054     goto close_master;
2056   if (isastream (slave))
2057     @{
2058       if (ioctl (slave, I_PUSH, "ptem") < 0
2059           || ioctl (slave, I_PUSH, "ldterm") < 0)
2060         goto close_slave;
2061     @}
2063   *amaster = master;
2064   *aslave = slave;
2065   return 1;
2067 close_slave:
2068   close (slave);
2070 close_master:
2071   close (master);
2072   return 0;
2074 @end smallexample
2076 @node Pseudo-Terminal Pairs
2077 @subsection Opening a Pseudo-Terminal Pair
2078 @cindex opening a pseudo-terminal pair
2080 These functions, derived from BSD, are available in the separate
2081 @file{libutil} library, and declared in @file{pty.h}.
2083 @comment pty.h
2084 @comment BSD
2085 @deftypefun int openpty (int *@var{amaster}, int *@var{aslave}, char *@var{name}, const struct termios *@var{termp}, const struct winsize *@var{winp})
2086 This function allocates and opens a pseudo-terminal pair, returning the
2087 file descriptor for the master in @var{*amaster}, and the file
2088 descriptor for the slave in @var{*aslave}.  If the argument @var{name}
2089 is not a null pointer, the file name of the slave pseudo-terminal
2090 device is stored in @code{*name}.  If @var{termp} is not a null pointer,
2091 the terminal attributes of the slave are set to the ones specified in
2092 the structure that @var{termp} points to (@pxref{Terminal Modes}).
2093 Likewise, if the @var{winp} is not a null pointer, the screen size of
2094 the slave is set to the values specified in the structure that
2095 @var{winp} points to.
2097 The normal return value from @code{openpty} is @math{0}; a value of
2098 @math{-1} is returned in case of failure.  The following @code{errno}
2099 conditions are defined for this function:
2101 @table @code
2102 @item ENOENT
2103 There are no free pseudo-terminal pairs available.
2104 @end table
2106 @strong{Warning:} Using the @code{openpty} function with @var{name} not
2107 set to @code{NULL} is @strong{very dangerous} because it provides no
2108 protection against overflowing the string @var{name}.  You should use
2109 the @code{ttyname} function on the file descriptor returned in
2110 @var{*slave} to find out the file name of the slave pseudo-terminal
2111 device instead.
2112 @end deftypefun
2114 @comment pty.h
2115 @comment BSD
2116 @deftypefun int forkpty (int *@var{amaster}, char *@var{name}, const struct termios *@var{termp}, const struct winsize *@var{winp})
2117 This function is similar to the @code{openpty} function, but in
2118 addition, forks a new process (@pxref{Creating a Process}) and makes the
2119 newly opened slave pseudo-terminal device the controlling terminal
2120 (@pxref{Controlling Terminal}) for the child process.
2122 If the operation is successful, there are then both parent and child
2123 processes and both see @code{forkpty} return, but with different values:
2124 it returns a value of @math{0} in the child process and returns the child's
2125 process ID in the parent process.
2127 If the allocation of a pseudo-terminal pair or the process creation
2128 failed, @code{forkpty} returns a value of @math{-1} in the parent
2129 process.
2131 @strong{Warning:} The @code{forkpty} function has the same problems with
2132 respect to the @var{name} argument as @code{openpty}.
2133 @end deftypefun