_fopen.c 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222
  1. /* Copyright (C) 2004 Manuel Novoa III <mjn3@codepoet.org>
  2. *
  3. * GNU Library General Public License (LGPL) version 2 or later.
  4. *
  5. * Dedicated to Toni. See uClibc/DEDICATION.mjn3 for details.
  6. */
  7. #include "_stdio.h"
  8. /*
  9. * Cases:
  10. * fopen64 : filename != NULL, stream == NULL, filedes == -2
  11. * fopen : filename != NULL, stream == NULL, filedes == -1
  12. * freopen : filename != NULL, stream != NULL, filedes == -1
  13. * fdopen : filename == NULL, stream == NULL, filedes valid
  14. *
  15. * fsfopen : filename != NULL, stream != NULL, filedes == -1
  16. */
  17. #if (O_ACCMODE != 3) || (O_RDONLY != 0) || (O_WRONLY != 1) || (O_RDWR != 2)
  18. #error Assumption violated - mode constants
  19. #endif
  20. #ifndef O_LARGEFILE
  21. #define O_LARGEFILE 0
  22. #endif
  23. /* Internal function -- reentrant (locks open file list) */
  24. FILE attribute_hidden *_stdio_fopen(intptr_t fname_or_mode,
  25. register const char * __restrict mode,
  26. register FILE * __restrict stream, int filedes)
  27. {
  28. __mode_t open_mode;
  29. int i;
  30. /* Parse the specified mode. */
  31. open_mode = O_RDONLY;
  32. if (*mode != 'r') { /* Not read... */
  33. open_mode = (O_WRONLY | O_CREAT | O_TRUNC);
  34. if (*mode != 'w') { /* Not write (create or truncate)... */
  35. open_mode = (O_WRONLY | O_CREAT | O_APPEND);
  36. if (*mode != 'a') { /* Not write (create or append)... */
  37. DO_EINVAL:
  38. __set_errno(EINVAL); /* So illegal mode. */
  39. if (stream) {
  40. FREE_STREAM:
  41. assert(!(stream->__modeflags & __FLAG_FREEBUF));
  42. __STDIO_STREAM_FREE_FILE(stream);
  43. }
  44. return NULL;
  45. }
  46. }
  47. }
  48. if ((mode[1] == 'b')) { /* Binary mode (NO-OP currently). */
  49. ++mode;
  50. }
  51. if (mode[1] == '+') { /* Read and Write. */
  52. ++mode;
  53. open_mode |= (O_RDONLY | O_WRONLY);
  54. open_mode += (O_RDWR - (O_RDONLY | O_WRONLY));
  55. }
  56. #if defined(__UCLIBC_HAS_FOPEN_EXCLUSIVE_MODE__) || defined(__UCLIBC_HAS_FOPEN_LARGEFILE_MODE__) || \
  57. defined(__UCLIBC_HAS_FOPEN_CLOSEEXEC_MODE__)
  58. while (*++mode) {
  59. # ifdef __UCLIBC_HAS_FOPEN_EXCLUSIVE_MODE__
  60. if (*mode == 'x') { /* Open exclusive (a glibc extension). */
  61. open_mode |= O_EXCL;
  62. continue;
  63. }
  64. # endif
  65. # ifdef __UCLIBC_HAS_FOPEN_LARGEFILE_MODE__
  66. if (*mode == 'F') { /* Open as large file (uClibc extension). */
  67. open_mode |= O_LARGEFILE;
  68. continue;
  69. }
  70. # endif
  71. # ifdef __UCLIBC_HAS_FOPEN_CLOSEEXEC_MODE__
  72. if (*mode == 'e') { /* Close on exec (a glibc extension). */
  73. open_mode |= O_CLOEXEC;
  74. continue;
  75. }
  76. # endif
  77. }
  78. #endif
  79. if (!stream) { /* Need to allocate a FILE (not freopen). */
  80. if ((stream = malloc(sizeof(FILE))) == NULL) {
  81. return stream;
  82. }
  83. stream->__modeflags = __FLAG_FREEFILE;
  84. #ifdef __STDIO_BUFFERS
  85. stream->__bufstart = NULL; /* We allocate a buffer below. */
  86. #endif
  87. #ifdef __UCLIBC_HAS_THREADS__
  88. /* We only initialize the mutex in the non-freopen case. */
  89. /* stream->__user_locking = _stdio_user_locking; */
  90. STDIO_INIT_MUTEX(stream->__lock);
  91. #endif
  92. }
  93. if (filedes >= 0) { /* Handle fdopen trickery. */
  94. stream->__filedes = filedes;
  95. /* NOTE: it is insufficient to just check R/W/RW agreement.
  96. * We must also check largefile compatibility if applicable.
  97. * Also, if append mode is desired for fdopen but O_APPEND isn't
  98. * currently set, then set it as recommended by SUSv3. However,
  99. * if append mode is not specified for fdopen but O_APPEND is set,
  100. * leave it set (glibc compat). */
  101. i = (open_mode & (O_ACCMODE|O_LARGEFILE)) + 1;
  102. /* NOTE: fopencookie needs changing if the basic check changes! */
  103. if ((i & ((int)fname_or_mode + 1)) != i) /* Basic agreement? */
  104. goto DO_EINVAL;
  105. if ((open_mode & ~(__mode_t)fname_or_mode) & O_APPEND) {
  106. if (fcntl(filedes, F_SETFL, O_APPEND)) /* Need O_APPEND. */
  107. goto DO_EINVAL;
  108. }
  109. /* For later... to reflect largefile setting in stream flags. */
  110. __STDIO_WHEN_LFS( open_mode |= (((__mode_t) fname_or_mode)
  111. & O_LARGEFILE) );
  112. } else {
  113. __STDIO_WHEN_LFS( if (filedes < -1) open_mode |= O_LARGEFILE );
  114. if ((stream->__filedes = open(((const char *) fname_or_mode),
  115. open_mode, 0666)) < 0) {
  116. goto FREE_STREAM;
  117. }
  118. }
  119. stream->__modeflags &= __FLAG_FREEFILE;
  120. /* stream->__modeflags &= ~(__FLAG_READONLY|__FLAG_WRITEONLY); */
  121. stream->__modeflags |= /* Note: Makes assumptions about flag vals! */
  122. #if (O_APPEND != __FLAG_APPEND) || ((O_LARGEFILE != __FLAG_LARGEFILE) && (O_LARGEFILE != 0))
  123. # if (O_APPEND != __FLAG_APPEND)
  124. ((open_mode & O_APPEND) ? __FLAG_APPEND : 0) |
  125. # else
  126. (open_mode & O_APPEND) |
  127. # endif
  128. # if (O_LARGEFILE != __FLAG_LARGEFILE) && (O_LARGEFILE != 0)
  129. ((open_mode & O_LARGEFILE) ? __FLAG_LARGEFILE : 0) |
  130. # else
  131. (open_mode & O_LARGEFILE) |
  132. # endif
  133. #else
  134. (open_mode & (O_APPEND|O_LARGEFILE)) | /* i386 linux and elks */
  135. #endif
  136. ((((open_mode & O_ACCMODE) + 1) ^ 0x03) * __FLAG_WRITEONLY);
  137. #ifdef __STDIO_BUFFERS
  138. if (stream->__filedes != INT_MAX) {
  139. /* NB: fopencookie uses bogus filedes == INT_MAX,
  140. * avoiding isatty() in that case.
  141. */
  142. i = errno; /* preserve errno against isatty call. */
  143. if (isatty(stream->__filedes))
  144. stream->__modeflags |= __FLAG_LBF;
  145. __set_errno(i);
  146. }
  147. if (!stream->__bufstart) {
  148. if ((stream->__bufstart = malloc(BUFSIZ)) != NULL) {
  149. stream->__bufend = stream->__bufstart + BUFSIZ;
  150. stream->__modeflags |= __FLAG_FREEBUF;
  151. } else {
  152. # if __STDIO_BUILTIN_BUF_SIZE > 0
  153. stream->__bufstart = stream->__builtinbuf;
  154. stream->__bufend = stream->__builtinbuf + sizeof(stream->__builtinbuf);
  155. # else /* __STDIO_BUILTIN_BUF_SIZE > 0 */
  156. stream->__bufend = stream->__bufstart;
  157. # endif /* __STDIO_BUILTIN_BUF_SIZE > 0 */
  158. }
  159. }
  160. __STDIO_STREAM_DISABLE_GETC(stream);
  161. __STDIO_STREAM_DISABLE_PUTC(stream);
  162. __STDIO_STREAM_INIT_BUFREAD_BUFPOS(stream);
  163. #endif
  164. #ifdef __UCLIBC_HAS_WCHAR__
  165. stream->__ungot_width[0] = 0;
  166. #endif
  167. #ifdef __STDIO_MBSTATE
  168. __INIT_MBSTATE(&(stream->__state));
  169. #endif
  170. #ifdef __UCLIBC_HAS_THREADS__
  171. /* Even in the freopen case, we reset the user locking flag. */
  172. stream->__user_locking = _stdio_user_locking;
  173. /* STDIO_INIT_MUTEX(stream->__lock); */
  174. #endif
  175. #ifdef __STDIO_HAS_OPENLIST
  176. #if defined(__UCLIBC_HAS_THREADS__) && defined(__STDIO_BUFFERS)
  177. if (!(stream->__modeflags & __FLAG_FREEFILE))
  178. {
  179. /* An freopen call so the file was never removed from the list. */
  180. }
  181. else
  182. #endif
  183. {
  184. /* We have to lock the del mutex in case another thread wants to fclose()
  185. * the last file. */
  186. __STDIO_THREADLOCK_OPENLIST_DEL;
  187. __STDIO_THREADLOCK_OPENLIST_ADD;
  188. stream->__nextopen = _stdio_openlist; /* New files are inserted at */
  189. _stdio_openlist = stream; /* the head of the list. */
  190. __STDIO_THREADUNLOCK_OPENLIST_ADD;
  191. __STDIO_THREADUNLOCK_OPENLIST_DEL;
  192. }
  193. #endif
  194. __STDIO_STREAM_VALIDATE(stream);
  195. return stream;
  196. }