36 lines · cpp
1//===-- Implementation of stpncpy -----------------------------------------===//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-exception6//7//===----------------------------------------------------------------------===//8 9#include "src/string/stpncpy.h"10#include "src/__support/macros/config.h"11#include "src/__support/macros/null_check.h"12#include "src/string/memory_utils/inline_bzero.h"13 14#include "src/__support/common.h"15 16namespace LIBC_NAMESPACE_DECL {17 18LLVM_LIBC_FUNCTION(char *, stpncpy,19 (char *__restrict dest, const char *__restrict src,20 size_t n)) {21 if (n) {22 LIBC_CRASH_ON_NULLPTR(dest);23 LIBC_CRASH_ON_NULLPTR(src);24 }25 size_t i;26 // Copy up until \0 is found.27 for (i = 0; i < n && src[i] != '\0'; ++i)28 dest[i] = src[i];29 // When n>strlen(src), n-strlen(src) \0 are appended.30 if (n > i)31 inline_bzero(dest + i, n - i);32 return dest + i;33}34 35} // namespace LIBC_NAMESPACE_DECL36