Coverage Report

Created: 2024-03-08 06:32

/src/wget2/libwget/strlcpy.c
Line
Count
Source (jump to first uncovered line)
1
/*
2
 * Copyright (c) 2013 Tim Ruehsen
3
 * Copyright (c) 2015-2024 Free Software Foundation, Inc.
4
 *
5
 * This file is part of libwget.
6
 *
7
 * Libwget is free software: you can redistribute it and/or modify
8
 * it under the terms of the GNU Lesser General Public License as published by
9
 * the Free Software Foundation, either version 3 of the License, or
10
 * (at your option) any later version.
11
 *
12
 * Libwget is distributed in the hope that it will be useful,
13
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15
 * GNU Lesser General Public License for more details.
16
 *
17
 * You should have received a copy of the GNU Lesser General Public License
18
 * along with libwget.  If not, see <https://www.gnu.org/licenses/>.
19
 *
20
 *
21
 * a collection of compatibility routines
22
 *
23
 * Changelog
24
 * 11.01.2013  Tim Ruehsen  created
25
 *
26
 */
27
28
#include <config.h>
29
30
#include <stddef.h>
31
#include <string.h>
32
33
#include <wget.h>
34
35
/**
36
 * \ingroup libwget-utils
37
 * \param[out] dst Output string buffer
38
 * \param[in] src Input string
39
 * \param[in] size Size of \p dst
40
 * \return Length of \p src
41
 *
42
 * Copy string \p src into \p dst with overflow checking.
43
 *
44
 * This is the same as snprintf(dst,size,"%s",src) but faster and more elegant.
45
 *
46
 * If \p src is %NULL, the return value is 0 and nothing is written.
47
 * If \ dst is %NULL, the return value is the length of \p src and nothing is written.
48
 */
49
size_t wget_strlcpy(char *dst, const char *src, size_t size)
50
0
{
51
0
  if (!src)
52
0
    return 0;
53
54
0
  if (!dst)
55
0
    return strlen(src);
56
57
0
#ifndef HAVE_STRLCPY
58
0
  const char *old = src;
59
60
  // Copy as many bytes as will fit
61
0
  if (size) {
62
0
    while (--size) {
63
0
      if (!(*dst++ = *src++))
64
0
        return src - old - 1;
65
0
    }
66
67
0
    *dst = 0;
68
0
  }
69
70
0
  while (*src++);
71
0
  return src - old - 1;
72
#else
73
  return strlcpy(dst, src, size);
74
#endif
75
0
}