fwrite.c 1.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061
  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. hidden_strong_alias(__fwrite_unlocked,__fwrite)
  31. weak_alias(__fwrite_unlocked,fwrite)
  32. #endif
  33. #elif defined __UCLIBC_HAS_THREADS__
  34. size_t attribute_hidden __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. strong_alias(__fwrite,fwrite)
  45. #endif