libc: Implement fwrite()
Now that we have the write() syscall and libc wrapper, fwrite can finally be implemented.
This commit is contained in:
parent
80ab982fe4
commit
2f46e46aa4
@ -5,6 +5,9 @@
|
|||||||
#include <stdlib.h>
|
#include <stdlib.h>
|
||||||
#include <unistd.h>
|
#include <unistd.h>
|
||||||
|
|
||||||
|
FILE* __stderr;
|
||||||
|
FILE* __stdout;
|
||||||
|
|
||||||
extern "C"
|
extern "C"
|
||||||
{
|
{
|
||||||
int fclose(FILE* stream)
|
int fclose(FILE* stream)
|
||||||
@ -29,7 +32,7 @@ extern "C"
|
|||||||
|
|
||||||
FILE* fopen(const char* pathname, const char*)
|
FILE* fopen(const char* pathname, const char*)
|
||||||
{
|
{
|
||||||
int fd = open(pathname, O_RDONLY); // FIXME: Use the mode string.
|
int fd = open(pathname, O_RDWR); // FIXME: Use the mode string.
|
||||||
if (fd < 0) { return 0; }
|
if (fd < 0) { return 0; }
|
||||||
FILE* stream = (FILE*)malloc(sizeof(FILE));
|
FILE* stream = (FILE*)malloc(sizeof(FILE));
|
||||||
stream->f_fd = fd;
|
stream->f_fd = fd;
|
||||||
@ -74,9 +77,16 @@ extern "C"
|
|||||||
NOT_IMPLEMENTED("ftell");
|
NOT_IMPLEMENTED("ftell");
|
||||||
}
|
}
|
||||||
|
|
||||||
size_t fwrite(const void*, size_t, size_t, FILE*)
|
size_t fwrite(const void* buf, size_t size, size_t nmemb, FILE* stream)
|
||||||
{
|
{
|
||||||
NOT_IMPLEMENTED("fwrite");
|
ssize_t status = write(stream->f_fd, buf, size * nmemb);
|
||||||
|
if (status < 0)
|
||||||
|
{
|
||||||
|
stream->f_err = 1;
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
if (status == 0) stream->f_eof = 1;
|
||||||
|
return (size_t)status;
|
||||||
}
|
}
|
||||||
|
|
||||||
void setbuf(FILE*, char*)
|
void setbuf(FILE*, char*)
|
||||||
|
Loading…
Reference in New Issue
Block a user