[liba] implement strlcpy

Change-Id: I083129b31cbb070e8c1d87d7e3db91f8134e3540
This commit is contained in:
Émilie Feral
2016-09-23 17:37:28 +02:00
committed by Romain Goyet
parent a6ed6fcc31
commit 5fdb7d729e
4 changed files with 29 additions and 2 deletions

View File

@@ -2,8 +2,8 @@ SFLAGS += -Iliba/include
liba/src/external/sqlite/mem5.o: CFLAGS += -w
objs += $(addprefix liba/src/, assert.o errno.o malloc.o memcpy.o memset.o powf.o strcmp.o strlen.o external/sqlite/mem5.o)
tests += $(addprefix liba/test/, stdint.c)
objs += $(addprefix liba/src/, assert.o errno.o malloc.o memcpy.o memset.o powf.o strcmp.o strlcpy.o strlen.o external/sqlite/mem5.o)
tests += $(addprefix liba/test/, stdint.c strlcpy.c)
# The use of aeabi-rt could be made conditional to an AEABI target.
# In practice we're always using liba on such a target.

View File

@@ -10,6 +10,7 @@ void * memcpy(void * dst, const void * src, size_t n);
void * memset(void * b, int c, size_t len);
size_t strlen(const char * s);
int strcmp(const char *s1, const char *s2);
size_t strlcpy(char * dst, const char * src, size_t len);
LIBA_END_DECLS

14
liba/src/strlcpy.c Normal file
View File

@@ -0,0 +1,14 @@
#include <string.h>
size_t strlcpy(char * dst, const char * src, size_t len) {
if (len == 0) {
return 0;
}
const char * cur = src;
const char * end = src+len-1;
while (*cur != 0 && cur < end) {
*dst++ = *cur++;
}
*dst = 0;
return cur-src;
}

12
liba/test/strlcpy.c Normal file
View File

@@ -0,0 +1,12 @@
#include <quiz.h>
#include <string.h>
#include <assert.h>
QUIZ_CASE(liba_strlcpy) {
char * t = "Hello";
char buffer[16];
size_t result = strlcpy(buffer, t, 2);
assert(result == 1);
assert(buffer[0] == 'H');
assert(buffer[1] == NULL);
}