fwrite.c 1.4 KB

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