19b6f8b98SMichael Jones //===-- Implementation of stpncpy -----------------------------------------===//
29b6f8b98SMichael Jones //
39b6f8b98SMichael Jones // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
49b6f8b98SMichael Jones // See https://llvm.org/LICENSE.txt for license information.
59b6f8b98SMichael Jones // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
69b6f8b98SMichael Jones //
79b6f8b98SMichael Jones //===----------------------------------------------------------------------===//
89b6f8b98SMichael Jones 
99b6f8b98SMichael Jones #include "src/string/stpncpy.h"
10*7b59fcb7SSiva Chandra Reddy #include "src/string/memory_utils/memset_implementations.h"
119b6f8b98SMichael Jones 
129b6f8b98SMichael Jones #include "src/__support/common.h"
139b6f8b98SMichael Jones 
149b6f8b98SMichael Jones namespace __llvm_libc {
159b6f8b98SMichael Jones 
169b6f8b98SMichael Jones LLVM_LIBC_FUNCTION(char *, stpncpy,
179b6f8b98SMichael Jones                    (char *__restrict dest, const char *__restrict src,
189b6f8b98SMichael Jones                     size_t n)) {
199b6f8b98SMichael Jones   size_t i;
209b6f8b98SMichael Jones   // Copy up until \0 is found.
219b6f8b98SMichael Jones   for (i = 0; i < n && src[i] != '\0'; ++i)
229b6f8b98SMichael Jones     dest[i] = src[i];
239b6f8b98SMichael Jones   // When n>strlen(src), n-strlen(src) \0 are appended.
249b6f8b98SMichael Jones   if (n > i)
25*7b59fcb7SSiva Chandra Reddy     inline_memset(dest + i, 0, n - i);
269b6f8b98SMichael Jones   return dest + i;
279b6f8b98SMichael Jones }
289b6f8b98SMichael Jones 
299b6f8b98SMichael Jones } // namespace __llvm_libc
30