-
Notifications
You must be signed in to change notification settings - Fork 0
/
string.c
71 lines (58 loc) · 1.34 KB
/
string.c
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
/*
* Basic string functions
*
* Copyright (C) 2017 Jonathan Neuschäfer <[email protected]>
*
* 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, version 2.
*
* 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, in the file LICENSE.GPLv2.
*/
#include <string.h>
void *memset(void *s, int c, size_t n)
{
char *p = s;
size_t i;
for (i = 0; i < n; i++) {
p[i] = c;
}
return s;
}
void *memcpy(void *dest, const void *src, size_t n)
{
const char *s = src;
char *d = dest;
size_t i;
for (i = 0; i < n; i++) {
d[i] = s[i];
}
return dest;
}
size_t strlen(const char *s)
{
size_t res = 0;
while (*s++)
res++;
return res;
}
#undef strcmp
int strcmp(const char *a, const char *b)
{
size_t i;
const unsigned char *au = (const unsigned char *)a;
const unsigned char *bu = (const unsigned char *)b;
for (i = 0; au[i] && bu[i]; i++) {
if (au[i] < bu[i])
return -1;
if (au[i] > bu[i])
return 1;
}
return 0;
}