Skip to content

Commit a64ef54

Browse files
authored
emulation on host: add missing strlcat strlcpy (esp8266#6327)
1 parent 82a1382 commit a64ef54

File tree

3 files changed

+90
-0
lines changed

3 files changed

+90
-0
lines changed

tests/host/Makefile

+1
Original file line numberDiff line numberDiff line change
@@ -287,6 +287,7 @@ MOCK_ARDUINO_LIBS := $(addprefix common/,\
287287
MockEsp.cpp \
288288
MockEEPROM.cpp \
289289
MockSPI.cpp \
290+
strl.cpp \
290291
)
291292

292293
CPP_SOURCES_CORE_EMU = \

tests/host/common/mock.h

+3
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@
3737
#ifdef __cplusplus
3838
#include <vector>
3939
#endif
40+
#include <stddef.h>
4041

4142

4243
#ifdef __cplusplus
@@ -49,6 +50,8 @@ char* ltoa (long val, char *s, int radix);
4950
}
5051
#endif
5152

53+
size_t strlcat(char *dst, const char *src, size_t size);
54+
size_t strlcpy(char *dst, const char *src, size_t size);
5255

5356
// exotic typedefs used in the sdk
5457

tests/host/common/strl.cpp

+86
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,86 @@
1+
// https://gist.github.com/Fonger/98cc95ac39fbe1a7e4d9
2+
3+
#ifndef HAVE_STRLCAT
4+
/*
5+
'_cups_strlcat()' - Safely concatenate two strings.
6+
*/
7+
8+
size_t /* O - Length of string */
9+
strlcat(char *dst, /* O - Destination string */
10+
const char *src, /* I - Source string */
11+
size_t size) /* I - Size of destination string buffer */
12+
{
13+
size_t srclen; /* Length of source string */
14+
size_t dstlen; /* Length of destination string */
15+
16+
17+
/*
18+
Figure out how much room is left...
19+
*/
20+
21+
dstlen = strlen(dst);
22+
size -= dstlen + 1;
23+
24+
if (!size)
25+
{
26+
return (dstlen); /* No room, return immediately... */
27+
}
28+
29+
/*
30+
Figure out how much room is needed...
31+
*/
32+
33+
srclen = strlen(src);
34+
35+
/*
36+
Copy the appropriate amount...
37+
*/
38+
39+
if (srclen > size)
40+
{
41+
srclen = size;
42+
}
43+
44+
memcpy(dst + dstlen, src, srclen);
45+
dst[dstlen + srclen] = '\0';
46+
47+
return (dstlen + srclen);
48+
}
49+
#endif /* !HAVE_STRLCAT */
50+
51+
#ifndef HAVE_STRLCPY
52+
/*
53+
'_cups_strlcpy()' - Safely copy two strings.
54+
*/
55+
56+
size_t /* O - Length of string */
57+
strlcpy(char *dst, /* O - Destination string */
58+
const char *src, /* I - Source string */
59+
size_t size) /* I - Size of destination string buffer */
60+
{
61+
size_t srclen; /* Length of source string */
62+
63+
64+
/*
65+
Figure out how much room is needed...
66+
*/
67+
68+
size --;
69+
70+
srclen = strlen(src);
71+
72+
/*
73+
Copy the appropriate amount...
74+
*/
75+
76+
if (srclen > size)
77+
{
78+
srclen = size;
79+
}
80+
81+
memcpy(dst, src, srclen);
82+
dst[srclen] = '\0';
83+
84+
return (srclen);
85+
}
86+
#endif /* !HAVE_STRLCPY */

0 commit comments

Comments
 (0)