-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathstring.c
More file actions
91 lines (79 loc) · 1.5 KB
/
Copy pathstring.c
File metadata and controls
91 lines (79 loc) · 1.5 KB
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
#include "string.h"
int k_strcmp(const char *s1, const char *s2) {
while (*s1 && (*s1 == *s2)) {
s1++;
s2++;
}
return *(unsigned char *)s1 - *(unsigned char *)s2;
}
size_t k_strlen(const char *str) {
size_t len = 0;
while (str[len])
len++;
return len;
}
char *k_strcpy(char *dest, const char *src) {
char *ret = dest;
while ((*dest++ = *src++))
;
return ret;
}
void *k_memset(void *s, int c, size_t n) {
unsigned char *p = s;
while (n--)
*p++ = (unsigned char)c;
return s;
}
void *k_memcpy(void *dest, const void *src, size_t n) {
char *d = dest;
const char *s = src;
while (n--)
*d++ = *s++;
return dest;
}
void *k_memmove(void *dest, const void *src, size_t n) {
unsigned char *d = dest;
const unsigned char *s = src;
if (d < s) {
while (n--)
*d++ = *s++;
} else {
d += n;
s += n;
while (n--)
*--d = *--s;
}
return dest;
}
int k_strncmp(const char *s1, const char *s2, size_t n) {
while (n && *s1 && (*s1 == *s2)) {
s1++;
s2++;
n--;
}
if (n == 0)
return 0;
return *(unsigned char *)s1 - *(unsigned char *)s2;
}
static void reverse(char s[]) {
int i, j;
char c;
for (i = 0, j = k_strlen(s) - 1; i < j; i++, j--) {
c = s[i];
s[i] = s[j];
s[j] = c;
}
}
void itoa(int n, char s[], int base) {
int i, sign;
if ((sign = n) < 0)
n = -n;
i = 0;
do {
s[i++] = n % base + '0';
} while ((n /= base) > 0);
if (sign < 0)
s[i++] = '-';
s[i] = '\0';
reverse(s);
}