20 changes: 20 additions & 0 deletions libc/src/stdio/feof_unlocked.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
//===-- Implementation header of feof_unlocked ------------------*- 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_FEOF_UNLOCKED_H
#define LLVM_LIBC_SRC_STDIO_FEOF_UNLOCKED_H

#include <stdio.h>

namespace __llvm_libc {

int feof_unlocked(::FILE *stream);

} // namespace __llvm_libc

#endif // LLVM_LIBC_SRC_STDIO_FEOF_UNLOCKED_H
20 changes: 20 additions & 0 deletions libc/src/stdio/ferror.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
//===-- Implementation of ferror ------------------------------------------===//
//
// 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/ferror.h"
#include "src/__support/File/file.h"

#include <stdio.h>

namespace __llvm_libc {

LLVM_LIBC_FUNCTION(int, ferror, (::FILE * stream)) {
return reinterpret_cast<__llvm_libc::File *>(stream)->error();
}

} // namespace __llvm_libc
20 changes: 20 additions & 0 deletions libc/src/stdio/ferror.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
//===-- Implementation header of ferror -------------------------*- 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_FERROR_H
#define LLVM_LIBC_SRC_STDIO_FERROR_H

#include <stdio.h>

namespace __llvm_libc {

int ferror(::FILE *stream);

} // namespace __llvm_libc

#endif // LLVM_LIBC_SRC_STDIO_FERROR_H
20 changes: 20 additions & 0 deletions libc/src/stdio/ferror_unlocked.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
//===-- Implementation of ferror_unlocked ---------------------------------===//
//
// 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/ferror_unlocked.h"
#include "src/__support/File/file.h"

#include <stdio.h>

namespace __llvm_libc {

LLVM_LIBC_FUNCTION(int, ferror_unlocked, (::FILE * stream)) {
return reinterpret_cast<__llvm_libc::File *>(stream)->error_unlocked();
}

} // namespace __llvm_libc
20 changes: 20 additions & 0 deletions libc/src/stdio/ferror_unlocked.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
//===-- Implementation header of ferror_unlocked ----------------*- 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_FERROR_UNLOCKED_H
#define LLVM_LIBC_SRC_STDIO_FERROR_UNLOCKED_H

#include <stdio.h>

namespace __llvm_libc {

int ferror_unlocked(::FILE *stream);

} // namespace __llvm_libc

#endif // LLVM_LIBC_SRC_STDIO_FERROR_UNLOCKED_H
13 changes: 13 additions & 0 deletions libc/test/src/stdio/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,12 @@ add_libc_unittest(
SRCS
fileop_test.cpp
DEPENDS
libc.include.errno
libc.include.stdio
libc.src.stdio.clearerr
libc.src.stdio.fclose
libc.src.stdio.feof
libc.src.stdio.ferror
libc.src.stdio.fflush
libc.src.stdio.fopen
libc.src.stdio.fread
Expand All @@ -22,7 +27,12 @@ add_libc_unittest(
SRCS
unlocked_fileop_test.cpp
DEPENDS
libc.include.errno
libc.include.stdio
libc.src.stdio.clearerr_unlocked
libc.src.stdio.fclose
libc.src.stdio.feof_unlocked
libc.src.stdio.ferror_unlocked
libc.src.stdio.flockfile
libc.src.stdio.fopen
libc.src.stdio.fread_unlocked
Expand All @@ -40,7 +50,10 @@ add_libc_unittest(
libc.include.errno
libc.include.stdio
libc.include.stdlib
libc.src.stdio.clearerr
libc.src.stdio.fclose
libc.src.stdio.feof
libc.src.stdio.ferror
libc.src.stdio.fflush
libc.src.stdio.fopencookie
libc.src.stdio.fread
Expand Down
33 changes: 31 additions & 2 deletions libc/test/src/stdio/fileop_test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -6,23 +6,38 @@
//
//===----------------------------------------------------------------------===//

#include "src/stdio/clearerr.h"
#include "src/stdio/fclose.h"
#include "src/stdio/feof.h"
#include "src/stdio/ferror.h"
#include "src/stdio/fflush.h"
#include "src/stdio/fopen.h"
#include "src/stdio/fread.h"
#include "src/stdio/fseek.h"
#include "src/stdio/fwrite.h"
#include "utils/UnitTest/Test.h"

#include <errno.h>
#include <stdio.h>

TEST(LlvmLibcStdio, SimpleOperations) {
TEST(LlvmLibcFILETest, SimpleFileOperations) {
constexpr char FILENAME[] = "testdata/simple_operations.test";
::FILE *file = __llvm_libc::fopen(FILENAME, "w");
ASSERT_FALSE(file == nullptr);
constexpr char CONTENT[] = "1234567890987654321";
ASSERT_EQ(sizeof(CONTENT) - 1,
__llvm_libc::fwrite(CONTENT, 1, sizeof(CONTENT) - 1, file));

// This is not a readable file.
char read_data[sizeof(CONTENT)];
ASSERT_EQ(__llvm_libc::fread(read_data, 1, sizeof(CONTENT), file), size_t(0));
ASSERT_NE(__llvm_libc::ferror(file), 0);
EXPECT_NE(errno, 0);
errno = 0;

__llvm_libc::clearerr(file);
ASSERT_EQ(__llvm_libc::ferror(file), 0);

ASSERT_EQ(0, __llvm_libc::fclose(file));

file = __llvm_libc::fopen(FILENAME, "r");
Expand All @@ -40,10 +55,24 @@ TEST(LlvmLibcStdio, SimpleOperations) {
ASSERT_EQ(__llvm_libc::fread(data, 1, READ_SIZE - 1, file), READ_SIZE - 1);
ASSERT_STREQ(data, "9098");

// Reading another time should trigger eof.
ASSERT_NE(sizeof(CONTENT),
__llvm_libc::fread(read_data, 1, sizeof(CONTENT), file));
ASSERT_NE(__llvm_libc::feof(file), 0);

// Should be an error to write.
ASSERT_EQ(size_t(0), __llvm_libc::fwrite(CONTENT, 1, sizeof(CONTENT), file));
ASSERT_NE(__llvm_libc::ferror(file), 0);
ASSERT_NE(errno, 0);
errno = 0;

__llvm_libc::clearerr(file);
ASSERT_EQ(__llvm_libc::ferror(file), 0);

ASSERT_EQ(__llvm_libc::fclose(file), 0);
}

TEST(LlvmLibcFILE, FFlushTest) {
TEST(LlvmLibcFILETest, FFlush) {
constexpr char FILENAME[] = "testdata/fflush.test";
::FILE *file = __llvm_libc::fopen(FILENAME, "w+");
ASSERT_FALSE(file == nullptr);
Expand Down
22 changes: 21 additions & 1 deletion libc/test/src/stdio/fopencookie_test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,10 @@
//
//===----------------------------------------------------------------------===//

#include "src/stdio/clearerr.h"
#include "src/stdio/fclose.h"
#include "src/stdio/feof.h"
#include "src/stdio/ferror.h"
#include "src/stdio/fflush.h"
#include "src/stdio/fopencookie.h"
#include "src/stdio/fread.h"
Expand Down Expand Up @@ -102,12 +105,21 @@ TEST(LlvmLibcFOpenCookie, ReadOnlyCookieTest) {
__llvm_libc::fread(read_data, 1, sizeof(CONTENT), f));
ASSERT_STREQ(read_data, CONTENT);

// Reading another time should trigger eof.
ASSERT_NE(sizeof(CONTENT),
__llvm_libc::fread(read_data, 1, sizeof(CONTENT), f));
ASSERT_NE(__llvm_libc::feof(f), 0);

ASSERT_EQ(0, __llvm_libc::fseek(f, 0, SEEK_SET));
// Should be an error to write.
ASSERT_EQ(size_t(0), __llvm_libc::fwrite(CONTENT, 1, sizeof(CONTENT), f));
ASSERT_EQ(errno, EBADF);
ASSERT_NE(__llvm_libc::ferror(f), 0);
ASSERT_NE(errno, 0);
errno = 0;

__llvm_libc::clearerr(f);
ASSERT_EQ(__llvm_libc::ferror(f), 0);

ASSERT_EQ(0, __llvm_libc::fclose(f));
free(ss);
}
Expand All @@ -134,9 +146,13 @@ TEST(LlvmLibcFOpenCookie, WriteOnlyCookieTest) {
char read_data[sizeof(WRITE_DATA)];
// Should be an error to read.
ASSERT_EQ(size_t(0), __llvm_libc::fread(read_data, 1, sizeof(WRITE_DATA), f));
ASSERT_NE(__llvm_libc::ferror(f), 0);
ASSERT_EQ(errno, EBADF);
errno = 0;

__llvm_libc::clearerr(f);
ASSERT_EQ(__llvm_libc::ferror(f), 0);

ASSERT_EQ(0, __llvm_libc::fclose(f));
free(ss);
}
Expand All @@ -159,9 +175,13 @@ TEST(LlvmLibcFOpenCookie, AppendOnlyCookieTest) {
char read_data[READ_SIZE];
// This is not a readable file.
ASSERT_EQ(__llvm_libc::fread(read_data, 1, READ_SIZE, f), size_t(0));
ASSERT_NE(__llvm_libc::ferror(f), 0);
EXPECT_NE(errno, 0);
errno = 0;

__llvm_libc::clearerr(f);
ASSERT_EQ(__llvm_libc::ferror(f), 0);

ASSERT_EQ(__llvm_libc::fwrite(WRITE_DATA, 1, sizeof(WRITE_DATA), f),
sizeof(WRITE_DATA));
EXPECT_EQ(__llvm_libc::fflush(f), 0);
Expand Down
68 changes: 50 additions & 18 deletions libc/test/src/stdio/unlocked_fileop_test.cpp
Original file line number Diff line number Diff line change
@@ -1,44 +1,76 @@
//===-- Unittests for file operations like fopen, flcose etc --------------===//
//===-- Unittests for f operations like fopen, flcose etc --------------===//
//
// 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/clearerr_unlocked.h"
#include "src/stdio/fclose.h"
#include "src/stdio/feof_unlocked.h"
#include "src/stdio/ferror_unlocked.h"
#include "src/stdio/flockfile.h"
#include "src/stdio/fopen.h"
#include "src/stdio/fread_unlocked.h"
#include "src/stdio/funlockfile.h"
#include "src/stdio/fwrite_unlocked.h"
#include "utils/UnitTest/Test.h"

#include <errno.h>
#include <stdio.h>

TEST(LlvmLibcFILETest, UnlockedReadAndWrite) {
constexpr char FILENAME[] = "testdata/unlocked_read_and_write.test";
::FILE *file = __llvm_libc::fopen(FILENAME, "w");
ASSERT_FALSE(file == nullptr);
constexpr char fNAME[] = "testdata/unlocked_read_and_write.test";
::FILE *f = __llvm_libc::fopen(fNAME, "w");
ASSERT_FALSE(f == nullptr);
constexpr char CONTENT[] = "1234567890987654321";
__llvm_libc::flockfile(file);
ASSERT_EQ(sizeof(CONTENT) - 1, __llvm_libc::fwrite_unlocked(
CONTENT, 1, sizeof(CONTENT) - 1, file));
__llvm_libc::funlockfile(file);
ASSERT_EQ(0, __llvm_libc::fclose(file));

file = __llvm_libc::fopen(FILENAME, "r");
ASSERT_FALSE(file == nullptr);

__llvm_libc::flockfile(f);
ASSERT_EQ(sizeof(CONTENT) - 1,
__llvm_libc::fwrite_unlocked(CONTENT, 1, sizeof(CONTENT) - 1, f));
// Should be an error to read.
constexpr size_t READ_SIZE = 5;
char data[READ_SIZE * 2 + 1];
data[READ_SIZE * 2] = '\0';
__llvm_libc::flockfile(file);
ASSERT_EQ(__llvm_libc::fread_unlocked(data, 1, READ_SIZE, file), READ_SIZE);
ASSERT_EQ(__llvm_libc::fread_unlocked(data + READ_SIZE, 1, READ_SIZE, file),

ASSERT_EQ(size_t(0),
__llvm_libc::fread_unlocked(data, 1, sizeof(READ_SIZE), f));
ASSERT_NE(__llvm_libc::ferror_unlocked(f), 0);
ASSERT_NE(errno, 0);
errno = 0;

__llvm_libc::clearerr_unlocked(f);
ASSERT_EQ(__llvm_libc::ferror_unlocked(f), 0);

__llvm_libc::funlockfile(f);
ASSERT_EQ(0, __llvm_libc::fclose(f));

f = __llvm_libc::fopen(fNAME, "r");
ASSERT_FALSE(f == nullptr);

__llvm_libc::flockfile(f);
ASSERT_EQ(__llvm_libc::fread_unlocked(data, 1, READ_SIZE, f), READ_SIZE);
ASSERT_EQ(__llvm_libc::fread_unlocked(data + READ_SIZE, 1, READ_SIZE, f),
READ_SIZE);
__llvm_libc::funlockfile(file);

// Should be an error to write.
ASSERT_EQ(size_t(0),
__llvm_libc::fwrite_unlocked(CONTENT, 1, sizeof(CONTENT), f));
ASSERT_NE(__llvm_libc::ferror_unlocked(f), 0);
ASSERT_NE(errno, 0);
errno = 0;

__llvm_libc::clearerr_unlocked(f);
ASSERT_EQ(__llvm_libc::ferror_unlocked(f), 0);

// Reading more should trigger eof.
char large_data[sizeof(CONTENT)];
ASSERT_NE(sizeof(CONTENT),
__llvm_libc::fread_unlocked(large_data, 1, sizeof(CONTENT), f));
ASSERT_NE(__llvm_libc::feof_unlocked(f), 0);

__llvm_libc::funlockfile(f);
ASSERT_STREQ(data, "1234567890");

ASSERT_EQ(__llvm_libc::fclose(file), 0);
ASSERT_EQ(__llvm_libc::fclose(f), 0);
}