xref: /llvm-project/libc/src/string/strncpy.cpp (revision 5ff3ff33ff930e4ec49da7910612d8a41eb068cb)
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"
12*5ff3ff33SPetr Hosek #include "src/__support/macros/config.h"
1360cef893SCheng Wang #include <stddef.h> // For size_t.
1460cef893SCheng Wang 
15*5ff3ff33SPetr Hosek namespace LIBC_NAMESPACE_DECL {
1660cef893SCheng Wang 
17a0b65a7bSMichael Jones LLVM_LIBC_FUNCTION(char *, strncpy,
18a0b65a7bSMichael Jones                    (char *__restrict dest, const char *__restrict src,
19a0b65a7bSMichael Jones                     size_t n)) {
2060cef893SCheng Wang   size_t i = 0;
2160cef893SCheng Wang   // Copy up until \0 is found.
2260cef893SCheng Wang   for (; i < n && src[i] != '\0'; ++i)
2360cef893SCheng Wang     dest[i] = src[i];
2460cef893SCheng Wang   // When n>strlen(src), n-strlen(src) \0 are appended.
2560cef893SCheng Wang   for (; i < n; ++i)
2660cef893SCheng Wang     dest[i] = '\0';
2760cef893SCheng Wang   return dest;
2860cef893SCheng Wang }
2960cef893SCheng Wang 
30*5ff3ff33SPetr Hosek } // namespace LIBC_NAMESPACE_DECL
31