seL4/src/string.c
Adrian Danis 82c997aebe Expose string functions in all builds
These are useful beyond just debug and printing builds
2018-04-18 10:10:14 +10:00

46 lines
1.1 KiB
C

/*
* Copyright 2014, General Dynamics C4 Systems
*
* This software may be distributed and modified according to the terms of
* the GNU General Public License version 2. Note that NO WARRANTY is provided.
* See "LICENSE_GPLv2.txt" for details.
*
* @TAG(GD_GPL)
*/
#include <config.h>
#include <assert.h>
#include <string.h>
word_t strnlen(const char *s, word_t maxlen)
{
word_t len;
for (len = 0; len < maxlen && s[len]; len++);
return len;
}
word_t strlcpy(char *dest, const char *src, word_t size)
{
word_t len;
for (len = 0; len + 1 < size && src[len]; len++) {
dest[len] = src[len];
}
dest[len] = '\0';
return len;
}
word_t strlcat(char *dest, const char *src, word_t size)
{
word_t len;
/* get to the end of dest */
for (len = 0; len < size && dest[len]; len++);
/* check that dest was at least 'size' length to prevent inserting
* a null byte when we shouldn't */
if (len < size) {
for (; len + 1 < size && *src; len++, src++) {
dest[len] = *src;
}
dest[len] = '\0';
}
return len;
}