termios.c 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326
  1. /* Copyright (C) 1992, 1993, 1996, 1997, 1998 Free Software Foundation, Inc.
  2. This file is part of the GNU C Library.
  3. The GNU C Library is free software; you can redistribute it and/or
  4. modify it under the terms of the GNU Library General Public License as
  5. published by the Free Software Foundation; either version 2 of the
  6. License, or (at your option) any later version.
  7. The GNU C Library is distributed in the hope that it will be useful,
  8. but WITHOUT ANY WARRANTY; without even the implied warranty of
  9. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  10. Library General Public License for more details.
  11. You should have received a copy of the GNU Library General Public
  12. License along with the GNU C Library; see the file COPYING.LIB. If not,
  13. write to the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
  14. Boston, MA 02111-1307, USA.
  15. About the only thing remaining here fromthe original Linux-8086 C library
  16. version by Robert de Bath <robert@mayday.compulink.co.uk>, is the general
  17. layout. All else has been recently stolen from GNU libc, since that was
  18. much more current.
  19. */
  20. #define tcgetattr __tcgetattr
  21. #include <errno.h>
  22. #include <stddef.h>
  23. #include <sys/ioctl.h>
  24. #include <sys/types.h>
  25. #include <unistd.h>
  26. #include <termios.h>
  27. #ifdef L_isatty
  28. /* Return 1 if FD is a terminal, 0 if not. */
  29. int isatty(int fd)
  30. {
  31. struct termios term;
  32. return (tcgetattr (fd, &term) == 0);
  33. }
  34. #endif
  35. #ifdef L_tcdrain
  36. /* Wait for pending output to be written on FD. */
  37. int __libc_tcdrain (int fd)
  38. {
  39. /* With an argument of 1, TCSBRK waits for the output to drain. */
  40. return ioctl(fd, TCSBRK, 1);
  41. }
  42. weak_alias(__libc_tcdrain, tcdrain)
  43. #endif
  44. #ifdef L_tcflow
  45. /* Suspend or restart transmission on FD. */
  46. int tcflow ( int fd, int action)
  47. {
  48. return ioctl(fd, TCXONC, action);
  49. }
  50. #endif
  51. #ifdef L_tcflush
  52. /* Flush pending data on FD. */
  53. int tcflush ( int fd, int queue_selector)
  54. {
  55. return ioctl(fd, TCFLSH, queue_selector);
  56. }
  57. #endif
  58. #ifdef L_tcsendbreak
  59. /* Send zero bits on FD. */
  60. int tcsendbreak( int fd, int duration)
  61. {
  62. /* The break lasts 0.25 to 0.5 seconds if DURATION is zero,
  63. and an implementation-defined period if DURATION is nonzero.
  64. We define a positive DURATION to be number of milliseconds to break. */
  65. if (duration <= 0)
  66. return ioctl(fd, TCSBRK, 0);
  67. #ifdef TCSBRKP
  68. /* Probably Linux-specific: a positive third TCSBRKP ioctl argument is
  69. defined to be the number of 100ms units to break. */
  70. return ioctl(fd, TCSBRKP, (duration + 99) / 100);
  71. #else
  72. /* ioctl can't send a break of any other duration for us.
  73. This could be changed to use trickery (e.g. lower speed and
  74. send a '\0') to send the break, but for now just return an error. */
  75. __set_errno (EINVAL);
  76. return -1;
  77. #endif
  78. }
  79. #endif
  80. #ifdef L_tcsetpgrp
  81. /* Set the foreground process group ID of FD set PGRP_ID. */
  82. int tcsetpgrp ( int fd, pid_t pgrp_id)
  83. {
  84. return ioctl (fd, TIOCSPGRP, &pgrp_id);
  85. }
  86. #endif
  87. #ifdef L_tcgetpgrp
  88. /* Return the foreground process group ID of FD. */
  89. pid_t attribute_hidden __tcgetpgrp ( int fd)
  90. {
  91. int pgrp;
  92. if (ioctl (fd, TIOCGPGRP, &pgrp) < 0)
  93. return (pid_t) -1;
  94. return (pid_t) pgrp;
  95. }
  96. strong_alias(__tcgetpgrp,tcgetpgrp)
  97. #endif
  98. /* This is a gross hack around a kernel bug. If the cfsetispeed functions is
  99. * called with the SPEED argument set to zero this means use the same speed as
  100. * for output. But we don't have independent input and output speeds and
  101. * therefore cannot record this.
  102. *
  103. * We use an unused bit in the `c_iflag' field to keep track of this use of
  104. * `cfsetispeed'. The value here must correspond to the one used in
  105. * `tcsetattr.c'. */
  106. #define IBAUD0 020000000000
  107. #ifdef L_cfgetospeed
  108. /* Return the output baud rate stored in *TERMIOS_P. */
  109. speed_t cfgetospeed ( const struct termios *termios_p)
  110. {
  111. return termios_p->c_cflag & (CBAUD | CBAUDEX);
  112. }
  113. #endif
  114. #ifdef L_cfgetispeed
  115. /* Return the input baud rate stored in *TERMIOS_P.
  116. * Although for Linux there is no difference between input and output
  117. * speed, the numerical 0 is a special case for the input baud rate. It
  118. * should set the input baud rate to the output baud rate. */
  119. speed_t cfgetispeed (const struct termios *termios_p)
  120. {
  121. return ((termios_p->c_iflag & IBAUD0)
  122. ? 0 : termios_p->c_cflag & (CBAUD | CBAUDEX));
  123. }
  124. #endif
  125. #ifdef L_cfsetospeed
  126. /* Set the output baud rate stored in *TERMIOS_P to SPEED. */
  127. int attribute_hidden __cfsetospeed (struct termios *termios_p, speed_t speed)
  128. {
  129. if ((speed & ~CBAUD) != 0
  130. && (speed < B57600 || speed > B460800))
  131. {
  132. __set_errno(EINVAL);
  133. return -1;
  134. }
  135. termios_p->c_cflag &= ~(CBAUD | CBAUDEX);
  136. termios_p->c_cflag |= speed;
  137. return 0;
  138. }
  139. strong_alias(__cfsetospeed,cfsetospeed)
  140. #endif
  141. #ifdef L_cfsetispeed
  142. /* Set the input baud rate stored in *TERMIOS_P to SPEED.
  143. * Although for Linux there is no difference between input and output
  144. * speed, the numerical 0 is a special case for the input baud rate. It
  145. * should set the input baud rate to the output baud rate. */
  146. int attribute_hidden __cfsetispeed ( struct termios *termios_p, speed_t speed)
  147. {
  148. if ((speed & ~CBAUD) != 0
  149. && (speed < B57600 || speed > B460800))
  150. {
  151. __set_errno(EINVAL);
  152. return -1;
  153. }
  154. if (speed == 0)
  155. termios_p->c_iflag |= IBAUD0;
  156. else
  157. {
  158. termios_p->c_iflag &= ~IBAUD0;
  159. termios_p->c_cflag &= ~(CBAUD | CBAUDEX);
  160. termios_p->c_cflag |= speed;
  161. }
  162. return 0;
  163. }
  164. strong_alias(__cfsetispeed,cfsetispeed)
  165. #endif
  166. #ifdef L_cfsetspeed
  167. extern int __cfsetospeed (struct termios *__termios_p, speed_t __speed) __THROW attribute_hidden;
  168. extern int __cfsetispeed (struct termios *__termios_p, speed_t __speed) __THROW attribute_hidden;
  169. struct speed_struct
  170. {
  171. speed_t value;
  172. speed_t internal;
  173. };
  174. static const struct speed_struct speeds[] =
  175. {
  176. #ifdef B0
  177. { 0, B0 },
  178. #endif
  179. #ifdef B50
  180. { 50, B50 },
  181. #endif
  182. #ifdef B75
  183. { 75, B75 },
  184. #endif
  185. #ifdef B110
  186. { 110, B110 },
  187. #endif
  188. #ifdef B134
  189. { 134, B134 },
  190. #endif
  191. #ifdef B150
  192. { 150, B150 },
  193. #endif
  194. #ifdef B200
  195. { 200, B200 },
  196. #endif
  197. #ifdef B300
  198. { 300, B300 },
  199. #endif
  200. #ifdef B600
  201. { 600, B600 },
  202. #endif
  203. #ifdef B1200
  204. { 1200, B1200 },
  205. #endif
  206. #ifdef B1200
  207. { 1200, B1200 },
  208. #endif
  209. #ifdef B1800
  210. { 1800, B1800 },
  211. #endif
  212. #ifdef B2400
  213. { 2400, B2400 },
  214. #endif
  215. #ifdef B4800
  216. { 4800, B4800 },
  217. #endif
  218. #ifdef B9600
  219. { 9600, B9600 },
  220. #endif
  221. #ifdef B19200
  222. { 19200, B19200 },
  223. #endif
  224. #ifdef B38400
  225. { 38400, B38400 },
  226. #endif
  227. #ifdef B57600
  228. { 57600, B57600 },
  229. #endif
  230. #ifdef B76800
  231. { 76800, B76800 },
  232. #endif
  233. #ifdef B115200
  234. { 115200, B115200 },
  235. #endif
  236. #ifdef B153600
  237. { 153600, B153600 },
  238. #endif
  239. #ifdef B230400
  240. { 230400, B230400 },
  241. #endif
  242. #ifdef B307200
  243. { 307200, B307200 },
  244. #endif
  245. #ifdef B460800
  246. { 460800, B460800 },
  247. #endif
  248. };
  249. /* Set both the input and output baud rates stored in *TERMIOS_P to SPEED. */
  250. int cfsetspeed (struct termios *termios_p, speed_t speed)
  251. {
  252. size_t cnt;
  253. for (cnt = 0; cnt < sizeof (speeds) / sizeof (speeds[0]); ++cnt)
  254. if (speed == speeds[cnt].internal)
  255. {
  256. __cfsetispeed (termios_p, speed);
  257. __cfsetospeed (termios_p, speed);
  258. return 0;
  259. }
  260. else if (speed == speeds[cnt].value)
  261. {
  262. __cfsetispeed (termios_p, speeds[cnt].internal);
  263. __cfsetospeed (termios_p, speeds[cnt].internal);
  264. return 0;
  265. }
  266. __set_errno (EINVAL);
  267. return -1;
  268. }
  269. #endif
  270. #ifdef L_cfmakeraw
  271. /* Copyright (C) 1992, 1996, 1997 Free Software Foundation, Inc.
  272. This file is part of the GNU C Library.
  273. */
  274. #include <termios.h>
  275. /* Set *T to indicate raw mode. */
  276. void
  277. cfmakeraw (struct termios *t)
  278. {
  279. t->c_iflag &= ~(IGNBRK|BRKINT|PARMRK|ISTRIP|INLCR|IGNCR|ICRNL|IXON);
  280. t->c_oflag &= ~OPOST;
  281. t->c_lflag &= ~(ECHO|ECHONL|ICANON|ISIG|IEXTEN);
  282. t->c_cflag &= ~(CSIZE|PARENB);
  283. t->c_cflag |= CS8;
  284. t->c_cc[VMIN] = 1; /* read returns when one char is available. */
  285. t->c_cc[VTIME] = 0;
  286. }
  287. #endif