summaryrefslogtreecommitdiff
path: root/kern/string.c
blob: ba95f773e62b7800c452fb323077b6e181b1e809 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
/*
 * Copyright (c) 2012-2017 Richard Braun.
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 *
 *
 * Trivial, portable implementations.
 */

#include <errno.h>
#include <stddef.h>
#include <string.h>

#include <kern/macros.h>
#include <machine/string.h>

#ifndef STRING_ARCH_MEMCPY
void *
memcpy(void *dest, const void *src, size_t n)
{
    const char *src_ptr;
    char *dest_ptr;
    size_t i;

    dest_ptr = dest;
    src_ptr = src;

    for (i = 0; i < n; i++) {
        *dest_ptr = *src_ptr;
        dest_ptr++;
        src_ptr++;
    }

    return dest;
}
#endif /* STRING_ARCH_MEMCPY */

#ifndef STRING_ARCH_MEMMOVE
void *
memmove(void *dest, const void *src, size_t n)
{
    const char *src_ptr;
    char *dest_ptr;
    size_t i;

    if (dest <= src) {
        dest_ptr = dest;
        src_ptr = src;

        for (i = 0; i < n; i++) {
            *dest_ptr = *src_ptr;
            dest_ptr++;
            src_ptr++;
        }
    } else {
        dest_ptr = dest + n - 1;
        src_ptr = src + n - 1;

        for (i = 0; i < n; i++) {
            *dest_ptr = *src_ptr;
            dest_ptr--;
            src_ptr--;
        }
    }

    return dest;
}
#endif /* STRING_ARCH_MEMMOVE */

#ifndef STRING_ARCH_MEMSET
void *
memset(void *s, int c, size_t n)
{
    char *buffer;
    size_t i;

    buffer = s;

    for (i = 0; i < n; i++) {
        buffer[i] = c;
    }

    return s;
}
#endif /* STRING_ARCH_MEMSET */

#ifndef STRING_ARCH_MEMCMP
int
memcmp(const void *s1, const void *s2, size_t n)
{
    const unsigned char *a1, *a2;
    size_t i;

    a1 = s1;
    a2 = s2;

    for (i = 0; i < n; i++) {
        if (a1[i] != a2[i]) {
            return (int)a1[i] - (int)a2[i];
        }
    }

    return 0;
}
#endif /* STRING_ARCH_MEMCMP */

#ifndef STRING_ARCH_STRLEN
size_t
strlen(const char *s)
{
    const char *start;

    start = s;

    while (*s != '\0') {
        s++;
    }

    return (s - start);
}
#endif /* STRING_ARCH_STRLEN */

#ifndef STRING_ARCH_STRCPY
char *
strcpy(char *dest, const char *src)
{
    char *tmp;

    tmp = dest;

    while ((*dest = *src) != '\0') {
        dest++;
        src++;
    }

    return tmp;
}
#endif /* STRING_ARCH_STRCPY */

size_t
strlcpy(char *dest, const char *src, size_t n)
{
    size_t len;

    len = strlen(src);

    if (n == 0) {
        goto out;
    }

    n = (len < n) ? len : n - 1;
    memcpy(dest, src, n);
    dest[n] = '\0';

out:
    return len;
}

#ifndef STRING_ARCH_STRCMP
int
strcmp(const char *s1, const char *s2)
{
    char c1, c2;

    while ((c1 = *s1) == (c2 = *s2)) {
        if (c1 == '\0') {
            return 0;
        }

        s1++;
        s2++;
    }

    /* See C11 7.24.4 Comparison functions */
    return (int)(unsigned char)c1 - (int)(unsigned char)c2;
}
#endif /* STRING_ARCH_STRCMP */

#ifndef STRING_ARCH_STRNCMP
int
strncmp(const char *s1, const char *s2, size_t n)
{
    char c1, c2;

    if (unlikely(n == 0)) {
        return 0;
    }

    while ((n != 0) && (c1 = *s1) == (c2 = *s2)) {
        if (c1 == '\0') {
            return 0;
        }

        n--;
        s1++;
        s2++;
    }

    /* See C11 7.24.4 Comparison functions */
    return (int)(unsigned char)c1 - (int)(unsigned char)c2;
}
#endif /* STRING_ARCH_STRNCMP */

#ifndef STRING_ARCH_STRCHR
char *
strchr(const char *s, int c)
{
    for (;;) {
        if (*s == c) {
            return (char *)s;
        } else if (*s == '\0') {
            return NULL;
        }

        s++;
    }
}
#endif /* STRING_ARCH_STRCHR */

const char *
strerror(int error)
{
    switch (error) {
    case 0:
        return "success";
    case ENOMEM:
        return "out of memory";
    case EAGAIN:
        return "resource temporarily unavailable";
    case EINVAL:
        return "invalid argument";
    case EBUSY:
        return "device or resource busy";
    case EFAULT:
        return "bad address";
    case ENODEV:
        return "no such device";
    case EEXIST:
        return "entry exists";
    case EIO:
        return "input/output error";
    case ESRCH:
        return "no such process";
    case ETIMEDOUT:
        return "timeout error";
    case ENOENT:
        return "no such file or directory";
    default:
        return "unknown error";
    }
}