SpiderScript! (with a sample script)
[tpg/acess2.git] / Usermode / Libraries / libc.so_src / string.c
1 /*
2  * AcessOS Basic C Library
3  * string.c
4  */
5 #include <acess/sys.h>
6 #include <stdlib.h>
7 #include <stdio.h>
8 #include "lib.h"
9
10 /**
11  * \fn EXPORT int strcmp(const char *s1, const char *s2)
12  * \brief Compare two strings
13  */
14 EXPORT int strcmp(const char *s1, const char *s2)
15 {
16         while(*s1 == *s2 && *s1 != '\0' && *s2 != '\0') {
17                 s1++; s2++;
18         }
19         return (int)*s1 - (int)*s2;
20 }
21
22 /**
23  * \fn EXPORT int strncmp(const char *s1, const char *s2)
24  * \brief Compare two strings
25  */
26 EXPORT int strncmp(const char *s1, const char *s2, size_t n)
27 {
28         if( n == 0 )    return 0;
29         while(n -- && *s1 == *s2 && *s1 != '\0' && *s2 != '\0') {
30                 s1++; s2++;
31         }
32         return (int)*s1 - (int)*s2;
33 }
34
35 /**
36  * \fn EXPORT char *strcpy(char *dst, const char *src)
37  * \brief Copy a string to another
38  */
39 EXPORT char *strcpy(char *dst, const char *src)
40 {
41         char *_dst = dst;
42         while(*src) {
43                 *dst = *src;
44                 src++; dst++;
45         }
46         *dst = '\0';
47         return _dst;
48 }
49
50 /**
51  * \fn EXPORT char *strncpy(char *dst, const char *src)
52  * \brief Copy at most \a num characters from \a src to \a dst
53  * \return \a dst
54  */
55 EXPORT char *strncpy(char *dst, const char *src, size_t num)
56 {
57         char *to = dst;
58         while(*src && num--)    *to++ = *src++;
59         *to = '\0';
60         return dst;
61 }
62
63 /**
64  * \fn EXPORT char *strcat(char *dst, const char *src)
65  * \brief Append a string onto another
66  */
67 EXPORT char *strcat(char *dst, const char *src)
68 {
69         char    *to = dst;
70         // Find the end
71         while(*to)      to++;
72         // Copy
73         while(*src)     *to++ = *src++;
74         // End string
75         *to = '\0';
76         return dst;
77 }
78
79 /**
80  * \fn EXPORT int strlen(const char *str)
81  * \brief Get the length of a string
82  */
83 EXPORT int strlen(const char *str)
84 {
85         int retval;
86         for(retval = 0; *str != '\0'; str++)
87                 retval++;
88         return retval;
89 }
90
91 /**
92  * \fn EXPORT int strncmp(const char *s1, const char *s2, size_t len)
93  * \brief Compare two strings with a limit
94  */
95 EXPORT int strncmp(const char *s1, const char *s2, size_t len)
96 {
97         while(--len && *s1 == *s2 && *s1 != '\0' && *s2 != '\0') {
98                 s1++; s2++;
99         }
100         return (int)*s1 - (int)*s2;
101 }
102
103 /**
104  * \fn EXPORT char *strdup(const char *str)
105  * \brief Duplicate a string using heap memory
106  * \note Defined in POSIX Spec, not C spec
107  */
108 EXPORT char *strdup(const char *str)
109 {
110         size_t  len = strlen(str);
111         char    *ret = malloc(len+1);
112         if(ret == NULL) return NULL;
113         strcpy(ret, str);
114         return ret;
115 }
116
117 /**
118  * \fn EXPORT char *strndup(const char *str, size_t maxlen)
119  * \brief Duplicate a string into the heap with a maximum length
120  * \param str   Input string buffer
121  * \param maxlen        Maximum valid size of the \a str buffer
122  * \return Heap string with the same value of \a str
123  */
124 EXPORT char *strndup(const char *str, size_t maxlen)
125 {
126         size_t  len;
127         char    *ret;
128         for( len = 0; len < maxlen && str[len]; len ++) ;
129         ret = malloc( len + 1);
130         memcpy( ret, str, len );
131         ret[len] = '\0';
132         return ret;
133 }
134
135 /**
136  * \fn EXPORT char *strchr(char *str, int character)
137  * \brief Locate a character in a string
138  */
139 EXPORT char *strchr(char *str, int character)
140 {
141         while(*str)
142         {
143                 if(*str == character)   return str;
144         }
145         return NULL;
146 }
147
148 /**
149  * \fn EXPORT char *strrchr(char *str, int character)
150  * \brief Locate the last occurance of a character in a string
151  */
152 EXPORT char *strrchr(char *str, int character)
153 {
154          int    i;
155         i = strlen(str)-1;
156         while(i--)
157         {
158                 if(str[i] == character) return &str[i];
159         }
160         return NULL;
161 }
162
163 /**
164  * \fn EXPORT char *strstr(char *str1, const char *str2)
165  * \brief Search a \a str1 for the first occurance of \a str2
166  */
167 EXPORT char *strstr(char *str1, const char *str2)
168 {
169         const char      *test = str2;
170         
171         while(*str1)
172         {
173                 if(*test == '\0')       return str1;
174                 if(*str1 == *test)      test++;
175                 else    test = str2;
176                 str1 ++;
177         }
178         return NULL;
179 }
180
181 // --- Memory ---
182 /**
183  * \fn EXPORT void *memset(void *dest, int val, size_t num)
184  * \brief Clear memory with the specified value
185  */
186 EXPORT void *memset(void *dest, int val, size_t num)
187 {
188         unsigned char *p = dest;
189         while(num--)    *p++ = val;
190         return dest;
191 }
192
193 /**
194  * \fn EXPORT void *memcpy(void *dest, const void *src, size_t count)
195  * \brief Copy one memory area to another
196  */
197 EXPORT void *memcpy(void *dest, const void *src, size_t count)
198 {
199     char *sp = (char *)src;
200     char *dp = (char *)dest;
201     for(;count--;) *dp++ = *sp++;
202     return dest;
203 }
204
205 /**
206  * \fn EXPORT void *memmove(void *dest, const void *src, size_t count)
207  * \brief Copy data in memory, avoiding overlap problems
208  */
209 EXPORT void *memmove(void *dest, const void *src, size_t count)
210 {
211     char *sp = (char *)src;
212     char *dp = (char *)dest;
213         // Check if corruption will happen
214         if( (unsigned int)dest > (unsigned int)src && (unsigned int)dest < (unsigned int)src+count )
215                 for(;count--;) dp[count] = sp[count];
216         else
217         for(;count--;) *dp++ = *sp++;
218     return dest;
219 }
220
221 /**
222  * \fn EXPORT int memcmp(const void *mem1, const void *mem2, size_t count)
223  * \brief Compare two regions of memory
224  * \param mem1  Region 1
225  * \param mem2  Region 2
226  * \param count Number of bytes to check
227  */
228 EXPORT int memcmp(const void *mem1, const void *mem2, size_t count)
229 {
230         while(count--)
231         {
232                 if( *(unsigned char*)mem1 != *(unsigned char*)mem2 )
233                         return *(unsigned char*)mem1 - *(unsigned char*)mem2;
234                 mem1 ++;
235                 mem2 ++;
236         }
237         return 0;
238 }
239
240 /**
241  * \fn EXPORT void *memchr(void *ptr, int value, size_t num)
242  * \brief Locates the first occurence of \a value starting at \a ptr
243  * \param ptr   Starting memory location
244  * \param value Value to find
245  * \param num   Size of memory area to check
246  */
247 EXPORT void *memchr(void *ptr, int value, size_t num)
248 {
249         while(num--)
250         {
251                 if( *(unsigned char*)ptr == (unsigned char)value )
252                         return ptr;
253                 ptr ++;
254         }
255         return NULL;
256 }

UCC git Repository :: git.ucc.asn.au