tcsetattr.c 3.6 KB

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