_fopen.c 6.6 KB

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