libc: io: implement file io buffering and internal locking for concurrency
This commit is contained in:
@@ -0,0 +1,29 @@
|
||||
#include "file.h"
|
||||
|
||||
#include <unistd.h>
|
||||
|
||||
size_t fwrite(
|
||||
const void *buf,
|
||||
size_t size,
|
||||
size_t count,
|
||||
struct __opaque_file *stream)
|
||||
{
|
||||
__libc_file_lock(stream);
|
||||
size_t total = size * count;
|
||||
if (total == 0) {
|
||||
return 0;
|
||||
}
|
||||
|
||||
long ret = __libc_file_write(stream, buf, total);
|
||||
if (ret == 0) {
|
||||
stream->f_flags |= FILE_EOF;
|
||||
}
|
||||
|
||||
if (ret < 0) {
|
||||
stream->f_flags |= FILE_ERR;
|
||||
ret = 0;
|
||||
}
|
||||
|
||||
__libc_file_unlock(stream);
|
||||
return ret;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user