-
Notifications
You must be signed in to change notification settings - Fork 0
/
string.c
125 lines (106 loc) · 1.88 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
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
/*
* linux/lib/string.c
*
* Copyright (C) 1991, 1992 Linus Torvalds
*/
#include "string.h"
size_t strnlen(const char *s, size_t count)
{
const char *sc;
for (sc = s; count-- && *sc != '\0'; ++sc)
/* nothing */;
return sc - s;
}
size_t strlen(const char *s)
{
const char *sc;
for (sc = s; *sc != '\0'; ++sc)
/* nothing */;
return sc - s;
}
char *strncpy(char *dst, const char *src, size_t n)
{
char *ret = dst;
while (n && (*dst++ = *src++))
n--;
while (n--)
*dst++ = 0;
return ret;
}
char *strcpy(char *dst, const char *src)
{
char *ret = dst;
while ((*dst++ = *src++))
;
return ret;
}
int strcmp(const char *p, const char *q)
{
for (;;) {
unsigned char a, b;
a = *p++;
b = *q++;
if (a == 0 || a != b)
return a - b;
}
}
int strncmp(const char *p, const char *q, size_t n)
{
while (n-- != 0) {
unsigned char a, b;
a = *p++;
b = *q++;
if (a == 0 || a != b)
return a - b;
}
return 0;
}
void *memset(void *dst, int x, size_t n)
{
unsigned char *p;
for (p = dst; n; n--)
*p++ = x;
return dst;
}
//
//void udelay(u32 d)
//{
// // should be good to max .2% error
// u32 ticks = d * 19 / 10;
//
// if(ticks < 2)
// ticks = 2;
//
// u32 now = *(vu32*)0x0D800010;
//
// u32 then = now + ticks;
//
// if(then < now) {
// while(*(vu32*)0x0D800010 >= now);
// now = *(vu32*)0x0D800010;
// }
//
// while(now < then) {
// now = *(vu32*)0x0D800010;
// }
//}
int memcmp(const void *s1, const void *s2, size_t n)
{
unsigned char *us1 = (unsigned char *) s1;
unsigned char *us2 = (unsigned char *) s2;
while (n-- != 0) {
if (*us1 != *us2)
return (*us1 < *us2) ? -1 : +1;
us1++;
us2++;
}
return 0;
}
char *strchr(const char *s, int c)
{
do {
if(*s == c)
return (char *)s;
} while(*s++ != 0);
return NULL;
}