_fopen.c 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208
  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. #define isatty __isatty
  8. #include "_stdio.h"
  9. /*
  10. * Cases:
  11. * fopen64 : filename != NULL, stream == NULL, filedes == -2
  12. * fopen : filename != NULL, stream == NULL, filedes == -1
  13. * freopen : filename != NULL, stream != NULL, filedes == -1
  14. * fdopen : filename == NULL, stream == NULL, filedes valid
  15. *
  16. * fsfopen : filename != NULL, stream != NULL, filedes == -1
  17. */
  18. #if (O_ACCMODE != 3) || (O_RDONLY != 0) || (O_WRONLY != 1) || (O_RDWR != 2)
  19. #error Assumption violated - mode constants
  20. #endif
  21. #ifndef O_LARGEFILE
  22. #define O_LARGEFILE 0
  23. #endif
  24. /* Internal function -- reentrant (locks open file list) */
  25. FILE attribute_hidden *_stdio_fopen(intptr_t fname_or_mode,
  26. register const char * __restrict mode,
  27. register FILE * __restrict stream, int filedes)
  28. {
  29. __mode_t open_mode;
  30. int i;
  31. /* Parse the specified mode. */
  32. open_mode = O_RDONLY;
  33. if (*mode != 'r') { /* Not read... */
  34. open_mode = (O_WRONLY | O_CREAT | O_TRUNC);
  35. if (*mode != 'w') { /* Not write (create or truncate)... */
  36. open_mode = (O_WRONLY | O_CREAT | O_APPEND);
  37. if (*mode != 'a') { /* Not write (create or append)... */
  38. DO_EINVAL:
  39. __set_errno(EINVAL); /* So illegal mode. */
  40. if (stream) {
  41. FREE_STREAM:
  42. assert(!(stream->__modeflags & __FLAG_FREEBUF));
  43. __STDIO_STREAM_FREE_FILE(stream);
  44. }
  45. return NULL;
  46. }
  47. }
  48. }
  49. if ((mode[1] == 'b')) { /* Binary mode (NO-OP currently). */
  50. ++mode;
  51. }
  52. if (mode[1] == '+') { /* Read and Write. */
  53. ++mode;
  54. open_mode |= (O_RDONLY | O_WRONLY);
  55. open_mode += (O_RDWR - (O_RDONLY | O_WRONLY));
  56. }
  57. #ifdef __UCLIBC_MJN3_ONLY__
  58. #warning CONSIDER: Implement glibc ccs option to bind a codeset?
  59. #warning CONSIDER: Implement glibc mmap option for readonly files?
  60. #warning CONSIDER: Implement a text mode using custom read/write funcs?
  61. #endif
  62. #if defined(__UCLIBC_HAS_FOPEN_EXCLUSIVE_MODE__) || defined(__UCLIBC_HAS_FOPEN_LARGEFILE_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. }
  77. #endif
  78. if (!stream) { /* Need to allocate a FILE (not freopen). */
  79. if ((stream = malloc(sizeof(FILE))) == NULL) {
  80. return stream;
  81. }
  82. stream->__modeflags = __FLAG_FREEFILE;
  83. #ifdef __STDIO_BUFFERS
  84. stream->__bufstart = NULL; /* We allocate a buffer below. */
  85. #endif
  86. #ifdef __UCLIBC_HAS_THREADS__
  87. /* We only initialize the mutex in the non-freopen case. */
  88. /* stream->__user_locking = _stdio_user_locking; */
  89. __stdio_init_mutex(&stream->__lock);
  90. #endif
  91. }
  92. #ifdef __UCLIBC_MJN3_ONLY__
  93. #warning TODO: Verify fdopen append behavior of glibc.
  94. #endif
  95. if (filedes >= 0) { /* Handle fdopen trickery. */
  96. stream->__filedes = filedes;
  97. /* NOTE: it is insufficient to just check R/W/RW agreement.
  98. * We must also check largefile compatibility if applicable.
  99. * Also, if append mode is desired for fdopen but O_APPEND isn't
  100. * currently set, then set it as recommended by SUSv3. However,
  101. * if append mode is not specified for fdopen but O_APPEND is set,
  102. * leave it set (glibc compat). */
  103. i = (open_mode & (O_ACCMODE|O_LARGEFILE)) + 1;
  104. /* NOTE: fopencookie needs changing if the basic check changes! */
  105. if (((i & (((int) fname_or_mode) + 1)) != i) /* Basic agreement? */
  106. || (((open_mode & ~((__mode_t) fname_or_mode)) & O_APPEND)
  107. && fcntl(filedes, F_SETFL, O_APPEND)) /* Need O_APPEND. */
  108. ) {
  109. goto DO_EINVAL;
  110. }
  111. /* For later... to reflect largefile setting in stream flags. */
  112. __STDIO_WHEN_LFS( open_mode |= (((__mode_t) fname_or_mode)
  113. & O_LARGEFILE) );
  114. } else {
  115. __STDIO_WHEN_LFS( if (filedes < -1) open_mode |= O_LARGEFILE );
  116. if ((stream->__filedes = __open(((const char *) fname_or_mode),
  117. open_mode, 0666)) < 0) {
  118. goto FREE_STREAM;
  119. }
  120. }
  121. stream->__modeflags &= __FLAG_FREEFILE;
  122. /* stream->__modeflags &= ~(__FLAG_READONLY|__FLAG_WRITEONLY); */
  123. stream->__modeflags |= /* Note: Makes assumptions about flag vals! */
  124. #if (O_APPEND != __FLAG_APPEND) || ((O_LARGEFILE != __FLAG_LARGEFILE) && (O_LARGEFILE != 0))
  125. # if (O_APPEND != __FLAG_APPEND)
  126. ((open_mode & O_APPEND) ? __FLAG_APPEND : 0) |
  127. # else
  128. (open_mode & O_APPEND) |
  129. # endif
  130. # if (O_LARGEFILE != __FLAG_LARGEFILE) && (O_LARGEFILE != 0)
  131. ((open_mode & O_LARGEFILE) ? __FLAG_LARGEFILE : 0) |
  132. # else
  133. (open_mode & O_LARGEFILE) |
  134. # endif
  135. #else
  136. (open_mode & (O_APPEND|O_LARGEFILE)) | /* i386 linux and elks */
  137. #endif
  138. ((((open_mode & O_ACCMODE) + 1) ^ 0x03) * __FLAG_WRITEONLY);
  139. #ifdef __STDIO_BUFFERS
  140. i = errno; /* Preserve errno against isatty call. */
  141. stream->__modeflags |= (isatty(stream->__filedes) * __FLAG_LBF);
  142. __set_errno(i);
  143. if (!stream->__bufstart) {
  144. if ((stream->__bufstart = malloc(BUFSIZ)) != NULL) {
  145. stream->__bufend = stream->__bufstart + BUFSIZ;
  146. stream->__modeflags |= __FLAG_FREEBUF;
  147. } else {
  148. # if __STDIO_BUILTIN_BUF_SIZE > 0
  149. stream->__bufstart = stream->__builtinbuf;
  150. stream->__bufend = stream->__builtinbuf + sizeof(stream->__builtinbuf);
  151. # else /* __STDIO_BUILTIN_BUF_SIZE > 0 */
  152. stream->__bufend = stream->__bufstart;
  153. # endif /* __STDIO_BUILTIN_BUF_SIZE > 0 */
  154. }
  155. }
  156. __STDIO_STREAM_DISABLE_GETC(stream);
  157. __STDIO_STREAM_DISABLE_PUTC(stream);
  158. __STDIO_STREAM_INIT_BUFREAD_BUFPOS(stream);
  159. #endif
  160. __STDIO_STREAM_RESET_GCS(stream);
  161. #ifdef __UCLIBC_HAS_WCHAR__
  162. stream->__ungot_width[0] = 0;
  163. #endif
  164. #ifdef __STDIO_MBSTATE
  165. __INIT_MBSTATE(&(stream->__state));
  166. #endif
  167. #ifdef __UCLIBC_HAS_THREADS__
  168. /* Even in the freopen case, we reset the user locking flag. */
  169. stream->__user_locking = _stdio_user_locking;
  170. /* __stdio_init_mutex(&stream->__lock); */
  171. #endif
  172. #ifdef __STDIO_HAS_OPENLIST
  173. __STDIO_THREADLOCK_OPENLIST;
  174. stream->__nextopen = _stdio_openlist; /* New files are inserted at */
  175. _stdio_openlist = stream; /* the head of the list. */
  176. __STDIO_THREADUNLOCK_OPENLIST;
  177. #endif
  178. __STDIO_STREAM_VALIDATE(stream);
  179. return stream;
  180. }