1
Fork 0
mirror of https://github.com/RGBCube/serenity synced 2025-07-25 14:07:46 +00:00

LibC: Add POSIX spec comments for stdio APIs

This commit is contained in:
Brian Gianforcaro 2021-12-21 15:58:10 -08:00 committed by Brian Gianforcaro
parent 7fd1de01a7
commit 140a544051

View file

@ -523,6 +523,7 @@ void __stdio_init()
__stdio_is_initialized = true; __stdio_is_initialized = true;
} }
// https://pubs.opengroup.org/onlinepubs/9699919799/functions/setvbuf.html
int setvbuf(FILE* stream, char* buf, int mode, size_t size) int setvbuf(FILE* stream, char* buf, int mode, size_t size)
{ {
VERIFY(stream); VERIFY(stream);
@ -535,6 +536,7 @@ int setvbuf(FILE* stream, char* buf, int mode, size_t size)
return 0; return 0;
} }
// https://pubs.opengroup.org/onlinepubs/9699919799/functions/setbuf.html
void setbuf(FILE* stream, char* buf) void setbuf(FILE* stream, char* buf)
{ {
setvbuf(stream, buf, buf ? _IOFBF : _IONBF, BUFSIZ); setvbuf(stream, buf, buf ? _IOFBF : _IONBF, BUFSIZ);
@ -545,6 +547,7 @@ void setlinebuf(FILE* stream)
setvbuf(stream, nullptr, _IOLBF, 0); setvbuf(stream, nullptr, _IOLBF, 0);
} }
// https://pubs.opengroup.org/onlinepubs/9699919799/functions/fileno.html
int fileno(FILE* stream) int fileno(FILE* stream)
{ {
VERIFY(stream); VERIFY(stream);
@ -552,6 +555,7 @@ int fileno(FILE* stream)
return stream->fileno(); return stream->fileno();
} }
// https://pubs.opengroup.org/onlinepubs/9699919799/functions/feof.html
int feof(FILE* stream) int feof(FILE* stream)
{ {
VERIFY(stream); VERIFY(stream);
@ -559,6 +563,7 @@ int feof(FILE* stream)
return stream->eof(); return stream->eof();
} }
// https://pubs.opengroup.org/onlinepubs/9699919799/functions/fflush.html
int fflush(FILE* stream) int fflush(FILE* stream)
{ {
if (!stream) { if (!stream) {
@ -569,6 +574,7 @@ int fflush(FILE* stream)
return stream->flush() ? 0 : EOF; return stream->flush() ? 0 : EOF;
} }
// https://pubs.opengroup.org/onlinepubs/9699919799/functions/fgets.html
char* fgets(char* buffer, int size, FILE* stream) char* fgets(char* buffer, int size, FILE* stream)
{ {
VERIFY(stream); VERIFY(stream);
@ -577,6 +583,7 @@ char* fgets(char* buffer, int size, FILE* stream)
return ok ? buffer : nullptr; return ok ? buffer : nullptr;
} }
// https://pubs.opengroup.org/onlinepubs/9699919799/functions/fgetc.html
int fgetc(FILE* stream) int fgetc(FILE* stream)
{ {
VERIFY(stream); VERIFY(stream);
@ -597,6 +604,7 @@ int fgetc_unlocked(FILE* stream)
return EOF; return EOF;
} }
// https://pubs.opengroup.org/onlinepubs/9699919799/functions/getc.html
int getc(FILE* stream) int getc(FILE* stream)
{ {
return fgetc(stream); return fgetc(stream);
@ -607,11 +615,13 @@ int getc_unlocked(FILE* stream)
return fgetc_unlocked(stream); return fgetc_unlocked(stream);
} }
// https://pubs.opengroup.org/onlinepubs/9699919799/functions/getchar.html
int getchar() int getchar()
{ {
return getc(stdin); return getc(stdin);
} }
// https://pubs.opengroup.org/onlinepubs/9699919799/functions/getdelim.html
ssize_t getdelim(char** lineptr, size_t* n, int delim, FILE* stream) ssize_t getdelim(char** lineptr, size_t* n, int delim, FILE* stream)
{ {
if (!lineptr || !n) { if (!lineptr || !n) {
@ -658,11 +668,13 @@ ssize_t getdelim(char** lineptr, size_t* n, int delim, FILE* stream)
} }
} }
// https://pubs.opengroup.org/onlinepubs/9699919799/functions/getline.html
ssize_t getline(char** lineptr, size_t* n, FILE* stream) ssize_t getline(char** lineptr, size_t* n, FILE* stream)
{ {
return getdelim(lineptr, n, '\n', stream); return getdelim(lineptr, n, '\n', stream);
} }
// https://pubs.opengroup.org/onlinepubs/9699919799/functions/ungetc.html
int ungetc(int c, FILE* stream) int ungetc(int c, FILE* stream)
{ {
VERIFY(stream); VERIFY(stream);
@ -671,6 +683,7 @@ int ungetc(int c, FILE* stream)
return ok ? c : EOF; return ok ? c : EOF;
} }
// https://pubs.opengroup.org/onlinepubs/9699919799/functions/fputc.html
int fputc(int ch, FILE* stream) int fputc(int ch, FILE* stream)
{ {
VERIFY(stream); VERIFY(stream);
@ -683,16 +696,19 @@ int fputc(int ch, FILE* stream)
return byte; return byte;
} }
// https://pubs.opengroup.org/onlinepubs/9699919799/functions/putc.html
int putc(int ch, FILE* stream) int putc(int ch, FILE* stream)
{ {
return fputc(ch, stream); return fputc(ch, stream);
} }
// https://pubs.opengroup.org/onlinepubs/9699919799/functions/putchar.html
int putchar(int ch) int putchar(int ch)
{ {
return putc(ch, stdout); return putc(ch, stdout);
} }
// https://pubs.opengroup.org/onlinepubs/9699919799/functions/fputs.html
int fputs(const char* s, FILE* stream) int fputs(const char* s, FILE* stream)
{ {
VERIFY(stream); VERIFY(stream);
@ -704,6 +720,7 @@ int fputs(const char* s, FILE* stream)
return 1; return 1;
} }
// https://pubs.opengroup.org/onlinepubs/9699919799/functions/puts.html
int puts(const char* s) int puts(const char* s)
{ {
int rc = fputs(s, stdout); int rc = fputs(s, stdout);
@ -712,6 +729,7 @@ int puts(const char* s)
return fputc('\n', stdout); return fputc('\n', stdout);
} }
// https://pubs.opengroup.org/onlinepubs/9699919799/functions/clearerr.html
void clearerr(FILE* stream) void clearerr(FILE* stream)
{ {
VERIFY(stream); VERIFY(stream);
@ -719,6 +737,7 @@ void clearerr(FILE* stream)
stream->clear_err(); stream->clear_err();
} }
// https://pubs.opengroup.org/onlinepubs/9699919799/functions/ferror.html
int ferror(FILE* stream) int ferror(FILE* stream)
{ {
VERIFY(stream); VERIFY(stream);
@ -737,6 +756,7 @@ size_t fread_unlocked(void* ptr, size_t size, size_t nmemb, FILE* stream)
return nread / size; return nread / size;
} }
// https://pubs.opengroup.org/onlinepubs/9699919799/functions/fread.html
size_t fread(void* ptr, size_t size, size_t nmemb, FILE* stream) size_t fread(void* ptr, size_t size, size_t nmemb, FILE* stream)
{ {
VERIFY(stream); VERIFY(stream);
@ -744,6 +764,7 @@ size_t fread(void* ptr, size_t size, size_t nmemb, FILE* stream)
return fread_unlocked(ptr, size, nmemb, stream); return fread_unlocked(ptr, size, nmemb, stream);
} }
// https://pubs.opengroup.org/onlinepubs/9699919799/functions/fwrite.html
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);
@ -756,6 +777,7 @@ size_t fwrite(const void* ptr, size_t size, size_t nmemb, FILE* stream)
return nwritten / size; return nwritten / size;
} }
// https://pubs.opengroup.org/onlinepubs/9699919799/functions/fseek.html
int fseek(FILE* stream, long offset, int whence) int fseek(FILE* stream, long offset, int whence)
{ {
VERIFY(stream); VERIFY(stream);
@ -763,6 +785,7 @@ int fseek(FILE* stream, long offset, int whence)
return stream->seek(offset, whence); return stream->seek(offset, whence);
} }
// https://pubs.opengroup.org/onlinepubs/9699919799/functions/fseeko.html
int fseeko(FILE* stream, off_t offset, int whence) int fseeko(FILE* stream, off_t offset, int whence)
{ {
VERIFY(stream); VERIFY(stream);
@ -770,6 +793,7 @@ int fseeko(FILE* stream, off_t offset, int whence)
return stream->seek(offset, whence); return stream->seek(offset, whence);
} }
// https://pubs.opengroup.org/onlinepubs/9699919799/functions/ftell.html
long ftell(FILE* stream) long ftell(FILE* stream)
{ {
VERIFY(stream); VERIFY(stream);
@ -777,6 +801,7 @@ long ftell(FILE* stream)
return stream->tell(); return stream->tell();
} }
// https://pubs.opengroup.org/onlinepubs/9699919799/functions/ftello.html
off_t ftello(FILE* stream) off_t ftello(FILE* stream)
{ {
VERIFY(stream); VERIFY(stream);
@ -784,6 +809,7 @@ off_t ftello(FILE* stream)
return stream->tell(); return stream->tell();
} }
// https://pubs.opengroup.org/onlinepubs/9699919799/functions/fgetpos.html
int fgetpos(FILE* stream, fpos_t* pos) int fgetpos(FILE* stream, fpos_t* pos)
{ {
VERIFY(stream); VERIFY(stream);
@ -798,6 +824,7 @@ int fgetpos(FILE* stream, fpos_t* pos)
return 0; return 0;
} }
// https://pubs.opengroup.org/onlinepubs/9699919799/functions/fsetpos.html
int fsetpos(FILE* stream, const fpos_t* pos) int fsetpos(FILE* stream, const fpos_t* pos)
{ {
VERIFY(stream); VERIFY(stream);
@ -807,6 +834,7 @@ int fsetpos(FILE* stream, const fpos_t* pos)
return stream->seek(*pos, SEEK_SET); return stream->seek(*pos, SEEK_SET);
} }
// https://pubs.opengroup.org/onlinepubs/9699919799/functions/rewind.html
void rewind(FILE* stream) void rewind(FILE* stream)
{ {
fseek(stream, 0, SEEK_SET); fseek(stream, 0, SEEK_SET);
@ -824,12 +852,14 @@ ALWAYS_INLINE static void stream_putch(char*&, char ch)
fputc(ch, __current_stream); fputc(ch, __current_stream);
} }
// https://pubs.opengroup.org/onlinepubs/9699919799/functions/vfprintf.html
int vfprintf(FILE* stream, const char* fmt, va_list ap) int vfprintf(FILE* stream, const char* fmt, va_list ap)
{ {
__current_stream = stream; __current_stream = stream;
return printf_internal(stream_putch, nullptr, fmt, ap); return printf_internal(stream_putch, nullptr, fmt, ap);
} }
// https://pubs.opengroup.org/onlinepubs/9699919799/functions/fprintf.html
int fprintf(FILE* stream, const char* fmt, ...) int fprintf(FILE* stream, const char* fmt, ...)
{ {
va_list ap; va_list ap;
@ -839,11 +869,13 @@ int fprintf(FILE* stream, const char* fmt, ...)
return ret; return ret;
} }
// https://pubs.opengroup.org/onlinepubs/9699919799/functions/vprintf.html
int vprintf(const char* fmt, va_list ap) int vprintf(const char* fmt, va_list ap)
{ {
return printf_internal(stdout_putch, nullptr, fmt, ap); return printf_internal(stdout_putch, nullptr, fmt, ap);
} }
// https://pubs.opengroup.org/onlinepubs/9699919799/functions/printf.html
int printf(const char* fmt, ...) int printf(const char* fmt, ...)
{ {
va_list ap; va_list ap;
@ -853,6 +885,7 @@ int printf(const char* fmt, ...)
return ret; return ret;
} }
// https://pubs.opengroup.org/onlinepubs/9699919799/functions/vasprintf.html
int vasprintf(char** strp, const char* fmt, va_list ap) int vasprintf(char** strp, const char* fmt, va_list ap)
{ {
StringBuilder builder; StringBuilder builder;
@ -863,6 +896,7 @@ int vasprintf(char** strp, const char* fmt, va_list ap)
return length; return length;
} }
// https://pubs.opengroup.org/onlinepubs/9699919799/functions/asprintf.html
int asprintf(char** strp, const char* fmt, ...) int asprintf(char** strp, const char* fmt, ...)
{ {
StringBuilder builder; StringBuilder builder;
@ -881,6 +915,7 @@ static void buffer_putch(char*& bufptr, char ch)
*bufptr++ = ch; *bufptr++ = ch;
} }
// https://pubs.opengroup.org/onlinepubs/9699919799/functions/vsprintf.html
int vsprintf(char* buffer, const char* fmt, va_list ap) int vsprintf(char* buffer, const char* fmt, va_list ap)
{ {
int ret = printf_internal(buffer_putch, buffer, fmt, ap); int ret = printf_internal(buffer_putch, buffer, fmt, ap);
@ -888,6 +923,7 @@ int vsprintf(char* buffer, const char* fmt, va_list ap)
return ret; return ret;
} }
// https://pubs.opengroup.org/onlinepubs/9699919799/functions/sprintf.html
int sprintf(char* buffer, const char* fmt, ...) int sprintf(char* buffer, const char* fmt, ...)
{ {
va_list ap; va_list ap;
@ -906,6 +942,7 @@ ALWAYS_INLINE void sized_buffer_putch(char*& bufptr, char ch)
} }
} }
// https://pubs.opengroup.org/onlinepubs/9699919799/functions/vsnprintf.html
int vsnprintf(char* buffer, size_t size, const char* fmt, va_list ap) int vsnprintf(char* buffer, size_t size, const char* fmt, va_list ap)
{ {
if (size) { if (size) {
@ -922,6 +959,7 @@ int vsnprintf(char* buffer, size_t size, const char* fmt, va_list ap)
return ret; return ret;
} }
// https://pubs.opengroup.org/onlinepubs/9699919799/functions/snprintf.html
int snprintf(char* buffer, size_t size, const char* fmt, ...) int snprintf(char* buffer, size_t size, const char* fmt, ...)
{ {
va_list ap; va_list ap;
@ -931,6 +969,7 @@ int snprintf(char* buffer, size_t size, const char* fmt, ...)
return ret; return ret;
} }
// https://pubs.opengroup.org/onlinepubs/9699919799/functions/perror.html
void perror(const char* s) void perror(const char* s)
{ {
int saved_errno = errno; int saved_errno = errno;
@ -975,6 +1014,7 @@ static int parse_mode(const char* mode)
return flags; return flags;
} }
// https://pubs.opengroup.org/onlinepubs/9699919799/functions/fopen.html
FILE* fopen(const char* pathname, const char* mode) FILE* fopen(const char* pathname, const char* mode)
{ {
int flags = parse_mode(mode); int flags = parse_mode(mode);
@ -984,6 +1024,7 @@ FILE* fopen(const char* pathname, const char* mode)
return FILE::create(fd, flags); return FILE::create(fd, flags);
} }
// https://pubs.opengroup.org/onlinepubs/9699919799/functions/freopen.html
FILE* freopen(const char* pathname, const char* mode, FILE* stream) FILE* freopen(const char* pathname, const char* mode, FILE* stream)
{ {
VERIFY(stream); VERIFY(stream);
@ -1001,6 +1042,7 @@ FILE* freopen(const char* pathname, const char* mode, FILE* stream)
return stream; return stream;
} }
// https://pubs.opengroup.org/onlinepubs/9699919799/functions/fdopen.html
FILE* fdopen(int fd, const char* mode) FILE* fdopen(int fd, const char* mode)
{ {
int flags = parse_mode(mode); int flags = parse_mode(mode);
@ -1015,6 +1057,7 @@ static inline bool is_default_stream(FILE* stream)
return stream == stdin || stream == stdout || stream == stderr; return stream == stdin || stream == stdout || stream == stderr;
} }
// https://pubs.opengroup.org/onlinepubs/9699919799/functions/fclose.html
int fclose(FILE* stream) int fclose(FILE* stream)
{ {
VERIFY(stream); VERIFY(stream);
@ -1033,6 +1076,7 @@ int fclose(FILE* stream)
return ok ? 0 : EOF; return ok ? 0 : EOF;
} }
// https://pubs.opengroup.org/onlinepubs/9699919799/functions/rename.html
int rename(const char* oldpath, const char* newpath) int rename(const char* oldpath, const char* newpath)
{ {
if (!oldpath || !newpath) { if (!oldpath || !newpath) {
@ -1049,12 +1093,14 @@ void dbgputstr(const char* characters, size_t length)
syscall(SC_dbgputstr, characters, length); syscall(SC_dbgputstr, characters, length);
} }
// https://pubs.opengroup.org/onlinepubs/9699919799/functions/tmpnam.html
char* tmpnam(char*) char* tmpnam(char*)
{ {
dbgln("FIXME: Implement tmpnam()"); dbgln("FIXME: Implement tmpnam()");
TODO(); TODO();
} }
// https://pubs.opengroup.org/onlinepubs/9699919799/functions/popen.html
FILE* popen(const char* command, const char* type) FILE* popen(const char* command, const char* type)
{ {
if (!type || (*type != 'r' && *type != 'w')) { if (!type || (*type != 'r' && *type != 'w')) {
@ -1112,6 +1158,7 @@ FILE* popen(const char* command, const char* type)
return file; return file;
} }
// https://pubs.opengroup.org/onlinepubs/9699919799/functions/pclose.html
int pclose(FILE* stream) int pclose(FILE* stream)
{ {
VERIFY(stream); VERIFY(stream);
@ -1124,6 +1171,7 @@ int pclose(FILE* stream)
return wstatus; return wstatus;
} }
// https://pubs.opengroup.org/onlinepubs/9699919799/functions/remove.html
int remove(const char* pathname) int remove(const char* pathname)
{ {
if (unlink(pathname) < 0) { if (unlink(pathname) < 0) {
@ -1134,6 +1182,7 @@ int remove(const char* pathname)
return 0; return 0;
} }
// https://pubs.opengroup.org/onlinepubs/9699919799/functions/scanf.html
int scanf(const char* fmt, ...) int scanf(const char* fmt, ...)
{ {
va_list ap; va_list ap;
@ -1143,6 +1192,7 @@ int scanf(const char* fmt, ...)
return count; return count;
} }
// https://pubs.opengroup.org/onlinepubs/9699919799/functions/fscanf.html
int fscanf(FILE* stream, const char* fmt, ...) int fscanf(FILE* stream, const char* fmt, ...)
{ {
va_list ap; va_list ap;
@ -1152,6 +1202,7 @@ int fscanf(FILE* stream, const char* fmt, ...)
return count; return count;
} }
// https://pubs.opengroup.org/onlinepubs/9699919799/functions/sscanf.html
int sscanf(const char* buffer, const char* fmt, ...) int sscanf(const char* buffer, const char* fmt, ...)
{ {
va_list ap; va_list ap;
@ -1161,6 +1212,7 @@ int sscanf(const char* buffer, const char* fmt, ...)
return count; return count;
} }
// https://pubs.opengroup.org/onlinepubs/9699919799/functions/vfscanf.html
int vfscanf(FILE* stream, const char* fmt, va_list ap) int vfscanf(FILE* stream, const char* fmt, va_list ap)
{ {
char buffer[BUFSIZ]; char buffer[BUFSIZ];
@ -1169,21 +1221,25 @@ int vfscanf(FILE* stream, const char* fmt, va_list ap)
return vsscanf(buffer, fmt, ap); return vsscanf(buffer, fmt, ap);
} }
// https://pubs.opengroup.org/onlinepubs/9699919799/functions/vscanf.html
int vscanf(const char* fmt, va_list ap) int vscanf(const char* fmt, va_list ap)
{ {
return vfscanf(stdin, fmt, ap); return vfscanf(stdin, fmt, ap);
} }
// https://pubs.opengroup.org/onlinepubs/9699919799/functions/flockfile.html
void flockfile([[maybe_unused]] FILE* filehandle) void flockfile([[maybe_unused]] FILE* filehandle)
{ {
dbgln("FIXME: Implement flockfile()"); dbgln("FIXME: Implement flockfile()");
} }
// https://pubs.opengroup.org/onlinepubs/9699919799/functions/funlockfile.html
void funlockfile([[maybe_unused]] FILE* filehandle) void funlockfile([[maybe_unused]] FILE* filehandle)
{ {
dbgln("FIXME: Implement funlockfile()"); dbgln("FIXME: Implement funlockfile()");
} }
// https://pubs.opengroup.org/onlinepubs/9699919799/functions/tmpfile.html
FILE* tmpfile() FILE* tmpfile()
{ {
char tmp_path[] = "/tmp/XXXXXX"; char tmp_path[] = "/tmp/XXXXXX";