]> git.proxmox.com Git - mirror_frr.git/blame - lib/strlcpy.c
zebra, lib: fix the ZEBRA_INTERFACE_VRF_UPDATE zapi message
[mirror_frr.git] / lib / strlcpy.c
CommitLineData
c5d9d3bb 1/* Copy a null-terminated string to a fixed-size buffer, with length checking.
896014f4
DL
2 * Copyright (C) 2016 Free Software Foundation, Inc.
3 * This file is part of the GNU C Library.
4 *
5 * The GNU C Library is free software; you can redistribute it and/or
6 * modify it under the terms of the GNU Lesser General Public
7 * License as published by the Free Software Foundation; either
8 * version 2.1 of the License, or (at your option) any later version.
9 *
10 * The GNU C Library is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 * Lesser General Public License for more details.
14 *
15 * You should have received a copy of the GNU Lesser General Public
16 * License along with the GNU C Library; if not, see
d62a17ae 17 * <http://www.gnu.org/licenses/>.
896014f4 18 */
c5d9d3bb
DL
19
20/* adapted for Quagga from glibc patch submission originally from
21 * Florian Weimer <fweimer@redhat.com>, 2016-05-18 */
22
b45ac5f5 23#ifdef HAVE_CONFIG_H
c5d9d3bb 24#include "config.h"
b45ac5f5
DL
25#endif
26
27#include <string.h>
c5d9d3bb
DL
28
29#ifndef HAVE_STRLCPY
30#undef strlcpy
31
c9a164df
DS
32size_t strlcpy(char *__restrict dest,
33 const char *__restrict src, size_t destsize);
c5d9d3bb 34
c9a164df
DS
35size_t strlcpy(char *__restrict dest,
36 const char *__restrict src, size_t destsize)
c5d9d3bb 37{
d62a17ae 38 size_t src_length = strlen(src);
c5d9d3bb 39
c9a164df
DS
40 if (__builtin_expect(src_length >= destsize, 0)) {
41 if (destsize > 0) {
42 /*
43 * Copy the leading portion of the string. The last
44 * character is subsequently overwritten with the NUL
45 * terminator, but the destination destsize is usually
46 * a multiple of a small power of two, so writing it
47 * twice should be more efficient than copying an odd
48 * number of bytes.
49 */
50 memcpy(dest, src, destsize);
51 dest[destsize - 1] = '\0';
d62a17ae 52 }
53 } else
54 /* Copy the string and its terminating NUL character. */
55 memcpy(dest, src, src_length + 1);
56 return src_length;
c5d9d3bb
DL
57}
58#endif /* HAVE_STRLCPY */