Skip to content

Commit 267443e

Browse files
authored
Merge pull request neutrinolabs#3477 from matt335672/add_strlcpy
Add support for strlcpy()
2 parents 6613d66 + 960ea7c commit 267443e

File tree

3 files changed

+59
-1
lines changed

3 files changed

+59
-1
lines changed

common/string_calls.c

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,23 @@
1515
* See the License for the specific language governing permissions and
1616
* limitations under the License.
1717
*
18+
* The strlcpy implementation is taken from OpenBSD and reformatted. The
19+
* original has the following notice attached:-
20+
* |
21+
* | Copyright (c) 1998, 2015 Todd C. Miller <millert@openbsd.org>
22+
* |
23+
* | Permission to use, copy, modify, and distribute this software for any
24+
* | purpose with or without fee is hereby granted, provided that the above
25+
* | copyright notice and this permission notice appear in all copies.
26+
* |
27+
* | THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
28+
* | WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
29+
* | MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
30+
* | ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
31+
* | WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
32+
* | ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
33+
* | OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.*
34+
*
1835
* generic string handling calls
1936
*/
2037

@@ -448,6 +465,42 @@ g_atoix(const char *str)
448465
return strtol(str, NULL, base);
449466
}
450467

468+
/*****************************************************************************/
469+
#if !defined(HAVE_STRLCPY)
470+
size_t strlcpy(char *dst, const char *src, size_t dsize)
471+
{
472+
const char *osrc = src;
473+
size_t nleft = dsize;
474+
475+
/* Copy as many bytes as will fit. */
476+
if (nleft != 0)
477+
{
478+
while (--nleft != 0)
479+
{
480+
if ((*dst++ = *src++) == '\0')
481+
{
482+
break;
483+
}
484+
}
485+
}
486+
487+
/* Not enough room in dst, add NUL and traverse rest of src. */
488+
if (nleft == 0)
489+
{
490+
if (dsize != 0)
491+
{
492+
*dst = '\0'; /* NUL-terminate dst */
493+
}
494+
while (*src++)
495+
{
496+
;
497+
}
498+
}
499+
500+
return (src - osrc - 1); /* count does not include NUL */
501+
}
502+
#endif
503+
451504
/*****************************************************************************/
452505
int
453506
g_htoi(char *str)

common/string_calls.h

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -296,6 +296,11 @@ int g_strcasecmp(const char *c1, const char *c2);
296296
int g_strncasecmp(const char *c1, const char *c2, int len);
297297
int g_atoi(const char *str);
298298

299+
/* Non-standard but useful functions */
300+
#if !defined(HAVE_STRLCPY)
301+
size_t strlcpy(char *dst, const char *src, size_t dsize);
302+
#endif
303+
299304
/**
300305
* Extends g_atoi(), Converts decimal and hexadecimal number String to integer
301306
*

configure.ac

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -238,7 +238,7 @@ AM_COND_IF([DEVEL_DEBUG],
238238
AC_SEARCH_LIBS([setusercontext], [util])
239239

240240
# Define HAVE_XXXXX macros for some system functions
241-
AC_CHECK_FUNCS([setusercontext getgrouplist clearenv])
241+
AC_CHECK_FUNCS([setusercontext getgrouplist clearenv strlcpy])
242242

243243
# The type used by getgrouplist() is the same type used by getgroups()
244244
AC_TYPE_GETGROUPS

0 commit comments

Comments
 (0)