oldiofdopen.c 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123
  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 <shlib-compat.h>
  23. #if SHLIB_COMPAT (libc, GLIBC_2_0, GLIBC_2_1)
  24. #define _IO_USE_OLD_IO_FILE
  25. #include <stdlib.h>
  26. #include "libioP.h"
  27. #include <fcntl.h>
  28. FILE *
  29. attribute_compat_text_section
  30. _IO_old_fdopen (int fd, const char *mode)
  31. {
  32. int read_write;
  33. int posix_mode = 0;
  34. struct locked_FILE
  35. {
  36. struct _IO_FILE_complete_plus fp;
  37. #ifdef _IO_MTSAFE_IO
  38. _IO_lock_t lock;
  39. #endif
  40. } *new_f;
  41. int fd_flags;
  42. switch (*mode++)
  43. {
  44. case 'r':
  45. read_write = _IO_NO_WRITES;
  46. break;
  47. case 'w':
  48. read_write = _IO_NO_READS;
  49. break;
  50. case 'a':
  51. posix_mode = O_APPEND;
  52. read_write = _IO_NO_READS|_IO_IS_APPENDING;
  53. break;
  54. default:
  55. __set_errno (EINVAL);
  56. return NULL;
  57. }
  58. if (mode[0] == '+' || (mode[0] == 'b' && mode[1] == '+'))
  59. read_write &= _IO_IS_APPENDING;
  60. fd_flags = __fcntl (fd, F_GETFL);
  61. if (fd_flags == -1
  62. || ((fd_flags & O_ACCMODE) == O_RDONLY && !(read_write & _IO_NO_WRITES))
  63. || ((fd_flags & O_ACCMODE) == O_WRONLY && !(read_write & _IO_NO_READS)))
  64. return NULL;
  65. /* The May 93 draft of P1003.4/D14.1 (redesignated as 1003.1b)
  66. [System Application Program Interface (API) Amendment 1:
  67. Realtime Extensions], Rationale B.8.3.3
  68. Open a Stream on a File Descriptor says:
  69. Although not explicitly required by POSIX.1, a good
  70. implementation of append ("a") mode would cause the
  71. O_APPEND flag to be set.
  72. (Historical implementations [such as Solaris2] do a one-time
  73. seek in fdopen.)
  74. However, we do not turn O_APPEND off if the mode is "w" (even
  75. though that would seem consistent) because that would be more
  76. likely to break historical programs.
  77. */
  78. if ((posix_mode & O_APPEND) && !(fd_flags & O_APPEND))
  79. {
  80. if (__fcntl (fd, F_SETFL, fd_flags | O_APPEND) == -1)
  81. return NULL;
  82. }
  83. new_f = (struct locked_FILE *) malloc (sizeof (struct locked_FILE));
  84. if (new_f == NULL)
  85. return NULL;
  86. #ifdef _IO_MTSAFE_IO
  87. new_f->fp.file._file._lock = &new_f->lock;
  88. #endif
  89. _IO_old_init (&new_f->fp.file._file, 0);
  90. _IO_JUMPS_FILE_plus (&new_f->fp) = &_IO_old_file_jumps;
  91. _IO_old_file_init_internal ((struct _IO_FILE_plus *) &new_f->fp);
  92. if (_IO_old_file_attach (&new_f->fp.file._file, fd) == NULL)
  93. {
  94. _IO_un_link ((struct _IO_FILE_plus *) &new_f->fp);
  95. free (new_f);
  96. return NULL;
  97. }
  98. new_f->fp.file._file._flags &= ~_IO_DELETE_DONT_CLOSE;
  99. _IO_mask_flags (&new_f->fp.file._file, read_write,
  100. _IO_NO_READS+_IO_NO_WRITES+_IO_IS_APPENDING);
  101. return (FILE *) &new_f->fp;
  102. }
  103. strong_alias (_IO_old_fdopen, __old_fdopen)
  104. compat_symbol (libc, _IO_old_fdopen, _IO_fdopen, GLIBC_2_0);
  105. compat_symbol (libc, __old_fdopen, fdopen, GLIBC_2_0);
  106. #endif