1#include "types.h"
2#include "x86.h"
3
4void*
5memset(void *dst, int c, uint n)
6{
7 if ((int)dst%4 == 0 && n%4 == 0){
8 c &= 0xFF;
9 stosl(dst, (c<<24)|(c<<16)|(c<<8)|c, n/4);
10 } else
11 stosb(dst, c, n);
12 return dst;
13}
14
15int
16memcmp(const void *v1, const void *v2, uint n)
17{
18 const uchar *s1, *s2;
19
20 s1 = v1;
21 s2 = v2;
22 while(n-- > 0){
23 if(*s1 != *s2)
24 return *s1 - *s2;
25 s1++, s2++;
26 }
27
28 return 0;
29}
30
31void*
32memmove(void *dst, const void *src, uint n)
33{
34 const char *s;
35 char *d;
36
37 s = src;
38 d = dst;
39 if(s < d && s + n > d){
40 s += n;
41 d += n;
42 while(n-- > 0)
43 *--d = *--s;
44 } else
45 while(n-- > 0)
46 *d++ = *s++;
47
48 return dst;
49}
50
51// memcpy exists to placate GCC. Use memmove.
52void*
53memcpy(void *dst, const void *src, uint n)
54{
55 return memmove(dst, src, n);
56}
57
58int
59strncmp(const char *p, const char *q, uint n)
60{
61 while(n > 0 && *p && *p == *q)
62 n--, p++, q++;
63 if(n == 0)
64 return 0;
65 return (uchar)*p - (uchar)*q;
66}
67
68char*
69strncpy(char *s, const char *t, int n)
70{
71 char *os;
72
73 os = s;
74 while(n-- > 0 && (*s++ = *t++) != 0)
75 ;
76 while(n-- > 0)
77 *s++ = 0;
78 return os;
79}
80
81// Like strncpy but guaranteed to NUL-terminate.
82char*
83safestrcpy(char *s, const char *t, int n)
84{
85 char *os;
86
87 os = s;
88 if(n <= 0)
89 return os;
90 while(--n > 0 && (*s++ = *t++) != 0)
91 ;
92 *s = 0;
93 return os;
94}
95
96int
97strlen(const char *s)
98{
99 int n;
100
101 for(n = 0; s[n]; n++)
102 ;
103 return n;
104}
105
106