freopen.c 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  1. /* Copyright (C) 1993-2019 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. <http://www.gnu.org/licenses/>.
  14. As a special exception, if you link the code in this file with
  15. files compiled with a GNU compiler to produce an executable,
  16. that does not cause the resulting executable to be covered by
  17. the GNU Lesser General Public License. This exception does not
  18. however invalidate any other reasons why the executable file
  19. might be covered by the GNU Lesser General Public License.
  20. This exception applies to code released by its copyright holders
  21. in files containing the exception. */
  22. #include <stdio.h>
  23. #include <fcntl.h>
  24. #include <stdlib.h>
  25. #include <unistd.h>
  26. #include <libioP.h>
  27. #include <fd_to_filename.h>
  28. #include <shlib-compat.h>
  29. FILE *
  30. freopen (const char *filename, const char *mode, FILE *fp)
  31. {
  32. FILE *result = NULL;
  33. char fdfilename[FD_TO_FILENAME_SIZE];
  34. CHECK_FILE (fp, NULL);
  35. _IO_acquire_lock (fp);
  36. /* First flush the stream (failure should be ignored). */
  37. _IO_SYNC (fp);
  38. if (!(fp->_flags & _IO_IS_FILEBUF))
  39. goto end;
  40. int fd = _IO_fileno (fp);
  41. const char *gfilename
  42. = filename != NULL ? filename : fd_to_filename (fd, fdfilename);
  43. fp->_flags2 |= _IO_FLAGS2_NOCLOSE;
  44. #if SHLIB_COMPAT (libc, GLIBC_2_0, GLIBC_2_1)
  45. if (&_IO_stdin_used == NULL)
  46. {
  47. /* If the shared C library is used by the application binary which
  48. was linked against the older version of libio, we just use the
  49. older one even for internal use to avoid trouble since a pointer
  50. to the old libio may be passed into shared C library and wind
  51. up here. */
  52. _IO_old_file_close_it (fp);
  53. _IO_JUMPS_FILE_plus (fp) = &_IO_old_file_jumps;
  54. result = _IO_old_file_fopen (fp, gfilename, mode);
  55. }
  56. else
  57. #endif
  58. {
  59. _IO_file_close_it (fp);
  60. _IO_JUMPS_FILE_plus (fp) = &_IO_file_jumps;
  61. if (_IO_vtable_offset (fp) == 0 && fp->_wide_data != NULL)
  62. fp->_wide_data->_wide_vtable = &_IO_wfile_jumps;
  63. result = _IO_file_fopen (fp, gfilename, mode, 1);
  64. if (result != NULL)
  65. result = __fopen_maybe_mmap (result);
  66. }
  67. fp->_flags2 &= ~_IO_FLAGS2_NOCLOSE;
  68. if (result != NULL)
  69. {
  70. /* unbound stream orientation */
  71. result->_mode = 0;
  72. if (fd != -1 && _IO_fileno (result) != fd)
  73. {
  74. /* At this point we have both file descriptors already allocated,
  75. so __dup3 will not fail with EBADF, EINVAL, or EMFILE. But
  76. we still need to check for EINVAL and, due Linux internal
  77. implementation, EBUSY. It is because on how it internally opens
  78. the file by splitting the buffer allocation operation and VFS
  79. opening (a dup operation may run when a file is still pending
  80. 'install' on VFS). */
  81. if (__dup3 (_IO_fileno (result), fd,
  82. (result->_flags2 & _IO_FLAGS2_CLOEXEC) != 0
  83. ? O_CLOEXEC : 0) == -1)
  84. {
  85. _IO_file_close_it (result);
  86. result = NULL;
  87. goto end;
  88. }
  89. __close (_IO_fileno (result));
  90. _IO_fileno (result) = fd;
  91. }
  92. }
  93. else if (fd != -1)
  94. __close (fd);
  95. end:
  96. _IO_release_lock (fp);
  97. return result;
  98. }