_fopen.c 6.8 KB

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