Skip to content

Commit 55b7403

Browse files
authored
[libc][POSIX] implement fseeko, ftello (#86928)
Fixes: #85287
1 parent 5bbc640 commit 55b7403

File tree

11 files changed

+130
-4
lines changed

11 files changed

+130
-4
lines changed

libc/config/linux/x86_64/entrypoints.txt

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -681,6 +681,8 @@ if(LLVM_LIBC_FULL_BUILD)
681681
libc.src.stdio.fread_unlocked
682682
libc.src.stdio.fseek
683683
libc.src.stdio.ftell
684+
libc.src.stdio.fseeko
685+
libc.src.stdio.ftello
684686
libc.src.stdio.funlockfile
685687
libc.src.stdio.fwrite
686688
libc.src.stdio.fwrite_unlocked

libc/src/stdio/CMakeLists.txt

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -270,6 +270,8 @@ add_stdio_entrypoint_object(ferror)
270270
add_stdio_entrypoint_object(ferror_unlocked)
271271
add_stdio_entrypoint_object(fseek)
272272
add_stdio_entrypoint_object(ftell)
273+
add_stdio_entrypoint_object(fseeko)
274+
add_stdio_entrypoint_object(ftello)
273275
add_stdio_entrypoint_object(fflush)
274276
add_stdio_entrypoint_object(clearerr)
275277
add_stdio_entrypoint_object(clearerr_unlocked)

libc/src/stdio/fseeko.h

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
//===-- Implementation header of fseeko -------------------------*- C++ -*-===//
2+
//
3+
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4+
// See https://llvm.org/LICENSE.txt for license information.
5+
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6+
//
7+
//===----------------------------------------------------------------------===//
8+
9+
#ifndef LLVM_LIBC_SRC_STDIO_FSEEKO_H
10+
#define LLVM_LIBC_SRC_STDIO_FSEEKO_H
11+
12+
#include <stdio.h>
13+
14+
namespace LIBC_NAMESPACE {
15+
16+
int fseeko(::FILE *stream, off_t offset, int whence);
17+
18+
} // namespace LIBC_NAMESPACE
19+
20+
#endif // LLVM_LIBC_SRC_STDIO_FSEEKO_H

libc/src/stdio/ftello.h

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
//===-- Implementation header of ftello -------------------------*- C++ -*-===//
2+
//
3+
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4+
// See https://llvm.org/LICENSE.txt for license information.
5+
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6+
//
7+
//===----------------------------------------------------------------------===//
8+
9+
#ifndef LLVM_LIBC_SRC_STDIO_FTELLO_H
10+
#define LLVM_LIBC_SRC_STDIO_FTELLO_H
11+
12+
#include <stdio.h>
13+
14+
namespace LIBC_NAMESPACE {
15+
16+
off_t ftello(::FILE *f);
17+
18+
} // namespace LIBC_NAMESPACE
19+
20+
#endif // LLVM_LIBC_SRC_STDIO_FTELLO_H

libc/src/stdio/generic/CMakeLists.txt

Lines changed: 24 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -103,7 +103,6 @@ add_entrypoint_object(
103103
../fseek.h
104104
DEPENDS
105105
libc.src.errno.errno
106-
libc.include.stdio
107106
libc.src.__support.File.file
108107
libc.src.__support.File.platform_file
109108
)
@@ -116,7 +115,30 @@ add_entrypoint_object(
116115
../ftell.h
117116
DEPENDS
118117
libc.src.errno.errno
119-
libc.include.stdio
118+
libc.src.__support.File.file
119+
libc.src.__support.File.platform_file
120+
)
121+
122+
add_entrypoint_object(
123+
fseeko
124+
SRCS
125+
fseeko.cpp
126+
HDRS
127+
../fseeko.h
128+
DEPENDS
129+
libc.src.errno.errno
130+
libc.src.__support.File.file
131+
libc.src.__support.File.platform_file
132+
)
133+
134+
add_entrypoint_object(
135+
ftello
136+
SRCS
137+
ftello.cpp
138+
HDRS
139+
../ftello.h
140+
DEPENDS
141+
libc.src.errno.errno
120142
libc.src.__support.File.file
121143
libc.src.__support.File.platform_file
122144
)

libc/src/stdio/generic/fseek.cpp

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,6 @@
1010
#include "src/__support/File/file.h"
1111

1212
#include "src/errno/libc_errno.h"
13-
#include <stdio.h>
1413

1514
namespace LIBC_NAMESPACE {
1615

libc/src/stdio/generic/fseeko.cpp

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
//===-- Implementation of fseeko ------------------------------------------===//
2+
//
3+
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4+
// See https://llvm.org/LICENSE.txt for license information.
5+
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6+
//
7+
//===----------------------------------------------------------------------===//
8+
9+
#include "src/stdio/fseeko.h"
10+
#include "src/__support/File/file.h"
11+
12+
#include "src/errno/libc_errno.h"
13+
14+
namespace LIBC_NAMESPACE {
15+
16+
LLVM_LIBC_FUNCTION(int, fseeko, (::FILE * stream, off_t offset, int whence)) {
17+
auto result =
18+
reinterpret_cast<LIBC_NAMESPACE::File *>(stream)->seek(offset, whence);
19+
if (!result.has_value()) {
20+
libc_errno = result.error();
21+
return -1;
22+
}
23+
return 0;
24+
}
25+
26+
} // namespace LIBC_NAMESPACE

libc/src/stdio/generic/ftell.cpp

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,6 @@
1010
#include "src/__support/File/file.h"
1111

1212
#include "src/errno/libc_errno.h"
13-
#include <stdio.h>
1413

1514
namespace LIBC_NAMESPACE {
1615

libc/src/stdio/generic/ftello.cpp

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
//===-- Implementation of ftello ------------------------------------------===//
2+
//
3+
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4+
// See https://llvm.org/LICENSE.txt for license information.
5+
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6+
//
7+
//===----------------------------------------------------------------------===//
8+
9+
#include "src/stdio/ftello.h"
10+
#include "src/__support/File/file.h"
11+
12+
#include "src/errno/libc_errno.h"
13+
14+
namespace LIBC_NAMESPACE {
15+
16+
LLVM_LIBC_FUNCTION(off_t, ftello, (::FILE * stream)) {
17+
auto result = reinterpret_cast<LIBC_NAMESPACE::File *>(stream)->tell();
18+
if (!result.has_value()) {
19+
libc_errno = result.error();
20+
return -1;
21+
}
22+
return result.value();
23+
}
24+
25+
} // namespace LIBC_NAMESPACE

libc/test/src/stdio/CMakeLists.txt

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -442,6 +442,8 @@ add_libc_test(
442442
libc.src.stdio.fread
443443
libc.src.stdio.fseek
444444
libc.src.stdio.ftell
445+
libc.src.stdio.fseeko
446+
libc.src.stdio.ftello
445447
libc.src.stdio.fwrite
446448
libc.src.stdio.setvbuf
447449
)

libc/test/src/stdio/ftell_test.cpp

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,9 @@
1010
#include "src/stdio/fopen.h"
1111
#include "src/stdio/fread.h"
1212
#include "src/stdio/fseek.h"
13+
#include "src/stdio/fseeko.h"
1314
#include "src/stdio/ftell.h"
15+
#include "src/stdio/ftello.h"
1416
#include "src/stdio/fwrite.h"
1517
#include "src/stdio/setvbuf.h"
1618
#include "test/UnitTest/Test.h"
@@ -37,6 +39,13 @@ class LlvmLibcFTellTest : public LIBC_NAMESPACE::testing::Test {
3739
// still return the correct effective offset.
3840
ASSERT_EQ(size_t(LIBC_NAMESPACE::ftell(file)), WRITE_SIZE);
3941

42+
off_t offseto = 42;
43+
ASSERT_EQ(0, LIBC_NAMESPACE::fseeko(file, offseto, SEEK_SET));
44+
ASSERT_EQ(LIBC_NAMESPACE::ftello(file), offseto);
45+
ASSERT_EQ(0, LIBC_NAMESPACE::fseeko(file, -offseto, SEEK_END));
46+
ASSERT_EQ(size_t(LIBC_NAMESPACE::ftello(file)),
47+
size_t(WRITE_SIZE - offseto));
48+
4049
long offset = 5;
4150
ASSERT_EQ(0, LIBC_NAMESPACE::fseek(file, offset, SEEK_SET));
4251
ASSERT_EQ(LIBC_NAMESPACE::ftell(file), offset);

0 commit comments

Comments
 (0)