This source file includes following definitions.
- strlen
- strcmp
- strncmp
- strcpy
- strncpy
- strcat
- strncat
- strchr
- strpbrk
- strspn
- strtok
- memset
- memcpy
- memmove
- memcmp
1
2
3
4
5
6
7 #ifndef _SPARC_STRING_H
8 #define _SPARC_STRING_H
9
10 extern __inline__ size_t strlen(const char * str)
11 {
12 const char *sc;
13
14 for (sc = str; *sc != '\0'; ++sc)
15 ;
16 return sc - str;
17 }
18
19 extern __inline__ int strcmp(const char* str1, const char* str2)
20 {
21 register signed char __res;
22
23 while (1) {
24 if ((__res = *str1 - *str2++) != 0 || !*str1++)
25 break;
26 }
27
28 return __res;
29 }
30
31 extern __inline__ int strncmp(const char* str1, const char* str2, size_t strlen)
32 {
33 register signed char __res = 0;
34
35 while (strlen) {
36 if ((__res = *str1 - *str2++) != 0 || !*str1++)
37 break;
38 strlen--;
39 }
40
41 return __res;
42 }
43
44
45 extern __inline__ char *strcpy(char* dest, const char* source)
46 {
47 char *tmp = dest;
48
49 while ((*dest++ = *source++) != '\0')
50 ;
51 return tmp;
52 }
53
54 extern __inline__ char *strncpy(char *dest, const char *source, size_t cpylen)
55 {
56 char *tmp = dest;
57
58 while (cpylen-- && (*dest++ = *source++) != '\0')
59 ;
60
61 return tmp;
62 }
63
64 extern __inline__ char *strcat(char *dest, const char *src)
65 {
66 char *tmp = dest;
67
68 while (*dest)
69 dest++;
70 while ((*dest++ = *src++) != '\0')
71 ;
72
73 return tmp;
74 }
75
76 extern __inline__ char *strncat(char *dest, const char *src, size_t len)
77 {
78 char *tmp = dest;
79
80 if (len) {
81 while (*dest)
82 dest++;
83 while ((*dest++ = *src++)) {
84 if (--len == 0)
85 break;
86 }
87 }
88
89 return tmp;
90 }
91
92 extern __inline__ char *strchr(const char *src, int c)
93 {
94 for(; *src != c; ++src)
95 if (*src == '\0')
96 return NULL;
97 return (char *) src;
98 }
99
100 extern __inline__ char *strpbrk(const char *cs, const char *ct)
101 {
102 const char *sc1,*sc2;
103
104 for( sc1 = cs; *sc1 != '\0'; ++sc1) {
105 for( sc2 = ct; *sc2 != '\0'; ++sc2) {
106 if (*sc1 == *sc2)
107 return (char *) sc1;
108 }
109 }
110 return NULL;
111 }
112
113
114 extern __inline__ size_t strspn(const char *s, const char *accept)
115 {
116 const char *p;
117 const char *a;
118 size_t count = 0;
119
120 for (p = s; *p != '\0'; ++p) {
121 for (a = accept; *a != '\0'; ++a) {
122 if (*p == *a)
123 break;
124 }
125 if (*a == '\0')
126 return count;
127 ++count;
128 }
129
130 return count;
131 }
132
133 extern __inline__ char *strtok(char *s, const char *ct)
134 {
135 char *sbegin, *send;
136
137 sbegin = s ? s : ___strtok;
138 if (!sbegin) {
139 return NULL;
140 }
141 sbegin += strspn(sbegin,ct);
142 if (*sbegin == '\0') {
143 ___strtok = NULL;
144 return( NULL );
145 }
146 send = strpbrk( sbegin, ct);
147 if (send && *send != '\0')
148 *send++ = '\0';
149 ___strtok = send;
150 return (sbegin);
151 }
152
153
154 extern __inline__ void *memset(void *src, int c, size_t count)
155 {
156 char *xs = (char *) src;
157
158 while (count--)
159 *xs++ = c;
160
161 return src;
162 }
163
164 extern __inline__ void *memcpy(void *dest, const void *src, size_t count)
165 {
166 char *tmp = (char *) dest, *s = (char *) src;
167
168 while (count--)
169 *tmp++ = *s++;
170
171 return dest;
172 }
173
174 extern __inline__ void *memmove(void *dest, const void *src, size_t count)
175 {
176 char *tmp, *s;
177
178 if (dest <= src) {
179 tmp = (char *) dest;
180 s = (char *) src;
181 while (count--)
182 *tmp++ = *s++;
183 }
184 else {
185 tmp = (char *) dest + count;
186 s = (char *) src + count;
187 while (count--)
188 *--tmp = *--s;
189 }
190
191 return dest;
192 }
193
194 extern __inline__ int memcmp(const void *cs, const void *ct, size_t count)
195 {
196 const unsigned char *su1, *su2;
197 signed char res = 0;
198
199 for( su1 = cs, su2 = ct; 0 < count; ++su1, ++su2, count--)
200 if ((res = *su1 - *su2) != 0)
201 break;
202 return res;
203 }
204
205 #endif