speed.c 3.1 KB

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