1/**
2 * \file
3 * \brief Library routines cause we don't have a libc
4 */
5
6/*
7 * Copyright (c) 2007, 2008, 2009, 2014, ETH Zurich.
8 * All rights reserved.
9 *
10 * This file is distributed under the terms in the attached LICENSE file.
11 * If you do not find this file, copies can be found by writing to:
12 * ETH Zurich D-INFK, CAB F.78, Universitaetstr. 6, CH-8092 Zurich,
13 * Attn: Systems Group.
14 */
15
16#include <assert.h>
17#include <stdio.h>
18#include <string.h>
19#include <stdint.h>
20
21void __assert(const char *exp, const char *file, const char *func, int line)
22{
23/*     panic("elver assertion \"%s\" failed at %s:%d", exp, file, line); */
24    for (;;) ;
25}
26
27int printf(const char *fmt, ...)
28{
29    return -1;
30}
31
32void *
33memset (void *s, int c, size_t n)
34{
35    uint8_t *p = (uint8_t *)s;
36    for (size_t m = 0; m < n; m++) {
37        *p++ = c;
38    }
39    return s;
40}
41
42char *
43strstr(const char *a, const char *b)
44{
45    const char *res = a;
46
47    for (; *res != '\0'; res++) {
48        if (strcmp(res, b) == 0) {
49            return (char *)res;
50        }
51    }
52
53    return NULL;
54}
55
56void *
57memcpy(void *dst, const void *src, size_t len)
58{
59    char *d = dst;
60    const char *s = src;
61
62    /* check that we don't overlap (should use memmove()) */
63    assert((src < dst && src + len <= dst) || (dst < src && dst + len <= src));
64
65    while (len--)
66        *d++ = *s++;
67
68    return dst;
69}
70
71char *
72strrchr(const char *s, int c)
73{
74    unsigned int i;
75
76    if(strlen(s) == 0)
77        return NULL;
78
79    for(i = strlen(s) - 1; i >= 0; i--) {
80        if(s[i] == c)
81            return (char *)&s[i];
82    }
83
84    return NULL;
85}
86
87int
88strncmp(const char *s1, const char *s2, size_t n)
89{
90    int result;
91
92    for(unsigned int i = 0; i < n; i++) {
93        if((result = s2[i] - s1[i]) != 0) {
94            return result;
95        }
96
97        if(s1[i] == '\0' || s2[i] == '\0') {
98            break;
99        }
100    }
101
102    return 0;
103}
104
105size_t
106strlen(const char *s)
107{
108    size_t i = 0;
109
110    while (*s != '\0') {
111        i++;
112        s++;
113    }
114
115    return i;
116}
117
118int
119strcmp(const char* a, const char* b)
120{
121    while (*a == *b && *a != '\0')
122    {
123        a++;
124        b++;
125    }
126
127    return *a - *b;
128}
129