160cef893SCheng Wang //===-- Implementation of strncpy -----------------------------------------===//
260cef893SCheng Wang //
360cef893SCheng Wang // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
460cef893SCheng Wang // See https://llvm.org/LICENSE.txt for license information.
560cef893SCheng Wang // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
660cef893SCheng Wang //
760cef893SCheng Wang //===----------------------------------------------------------------------===//
860cef893SCheng Wang 
960cef893SCheng Wang #include "src/string/strncpy.h"
1060cef893SCheng Wang 
1160cef893SCheng Wang #include "src/__support/common.h"
1260cef893SCheng Wang #include <stddef.h> // For size_t.
1360cef893SCheng Wang 
1460cef893SCheng Wang namespace __llvm_libc {
1560cef893SCheng Wang 
16*a0b65a7bSMichael Jones LLVM_LIBC_FUNCTION(char *, strncpy,
17*a0b65a7bSMichael Jones                    (char *__restrict dest, const char *__restrict src,
18*a0b65a7bSMichael Jones                     size_t n)) {
1960cef893SCheng Wang   size_t i = 0;
2060cef893SCheng Wang   // Copy up until \0 is found.
2160cef893SCheng Wang   for (; i < n && src[i] != '\0'; ++i)
2260cef893SCheng Wang     dest[i] = src[i];
2360cef893SCheng Wang   // When n>strlen(src), n-strlen(src) \0 are appended.
2460cef893SCheng Wang   for (; i < n; ++i)
2560cef893SCheng Wang     dest[i] = '\0';
2660cef893SCheng Wang   return dest;
2760cef893SCheng Wang }
2860cef893SCheng Wang 
2960cef893SCheng Wang } // namespace __llvm_libc
30