speed.c 3.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697
  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, write to the Free
  15. Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
  16. 02111-1307 USA. */
  17. #include <stddef.h>
  18. #include <errno.h>
  19. #include <termios.h>
  20. libc_hidden_proto(cfsetispeed)
  21. libc_hidden_proto(cfsetospeed)
  22. /* This is a gross hack around a kernel bug. If the cfsetispeed functions
  23. is called with the SPEED argument set to zero this means use the same
  24. speed as for output. But we don't have independent input and output
  25. speeds and therefore cannot record this.
  26. We use an unused bit in the `c_iflag' field to keep track of this
  27. use of `cfsetispeed'. The value here must correspond to the one used
  28. in `tcsetattr.c'. */
  29. #define IBAUD0 020000000000
  30. /* Return the output baud rate stored in *TERMIOS_P. */
  31. speed_t cfgetospeed (const struct termios *termios_p)
  32. {
  33. return termios_p->c_cflag & (CBAUD | CBAUDEX);
  34. }
  35. /* Return the input baud rate stored in *TERMIOS_P.
  36. Although for Linux there is no difference between input and output
  37. speed, the numerical 0 is a special case for the input baud rate. It
  38. should set the input baud rate to the output baud rate. */
  39. speed_t cfgetispeed (const struct termios *termios_p)
  40. {
  41. return ((termios_p->c_iflag & IBAUD0)
  42. ? 0 : termios_p->c_cflag & (CBAUD | CBAUDEX));
  43. }
  44. /* Set the output baud rate stored in *TERMIOS_P to SPEED. */
  45. int cfsetospeed (struct termios *termios_p, speed_t speed)
  46. {
  47. if ((speed & ~CBAUD) != 0
  48. && (speed < B57600 || speed > __MAX_BAUD))
  49. {
  50. __set_errno (EINVAL);
  51. return -1;
  52. }
  53. termios_p->c_cflag &= ~(CBAUD | CBAUDEX);
  54. termios_p->c_cflag |= speed;
  55. return 0;
  56. }
  57. libc_hidden_def (cfsetospeed)
  58. /* Set the input baud rate stored in *TERMIOS_P to SPEED.
  59. Although for Linux there is no difference between input and output
  60. speed, the numerical 0 is a special case for the input baud rate. It
  61. should set the input baud rate to the output baud rate. */
  62. int cfsetispeed (struct termios *termios_p, speed_t speed)
  63. {
  64. if ((speed & ~CBAUD) != 0
  65. && (speed < B57600 || speed > __MAX_BAUD))
  66. {
  67. __set_errno (EINVAL);
  68. return -1;
  69. }
  70. if (speed == 0)
  71. termios_p->c_iflag |= IBAUD0;
  72. else
  73. {
  74. termios_p->c_iflag &= ~IBAUD0;
  75. termios_p->c_cflag &= ~(CBAUD | CBAUDEX);
  76. termios_p->c_cflag |= speed;
  77. }
  78. return 0;
  79. }
  80. libc_hidden_def (cfsetispeed)