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 char *strcpy(char *dst, const char *src)
24 * \brief Copy a string to another
26 EXPORT char *strcpy(char *dst, const char *src)
38 * \fn EXPORT char *strncpy(char *dst, const char *src)
39 * \brief Copy at most \a num characters from \a src to \a dst
42 EXPORT char *strncpy(char *dst, const char *src, size_t num)
45 while(*src && num--) *to++ = *src++;
51 * \fn EXPORT char *strcat(char *dst, const char *src)
52 * \brief Append a string onto another
54 EXPORT char *strcat(char *dst, const char *src)
60 while(*src) *to++ = *src++;
67 * \fn EXPORT int strlen(const char *str)
68 * \brief Get the length of a string
70 EXPORT int strlen(const char *str)
73 for(retval = 0; *str != '\0'; str++)
79 * \fn EXPORT int strncmp(const char *s1, const char *s2, size_t len)
80 * \brief Compare two strings with a limit
82 EXPORT int strncmp(const char *s1, const char *s2, size_t len)
84 while(--len && *s1 == *s2 && *s1 != '\0' && *s2 != '\0') {
87 return (int)*s1 - (int)*s2;
91 * \fn EXPORT char *strdup(const char *str)
92 * \brief Duplicate a string using heap memory
93 * \note Defined in POSIX Spec, not C spec
95 EXPORT char *strdup(const char *str)
97 size_t len = strlen(str);
98 char *ret = malloc(len+1);
99 if(ret == NULL) return NULL;
105 * \fn EXPORT char *strndup(const char *str, size_t maxlen)
106 * \brief Duplicate a string into the heap with a maximum length
107 * \param str Input string buffer
108 * \param maxlen Maximum valid size of the \a str buffer
109 * \return Heap string with the same value of \a str
111 EXPORT char *strndup(const char *str, size_t maxlen)
115 for( len = 0; len < maxlen && str[len]; len ++) ;
116 ret = malloc( len + 1);
117 memcpy( ret, str, len );
123 * \fn EXPORT char *strchr(char *str, int character)
124 * \brief Locate a character in a string
126 EXPORT char *strchr(char *str, int character)
130 if(*str == character) return str;
136 * \fn EXPORT char *strrchr(char *str, int character)
137 * \brief Locate the last occurance of a character in a string
139 EXPORT char *strrchr(char *str, int character)
145 if(str[i] == character) return &str[i];
151 * \fn EXPORT char *strstr(char *str1, const char *str2)
152 * \brief Search a \a str1 for the first occurance of \a str2
154 EXPORT char *strstr(char *str1, const char *str2)
156 const char *test = str2;
160 if(*test == '\0') return str1;
161 if(*str1 == *test) test++;
170 * \fn EXPORT void *memset(void *dest, int val, size_t num)
171 * \brief Clear memory with the specified value
173 EXPORT void *memset(void *dest, int val, size_t num)
175 unsigned char *p = dest;
176 while(num--) *p++ = val;
181 * \fn EXPORT void *memcpy(void *dest, const void *src, size_t count)
182 * \brief Copy one memory area to another
184 EXPORT void *memcpy(void *dest, const void *src, size_t count)
186 char *sp = (char *)src;
187 char *dp = (char *)dest;
188 for(;count--;) *dp++ = *sp++;
193 * \fn EXPORT void *memmove(void *dest, const void *src, size_t count)
194 * \brief Copy data in memory, avoiding overlap problems
196 EXPORT void *memmove(void *dest, const void *src, size_t count)
198 char *sp = (char *)src;
199 char *dp = (char *)dest;
200 // Check if corruption will happen
201 if( (unsigned int)dest > (unsigned int)src && (unsigned int)dest < (unsigned int)src+count )
202 for(;count--;) dp[count] = sp[count];
204 for(;count--;) *dp++ = *sp++;
209 * \fn EXPORT int memcmp(const void *mem1, const void *mem2, size_t count)
210 * \brief Compare two regions of memory
211 * \param mem1 Region 1
212 * \param mem2 Region 2
213 * \param count Number of bytes to check
215 EXPORT int memcmp(const void *mem1, const void *mem2, size_t count)
219 if( *(unsigned char*)mem1 != *(unsigned char*)mem2 )
220 return *(unsigned char*)mem1 - *(unsigned char*)mem2;
228 * \fn EXPORT void *memchr(void *ptr, int value, size_t num)
229 * \brief Locates the first occurence of \a value starting at \a ptr
230 * \param ptr Starting memory location
231 * \param value Value to find
232 * \param num Size of memory area to check
234 EXPORT void *memchr(void *ptr, int value, size_t num)
238 if( *(unsigned char*)ptr == (unsigned char)value )