| //===----------------------------------------------------------------------===// | |
| // | |
| // 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 | |
| // | |
| //===----------------------------------------------------------------------===// | |
| /// | |
| /// \file | |
| /// Implementation of fsetpos | |
| /// | |
| //===----------------------------------------------------------------------===// | |
| #include "src/stdio/fsetpos.h" | |
| #include "hdr/types/FILE.h" | |
| #include "hdr/types/fpos_t.h" | |
| #include "hdr/types/mbstate_t.h" | |
| #include "src/__support/CPP/type_traits.h" | |
| #include "src/__support/File/file.h" | |
| #include "src/__support/common.h" | |
| #include "src/__support/error_or.h" | |
| #include "src/__support/libc_errno.h" | |
| #include "src/__support/macros/config.h" | |
| #include "src/__support/macros/null_check.h" | |
| #include "src/__support/wchar/mbstate.h" | |
| #include "src/string/memory_utils/inline_memcpy.h" | |
| namespace LIBC_NAMESPACE_DECL { | |
| static_assert(sizeof(mbstate_t) >= sizeof(internal::mbstate), | |
| "mbstate_t is too small for internal::mbstate"); | |
| static_assert(alignof(mbstate_t) >= alignof(internal::mbstate), | |
| "mbstate_t has insufficient alignment for internal::mbstate"); | |
| static_assert(cpp::is_trivially_copyable_v<mbstate_t>, | |
| "mbstate_t must be trivially copyable"); | |
| static_assert(cpp::is_trivially_copyable_v<internal::mbstate>, | |
| "internal::mbstate must be trivially copyable"); | |
| LLVM_LIBC_FUNCTION(int, fsetpos, (::FILE * stream, const ::fpos_t *pos)) { | |
| LIBC_CRASH_ON_NULLPTR(stream); | |
| LIBC_CRASH_ON_NULLPTR(pos); | |
| File::Position fpos; | |
| fpos.offset = pos->__pos; | |
| inline_memcpy(&fpos.state, &pos->__state, sizeof(internal::mbstate)); | |
| auto result = reinterpret_cast<LIBC_NAMESPACE::File *>(stream)->set_pos(fpos); | |
| if (!result.has_value()) { | |
| libc_errno = result.error(); | |
| return -1; | |
| } | |
| return 0; | |
| } | |
| } // namespace LIBC_NAMESPACE_DECL |