tcgetattr.c 2.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980
  1. /* Copyright (C) 1992-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 <unistd.h>
  18. #include <sys/ioctl.h>
  19. #include <sys/types.h>
  20. #include <sysdep.h>
  21. /* The difference here is that the termios structure used in the
  22. kernel is not the same as we use in the libc. Therefore we must
  23. translate it here. */
  24. #include <kernel_termios.h>
  25. /* Put the state of FD into *TERMIOS_P. */
  26. int
  27. __tcgetattr (int fd, struct termios *termios_p)
  28. {
  29. struct __kernel_termios k_termios;
  30. int retval;
  31. retval = INLINE_SYSCALL (ioctl, 3, fd, TCGETS, &k_termios);
  32. if (__glibc_likely (retval == 0))
  33. {
  34. termios_p->c_iflag = k_termios.c_iflag;
  35. termios_p->c_oflag = k_termios.c_oflag;
  36. termios_p->c_cflag = k_termios.c_cflag;
  37. termios_p->c_lflag = k_termios.c_lflag;
  38. termios_p->c_line = k_termios.c_line;
  39. #if _HAVE_STRUCT_TERMIOS_C_ISPEED
  40. # if _HAVE_C_ISPEED
  41. termios_p->c_ispeed = k_termios.c_ispeed;
  42. # else
  43. termios_p->c_ispeed = k_termios.c_cflag & (CBAUD | CBAUDEX);
  44. # endif
  45. #endif
  46. #if _HAVE_STRUCT_TERMIOS_C_OSPEED
  47. # if _HAVE_C_OSPEED
  48. termios_p->c_ospeed = k_termios.c_ospeed;
  49. # else
  50. termios_p->c_ospeed = k_termios.c_cflag & (CBAUD | CBAUDEX);
  51. # endif
  52. #endif
  53. if (sizeof (cc_t) == 1 || _POSIX_VDISABLE == 0
  54. || (unsigned char) _POSIX_VDISABLE == (unsigned char) -1)
  55. memset (__mempcpy (&termios_p->c_cc[0], &k_termios.c_cc[0],
  56. __KERNEL_NCCS * sizeof (cc_t)),
  57. _POSIX_VDISABLE, (NCCS - __KERNEL_NCCS) * sizeof (cc_t));
  58. else
  59. {
  60. memcpy (&termios_p->c_cc[0], &k_termios.c_cc[0],
  61. __KERNEL_NCCS * sizeof (cc_t));
  62. for (size_t cnt = __KERNEL_NCCS; cnt < NCCS; ++cnt)
  63. termios_p->c_cc[cnt] = _POSIX_VDISABLE;
  64. }
  65. }
  66. return retval;
  67. }
  68. libc_hidden_def (__tcgetattr)
  69. weak_alias (__tcgetattr, tcgetattr)