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, Haldeneggsteig 4, 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
32char    *strstr(const char *a, const char *b)
33{
34    return NULL;
35}
36
37void *
38memset (void *s, int c, size_t n)
39{
40    uint8_t *p = (uint8_t *)s;
41    for (size_t m = 0; m < n; m++) {
42        *p++ = c;
43    }
44    return s;
45}
46
47void *
48memcpy(void *dst, const void *src, size_t len)
49{
50    char *d = dst;
51    const char *s = src;
52
53    /* check that we don't overlap (should use memmove()) */
54    assert((src < dst && src + len <= dst) || (dst < src && dst + len <= src));
55
56    while (len--)
57        *d++ = *s++;
58
59    return dst;
60}
61
62char *
63strrchr(const char *s, int c)
64{
65    unsigned int i;
66
67    if(strlen(s) == 0)
68        return NULL;
69
70    for(i = strlen(s) - 1; i != 0; i--) {
71        if(s[i] == c)
72            return (char *)&s[i];
73    }
74
75    return NULL;
76}
77
78int
79strncmp(const char *s1, const char *s2, size_t n)
80{
81    int result;
82
83    for(unsigned int i = 0; i < n; i++) {
84        if((result = s2[i] - s1[i]) != 0) {
85            return result;
86        }
87
88        if(s1[i] == '\0' || s2[i] == '\0') {
89            break;
90        }
91    }
92
93    return 0;
94}
95
96size_t
97strlen(const char *s)
98{
99    size_t i = 0;
100
101    while (*s != '\0') {
102        i++;
103        s++;
104    }
105
106    return i;
107}
108
109int
110strcmp(const char* a, const char* b)
111{
112    while (*a == *b && *a != '\0')
113    {
114        a++;
115        b++;
116    }
117
118    return *a - *b;
119}
120
121// Existence implied by (certainly configured) GCC.
122
123void __stack_chk_fail_local(void);
124void __stack_chk_fail(void);
125
126void __stack_chk_fail_local(void)
127{
128  __stack_chk_fail();
129}
130
131void __stack_chk_fail(void)
132{
133    for (;;) ;
134}
135