setvbuf.c 784 B

1234567891011121314151617181920212223242526272829
  1. #include "stdio_impl.h"
  2. /* The behavior of this function is undefined except when it is the first
  3. * operation on the stream, so the presence or absence of locking is not
  4. * observable in a program whose behavior is defined. Thus no locking is
  5. * performed here. No allocation of buffers is performed, but a buffer
  6. * provided by the caller is used as long as it is suitably sized. */
  7. int setvbuf(FILE *restrict f, char *restrict buf, int type, size_t size)
  8. {
  9. f->lbf = EOF;
  10. if (type == _IONBF) {
  11. f->buf_size = 0;
  12. } else if (type == _IOLBF || type == _IOFBF) {
  13. if (buf && size >= UNGET) {
  14. f->buf = (void *)(buf + UNGET);
  15. f->buf_size = size - UNGET;
  16. }
  17. if (type == _IOLBF && f->buf_size)
  18. f->lbf = '\n';
  19. } else {
  20. return -1;
  21. }
  22. f->flags |= F_SVB;
  23. return 0;
  24. }