diff options
author | Gunnar Beutner <gbeutner@serenityos.org> | 2021-04-29 22:16:48 +0200 |
---|---|---|
committer | Andreas Kling <kling@serenityos.org> | 2021-04-29 23:12:05 +0200 |
commit | 6adb0dbdba015b7b6eddb4becc602172900bcd98 (patch) | |
tree | e30a7ebec8b34d7e226d54122b3bff8019860aeb /Userland/Libraries/LibC/stdio.cpp | |
parent | 36ee8a8c259cb77de90d655262d74c3ce4b45f22 (diff) | |
download | serenity-6adb0dbdba015b7b6eddb4becc602172900bcd98.zip |
LibC: Implement fgetc_unlocked(), fread_unlocked() and getc_unlocked()
Diffstat (limited to 'Userland/Libraries/LibC/stdio.cpp')
-rw-r--r-- | Userland/Libraries/LibC/stdio.cpp | 23 |
1 files changed, 19 insertions, 4 deletions
diff --git a/Userland/Libraries/LibC/stdio.cpp b/Userland/Libraries/LibC/stdio.cpp index 7380700d05..3e75b859fe 100644 --- a/Userland/Libraries/LibC/stdio.cpp +++ b/Userland/Libraries/LibC/stdio.cpp @@ -666,6 +666,16 @@ int fgetc(FILE* stream) return EOF; } +int fgetc_unlocked(FILE* stream) +{ + VERIFY(stream); + char ch; + size_t nread = fread_unlocked(&ch, sizeof(char), 1, stream); + if (nread == 1) + return ch; + return EOF; +} + int getc(FILE* stream) { return fgetc(stream); @@ -673,8 +683,7 @@ int getc(FILE* stream) int getc_unlocked(FILE* stream) { - // FIXME: This currently locks the file - return fgetc(stream); + return fgetc_unlocked(stream); } int getchar() @@ -796,18 +805,24 @@ int ferror(FILE* stream) return stream->error(); } -size_t fread(void* ptr, size_t size, size_t nmemb, FILE* stream) +size_t fread_unlocked(void* ptr, size_t size, size_t nmemb, FILE* stream) { VERIFY(stream); VERIFY(!Checked<size_t>::multiplication_would_overflow(size, nmemb)); - ScopedFileLock lock(stream); size_t nread = stream->read(reinterpret_cast<u8*>(ptr), size * nmemb); if (!nread) return 0; return nread / size; } +size_t fread(void* ptr, size_t size, size_t nmemb, FILE* stream) +{ + VERIFY(stream); + ScopedFileLock lock(stream); + return fread_unlocked(ptr, size, nmemb, stream); +} + size_t fwrite(const void* ptr, size_t size, size_t nmemb, FILE* stream) { VERIFY(stream); |