fgetws_chk.c 1.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152
  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. #include "libioP.h"
  15. #include <wchar.h>
  16. #include <sys/param.h>
  17. wchar_t *
  18. __fgetws_chk (wchar_t *buf, size_t size, int n, FILE *fp)
  19. {
  20. size_t count;
  21. wchar_t *result;
  22. int old_error;
  23. CHECK_FILE (fp, NULL);
  24. if (n <= 0)
  25. return NULL;
  26. _IO_acquire_lock (fp);
  27. /* This is very tricky since a file descriptor may be in the
  28. non-blocking mode. The error flag doesn't mean much in this
  29. case. We return an error only when there is a new error. */
  30. old_error = fp->_flags & _IO_ERR_SEEN;
  31. fp->_flags &= ~_IO_ERR_SEEN;
  32. count = _IO_getwline (fp, buf, MIN ((size_t) n - 1, size), L'\n', 1);
  33. /* If we read in some bytes and errno is EAGAIN, that error will
  34. be reported for next read. */
  35. if (count == 0 || (_IO_ferror_unlocked (fp) && errno != EAGAIN))
  36. result = NULL;
  37. else if (count >= size)
  38. __chk_fail ();
  39. else
  40. {
  41. buf[count] = '\0';
  42. result = buf;
  43. }
  44. fp->_flags |= old_error;
  45. _IO_release_lock (fp);
  46. return result;
  47. }