_fopen.c 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226
  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. #ifdef __UCLIBC_MJN3_ONLY__
  57. #warning CONSIDER: Implement glibc ccs option to bind a codeset?
  58. #warning CONSIDER: Implement glibc mmap option for readonly files?
  59. #warning CONSIDER: Implement a text mode using custom read/write funcs?
  60. #endif
  61. #if defined(__UCLIBC_HAS_FOPEN_EXCLUSIVE_MODE__) || defined(__UCLIBC_HAS_FOPEN_LARGEFILE_MODE__)
  62. while (*++mode) {
  63. # ifdef __UCLIBC_HAS_FOPEN_EXCLUSIVE_MODE__
  64. if (*mode == 'x') { /* Open exclusive (a glibc extension). */
  65. open_mode |= O_EXCL;
  66. continue;
  67. }
  68. # endif
  69. # ifdef __UCLIBC_HAS_FOPEN_LARGEFILE_MODE__
  70. if (*mode == 'F') { /* Open as large file (uClibc extension). */
  71. open_mode |= O_LARGEFILE;
  72. continue;
  73. }
  74. # endif
  75. }
  76. #endif
  77. if (!stream) { /* Need to allocate a FILE (not freopen). */
  78. if ((stream = malloc(sizeof(FILE))) == NULL) {
  79. return stream;
  80. }
  81. stream->__modeflags = __FLAG_FREEFILE;
  82. #ifdef __STDIO_BUFFERS
  83. stream->__bufstart = NULL; /* We allocate a buffer below. */
  84. #endif
  85. #ifdef __UCLIBC_HAS_THREADS__
  86. /* We only initialize the mutex in the non-freopen case. */
  87. /* stream->__user_locking = _stdio_user_locking; */
  88. __stdio_init_mutex(&stream->__lock);
  89. #endif
  90. }
  91. #ifdef __UCLIBC_MJN3_ONLY__
  92. #warning TODO: Verify fdopen append behavior of glibc.
  93. #endif
  94. if (filedes >= 0) { /* Handle fdopen trickery. */
  95. stream->__filedes = filedes;
  96. /* NOTE: it is insufficient to just check R/W/RW agreement.
  97. * We must also check largefile compatibility if applicable.
  98. * Also, if append mode is desired for fdopen but O_APPEND isn't
  99. * currently set, then set it as recommended by SUSv3. However,
  100. * if append mode is not specified for fdopen but O_APPEND is set,
  101. * leave it set (glibc compat). */
  102. i = (open_mode & (O_ACCMODE|O_LARGEFILE)) + 1;
  103. /* NOTE: fopencookie needs changing if the basic check changes! */
  104. if ((i & ((int)fname_or_mode + 1)) != i) /* Basic agreement? */
  105. goto DO_EINVAL;
  106. if ((open_mode & ~(__mode_t)fname_or_mode) & O_APPEND) {
  107. if (fcntl(filedes, F_SETFL, O_APPEND)) /* Need O_APPEND. */
  108. goto DO_EINVAL;
  109. }
  110. /* For later... to reflect largefile setting in stream flags. */
  111. __STDIO_WHEN_LFS( open_mode |= (((__mode_t) fname_or_mode)
  112. & O_LARGEFILE) );
  113. } else {
  114. __STDIO_WHEN_LFS( if (filedes < -1) open_mode |= O_LARGEFILE );
  115. if ((stream->__filedes = open(((const char *) fname_or_mode),
  116. open_mode, 0666)) < 0) {
  117. goto FREE_STREAM;
  118. }
  119. }
  120. stream->__modeflags &= __FLAG_FREEFILE;
  121. /* stream->__modeflags &= ~(__FLAG_READONLY|__FLAG_WRITEONLY); */
  122. stream->__modeflags |= /* Note: Makes assumptions about flag vals! */
  123. #if (O_APPEND != __FLAG_APPEND) || ((O_LARGEFILE != __FLAG_LARGEFILE) && (O_LARGEFILE != 0))
  124. # if (O_APPEND != __FLAG_APPEND)
  125. ((open_mode & O_APPEND) ? __FLAG_APPEND : 0) |
  126. # else
  127. (open_mode & O_APPEND) |
  128. # endif
  129. # if (O_LARGEFILE != __FLAG_LARGEFILE) && (O_LARGEFILE != 0)
  130. ((open_mode & O_LARGEFILE) ? __FLAG_LARGEFILE : 0) |
  131. # else
  132. (open_mode & O_LARGEFILE) |
  133. # endif
  134. #else
  135. (open_mode & (O_APPEND|O_LARGEFILE)) | /* i386 linux and elks */
  136. #endif
  137. ((((open_mode & O_ACCMODE) + 1) ^ 0x03) * __FLAG_WRITEONLY);
  138. #ifdef __STDIO_BUFFERS
  139. if (stream->__filedes != INT_MAX) {
  140. /* NB: fopencookie uses bogus filedes == INT_MAX,
  141. * avoiding isatty() in that case.
  142. */
  143. i = errno; /* preserve errno against isatty call. */
  144. if (isatty(stream->__filedes))
  145. stream->__modeflags |= __FLAG_LBF;
  146. __set_errno(i);
  147. }
  148. if (!stream->__bufstart) {
  149. if ((stream->__bufstart = malloc(BUFSIZ)) != NULL) {
  150. stream->__bufend = stream->__bufstart + BUFSIZ;
  151. stream->__modeflags |= __FLAG_FREEBUF;
  152. } else {
  153. # if __STDIO_BUILTIN_BUF_SIZE > 0
  154. stream->__bufstart = stream->__builtinbuf;
  155. stream->__bufend = stream->__builtinbuf + sizeof(stream->__builtinbuf);
  156. # else /* __STDIO_BUILTIN_BUF_SIZE > 0 */
  157. stream->__bufend = stream->__bufstart;
  158. # endif /* __STDIO_BUILTIN_BUF_SIZE > 0 */
  159. }
  160. }
  161. __STDIO_STREAM_DISABLE_GETC(stream);
  162. __STDIO_STREAM_DISABLE_PUTC(stream);
  163. __STDIO_STREAM_INIT_BUFREAD_BUFPOS(stream);
  164. #endif
  165. __STDIO_STREAM_RESET_GCS(stream);
  166. #ifdef __UCLIBC_HAS_WCHAR__
  167. stream->__ungot_width[0] = 0;
  168. #endif
  169. #ifdef __STDIO_MBSTATE
  170. __INIT_MBSTATE(&(stream->__state));
  171. #endif
  172. #ifdef __UCLIBC_HAS_THREADS__
  173. /* Even in the freopen case, we reset the user locking flag. */
  174. stream->__user_locking = _stdio_user_locking;
  175. /* __stdio_init_mutex(&stream->__lock); */
  176. #endif
  177. #ifdef __STDIO_HAS_OPENLIST
  178. #if defined(__UCLIBC_HAS_THREADS__) && defined(__STDIO_BUFFERS)
  179. if (!(stream->__modeflags & __FLAG_FREEFILE))
  180. {
  181. /* An freopen call so the file was never removed from the list. */
  182. }
  183. else
  184. #endif
  185. {
  186. /* We have to lock the del mutex in case another thread wants to fclose()
  187. * the last file. */
  188. __STDIO_THREADLOCK_OPENLIST_DEL;
  189. __STDIO_THREADLOCK_OPENLIST_ADD;
  190. stream->__nextopen = _stdio_openlist; /* New files are inserted at */
  191. _stdio_openlist = stream; /* the head of the list. */
  192. __STDIO_THREADUNLOCK_OPENLIST_ADD;
  193. __STDIO_THREADUNLOCK_OPENLIST_DEL;
  194. }
  195. #endif
  196. __STDIO_STREAM_VALIDATE(stream);
  197. return stream;
  198. }