tst-fopen-compat.c 2.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485
  1. /* Verify that fopen works with copy relocation on _IO_stderr_ in binaries
  2. linked with glibc 2.0.
  3. Copyright (C) 2024-2026 Free Software Foundation, Inc.
  4. This file is part of the GNU C Library.
  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. <https://www.gnu.org/licenses/>. */
  16. #include <shlib-compat.h>
  17. #if TEST_COMPAT (libc, GLIBC_2_0, GLIBC_2_1)
  18. # define _LIBC
  19. # define _IO_USE_OLD_IO_FILE
  20. # include <stdio.h>
  21. # include <string.h>
  22. # include <unistd.h>
  23. # include <limits.h>
  24. # include <sys/stat.h>
  25. # include <support/check.h>
  26. struct _IO_jump_t;
  27. struct _IO_FILE_plus
  28. {
  29. FILE file;
  30. const struct _IO_jump_t *vtable;
  31. };
  32. extern struct _IO_FILE_plus _IO_stderr_;
  33. compat_symbol_reference (libc, _IO_stderr_, _IO_stderr_, GLIBC_2_0);
  34. compat_symbol_reference (libc, fopen, fopen, GLIBC_2_0);
  35. compat_symbol_reference (libc, fclose, fclose, GLIBC_2_0);
  36. static int
  37. do_test (int argc, char *argv[])
  38. {
  39. static char filename[PATH_MAX + 1];
  40. struct stat st;
  41. char *name = NULL;
  42. int i;
  43. /* Try to trigger copy relocation. */
  44. TEST_VERIFY_EXIT (_IO_stderr_.file._fileno == STDERR_FILENO);
  45. for (i = 1; i < argc; i++)
  46. {
  47. name = argv[i];
  48. if (stat (name, &st) == 0)
  49. {
  50. TEST_VERIFY_EXIT (strlen (name) <= PATH_MAX);
  51. break;
  52. }
  53. }
  54. TEST_VERIFY_EXIT (name != NULL);
  55. strcpy (filename, name);
  56. FILE *fp = fopen (filename, "r");
  57. TEST_VERIFY_EXIT (strcmp (filename, name) == 0);
  58. TEST_VERIFY_EXIT (fp != NULL);
  59. TEST_VERIFY_EXIT (fclose (fp) == 0);
  60. return 0;
  61. }
  62. #else
  63. # include <support/test-driver.h>
  64. static int
  65. do_test (int argc, char *argv[])
  66. {
  67. return EXIT_UNSUPPORTED;
  68. }
  69. #endif
  70. #define TEST_FUNCTION_ARGV do_test
  71. #include <support/test-driver.c>