terminal.texi 84 KB

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