iofdopen.c 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167
  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 <stdlib.h>
  23. #include "libioP.h"
  24. #include <fcntl.h>
  25. #include <shlib-compat.h>
  26. FILE *
  27. _IO_new_fdopen (int fd, const char *mode)
  28. {
  29. int read_write;
  30. struct locked_FILE
  31. {
  32. struct _IO_FILE_plus fp;
  33. #ifdef _IO_MTSAFE_IO
  34. _IO_lock_t lock;
  35. #endif
  36. struct _IO_wide_data wd;
  37. } *new_f;
  38. int i;
  39. int use_mmap = 0;
  40. /* Decide whether we modify the offset of the file we attach to and seek to
  41. the end of file. We only do this if the mode is 'a' and if the file
  42. descriptor did not have O_APPEND in its flags already. */
  43. bool do_seek = false;
  44. switch (*mode)
  45. {
  46. case 'r':
  47. read_write = _IO_NO_WRITES;
  48. break;
  49. case 'w':
  50. read_write = _IO_NO_READS;
  51. break;
  52. case 'a':
  53. read_write = _IO_NO_READS|_IO_IS_APPENDING;
  54. break;
  55. default:
  56. __set_errno (EINVAL);
  57. return NULL;
  58. }
  59. for (i = 1; i < 5; ++i)
  60. {
  61. switch (*++mode)
  62. {
  63. case '\0':
  64. break;
  65. case '+':
  66. read_write &= _IO_IS_APPENDING;
  67. break;
  68. case 'm':
  69. use_mmap = 1;
  70. continue;
  71. case 'x':
  72. case 'b':
  73. default:
  74. /* Ignore */
  75. continue;
  76. }
  77. break;
  78. }
  79. int fd_flags = __fcntl (fd, F_GETFL);
  80. if (fd_flags == -1)
  81. return NULL;
  82. if (((fd_flags & O_ACCMODE) == O_RDONLY && !(read_write & _IO_NO_WRITES))
  83. || ((fd_flags & O_ACCMODE) == O_WRONLY && !(read_write & _IO_NO_READS)))
  84. {
  85. __set_errno (EINVAL);
  86. return NULL;
  87. }
  88. /* The May 93 draft of P1003.4/D14.1 (redesignated as 1003.1b)
  89. [System Application Program Interface (API) Amendment 1:
  90. Realtime Extensions], Rationale B.8.3.3
  91. Open a Stream on a File Descriptor says:
  92. Although not explicitly required by POSIX.1, a good
  93. implementation of append ("a") mode would cause the
  94. O_APPEND flag to be set.
  95. (Historical implementations [such as Solaris2] do a one-time
  96. seek in fdopen.)
  97. However, we do not turn O_APPEND off if the mode is "w" (even
  98. though that would seem consistent) because that would be more
  99. likely to break historical programs.
  100. */
  101. if ((read_write & _IO_IS_APPENDING) && !(fd_flags & O_APPEND))
  102. {
  103. do_seek = true;
  104. if (__fcntl (fd, F_SETFL, fd_flags | O_APPEND) == -1)
  105. return NULL;
  106. }
  107. new_f = (struct locked_FILE *) malloc (sizeof (struct locked_FILE));
  108. if (new_f == NULL)
  109. return NULL;
  110. #ifdef _IO_MTSAFE_IO
  111. new_f->fp.file._lock = &new_f->lock;
  112. #endif
  113. _IO_no_init (&new_f->fp.file, 0, 0, &new_f->wd,
  114. #if _G_HAVE_MMAP
  115. (use_mmap && (read_write & _IO_NO_WRITES))
  116. ? &_IO_wfile_jumps_maybe_mmap :
  117. #endif
  118. &_IO_wfile_jumps);
  119. _IO_JUMPS (&new_f->fp) =
  120. #if _G_HAVE_MMAP
  121. (use_mmap && (read_write & _IO_NO_WRITES)) ? &_IO_file_jumps_maybe_mmap :
  122. #endif
  123. &_IO_file_jumps;
  124. _IO_new_file_init_internal (&new_f->fp);
  125. /* We only need to record the fd because _IO_file_init_internal will
  126. have unset the offset. It is important to unset the cached
  127. offset because the real offset in the file could change between
  128. now and when the handle is activated and we would then mislead
  129. ftell into believing that we have a valid offset. */
  130. new_f->fp.file._fileno = fd;
  131. new_f->fp.file._flags &= ~_IO_DELETE_DONT_CLOSE;
  132. _IO_mask_flags (&new_f->fp.file, read_write,
  133. _IO_NO_READS+_IO_NO_WRITES+_IO_IS_APPENDING);
  134. /* For append mode, set the file offset to the end of the file if we added
  135. O_APPEND to the file descriptor flags. Don't update the offset cache
  136. though, since the file handle is not active. */
  137. if (do_seek && ((read_write & (_IO_IS_APPENDING | _IO_NO_READS))
  138. == (_IO_IS_APPENDING | _IO_NO_READS)))
  139. {
  140. off64_t new_pos = _IO_SYSSEEK (&new_f->fp.file, 0, _IO_seek_end);
  141. if (new_pos == _IO_pos_BAD && errno != ESPIPE)
  142. return NULL;
  143. }
  144. return &new_f->fp.file;
  145. }
  146. libc_hidden_ver (_IO_new_fdopen, _IO_fdopen)
  147. strong_alias (_IO_new_fdopen, __new_fdopen)
  148. versioned_symbol (libc, _IO_new_fdopen, _IO_fdopen, GLIBC_2_1);
  149. versioned_symbol (libc, __new_fdopen, fdopen, GLIBC_2_1);