]>
git.proxmox.com Git - mirror_frr.git/blob - lib/strlcpy.c
1 // SPDX-License-Identifier: LGPL-2.1-or-later
2 /* Copy a null-terminated string to a fixed-size buffer, with length checking.
3 * Copyright (C) 2016 Free Software Foundation, Inc.
4 * This file is part of the GNU C Library.
7 /* adapted for Quagga from glibc patch submission originally from
8 * Florian Weimer <fweimer@redhat.com>, 2016-05-18 */
19 size_t strlcpy(char *__restrict dest
,
20 const char *__restrict src
, size_t destsize
);
22 size_t strlcpy(char *__restrict dest
,
23 const char *__restrict src
, size_t destsize
)
25 size_t src_length
= strlen(src
);
27 if (__builtin_expect(src_length
>= destsize
, 0)) {
30 * Copy the leading portion of the string. The last
31 * character is subsequently overwritten with the NUL
32 * terminator, but the destination destsize is usually
33 * a multiple of a small power of two, so writing it
34 * twice should be more efficient than copying an odd
37 memcpy(dest
, src
, destsize
);
38 dest
[destsize
- 1] = '\0';
41 /* Copy the string and its terminating NUL character. */
42 memcpy(dest
, src
, src_length
+ 1);
45 #endif /* HAVE_STRLCPY */