| lh | 9ed821d | 2023-04-07 01:36:19 -0700 | [diff] [blame] | 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 |  | 
 | 8 | #include "_stdio.h" | 
 | 9 |  | 
 | 10 |  | 
 | 11 | #ifdef __DO_UNLOCKED | 
 | 12 |  | 
 | 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 |  | 
 | 18 | 	/* Note: If nmbem * size > SIZE_MAX then there is an application | 
 | 19 | 	 * bug since no array can be larger than SIZE_MAX in size. */ | 
 | 20 |  | 
 | 21 | 	if ((__STDIO_STREAM_IS_NARROW_WRITING(stream) | 
 | 22 | 		 || !__STDIO_STREAM_TRANS_TO_WRITE(stream, __FLAG_NARROW)) | 
 | 23 | 		&& size && nmemb | 
 | 24 | 		) { | 
 | 25 |  | 
 | 26 | 		if (nmemb <= (SIZE_MAX / size)) { | 
 | 27 | 			return __stdio_fwrite((const unsigned char *) ptr, | 
 | 28 | 								  size*nmemb, stream) / size; | 
 | 29 | 		} | 
 | 30 |  | 
 | 31 | 		__STDIO_STREAM_SET_ERROR(stream); | 
 | 32 | 		__set_errno(EINVAL); | 
 | 33 | 	} | 
 | 34 |  | 
 | 35 | 	return 0; | 
 | 36 | } | 
 | 37 | libc_hidden_def(fwrite_unlocked) | 
 | 38 |  | 
 | 39 | #ifndef __UCLIBC_HAS_THREADS__ | 
 | 40 | strong_alias(fwrite_unlocked,fwrite) | 
 | 41 | libc_hidden_def(fwrite) | 
 | 42 | #endif | 
 | 43 |  | 
 | 44 | #elif defined __UCLIBC_HAS_THREADS__ | 
 | 45 |  | 
 | 46 | size_t fwrite(const void * __restrict ptr, size_t size, | 
 | 47 | 			  size_t nmemb, register FILE * __restrict stream) | 
 | 48 | { | 
 | 49 | 	size_t retval; | 
 | 50 | 	__STDIO_AUTO_THREADLOCK_VAR; | 
 | 51 |  | 
 | 52 | 	__STDIO_AUTO_THREADLOCK(stream); | 
 | 53 |  | 
 | 54 | 	retval = fwrite_unlocked(ptr, size, nmemb, stream); | 
 | 55 |  | 
 | 56 | 	__STDIO_AUTO_THREADUNLOCK(stream); | 
 | 57 |  | 
 | 58 | 	return retval; | 
 | 59 | } | 
 | 60 | libc_hidden_def(fwrite) | 
 | 61 |  | 
 | 62 | #endif |