PageRenderTime 45ms CodeModel.GetById 20ms RepoModel.GetById 0ms app.codeStats 0ms

/tests/test-openpty.c

https://gitlab.com/changlimin/gnulib
C | 111 lines | 69 code | 15 blank | 27 comment | 10 complexity | ff09cde68980e0a01ecf24a8d3e3ccd2 MD5 | raw file
  1. /* Test of pty.h and openpty function.
  2. Copyright (C) 2009-2022 Free Software Foundation, Inc.
  3. This program is free software: you can redistribute it and/or modify
  4. it under the terms of the GNU General Public License as published by
  5. the Free Software Foundation, either version 3 of the License, or
  6. (at your option) any later version.
  7. This program 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
  10. GNU General Public License for more details.
  11. You should have received a copy of the GNU General Public License
  12. along with this program. If not, see <https://www.gnu.org/licenses/>. */
  13. /* Written by Simon Josefsson <simon@josefsson.org>, 2009
  14. and Bruno Haible <bruno@clisp.org>, 2010. */
  15. #include <config.h>
  16. #include <pty.h>
  17. #include "signature.h"
  18. SIGNATURE_CHECK (openpty, int, (int *, int *, char *, struct termios const *,
  19. struct winsize const *));
  20. #include <stdio.h>
  21. #include <string.h>
  22. #include <termios.h>
  23. #include <unistd.h>
  24. int
  25. main ()
  26. {
  27. {
  28. #ifndef _WIN32
  29. int master;
  30. int slave;
  31. /* Open a pseudo-terminal, as a master-slave pair. */
  32. {
  33. int res = openpty (&master, &slave, NULL, NULL, NULL);
  34. if (res != 0)
  35. {
  36. fprintf (stderr, "openpty returned %d\n", res);
  37. return 1;
  38. }
  39. }
  40. /* Set the terminal characteristics.
  41. On Linux or Mac OS X, they can be set on either the master or the slave;
  42. the effect is the same. But on Solaris, they have to be set on the
  43. master; tcgetattr on the slave fails. */
  44. {
  45. int tcfd = slave; /* You can try tcfd = master; here. */
  46. struct termios attributes;
  47. if (tcgetattr (tcfd, &attributes) < 0)
  48. {
  49. fprintf (stderr, "tcgetattr failed\n");
  50. return 1;
  51. }
  52. /* Enable canonical processing, including erase. */
  53. attributes.c_lflag |= ECHO | ICANON | ECHOE;
  54. attributes.c_cc[VERASE] = '\177';
  55. if (tcsetattr (tcfd, TCSANOW, &attributes) < 0)
  56. {
  57. fprintf (stderr, "tcsetattr failed\n");
  58. return 1;
  59. }
  60. }
  61. /* Write into the master side. */
  62. {
  63. static const char input[] = "Hello worst\177\177ld!\n";
  64. if (write (master, input, strlen (input)) < (int) strlen (input))
  65. {
  66. fprintf (stderr, "write failed\n");
  67. return 1;
  68. }
  69. }
  70. /* Read from the slave side. */
  71. {
  72. char buf[100];
  73. int res = read (slave, buf, sizeof (buf));
  74. static const char expected[] = "Hello world!\n";
  75. if (res < 0)
  76. {
  77. fprintf (stderr, "read failed\n");
  78. return 1;
  79. }
  80. if (!(res == strlen (expected)
  81. && memcmp (buf, expected, strlen (expected)) == 0))
  82. {
  83. fprintf (stderr, "read result unexpected\n");
  84. return 1;
  85. }
  86. }
  87. /* Close the master side before the slave side gets closed.
  88. This is necessary on Mac OS X 10.4.11. */
  89. close (master);
  90. #endif
  91. }
  92. return 0;
  93. }