freopen.c 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475
  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. #ifndef __DO_LARGEFILE
  9. # define FILEDES_ARG (-1)
  10. #endif
  11. FILE *freopen(const char * __restrict filename, const char * __restrict mode,
  12. register FILE * __restrict stream)
  13. {
  14. /*
  15. * ANSI/ISO allow (implementation-defined) change of mode for an
  16. * existing file if filename is NULL. It doesn't look like Linux
  17. * supports this, so we don't here.
  18. *
  19. * NOTE: Whether or not the stream is free'd on failure is unclear
  20. * w.r.t. ANSI/ISO. This implementation chooses to NOT free
  21. * the stream and associated buffer if they were dynamically
  22. * allocated.
  23. * NOTE: Previous versions of uClibc did free dynamic storage.
  24. *
  25. * TODO: Apparently linux allows setting append mode. Implement?
  26. */
  27. unsigned short dynmode;
  28. register FILE *fp;
  29. __STDIO_AUTO_THREADLOCK_VAR;
  30. __STDIO_AUTO_THREADLOCK(stream);
  31. __STDIO_STREAM_VALIDATE(stream);
  32. __STDIO_OPENLIST_INC_USE; /* Do not remove the file from the list. */
  33. /* First, flush and close, but don't deallocate, the stream. */
  34. /* This also removes the stream for the open file list. */
  35. dynmode = (stream->__modeflags & (__FLAG_FREEBUF|__FLAG_FREEFILE));
  36. stream->__modeflags &= ~(__FLAG_FREEBUF|__FLAG_FREEFILE);
  37. /* Only call fclose on the stream if it is not already closed. */
  38. if ((stream->__modeflags & (__FLAG_READONLY|__FLAG_WRITEONLY))
  39. != (__FLAG_READONLY|__FLAG_WRITEONLY)
  40. ) {
  41. fclose(stream); /* Failures are ignored. */
  42. /* NOTE: fclose always does __STDIO_OPENLIST_INC_DEL_CNT. But we don't
  43. * want to remove this FILE from the open list, even if the freopen fails.
  44. * Consider the case of a failed freopen() on stdin. You probably still
  45. * want to be able to call freopen() again. Similarly for other "malloc'd"
  46. * streams. */
  47. __STDIO_OPENLIST_DEC_DEL_CNT;
  48. }
  49. fp = _stdio_fopen(((intptr_t) filename), mode, stream, FILEDES_ARG);
  50. if (!fp) {
  51. /* Don't remove stream from the open file list and (potentially) free it.
  52. * See _stdio_openlist_dec_use() in fflush.c. */
  53. stream->__modeflags = __FLAG_READONLY|__FLAG_WRITEONLY|__FLAG_FAILED_FREOPEN;
  54. }
  55. /* Reset the allocation flags. */
  56. stream->__modeflags |= dynmode;
  57. __STDIO_OPENLIST_DEC_USE;
  58. __STDIO_AUTO_THREADUNLOCK(stream);
  59. return fp;
  60. }