GRASS 8 Programmer's Manual
8.5.0(2026)-8d6ceba290
Loading...
Searching...
No Matches
strlcpy.c
Go to the documentation of this file.
1
/*!
2
* \file lib/gis/strlcpy.c
3
*
4
* \brief GIS Library - GRASS implementation of strlcpy().
5
*
6
* Loïc Bartoletti - 2024-07-25
7
*
8
* Copyright (c) 1998, 2015 Todd C. Miller <Todd.Miller@courtesan.com>
9
*
10
* Permission to use, copy, modify, and distribute this software for any
11
* purpose with or without fee is hereby granted, provided that the above
12
* copyright notice and this permission notice appear in all copies.
13
*
14
* THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
15
* WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
16
* MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
17
* ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
18
* WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
19
* ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
20
* OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
21
*/
22
23
#include <stddef.h>
24
25
static
size_t
G__strlcpy(
char
*restrict dst,
const
char
*restrict src,
26
size_t
dsize);
27
28
/**
29
* \brief Safe string copy function.
30
*
31
* Copy string src to buffer dst of size dsize. At most dsize-1
32
* characters will be copied. Always NUL terminates (unless dsize == 0).
33
* This function is a safer alternative to strncpy.
34
*
35
* \param[out] dst Pointer to the destination buffer.
36
* \param[in] src Pointer to the source string. Must be a NUL-terminated C
37
* string.
38
* \param[in] dsize The size of the destination buffer.
39
*
40
* \return The total length of the string src (not including the terminating
41
* NUL character). If the return value is >= dsize, truncation occurred.
42
*
43
* \note If truncation occurred, the return value is the length of the string
44
* that would have been created if enough space had been available.
45
*
46
* \warning This function does not pad the destination buffer with NUL bytes
47
* if the source string is shorter than dsize-1 bytes, unlike strncpy.
48
*
49
* \warning The src string must be a valid NUL-terminated C string. Passing an
50
* unterminated string may result in buffer overrun.
51
*
52
* \since version 8.5
53
*/
54
size_t
G_strlcpy
(
char
*dst,
const
char
*src,
size_t
dsize)
55
{
56
#ifdef HAVE_STRLCPY
57
return
strlcpy(dst, src, dsize);
58
#else
59
return
G__strlcpy(dst, src, dsize);
60
#endif
61
}
62
63
static
size_t
G__strlcpy(
char
*restrict dst,
const
char
*restrict src,
64
size_t
dsize)
65
{
66
const
char
*osrc = src;
67
size_t
nleft = dsize;
68
69
/* Copy as many bytes as will fit. */
70
if
(nleft != 0) {
71
while
(--nleft != 0) {
72
if
((*dst++ = *src++) ==
'\0'
)
73
break
;
74
}
75
}
76
77
/* Not enough room in dst, add NUL and traverse rest of src. */
78
if
(nleft == 0) {
79
if
(dsize != 0)
80
*dst =
'\0'
;
/* NUL-terminate dst */
81
while
(*src++)
82
;
83
}
84
85
return
(src - osrc - 1);
/* count does not include NUL */
86
}
G_strlcpy
size_t G_strlcpy(char *dst, const char *src, size_t dsize)
Safe string copy function.
Definition
strlcpy.c:54
gis
strlcpy.c
Generated on
for GRASS 8 Programmer's Manual by
1.15.0