speed.c 3.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394
  1. /* `struct termios' speed frobnication functions. Linux version.
  2. Copyright (C) 1991,1992,1993,1995,1996,1997,1998,2000,2002,2003
  3. Free Software Foundation, Inc.
  4. This file is part of the GNU C Library.
  5. The GNU C Library is free software; you can redistribute it and/or
  6. modify it under the terms of the GNU Lesser General Public
  7. License as published by the Free Software Foundation; either
  8. version 2.1 of the License, or (at your option) any later version.
  9. The GNU C Library is distributed in the hope that it will be useful,
  10. but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  12. Lesser General Public License for more details.
  13. You should have received a copy of the GNU Lesser General Public
  14. License along with the GNU C Library; if not, see
  15. <http://www.gnu.org/licenses/>. */
  16. #include <stddef.h>
  17. #include <errno.h>
  18. #include <termios.h>
  19. /* This is a gross hack around a kernel bug. If the cfsetispeed functions
  20. is called with the SPEED argument set to zero this means use the same
  21. speed as for output. But we don't have independent input and output
  22. speeds and therefore cannot record this.
  23. We use an unused bit in the `c_iflag' field to keep track of this
  24. use of `cfsetispeed'. The value here must correspond to the one used
  25. in `tcsetattr.c'. */
  26. #define IBAUD0 020000000000
  27. /* Return the output baud rate stored in *TERMIOS_P. */
  28. speed_t cfgetospeed (const struct termios *termios_p)
  29. {
  30. return termios_p->c_cflag & (CBAUD | CBAUDEX);
  31. }
  32. /* Return the input baud rate stored in *TERMIOS_P.
  33. Although for Linux there is no difference between input and output
  34. speed, the numerical 0 is a special case for the input baud rate. It
  35. should set the input baud rate to the output baud rate. */
  36. speed_t cfgetispeed (const struct termios *termios_p)
  37. {
  38. return ((termios_p->c_iflag & IBAUD0)
  39. ? 0 : termios_p->c_cflag & (CBAUD | CBAUDEX));
  40. }
  41. /* Set the output baud rate stored in *TERMIOS_P to SPEED. */
  42. int cfsetospeed (struct termios *termios_p, speed_t speed)
  43. {
  44. if ((speed & ~CBAUD) != 0
  45. && (speed < B57600 || speed > __MAX_BAUD))
  46. {
  47. __set_errno (EINVAL);
  48. return -1;
  49. }
  50. termios_p->c_cflag &= ~(CBAUD | CBAUDEX);
  51. termios_p->c_cflag |= speed;
  52. return 0;
  53. }
  54. libc_hidden_def (cfsetospeed)
  55. /* Set the input baud rate stored in *TERMIOS_P to SPEED.
  56. Although for Linux there is no difference between input and output
  57. speed, the numerical 0 is a special case for the input baud rate. It
  58. should set the input baud rate to the output baud rate. */
  59. int cfsetispeed (struct termios *termios_p, speed_t speed)
  60. {
  61. if ((speed & ~CBAUD) != 0
  62. && (speed < B57600 || speed > __MAX_BAUD))
  63. {
  64. __set_errno (EINVAL);
  65. return -1;
  66. }
  67. if (speed == 0)
  68. termios_p->c_iflag |= IBAUD0;
  69. else
  70. {
  71. termios_p->c_iflag &= ~IBAUD0;
  72. termios_p->c_cflag &= ~(CBAUD | CBAUDEX);
  73. termios_p->c_cflag |= speed;
  74. }
  75. return 0;
  76. }
  77. libc_hidden_def (cfsetispeed)