iosetvbuf.c 3.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  1. /* Copyright (C) 1993-2019 Free Software Foundation, Inc.
  2. This file is part of the GNU C Library.
  3. The GNU C Library is free software; you can redistribute it and/or
  4. modify it under the terms of the GNU Lesser General Public
  5. License as published by the Free Software Foundation; either
  6. version 2.1 of the License, or (at your option) any later version.
  7. The GNU C Library is distributed in the hope that it will be useful,
  8. but WITHOUT ANY WARRANTY; without even the implied warranty of
  9. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  10. Lesser General Public License for more details.
  11. You should have received a copy of the GNU Lesser General Public
  12. License along with the GNU C Library; if not, see
  13. <http://www.gnu.org/licenses/>.
  14. As a special exception, if you link the code in this file with
  15. files compiled with a GNU compiler to produce an executable,
  16. that does not cause the resulting executable to be covered by
  17. the GNU Lesser General Public License. This exception does not
  18. however invalidate any other reasons why the executable file
  19. might be covered by the GNU Lesser General Public License.
  20. This exception applies to code released by its copyright holders
  21. in files containing the exception. */
  22. #include "libioP.h"
  23. #define _IOFBF 0 /* Fully buffered. */
  24. #define _IOLBF 1 /* Line buffered. */
  25. #define _IONBF 2 /* No buffering. */
  26. int
  27. _IO_setvbuf (FILE *fp, char *buf, int mode, size_t size)
  28. {
  29. int result;
  30. CHECK_FILE (fp, EOF);
  31. _IO_acquire_lock (fp);
  32. switch (mode)
  33. {
  34. case _IOFBF:
  35. fp->_flags &= ~(_IO_LINE_BUF|_IO_UNBUFFERED);
  36. if (buf == NULL)
  37. {
  38. if (fp->_IO_buf_base == NULL)
  39. {
  40. /* There is no flag to distinguish between "fully buffered
  41. mode has been explicitly set" as opposed to "line
  42. buffering has not been explicitly set". In both
  43. cases, _IO_LINE_BUF is off. If this is a tty, and
  44. _IO_filedoalloc later gets called, it cannot know if
  45. it should set the _IO_LINE_BUF flag (because that is
  46. the default), or not (because we have explicitly asked
  47. for fully buffered mode). So we make sure a buffer
  48. gets allocated now, and explicitly turn off line
  49. buffering.
  50. A possibly cleaner alternative would be to add an
  51. extra flag, but then flags are a finite resource. */
  52. if (_IO_DOALLOCATE (fp) < 0)
  53. {
  54. result = EOF;
  55. goto unlock_return;
  56. }
  57. fp->_flags &= ~_IO_LINE_BUF;
  58. }
  59. result = 0;
  60. goto unlock_return;
  61. }
  62. break;
  63. case _IOLBF:
  64. fp->_flags &= ~_IO_UNBUFFERED;
  65. fp->_flags |= _IO_LINE_BUF;
  66. if (buf == NULL)
  67. {
  68. result = 0;
  69. goto unlock_return;
  70. }
  71. break;
  72. case _IONBF:
  73. fp->_flags &= ~_IO_LINE_BUF;
  74. fp->_flags |= _IO_UNBUFFERED;
  75. buf = NULL;
  76. size = 0;
  77. break;
  78. default:
  79. result = EOF;
  80. goto unlock_return;
  81. }
  82. result = _IO_SETBUF (fp, buf, size) == NULL ? EOF : 0;
  83. unlock_return:
  84. _IO_release_lock (fp);
  85. return result;
  86. }
  87. libc_hidden_def (_IO_setvbuf)
  88. weak_alias (_IO_setvbuf, setvbuf)