iofopen.c 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596
  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 "libioP.h"
  23. #include <fcntl.h>
  24. #include <stdlib.h>
  25. #include <stddef.h>
  26. #include <shlib-compat.h>
  27. FILE *
  28. __fopen_maybe_mmap (FILE *fp)
  29. {
  30. #if _G_HAVE_MMAP
  31. if ((fp->_flags2 & _IO_FLAGS2_MMAP) && (fp->_flags & _IO_NO_WRITES))
  32. {
  33. /* Since this is read-only, we might be able to mmap the contents
  34. directly. We delay the decision until the first read attempt by
  35. giving it a jump table containing functions that choose mmap or
  36. vanilla file operations and reset the jump table accordingly. */
  37. if (fp->_mode <= 0)
  38. _IO_JUMPS_FILE_plus (fp) = &_IO_file_jumps_maybe_mmap;
  39. else
  40. _IO_JUMPS_FILE_plus (fp) = &_IO_wfile_jumps_maybe_mmap;
  41. fp->_wide_data->_wide_vtable = &_IO_wfile_jumps_maybe_mmap;
  42. }
  43. #endif
  44. return fp;
  45. }
  46. FILE *
  47. __fopen_internal (const char *filename, const char *mode, int is32)
  48. {
  49. struct locked_FILE
  50. {
  51. struct _IO_FILE_plus fp;
  52. #ifdef _IO_MTSAFE_IO
  53. _IO_lock_t lock;
  54. #endif
  55. struct _IO_wide_data wd;
  56. } *new_f = (struct locked_FILE *) malloc (sizeof (struct locked_FILE));
  57. if (new_f == NULL)
  58. return NULL;
  59. #ifdef _IO_MTSAFE_IO
  60. new_f->fp.file._lock = &new_f->lock;
  61. #endif
  62. _IO_no_init (&new_f->fp.file, 0, 0, &new_f->wd, &_IO_wfile_jumps);
  63. _IO_JUMPS (&new_f->fp) = &_IO_file_jumps;
  64. _IO_new_file_init_internal (&new_f->fp);
  65. if (_IO_file_fopen ((FILE *) new_f, filename, mode, is32) != NULL)
  66. return __fopen_maybe_mmap (&new_f->fp.file);
  67. _IO_un_link (&new_f->fp);
  68. free (new_f);
  69. return NULL;
  70. }
  71. FILE *
  72. _IO_new_fopen (const char *filename, const char *mode)
  73. {
  74. return __fopen_internal (filename, mode, 1);
  75. }
  76. strong_alias (_IO_new_fopen, __new_fopen)
  77. versioned_symbol (libc, _IO_new_fopen, _IO_fopen, GLIBC_2_1);
  78. versioned_symbol (libc, __new_fopen, fopen, GLIBC_2_1);
  79. # if !defined O_LARGEFILE || O_LARGEFILE == 0
  80. weak_alias (_IO_new_fopen, _IO_fopen64)
  81. weak_alias (_IO_new_fopen, fopen64)
  82. # endif