LibC: Implement fgetc_unlocked(), fread_unlocked() and getc_unlocked()
This commit is contained in:
parent
36ee8a8c25
commit
6adb0dbdba
Notes:
sideshowbarker
2024-07-18 18:53:22 +09:00
Author: https://github.com/gunnarbeutner Commit: https://github.com/SerenityOS/serenity/commit/6adb0dbdba0 Pull-request: https://github.com/SerenityOS/serenity/pull/6744
2 changed files with 21 additions and 4 deletions
|
@ -666,6 +666,16 @@ int fgetc(FILE* stream)
|
||||||
return EOF;
|
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)
|
int getc(FILE* stream)
|
||||||
{
|
{
|
||||||
return fgetc(stream);
|
return fgetc(stream);
|
||||||
|
@ -673,8 +683,7 @@ int getc(FILE* stream)
|
||||||
|
|
||||||
int getc_unlocked(FILE* stream)
|
int getc_unlocked(FILE* stream)
|
||||||
{
|
{
|
||||||
// FIXME: This currently locks the file
|
return fgetc_unlocked(stream);
|
||||||
return fgetc(stream);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
int getchar()
|
int getchar()
|
||||||
|
@ -796,18 +805,24 @@ int ferror(FILE* stream)
|
||||||
return stream->error();
|
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(stream);
|
||||||
VERIFY(!Checked<size_t>::multiplication_would_overflow(size, nmemb));
|
VERIFY(!Checked<size_t>::multiplication_would_overflow(size, nmemb));
|
||||||
|
|
||||||
ScopedFileLock lock(stream);
|
|
||||||
size_t nread = stream->read(reinterpret_cast<u8*>(ptr), size * nmemb);
|
size_t nread = stream->read(reinterpret_cast<u8*>(ptr), size * nmemb);
|
||||||
if (!nread)
|
if (!nread)
|
||||||
return 0;
|
return 0;
|
||||||
return nread / size;
|
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)
|
size_t fwrite(const void* ptr, size_t size, size_t nmemb, FILE* stream)
|
||||||
{
|
{
|
||||||
VERIFY(stream);
|
VERIFY(stream);
|
||||||
|
|
|
@ -47,6 +47,7 @@ char* fgets(char* buffer, int size, FILE*);
|
||||||
int fputc(int ch, FILE*);
|
int fputc(int ch, FILE*);
|
||||||
int fileno(FILE*);
|
int fileno(FILE*);
|
||||||
int fgetc(FILE*);
|
int fgetc(FILE*);
|
||||||
|
int fgetc_unlocked(FILE*);
|
||||||
int getc(FILE*);
|
int getc(FILE*);
|
||||||
int getc_unlocked(FILE* stream);
|
int getc_unlocked(FILE* stream);
|
||||||
int getchar();
|
int getchar();
|
||||||
|
@ -66,6 +67,7 @@ int ferror(FILE*);
|
||||||
int feof(FILE*);
|
int feof(FILE*);
|
||||||
int fflush(FILE*);
|
int fflush(FILE*);
|
||||||
size_t fread(void* ptr, size_t size, size_t nmemb, FILE*);
|
size_t fread(void* ptr, size_t size, size_t nmemb, FILE*);
|
||||||
|
size_t fread_unlocked(void* ptr, size_t size, size_t nmemb, FILE*);
|
||||||
size_t fwrite(const void* ptr, size_t size, size_t nmemb, FILE*);
|
size_t fwrite(const void* ptr, size_t size, size_t nmemb, FILE*);
|
||||||
int vprintf(const char* fmt, va_list) __attribute__((format(printf, 1, 0)));
|
int vprintf(const char* fmt, va_list) __attribute__((format(printf, 1, 0)));
|
||||||
int vfprintf(FILE*, const char* fmt, va_list) __attribute__((format(printf, 2, 0)));
|
int vfprintf(FILE*, const char* fmt, va_list) __attribute__((format(printf, 2, 0)));
|
||||||
|
|
Loading…
Add table
Reference in a new issue