diff --git a/libc/config/linux/x86_64/entrypoints.txt b/libc/config/linux/x86_64/entrypoints.txt --- a/libc/config/linux/x86_64/entrypoints.txt +++ b/libc/config/linux/x86_64/entrypoints.txt @@ -253,6 +253,7 @@ # stdio.h entrypoints libc.src.stdio.fclose libc.src.stdio.flockfile + libc.src.stdio.fflush libc.src.stdio.fopen libc.src.stdio.fread libc.src.stdio.fread_unlocked diff --git a/libc/spec/stdc.td b/libc/spec/stdc.td --- a/libc/spec/stdc.td +++ b/libc/spec/stdc.td @@ -483,6 +483,11 @@ RetValSpec, [ArgSpec] >, + FunctionSpec< + "fflush", + RetValSpec, + [ArgSpec] + >, FunctionSpec< "fopen", RetValSpec, diff --git a/libc/src/__support/File/file.cpp b/libc/src/__support/File/file.cpp --- a/libc/src/__support/File/file.cpp +++ b/libc/src/__support/File/file.cpp @@ -165,6 +165,7 @@ pos = 0; return platform_flush(this); } + // TODO: Add POSIX behavior for input streams. return 0; } diff --git a/libc/src/stdio/CMakeLists.txt b/libc/src/stdio/CMakeLists.txt --- a/libc/src/stdio/CMakeLists.txt +++ b/libc/src/stdio/CMakeLists.txt @@ -24,6 +24,18 @@ libc.src.__support.File.platform_file ) +add_entrypoint_object( + fflush + SRCS + fflush.cpp + HDRS + fflush.h + DEPENDS + libc.include.stdio + libc.src.__support.File.file + libc.src.__support.File.platform_file +) + add_entrypoint_object( flockfile SRCS diff --git a/libc/src/stdio/fflush.h b/libc/src/stdio/fflush.h new file mode 100644 --- /dev/null +++ b/libc/src/stdio/fflush.h @@ -0,0 +1,20 @@ +//===-- Implementation header of fflush -------------------------*- C++ -*-===// +// +// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. +// See https://llvm.org/LICENSE.txt for license information. +// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception +// +//===----------------------------------------------------------------------===// + +#ifndef LLVM_LIBC_SRC_STDIO_FFLUSH_H +#define LLVM_LIBC_SRC_STDIO_FFLUSH_H + +#include + +namespace __llvm_libc { + +int fflush(::FILE *stream); + +} // namespace __llvm_libc + +#endif // LLVM_LIBC_SRC_STDIO_FFLUSH_H diff --git a/libc/src/stdio/fflush.cpp b/libc/src/stdio/fflush.cpp new file mode 100644 --- /dev/null +++ b/libc/src/stdio/fflush.cpp @@ -0,0 +1,20 @@ +//===-- Implementation of fflush ------------------------------------------===// +// +// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. +// See https://llvm.org/LICENSE.txt for license information. +// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception +// +//===----------------------------------------------------------------------===// + +#include "src/stdio/fflush.h" +#include "src/__support/File/file.h" + +#include + +namespace __llvm_libc { + +LLVM_LIBC_FUNCTION(int, fflush, (::FILE * stream)) { + return reinterpret_cast<__llvm_libc::File *>(stream)->flush(); +} + +} // namespace __llvm_libc diff --git a/libc/test/src/stdio/CMakeLists.txt b/libc/test/src/stdio/CMakeLists.txt --- a/libc/test/src/stdio/CMakeLists.txt +++ b/libc/test/src/stdio/CMakeLists.txt @@ -8,6 +8,7 @@ fileop_test.cpp DEPENDS libc.src.stdio.fclose + libc.src.stdio.fflush libc.src.stdio.fopen libc.src.stdio.fread libc.src.stdio.fseek diff --git a/libc/test/src/stdio/fileop_test.cpp b/libc/test/src/stdio/fileop_test.cpp --- a/libc/test/src/stdio/fileop_test.cpp +++ b/libc/test/src/stdio/fileop_test.cpp @@ -7,6 +7,7 @@ //===----------------------------------------------------------------------===// #include "src/stdio/fclose.h" +#include "src/stdio/fflush.h" #include "src/stdio/fopen.h" #include "src/stdio/fread.h" #include "src/stdio/fseek.h" @@ -41,3 +42,24 @@ ASSERT_EQ(__llvm_libc::fclose(file), 0); } + +TEST(LlvmLibcFILE, FFlushTest) { + constexpr char FILENAME[] = "testdata/fflush.test"; + ::FILE *file = __llvm_libc::fopen(FILENAME, "w+"); + ASSERT_FALSE(file == nullptr); + constexpr char CONTENT[] = "1234567890987654321"; + ASSERT_EQ(sizeof(CONTENT), + __llvm_libc::fwrite(CONTENT, 1, sizeof(CONTENT), file)); + + // Flushing at this point should write the data to disk. So, we should be + // able to read it back. + ASSERT_EQ(0, __llvm_libc::fflush(file)); + + char data[sizeof(CONTENT)]; + ASSERT_EQ(__llvm_libc::fseek(file, 0, SEEK_SET), 0); + ASSERT_EQ(__llvm_libc::fread(data, 1, sizeof(CONTENT), file), + sizeof(CONTENT)); + ASSERT_STREQ(data, CONTENT); + + ASSERT_EQ(__llvm_libc::fclose(file), 0); +}