_fopen.c 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206
  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. || (((open_mode & ~((__mode_t) fname_or_mode)) & O_APPEND)
  106. && fcntl(filedes, F_SETFL, O_APPEND)) /* Need O_APPEND. */
  107. ) {
  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. i = errno; /* Preserve errno against isatty call. */
  140. stream->__modeflags |= (isatty(stream->__filedes) * __FLAG_LBF);
  141. __set_errno(i);
  142. if (!stream->__bufstart) {
  143. if ((stream->__bufstart = malloc(BUFSIZ)) != NULL) {
  144. stream->__bufend = stream->__bufstart + BUFSIZ;
  145. stream->__modeflags |= __FLAG_FREEBUF;
  146. } else {
  147. # if __STDIO_BUILTIN_BUF_SIZE > 0
  148. stream->__bufstart = stream->__builtinbuf;
  149. stream->__bufend = stream->__builtinbuf + sizeof(stream->__builtinbuf);
  150. # else /* __STDIO_BUILTIN_BUF_SIZE > 0 */
  151. stream->__bufend = stream->__bufstart;
  152. # endif /* __STDIO_BUILTIN_BUF_SIZE > 0 */
  153. }
  154. }
  155. __STDIO_STREAM_DISABLE_GETC(stream);
  156. __STDIO_STREAM_DISABLE_PUTC(stream);
  157. __STDIO_STREAM_INIT_BUFREAD_BUFPOS(stream);
  158. #endif
  159. __STDIO_STREAM_RESET_GCS(stream);
  160. #ifdef __UCLIBC_HAS_WCHAR__
  161. stream->__ungot_width[0] = 0;
  162. #endif
  163. #ifdef __STDIO_MBSTATE
  164. __INIT_MBSTATE(&(stream->__state));
  165. #endif
  166. #ifdef __UCLIBC_HAS_THREADS__
  167. /* Even in the freopen case, we reset the user locking flag. */
  168. stream->__user_locking = _stdio_user_locking;
  169. /* __stdio_init_mutex(&stream->__lock); */
  170. #endif
  171. #ifdef __STDIO_HAS_OPENLIST
  172. __STDIO_THREADLOCK_OPENLIST;
  173. stream->__nextopen = _stdio_openlist; /* New files are inserted at */
  174. _stdio_openlist = stream; /* the head of the list. */
  175. __STDIO_THREADUNLOCK_OPENLIST;
  176. #endif
  177. __STDIO_STREAM_VALIDATE(stream);
  178. return stream;
  179. }