_fopen.c 6.3 KB

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