fwrite.c 1.3 KB

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