1/*
2 * Copyright 2014, General Dynamics C4 Systems
3 *
4 * This software may be distributed and modified according to the terms of
5 * the GNU General Public License version 2. Note that NO WARRANTY is provided.
6 * See "LICENSE_GPLv2.txt" for details.
7 *
8 * @TAG(GD_GPL)
9 */
10
11#include <config.h>
12#include <assert.h>
13#include <string.h>
14
15word_t strnlen(const char *s, word_t maxlen)
16{
17    word_t len;
18    for (len = 0; len < maxlen && s[len]; len++);
19    return len;
20}
21
22word_t strlcpy(char *dest, const char *src, word_t size)
23{
24    word_t len;
25    for (len = 0; len + 1 < size && src[len]; len++) {
26        dest[len] = src[len];
27    }
28    dest[len] = '\0';
29    return len;
30}
31
32word_t strlcat(char *dest, const char *src, word_t size)
33{
34    word_t len;
35    /* get to the end of dest */
36    for (len = 0; len < size && dest[len]; len++);
37    /* check that dest was at least 'size' length to prevent inserting
38     * a null byte when we shouldn't */
39    if (len < size) {
40        for (; len + 1 < size && *src; len++, src++) {
41            dest[len] = *src;
42        }
43        dest[len] = '\0';
44    }
45    return len;
46}
47