--- /dev/null
+//===-- 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 <stdio.h>
+
+namespace __llvm_libc {
+
+LLVM_LIBC_FUNCTION(int, fflush, (::FILE * stream)) {
+ return reinterpret_cast<__llvm_libc::File *>(stream)->flush();
+}
+
+} // namespace __llvm_libc
--- /dev/null
+//===-- 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 <stdio.h>
+
+namespace __llvm_libc {
+
+int fflush(::FILE *stream);
+
+} // namespace __llvm_libc
+
+#endif // LLVM_LIBC_SRC_STDIO_FFLUSH_H
//===----------------------------------------------------------------------===//
#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"
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);
+}