tcsetattr.c 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
  1. /* Copyright (C) 1991-2026 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. <https://www.gnu.org/licenses/>. */
  14. #include <errno.h>
  15. #include <stddef.h>
  16. #include <termios.h>
  17. static int bad_speed (speed_t speed);
  18. /* Set the state of FD to *TERMIOS_P. */
  19. int
  20. __tcsetattr (int fd, int optional_actions, const struct termios *termios_p)
  21. {
  22. if (fd < 0)
  23. {
  24. __set_errno (EBADF);
  25. return -1;
  26. }
  27. if (termios_p == NULL)
  28. {
  29. __set_errno (EINVAL);
  30. return -1;
  31. }
  32. switch (optional_actions)
  33. {
  34. case TCSANOW:
  35. case TCSADRAIN:
  36. case TCSAFLUSH:
  37. break;
  38. default:
  39. __set_errno (EINVAL);
  40. return -1;
  41. }
  42. if (bad_speed(termios_p->__ospeed)
  43. || bad_speed(termios_p->__ispeed == 0
  44. ? termios_p->__ospeed : termios_p->__ispeed))
  45. {
  46. __set_errno (EINVAL);
  47. return -1;
  48. }
  49. __set_errno (ENOSYS);
  50. return -1;
  51. }
  52. libc_hidden_def (__tcsetattr)
  53. weak_alias (__tcsetattr, tcsetattr)
  54. /* Strychnine checking. */
  55. static int
  56. bad_speed (speed_t speed)
  57. {
  58. switch (speed)
  59. {
  60. case B0:
  61. case B50:
  62. case B75:
  63. case B110:
  64. case B134:
  65. case B150:
  66. case B200:
  67. case B300:
  68. case B600:
  69. case B1200:
  70. case B1800:
  71. case B2400:
  72. case B4800:
  73. case B9600:
  74. case B19200:
  75. case B38400:
  76. case B57600:
  77. case B115200:
  78. case B230400:
  79. case B460800:
  80. case B500000:
  81. case B576000:
  82. case B921600:
  83. case B1000000:
  84. case B1152000:
  85. case B1500000:
  86. case B2000000:
  87. case B2500000:
  88. case B3000000:
  89. case B3500000:
  90. case B4000000:
  91. return 0;
  92. default:
  93. return 1;
  94. }
  95. }
  96. stub_warning (tcsetattr)