tst-freopen.c 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  1. /* Test freopen with mmap stdio.
  2. Copyright (C) 2002-2019 Free Software Foundation, Inc.
  3. This file is part of the GNU C Library.
  4. Contributed by Jakub Jelinek <jakub@redhat.com>, 2002.
  5. The GNU C Library is free software; you can redistribute it and/or
  6. modify it under the terms of the GNU Lesser General Public
  7. License as published by the Free Software Foundation; either
  8. version 2.1 of the License, or (at your option) any later version.
  9. The GNU C Library is distributed in the hope that it will be useful,
  10. but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  12. Lesser General Public License for more details.
  13. You should have received a copy of the GNU Lesser General Public
  14. License along with the GNU C Library; if not, see
  15. <http://www.gnu.org/licenses/>. */
  16. #include <stdio.h>
  17. #include <stdlib.h>
  18. #include <string.h>
  19. #include <unistd.h>
  20. #include <support/check.h>
  21. #include <support/temp_file.h>
  22. static int fd;
  23. static char *name;
  24. static void
  25. do_prepare (int argc, char *argv[])
  26. {
  27. fd = create_temp_file ("tst-freopen.", &name);
  28. TEST_VERIFY_EXIT (fd != -1);
  29. }
  30. #define PREPARE do_prepare
  31. /* Basic tests for freopen. */
  32. static void
  33. do_test_basic (void)
  34. {
  35. const char * const test = "Let's test freopen.\n";
  36. char temp[strlen (test) + 1];
  37. FILE *f = fdopen (fd, "w");
  38. if (f == NULL)
  39. FAIL_EXIT1 ("fdopen: %m");
  40. fputs (test, f);
  41. fclose (f);
  42. f = fopen (name, "r");
  43. if (f == NULL)
  44. FAIL_EXIT1 ("fopen: %m");
  45. if (fread (temp, 1, strlen (test), f) != strlen (test))
  46. FAIL_EXIT1 ("fread: %m");
  47. temp [strlen (test)] = '\0';
  48. if (strcmp (test, temp))
  49. FAIL_EXIT1 ("read different string than was written: (%s, %s)",
  50. test, temp);
  51. f = freopen (name, "r+", f);
  52. if (f == NULL)
  53. FAIL_EXIT1 ("freopen: %m");
  54. if (fseek (f, 0, SEEK_SET) != 0)
  55. FAIL_EXIT1 ("fseek: %m");
  56. if (fread (temp, 1, strlen (test), f) != strlen (test))
  57. FAIL_EXIT1 ("fread: %m");
  58. temp [strlen (test)] = '\0';
  59. if (strcmp (test, temp))
  60. FAIL_EXIT1 ("read different string than was written: (%s, %s)",
  61. test, temp);
  62. fclose (f);
  63. }
  64. /* Test for BZ#21398, where it tries to freopen stdio after the close
  65. of its file descriptor. */
  66. static void
  67. do_test_bz21398 (void)
  68. {
  69. (void) close (STDIN_FILENO);
  70. FILE *f = freopen (name, "r", stdin);
  71. if (f == NULL)
  72. FAIL_EXIT1 ("freopen: %m");
  73. TEST_VERIFY_EXIT (ferror (f) == 0);
  74. char buf[128];
  75. char *ret = fgets (buf, sizeof (buf), stdin);
  76. TEST_VERIFY_EXIT (ret != NULL);
  77. TEST_VERIFY_EXIT (ferror (f) == 0);
  78. }
  79. static int
  80. do_test (void)
  81. {
  82. do_test_basic ();
  83. do_test_bz21398 ();
  84. return 0;
  85. }
  86. #include <support/test-driver.c>