GRASS GIS 8 Programmer's Manual
8.5.0dev(2024)-602118adcc
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
size_t
G_strlcpy
(
char
*
dst
,
const
char
*
src
,
size_t
dsize)
53
{
54
#ifdef HAVE_STRLCPY
55
return
strlcpy(
dst
,
src
, dsize);
56
#else
57
return
G__strlcpy(
dst
,
src
, dsize);
58
#endif
59
}
60
61
static
size_t
G__strlcpy(
char
*restrict
dst
,
const
char
*restrict
src
,
62
size_t
dsize)
63
{
64
const
char
*osrc =
src
;
65
size_t
nleft = dsize;
66
67
/* Copy as many bytes as will fit. */
68
if
(nleft != 0) {
69
while
(--nleft != 0) {
70
if
((*
dst
++ = *
src
++) ==
'\0'
)
71
break
;
72
}
73
}
74
75
/* Not enough room in dst, add NUL and traverse rest of src. */
76
if
(nleft == 0) {
77
if
(dsize != 0)
78
*
dst
=
'\0'
;
/* NUL-terminate dst */
79
while
(*
src
++)
80
;
81
}
82
83
return
(
src
- osrc - 1);
/* count does not include NUL */
84
}
dst
char * dst
Definition:
lz4.h:981
src
const char * src
Definition:
lz4.h:989
G_strlcpy
size_t G_strlcpy(char *dst, const char *src, size_t dsize)
Safe string copy function.
Definition:
strlcpy.c:52
lib
gis
strlcpy.c
Generated on Mon Nov 18 2024 06:59:56 for GRASS GIS 8 Programmer's Manual by
1.9.1