Skip to content

[libc][POSIX] implement fseeko, ftello #86928

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Apr 1, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions libc/config/linux/x86_64/entrypoints.txt
Original file line number Diff line number Diff line change
Expand Up @@ -681,6 +681,8 @@ if(LLVM_LIBC_FULL_BUILD)
libc.src.stdio.fread_unlocked
libc.src.stdio.fseek
libc.src.stdio.ftell
libc.src.stdio.fseeko
libc.src.stdio.ftello
libc.src.stdio.funlockfile
libc.src.stdio.fwrite
libc.src.stdio.fwrite_unlocked
Expand Down
2 changes: 2 additions & 0 deletions libc/src/stdio/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -270,6 +270,8 @@ add_stdio_entrypoint_object(ferror)
add_stdio_entrypoint_object(ferror_unlocked)
add_stdio_entrypoint_object(fseek)
add_stdio_entrypoint_object(ftell)
add_stdio_entrypoint_object(fseeko)
add_stdio_entrypoint_object(ftello)
add_stdio_entrypoint_object(fflush)
add_stdio_entrypoint_object(clearerr)
add_stdio_entrypoint_object(clearerr_unlocked)
Expand Down
20 changes: 20 additions & 0 deletions libc/src/stdio/fseeko.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
//===-- Implementation header of fseeko -------------------------*- 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_FSEEKO_H
#define LLVM_LIBC_SRC_STDIO_FSEEKO_H

#include <stdio.h>

namespace LIBC_NAMESPACE {

int fseeko(::FILE *stream, off_t offset, int whence);

} // namespace LIBC_NAMESPACE

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

#include <stdio.h>

namespace LIBC_NAMESPACE {

off_t ftello(::FILE *f);

} // namespace LIBC_NAMESPACE

#endif // LLVM_LIBC_SRC_STDIO_FTELLO_H
26 changes: 26 additions & 0 deletions libc/src/stdio/generic/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -121,6 +121,32 @@ add_entrypoint_object(
libc.src.__support.File.platform_file
)

add_entrypoint_object(
fseeko
SRCS
fseeko.cpp
HDRS
../fseeko.h
DEPENDS
libc.src.errno.errno
libc.include.stdio
libc.src.__support.File.file
libc.src.__support.File.platform_file
)

add_entrypoint_object(
ftello
SRCS
ftello.cpp
HDRS
../ftello.h
DEPENDS
libc.src.errno.errno
libc.include.stdio
libc.src.__support.File.file
libc.src.__support.File.platform_file
)

add_entrypoint_object(
fopen
SRCS
Expand Down
27 changes: 27 additions & 0 deletions libc/src/stdio/generic/fseeko.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
//===-- Implementation of fseeko ------------------------------------------===//
//
// 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/fseeko.h"
#include "src/__support/File/file.h"

#include "src/errno/libc_errno.h"
#include <stdio.h>

namespace LIBC_NAMESPACE {

LLVM_LIBC_FUNCTION(int, fseeko, (::FILE * stream, off_t offset, int whence)) {
auto result =
reinterpret_cast<LIBC_NAMESPACE::File *>(stream)->seek(offset, whence);
if (!result.has_value()) {
libc_errno = result.error();
return -1;
}
return 0;
}

} // namespace LIBC_NAMESPACE
26 changes: 26 additions & 0 deletions libc/src/stdio/generic/ftello.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
//===-- Implementation of ftello ------------------------------------------===//
//
// 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/ftello.h"
#include "src/__support/File/file.h"

#include "src/errno/libc_errno.h"
#include <stdio.h>

namespace LIBC_NAMESPACE {

LLVM_LIBC_FUNCTION(off_t, ftello, (::FILE * stream)) {
auto result = reinterpret_cast<LIBC_NAMESPACE::File *>(stream)->tell();
if (!result.has_value()) {
libc_errno = result.error();
return -1;
}
return result.value();
}

} // namespace LIBC_NAMESPACE
2 changes: 2 additions & 0 deletions libc/test/src/stdio/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -442,6 +442,8 @@ add_libc_test(
libc.src.stdio.fread
libc.src.stdio.fseek
libc.src.stdio.ftell
libc.src.stdio.fseeko
libc.src.stdio.ftello
libc.src.stdio.fwrite
libc.src.stdio.setvbuf
)
Expand Down
9 changes: 9 additions & 0 deletions libc/test/src/stdio/ftell_test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,9 @@
#include "src/stdio/fopen.h"
#include "src/stdio/fread.h"
#include "src/stdio/fseek.h"
#include "src/stdio/fseeko.h"
#include "src/stdio/ftell.h"
#include "src/stdio/ftello.h"
#include "src/stdio/fwrite.h"
#include "src/stdio/setvbuf.h"
#include "test/UnitTest/Test.h"
Expand All @@ -37,6 +39,13 @@ class LlvmLibcFTellTest : public LIBC_NAMESPACE::testing::Test {
// still return the correct effective offset.
ASSERT_EQ(size_t(LIBC_NAMESPACE::ftell(file)), WRITE_SIZE);

off_t offseto = 42;
ASSERT_EQ(0, LIBC_NAMESPACE::fseeko(file, offseto, SEEK_SET));
ASSERT_EQ(LIBC_NAMESPACE::ftello(file), offseto);
ASSERT_EQ(0, LIBC_NAMESPACE::fseeko(file, -offseto, SEEK_END));
ASSERT_EQ(size_t(LIBC_NAMESPACE::ftello(file)),
size_t(WRITE_SIZE - offseto));

long offset = 5;
ASSERT_EQ(0, LIBC_NAMESPACE::fseek(file, offset, SEEK_SET));
ASSERT_EQ(LIBC_NAMESPACE::ftell(file), offset);
Expand Down