1/**
2 * \file
3 * \brief Library routines cause we don't have a libc
4 */
5
6/*
7 * Copyright (c) 2007, 2008, 2009, 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, Universitaetstrasse 6, CH-8092 Zurich. Attn: Systems Group.
13 */
14
15#include <assert.h>
16#include <stdio.h>
17#include <string.h>
18#include <stdint.h>
19
20
21void __assert(const char *func, const char *file, int line, const char *exp)
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
42void *
43memcpy(void *dst, const void *src, size_t len)
44{
45    char *d = dst;
46    const char *s = src;
47
48    /* check that we don't overlap (should use memmove()) */
49    assert((src < dst && src + len <= dst) || (dst < src && dst + len <= src));
50
51    while (len--)
52        *d++ = *s++;
53
54    return dst;
55}
56
57char *
58strrchr(const char *s, int c)
59{
60    unsigned int i;
61
62    if(strlen(s) == 0)
63        return NULL;
64
65    for(i = strlen(s) - 1; i != 0; i--) {
66        if(s[i] == c)
67            return (char *)&s[i];
68    }
69
70    return NULL;
71}
72
73int
74strncmp(const char *s1, const char *s2, size_t n)
75{
76    int result;
77
78    for(unsigned int i = 0; i < n; i++) {
79        if((result = s2[i] - s1[i]) != 0) {
80            return result;
81        }
82
83        if(s1[i] == '\0' || s2[i] == '\0') {
84            break;
85        }
86    }
87
88    return 0;
89}
90
91size_t
92strlen(const char *s)
93{
94    size_t i = 0;
95
96    while (*s != '\0') {
97        i++;
98        s++;
99    }
100
101    return i;
102}
103
104int
105strcmp(const char* a, const char* b)
106{
107    while (*a == *b && *a != '\0')
108    {
109        a++;
110        b++;
111    }
112
113    return *a - *b;
114}
115