tst-ptsname.c 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106
  1. /* Test for ptsname/ptsname_r.
  2. Copyright (C) 2014-2026 Free Software Foundation, Inc.
  3. This file is part of the GNU C Library.
  4. The GNU C Library is free software; you can redistribute it and/or
  5. modify it under the terms of the GNU Lesser General Public
  6. License as published by the Free Software Foundation; either
  7. version 2.1 of the License, or (at your option) any later version.
  8. The GNU C Library is distributed in the hope that it will be useful,
  9. but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  11. Lesser General Public License for more details.
  12. You should have received a copy of the GNU Lesser General Public
  13. License along with the GNU C Library; if not, see
  14. <https://www.gnu.org/licenses/>. */
  15. #include <errno.h>
  16. #include <fcntl.h>
  17. #include <stdio.h>
  18. #include <stdlib.h>
  19. #include <string.h>
  20. #include <unistd.h>
  21. #define DEV_TTY "/dev/tty"
  22. #define PTSNAME_EINVAL "./ptsname-einval"
  23. static int
  24. do_single_test (int fd, char *buf, size_t buflen, int expected_err)
  25. {
  26. int ret = ptsname_r (fd, buf, buflen);
  27. int err = errno;
  28. if (expected_err == 0)
  29. {
  30. if (ret != 0)
  31. {
  32. printf ("ptsname_r: expected: return = 0\n");
  33. printf (" got: return = %d, errno = %d (%s)\n",
  34. ret, err, strerror (err));
  35. return 1;
  36. }
  37. }
  38. else
  39. {
  40. if (ret == 0 || errno != expected_err)
  41. {
  42. printf ("ptsname_r: expected: return = %d, errno = %d (%s)\n",
  43. -1, expected_err, strerror (expected_err));
  44. printf (" got: return = %d, errno = %d (%s)\n",
  45. ret, err, strerror (err));
  46. return 1;
  47. }
  48. }
  49. return 0;
  50. }
  51. static int
  52. do_test (void)
  53. {
  54. char buf[512];
  55. int result = 0;
  56. /* Tests with a real PTS master. */
  57. int fd = posix_openpt (O_RDWR);
  58. if (fd != -1)
  59. {
  60. result |= do_single_test (fd, buf, sizeof (buf), 0);
  61. result |= do_single_test (fd, buf, 1, ERANGE);
  62. close (fd);
  63. }
  64. else
  65. printf ("posix_openpt (O_RDWR) failed\nerrno %d (%s)\n",
  66. errno, strerror (errno));
  67. /* Test with a terminal device which is not a PTS master. */
  68. fd = open (DEV_TTY, O_RDONLY);
  69. if (fd != -1)
  70. {
  71. result |= do_single_test (fd, buf, sizeof (buf), ENOTTY);
  72. close (fd);
  73. }
  74. else
  75. printf ("open (\"%s\", O_RDWR) failed\nerrno %d (%s)\n",
  76. DEV_TTY, errno, strerror (errno));
  77. /* Test with a file. */
  78. fd = open (PTSNAME_EINVAL, O_RDWR | O_CREAT, 0600);
  79. if (fd != -1)
  80. {
  81. result |= do_single_test (fd, buf, sizeof (buf), ENOTTY);
  82. close (fd);
  83. unlink (PTSNAME_EINVAL);
  84. }
  85. else
  86. printf ("open (\"%s\", O_RDWR | OCREAT) failed\nerrno %d (%s)\n",
  87. PTSNAME_EINVAL, errno, strerror (errno));
  88. return result;
  89. }
  90. #define TEST_FUNCTION do_test ()
  91. #include "../test-skeleton.c"