Xen Test Framework
string.c
Go to the documentation of this file.
1#include <xtf/libc.h>
2
3size_t (strlen)(const char *str)
4{
5 const char *s = str;
6
7 while ( *s != '\0' )
8 ++s;
9
10 return s - str;
11}
12
13size_t strnlen(const char *str, size_t max)
14{
15 const char *s = str;
16
17 while ( max-- && *s != '\0' )
18 ++s;
19
20 return s - str;
21}
22
23char *(strcpy)(char *dst, const char *src)
24{
25 char *p = dst;
26
27 while ( (*p++ = *src++) )
28 ;
29
30 return dst;
31}
32
33char *(strncpy)(char *dst, const char *src, size_t n)
34{
35 char *p = dst;
36 size_t i;
37
38 for ( i = 0; i < n && src[i]; ++i )
39 p[i] = src[i];
40
41 for ( ; i < n; ++i )
42 p[i] = '\0';
43
44 return dst;
45}
46
47int (strcmp)(const char *_s1, const char *_s2)
48{
49 unsigned char s1, s2;
50
51 do {
52 s1 = *_s1++;
53 s2 = *_s2++;
54 } while ( s1 && s1 == s2 );
55
56 return (s1 < s2) ? -1 : (s1 > s2);
57}
58
59int (strncmp)(const char *_s1, const char *_s2, size_t n)
60{
61 for ( size_t i = 0; i < n; i++)
62 if ( _s1[i] != _s2[i] )
63 return _s1[i] - _s2[i];
64
65 return 0;
66}
67
68void *(memset)(void *s, int c, size_t n)
69{
70 char *p = s;
71
72 while ( n-- )
73 *p++ = c;
74
75 return s;
76}
77
78void *(memcpy)(void *_d, const void *_s, size_t n)
79{
80 char *d = _d;
81 const char *s = _s;
82
83 for ( ; n; --n )
84 *d++ = *s++;
85
86 return _d;
87}
88
89int (memcmp)(const void *s1, const void *s2, size_t n)
90{
91 const unsigned char *u1 = s1, *u2 = s2;
92 int res = 0;
93
94 for ( ; !res && n; --n )
95 res = *u1++ - *u2++;
96
97 return res;
98}
99
100/*
101 * Local variables:
102 * mode: C
103 * c-file-style: "BSD"
104 * c-basic-offset: 4
105 * tab-width: 4
106 * indent-tabs-mode: nil
107 * End:
108 */
static unsigned int str(void)
Definition: lib.h:366
#define max(a, b)
Definition: lib.h:36
__SIZE_TYPE__ size_t
Definition: stddef.h:9
size_t strnlen(const char *str, size_t max)
Definition: string.c:13
void *() memset(void *s, int c, size_t n)
Definition: string.c:68
size_t() strlen(const char *str)
Definition: string.c:3
int() memcmp(const void *s1, const void *s2, size_t n)
Definition: string.c:89
int() strncmp(const char *_s1, const char *_s2, size_t n)
Definition: string.c:59
char *() strcpy(char *dst, const char *src)
Definition: string.c:23
void *() memcpy(void *_d, const void *_s, size_t n)
Definition: string.c:78
char *() strncpy(char *dst, const char *src, size_t n)
Definition: string.c:33
int() strcmp(const char *_s1, const char *_s2)
Definition: string.c:47