tcsetattr.c 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081
  1. /* Copyright (C) 1993-2019 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. #include <sysdep.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. #define IBAUD0 020000000000
  32. /* Set the state of FD to *TERMIOS_P. */
  33. int
  34. __tcsetattr (int fd, int optional_actions, const struct termios *termios_p)
  35. {
  36. struct __kernel_termios k_termios;
  37. unsigned long int cmd;
  38. switch (optional_actions)
  39. {
  40. case TCSANOW:
  41. cmd = TCSETS;
  42. break;
  43. case TCSADRAIN:
  44. cmd = TCSETSW;
  45. break;
  46. case TCSAFLUSH:
  47. cmd = TCSETSF;
  48. break;
  49. default:
  50. return INLINE_SYSCALL_ERROR_RETURN_VALUE (EINVAL);
  51. }
  52. k_termios.c_iflag = termios_p->c_iflag & ~IBAUD0;
  53. k_termios.c_oflag = termios_p->c_oflag;
  54. k_termios.c_cflag = termios_p->c_cflag;
  55. k_termios.c_lflag = termios_p->c_lflag;
  56. k_termios.c_line = termios_p->c_line;
  57. #if _HAVE_C_ISPEED && _HAVE_STRUCT_TERMIOS_C_ISPEED
  58. k_termios.c_ispeed = termios_p->c_ispeed;
  59. #endif
  60. #if _HAVE_C_OSPEED && _HAVE_STRUCT_TERMIOS_C_OSPEED
  61. k_termios.c_ospeed = termios_p->c_ospeed;
  62. #endif
  63. memcpy (&k_termios.c_cc[0], &termios_p->c_cc[0],
  64. __KERNEL_NCCS * sizeof (cc_t));
  65. return INLINE_SYSCALL (ioctl, 3, fd, cmd, &k_termios);
  66. }
  67. weak_alias (__tcsetattr, tcsetattr)
  68. libc_hidden_def (tcsetattr)