xref: /llvm-project/libc/src/stdio/generic/ftell.cpp (revision 5ff3ff33ff930e4ec49da7910612d8a41eb068cb)
17ac8e26fSJoseph Huber //===-- Implementation of ftell -------------------------------------------===//
27ac8e26fSJoseph Huber //
37ac8e26fSJoseph Huber // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
47ac8e26fSJoseph Huber // See https://llvm.org/LICENSE.txt for license information.
57ac8e26fSJoseph Huber // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
67ac8e26fSJoseph Huber //
77ac8e26fSJoseph Huber //===----------------------------------------------------------------------===//
87ac8e26fSJoseph Huber 
97ac8e26fSJoseph Huber #include "src/stdio/ftell.h"
107ac8e26fSJoseph Huber #include "src/__support/File/file.h"
117ac8e26fSJoseph Huber 
12*5ff3ff33SPetr Hosek #include "src/__support/macros/config.h"
137ac8e26fSJoseph Huber #include "src/errno/libc_errno.h"
147ac8e26fSJoseph Huber 
15*5ff3ff33SPetr Hosek namespace LIBC_NAMESPACE_DECL {
167ac8e26fSJoseph Huber 
177ac8e26fSJoseph Huber LLVM_LIBC_FUNCTION(long, ftell, (::FILE * stream)) {
187ac8e26fSJoseph Huber   auto result = reinterpret_cast<LIBC_NAMESPACE::File *>(stream)->tell();
197ac8e26fSJoseph Huber   if (!result.has_value()) {
207ac8e26fSJoseph Huber     libc_errno = result.error();
217ac8e26fSJoseph Huber     return -1;
227ac8e26fSJoseph Huber   }
237776fba4SMikhail R. Gadelha   // tell() returns an off_t (64-bit signed integer), but this function returns
247776fba4SMikhail R. Gadelha   // a long (32-bit signed integer in 32-bit systems). We add a cast here to
257776fba4SMikhail R. Gadelha   // silence a "implicit conversion loses integer precision" warning when
267776fba4SMikhail R. Gadelha   // compiling for 32-bit systems.
277776fba4SMikhail R. Gadelha   return static_cast<long>(result.value());
287ac8e26fSJoseph Huber }
297ac8e26fSJoseph Huber 
30*5ff3ff33SPetr Hosek } // namespace LIBC_NAMESPACE_DECL
31