tcsetattr.c 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  1. /* Copyright (C) 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 Lesser General Public
  5. License as published by the Free Software Foundation; either
  6. version 2.1 of the 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. Lesser General Public License for more details.
  11. You should have received a copy of the GNU Lesser General Public
  12. License along with the GNU C Library; if not, see
  13. <http://www.gnu.org/licenses/>. */
  14. #include <errno.h>
  15. #include <string.h>
  16. #include <termios.h>
  17. #include <sys/ioctl.h>
  18. #include <sys/types.h>
  19. /* The difference here is that the termios structure used in the
  20. kernel is not the same as we use in the libc. Therefore we must
  21. translate it here. */
  22. #include "kernel_termios.h"
  23. /* This is a gross hack around a kernel bug. If the cfsetispeed functions
  24. is called with the SPEED argument set to zero this means use the same
  25. speed as for output. But we don't have independent input and output
  26. speeds and therefore cannot record this.
  27. We use an unused bit in the `c_iflag' field to keep track of this
  28. use of `cfsetispeed'. The value here must correspond to the one used
  29. in `speed.c'. */
  30. #if !defined _HAVE_C_ISPEED || !defined _HAVE_C_OSPEED
  31. # define IBAUD0 020000000000
  32. #else
  33. /* If we have separate values for input and output speed don't bother
  34. with this. Define the value as zero so the compiler sees we don't
  35. have to do the AND below. */
  36. # define IBAUD0 0
  37. #endif
  38. /* Set the state of FD to *TERMIOS_P. */
  39. int tcsetattr (int fd, int optional_actions, const struct termios *termios_p)
  40. {
  41. struct __kernel_termios k_termios;
  42. unsigned long int cmd;
  43. int retval;
  44. switch (optional_actions)
  45. {
  46. case TCSANOW:
  47. cmd = TCSETS;
  48. break;
  49. case TCSADRAIN:
  50. cmd = TCSETSW;
  51. break;
  52. case TCSAFLUSH:
  53. cmd = TCSETSF;
  54. break;
  55. default:
  56. __set_errno (EINVAL);
  57. return -1;
  58. }
  59. k_termios.c_iflag = termios_p->c_iflag & ~IBAUD0;
  60. k_termios.c_oflag = termios_p->c_oflag;
  61. k_termios.c_cflag = termios_p->c_cflag;
  62. k_termios.c_lflag = termios_p->c_lflag;
  63. k_termios.c_line = termios_p->c_line;
  64. #ifdef _HAVE_C_ISPEED
  65. k_termios.c_ispeed = termios_p->c_ispeed;
  66. #endif
  67. #ifdef _HAVE_C_OSPEED
  68. k_termios.c_ospeed = termios_p->c_ospeed;
  69. #endif
  70. memcpy (&k_termios.c_cc[0], &termios_p->c_cc[0],
  71. __KERNEL_NCCS * sizeof (cc_t));
  72. retval = ioctl (fd, cmd, &k_termios);
  73. if (retval == 0 && cmd == TCSETS)
  74. {
  75. /* The Linux kernel has a bug which silently ignore the invalid
  76. c_cflag on pty. We have to check it here. */
  77. int save = errno;
  78. retval = ioctl (fd, TCGETS, &k_termios);
  79. if (retval)
  80. {
  81. /* We cannot verify if the setting is ok. We don't return
  82. an error (?). */
  83. __set_errno (save);
  84. retval = 0;
  85. }
  86. else if ((termios_p->c_cflag & (PARENB | CREAD))
  87. != (k_termios.c_cflag & (PARENB | CREAD))
  88. || ((termios_p->c_cflag & CSIZE)
  89. && ((termios_p->c_cflag & CSIZE)
  90. != (k_termios.c_cflag & CSIZE))))
  91. {
  92. /* It looks like the Linux kernel silently changed the
  93. PARENB/CREAD/CSIZE bits in c_cflag. Report it as an
  94. error. */
  95. __set_errno (EINVAL);
  96. retval = -1;
  97. }
  98. }
  99. return retval;
  100. }
  101. libc_hidden_def(tcsetattr)