1 //===-- Implementation of strncpy -----------------------------------------===//
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/string/strncpy.h"
10 
11 #include "src/__support/common.h"
12 #include <stddef.h> // For size_t.
13 
14 namespace __llvm_libc {
15 
LLVM_LIBC_ENTRYPOINT(strncpy)16 char *LLVM_LIBC_ENTRYPOINT(strncpy)(char *__restrict dest,
17                                     const char *__restrict src, size_t n) {
18   size_t i = 0;
19   // Copy up until \0 is found.
20   for (; i < n && src[i] != '\0'; ++i)
21     dest[i] = src[i];
22   // When n>strlen(src), n-strlen(src) \0 are appended.
23   for (; i < n; ++i)
24     dest[i] = '\0';
25   return dest;
26 }
27 
28 } // namespace __llvm_libc
29