termios.c 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321
  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. #include <errno.h>
  21. #include <stddef.h>
  22. #include <sys/ioctl.h>
  23. #include <termios.h>
  24. #include <unistd.h>
  25. #ifdef L_isatty
  26. /* Return 1 if FD is a terminal, 0 if not. */
  27. #include "kernel_termios.h"
  28. int isatty(int fd)
  29. {
  30. struct __kernel_termios k_term;
  31. /*
  32. * When ioctl returns -1 we want to return 0 and
  33. * when ioctl returns 0 we want to return 1.
  34. * Incrementing is cheaper (size & speed) than a test.
  35. */
  36. return ioctl(fd, TCGETS, &k_term) + 1;
  37. }
  38. #endif
  39. #ifdef L_tcdrain
  40. /* Wait for pending output to be written on FD. */
  41. int tcdrain (int fd)
  42. {
  43. /* With an argument of 1, TCSBRK waits for the output to drain. */
  44. return ioctl(fd, TCSBRK, 1);
  45. }
  46. #endif
  47. #ifdef L_tcflow
  48. /* Suspend or restart transmission on FD. */
  49. int tcflow ( int fd, int action)
  50. {
  51. return ioctl(fd, TCXONC, action);
  52. }
  53. #endif
  54. #ifdef L_tcflush
  55. /* Flush pending data on FD. */
  56. int tcflush ( int fd, int queue_selector)
  57. {
  58. return ioctl(fd, TCFLSH, queue_selector);
  59. }
  60. #endif
  61. #ifdef L_tcsendbreak
  62. /* Send zero bits on FD. */
  63. int tcsendbreak( int fd, int duration)
  64. {
  65. /*
  66. * The break lasts 0.25 to 0.5 seconds if DURATION is zero, and an
  67. * implementation-defined period if DURATION is nonzero. We define a
  68. * positive DURATION to be number of milliseconds to break.
  69. */
  70. if (duration <= 0)
  71. return ioctl(fd, TCSBRK, 0);
  72. /*
  73. * ioctl can't send a break of any other duration for us. This could be
  74. * changed to use trickery (e.g. lower speed and send a '\0') to send
  75. * the break, but for now just return an error.
  76. */
  77. __set_errno(EINVAL);
  78. return -1;
  79. }
  80. #endif
  81. #ifdef L_tcsetpgrp
  82. /* Set the foreground process group ID of FD set PGRP_ID. */
  83. int tcsetpgrp ( int fd, pid_t pgrp_id)
  84. {
  85. return ioctl (fd, TIOCSPGRP, &pgrp_id);
  86. }
  87. #endif
  88. #ifdef L_tcgetpgrp
  89. /* Return the foreground process group ID of FD. */
  90. pid_t tcgetpgrp ( int fd)
  91. {
  92. int pgrp;
  93. if (ioctl (fd, TIOCGPGRP, &pgrp) < 0)
  94. return (pid_t) -1;
  95. return (pid_t) pgrp;
  96. }
  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 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. #endif
  140. #ifdef L_cfsetispeed
  141. /* Set the input baud rate stored in *TERMIOS_P to SPEED.
  142. * Although for Linux there is no difference between input and output
  143. * speed, the numerical 0 is a special case for the input baud rate. It
  144. * should set the input baud rate to the output baud rate. */
  145. int cfsetispeed ( struct termios *termios_p, speed_t speed)
  146. {
  147. if ((speed & ~CBAUD) != 0
  148. && (speed < B57600 || speed > B460800))
  149. {
  150. __set_errno(EINVAL);
  151. return -1;
  152. }
  153. if (speed == 0)
  154. termios_p->c_iflag |= IBAUD0;
  155. else
  156. {
  157. termios_p->c_iflag &= ~IBAUD0;
  158. termios_p->c_cflag &= ~(CBAUD | CBAUDEX);
  159. termios_p->c_cflag |= speed;
  160. }
  161. return 0;
  162. }
  163. #endif
  164. #ifdef L_tcspeed
  165. struct speed_struct
  166. {
  167. speed_t value;
  168. speed_t internal;
  169. };
  170. static const struct speed_struct speeds[] =
  171. {
  172. #ifdef B0
  173. { 0, B0 },
  174. #endif
  175. #ifdef B50
  176. { 50, B50 },
  177. #endif
  178. #ifdef B75
  179. { 75, B75 },
  180. #endif
  181. #ifdef B110
  182. { 110, B110 },
  183. #endif
  184. #ifdef B134
  185. { 134, B134 },
  186. #endif
  187. #ifdef B150
  188. { 150, B150 },
  189. #endif
  190. #ifdef B200
  191. { 200, B200 },
  192. #endif
  193. #ifdef B300
  194. { 300, B300 },
  195. #endif
  196. #ifdef B600
  197. { 600, B600 },
  198. #endif
  199. #ifdef B1200
  200. { 1200, B1200 },
  201. #endif
  202. #ifdef B1200
  203. { 1200, B1200 },
  204. #endif
  205. #ifdef B1800
  206. { 1800, B1800 },
  207. #endif
  208. #ifdef B2400
  209. { 2400, B2400 },
  210. #endif
  211. #ifdef B4800
  212. { 4800, B4800 },
  213. #endif
  214. #ifdef B9600
  215. { 9600, B9600 },
  216. #endif
  217. #ifdef B19200
  218. { 19200, B19200 },
  219. #endif
  220. #ifdef B38400
  221. { 38400, B38400 },
  222. #endif
  223. #ifdef B57600
  224. { 57600, B57600 },
  225. #endif
  226. #ifdef B76800
  227. { 76800, B76800 },
  228. #endif
  229. #ifdef B115200
  230. { 115200, B115200 },
  231. #endif
  232. #ifdef B153600
  233. { 153600, B153600 },
  234. #endif
  235. #ifdef B230400
  236. { 230400, B230400 },
  237. #endif
  238. #ifdef B307200
  239. { 307200, B307200 },
  240. #endif
  241. #ifdef B460800
  242. { 460800, B460800 },
  243. #endif
  244. };
  245. /* Set both the input and output baud rates stored in *TERMIOS_P to SPEED. */
  246. int cfsetspeed (struct termios *termios_p, speed_t speed)
  247. {
  248. size_t cnt;
  249. for (cnt = 0; cnt < sizeof (speeds) / sizeof (speeds[0]); ++cnt)
  250. if (speed == speeds[cnt].internal)
  251. {
  252. cfsetispeed (termios_p, speed);
  253. cfsetospeed (termios_p, speed);
  254. return 0;
  255. }
  256. else if (speed == speeds[cnt].value)
  257. {
  258. cfsetispeed (termios_p, speeds[cnt].internal);
  259. cfsetospeed (termios_p, speeds[cnt].internal);
  260. return 0;
  261. }
  262. __set_errno (EINVAL);
  263. return -1;
  264. }
  265. #endif
  266. #ifdef L_cfmakeraw
  267. /* Copyright (C) 1992, 1996, 1997 Free Software Foundation, Inc.
  268. This file is part of the GNU C Library.
  269. */
  270. #include <termios.h>
  271. /* Set *T to indicate raw mode. */
  272. void
  273. cfmakeraw (t)
  274. struct termios *t;
  275. {
  276. t->c_iflag &= ~(IGNBRK|BRKINT|PARMRK|ISTRIP|INLCR|IGNCR|ICRNL|IXON);
  277. t->c_oflag &= ~OPOST;
  278. t->c_lflag &= ~(ECHO|ECHONL|ICANON|ISIG|IEXTEN);
  279. t->c_cflag &= ~(CSIZE|PARENB);
  280. t->c_cflag |= CS8;
  281. t->c_cc[VMIN] = 1; /* read returns when one char is available. */
  282. t->c_cc[VTIME] = 0;
  283. }
  284. #endif