fwrite.c 1.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465
  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(fwrite_unlocked) */
  9. #ifdef __DO_UNLOCKED
  10. size_t fwrite_unlocked(const void * __restrict ptr, size_t size,
  11. size_t nmemb, register FILE * __restrict stream)
  12. {
  13. __STDIO_STREAM_VALIDATE(stream);
  14. /* Note: If nmbem * size > SIZE_MAX then there is an application
  15. * bug since no array can be larger than SIZE_MAX in size. */
  16. if ((__STDIO_STREAM_IS_NARROW_WRITING(stream)
  17. || !__STDIO_STREAM_TRANS_TO_WRITE(stream, __FLAG_NARROW))
  18. && size && nmemb
  19. ) {
  20. if (nmemb <= (SIZE_MAX / size)) {
  21. return __stdio_fwrite((const unsigned char *) ptr,
  22. size*nmemb, stream) / size;
  23. }
  24. __STDIO_STREAM_SET_ERROR(stream);
  25. __set_errno(EINVAL);
  26. }
  27. return 0;
  28. }
  29. libc_hidden_def(fwrite_unlocked)
  30. #ifndef __UCLIBC_HAS_THREADS__
  31. /* libc_hidden_proto(fwrite) */
  32. strong_alias(fwrite_unlocked,fwrite)
  33. libc_hidden_def(fwrite)
  34. #endif
  35. #elif defined __UCLIBC_HAS_THREADS__
  36. /* libc_hidden_proto(fwrite) */
  37. size_t fwrite(const void * __restrict ptr, size_t size,
  38. size_t nmemb, register FILE * __restrict stream)
  39. {
  40. size_t retval;
  41. __STDIO_AUTO_THREADLOCK_VAR;
  42. __STDIO_AUTO_THREADLOCK(stream);
  43. retval = fwrite_unlocked(ptr, size, nmemb, stream);
  44. __STDIO_AUTO_THREADUNLOCK(stream);
  45. return retval;
  46. }
  47. libc_hidden_def(fwrite)
  48. #endif