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. size_t attribute_hidden __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. weak_alias(__fwrite_unlocked,fwrite_unlocked);
  29. #ifndef __UCLIBC_HAS_THREADS__
  30. weak_alias(__fwrite_unlocked,fwrite);
  31. #endif
  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