From 34d29d3160647cc12313f4a863b02c8d0e8c0db6 Mon Sep 17 00:00:00 2001 From: Mark Pizzolato Date: Mon, 27 Mar 2017 07:51:13 -0700 Subject: [PATCH] SCP: Add sim_strlcpy and sim_strlcat for platforms missing strlcpy and strlcat --- scp.c | 67 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++- scp.h | 2 ++ 2 files changed, 68 insertions(+), 1 deletion(-) diff --git a/scp.c b/scp.c index 8aeb5a63..fefd3328 100644 --- a/scp.c +++ b/scp.c @@ -7816,7 +7816,7 @@ return 0; } /* strcasecmp() is not available on all platforms */ -int sim_strcasecmp (const char* string1, const char* string2) +int sim_strcasecmp (const char *string1, const char *string2) { size_t i = 0; unsigned char s1, s2; @@ -7842,6 +7842,71 @@ while (1) { return 0; } +/* strlcat() and strlcpy() are not available on all platforms */ +/* Copyright (c) 1998 Todd C. Miller */ +/* + * Appends src to string dst of size siz (unlike strncat, siz is the + * full size of dst, not space left). At most siz-1 characters + * will be copied. Always NUL terminates (unless siz <= strlen(dst)). + * Returns strlen(src) + MIN(siz, strlen(initial dst)). + * If retval >= siz, truncation occurred. + */ +size_t sim_strlcat(char *dst, const char *src, size_t size) +{ +char *d = dst; +const char *s = src; +size_t n = size; +size_t dlen; + +/* Find the end of dst and adjust bytes left but don't go past end */ +while (n-- != 0 && *d != '\0') + d++; +dlen = d - dst; +n = size - dlen; + +if (n == 0) + return (dlen + strlen(s)); +while (*s != '\0') { + if (n != 1) { + *d++ = *s; + n--; + } + s++; + } +*d = '\0'; + +return (dlen + (s - src)); /* count does not include NUL */ +} + +/* + * Copy src to string dst of size siz. At most siz-1 characters + * will be copied. Always NUL terminates (unless siz == 0). + * Returns strlen(src); if retval >= siz, truncation occurred. + */ +size_t sim_strlcpy (char *dst, const char *src, size_t size) +{ +char *d = dst; +const char *s = src; +size_t n = size; + +/* Copy as many bytes as will fit */ +if (n != 0) { + while (--n != 0) { + if ((*d++ = *s++) == '\0') + break; + } + } + + /* Not enough room in dst, add NUL and traverse rest of src */ + if (n == 0) { + if (size != 0) + *d = '\0'; /* NUL-terminate dst */ + while (*s++) + ; + } +return (s - src - 1); /* count does not include NUL */ +} + /* get_yn yes/no question Inputs: diff --git a/scp.h b/scp.h index 5484599d..7da85ec8 100644 --- a/scp.h +++ b/scp.h @@ -156,6 +156,8 @@ int sim_isgraph (char c); int sim_isalnum (char c); int sim_strncasecmp (const char *string1, const char *string2, size_t len); int sim_strcasecmp (const char *string1, const char *string2); +size_t sim_strlcat (char *dst, const char *src, size_t size); +size_t sim_strlcpy (char *dst, const char *src, size_t size); CONST char *get_sim_opt (int32 opt, CONST char *cptr, t_stat *st); const char *put_switches (char *buf, size_t bufsize, uint32 sw); CONST char *get_glyph (const char *iptr, char *optr, char mchar);