2 * AcessOS Basic C Library
11 * \fn EXPORT int strcmp(const char *s1, const char *s2)
12 * \brief Compare two strings
14 EXPORT int strcmp(const char *s1, const char *s2)
16 while(*s1 == *s2 && *s1 != '\0' && *s2 != '\0') {
19 return (int)*s1 - (int)*s2;
23 * \fn EXPORT int strncmp(const char *s1, const char *s2)
24 * \brief Compare two strings
26 EXPORT int strncmp(const char *s1, const char *s2, size_t n)
28 if( n == 0 ) return 0;
29 while(n -- && *s1 == *s2 && *s1 != '\0' && *s2 != '\0') {
32 return (int)*s1 - (int)*s2;
36 * \fn EXPORT char *strcpy(char *dst, const char *src)
37 * \brief Copy a string to another
39 EXPORT char *strcpy(char *dst, const char *src)
51 * \fn EXPORT char *strncpy(char *dst, const char *src)
52 * \brief Copy at most \a num characters from \a src to \a dst
55 EXPORT char *strncpy(char *dst, const char *src, size_t num)
58 while(*src && num--) *to++ = *src++;
64 * \fn EXPORT char *strcat(char *dst, const char *src)
65 * \brief Append a string onto another
67 EXPORT char *strcat(char *dst, const char *src)
73 while(*src) *to++ = *src++;
80 * \brief Get the length of a string
82 EXPORT size_t strlen(const char *str)
85 for(retval = 0; *str != '\0'; str++, retval++);
90 * \brief Get the length of a string, with a maximum of \a maxlen
92 * Gets the length of a string (excluding the terminating \0 byte)
94 EXPORT size_t strnlen(const char *str, size_t maxlen)
97 for( len = 0; maxlen -- && *str; str ++, len ++ );
102 * \fn EXPORT char *strdup(const char *str)
103 * \brief Duplicate a string using heap memory
104 * \note Defined in POSIX Spec, not C spec
106 EXPORT char *strdup(const char *str)
108 size_t len = strlen(str);
109 char *ret = malloc(len+1);
110 if(ret == NULL) return NULL;
116 * \fn EXPORT char *strndup(const char *str, size_t maxlen)
117 * \brief Duplicate a string into the heap with a maximum length
118 * \param str Input string buffer
119 * \param maxlen Maximum valid size of the \a str buffer
120 * \return Heap string with the same value of \a str
122 EXPORT char *strndup(const char *str, size_t maxlen)
126 for( len = 0; len < maxlen && str[len]; len ++) ;
127 ret = malloc( len + 1);
128 memcpy( ret, str, len );
134 * \fn EXPORT char *strchr(char *str, int character)
135 * \brief Locate a character in a string
137 EXPORT char *strchr(const char *str, int character)
141 if(*str == character)
148 * \fn EXPORT char *strrchr(char *str, int character)
149 * \brief Locate the last occurance of a character in a string
151 EXPORT char *strrchr(const char *str, int character)
157 if(str[i] == character)
158 return (void*)&str[i];
164 * \fn EXPORT char *strstr(char *str1, const char *str2)
165 * \brief Search a \a str1 for the first occurance of \a str2
167 EXPORT char *strstr(char *str1, const char *str2)
169 const char *test = str2;
173 if(*test == '\0') return str1;
174 if(*str1 == *test) test++;
182 * \fn EXPORT void *memset(void *dest, int val, size_t num)
183 * \brief Clear memory with the specified value
185 EXPORT void *memset(void *dest, int val, size_t num)
187 unsigned char *p = dest;
188 while(num--) *p++ = val;
193 * \fn EXPORT void *memcpy(void *dest, const void *src, size_t count)
194 * \brief Copy one memory area to another
196 EXPORT void *memcpy(void *dest, const void *src, size_t count)
198 char *sp = (char *)src;
199 char *dp = (char *)dest;
200 for(;count--;) *dp++ = *sp++;
205 * \fn EXPORT void *memmove(void *dest, const void *src, size_t count)
206 * \brief Copy data in memory, avoiding overlap problems
208 EXPORT void *memmove(void *dest, const void *src, size_t count)
210 char *sp = (char *)src;
211 char *dp = (char *)dest;
212 // Check if corruption will happen
213 if( (intptr_t)dest > (intptr_t)src && (intptr_t)dest < (intptr_t)src+count )
214 for(;count--;) dp[count] = sp[count];
216 for(;count--;) *dp++ = *sp++;
221 * \fn EXPORT int memcmp(const void *mem1, const void *mem2, size_t count)
222 * \brief Compare two regions of memory
223 * \param mem1 Region 1
224 * \param mem2 Region 2
225 * \param count Number of bytes to check
227 EXPORT int memcmp(const void *mem1, const void *mem2, size_t count)
231 if( *(unsigned char*)mem1 != *(unsigned char*)mem2 )
232 return *(unsigned char*)mem1 - *(unsigned char*)mem2;
240 * \fn EXPORT void *memchr(void *ptr, int value, size_t num)
241 * \brief Locates the first occurence of \a value starting at \a ptr
242 * \param ptr Starting memory location
243 * \param value Value to find
244 * \param num Size of memory area to check
246 EXPORT void *memchr(const void *ptr, int value, size_t num)
250 if( *(const unsigned char*)ptr == (unsigned char)value )