Fixed behavior of VTerm when driver is set at runtime
[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 char *strcpy(char *dst, const char *src)
24  * \brief Copy a string to another
25  */
26 EXPORT char *strcpy(char *dst, const char *src)
27 {
28         char *_dst = dst;
29         while(*src) {
30                 *dst = *src;
31                 src++; dst++;
32         }
33         *dst = '\0';
34         return _dst;
35 }
36
37 /**
38  * \fn EXPORT char *strncpy(char *dst, const char *src)
39  * \brief Copy at most \a num characters from \a src to \a dst
40  * \return \a dst
41  */
42 EXPORT char *strncpy(char *dst, const char *src, size_t num)
43 {
44         char *to = dst;
45         while(*src && num--)    *to++ = *src++;
46         *to = '\0';
47         return dst;
48 }
49
50 /**
51  * \fn EXPORT char *strcat(char *dst, const char *src)
52  * \brief Append a string onto another
53  */
54 EXPORT char *strcat(char *dst, const char *src)
55 {
56         char    *to = dst;
57         // Find the end
58         while(*to)      to++;
59         // Copy
60         while(*src)     *to++ = *src++;
61         // End string
62         *to = '\0';
63         return dst;
64 }
65
66 /**
67  * \fn EXPORT int strlen(const char *str)
68  * \brief Get the length of a string
69  */
70 EXPORT int strlen(const char *str)
71 {
72         int retval;
73         for(retval = 0; *str != '\0'; str++)
74                 retval++;
75         return retval;
76 }
77
78 /**
79  * \fn EXPORT int strncmp(const char *s1, const char *s2, size_t len)
80  * \brief Compare two strings with a limit
81  */
82 EXPORT int strncmp(const char *s1, const char *s2, size_t len)
83 {
84         while(--len && *s1 == *s2 && *s1 != '\0' && *s2 != '\0') {
85                 s1++; s2++;
86         }
87         return (int)*s1 - (int)*s2;
88 }
89
90 /**
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
94  */
95 EXPORT char *strdup(const char *str)
96 {
97         size_t  len = strlen(str);
98         char    *ret = malloc(len+1);
99         if(ret == NULL) return NULL;
100         strcpy(ret, str);
101         return ret;
102 }
103
104 /**
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
110  */
111 EXPORT char *strndup(const char *str, size_t maxlen)
112 {
113         size_t  len;
114         char    *ret;
115         for( len = 0; len < maxlen && str[len]; len ++) ;
116         ret = malloc( len + 1);
117         memcpy( ret, str, len );
118         ret[len] = '\0';
119         return ret;
120 }
121
122 /**
123  * \fn EXPORT char *strchr(char *str, int character)
124  * \brief Locate a character in a string
125  */
126 EXPORT char *strchr(char *str, int character)
127 {
128         while(*str)
129         {
130                 if(*str == character)   return str;
131         }
132         return NULL;
133 }
134
135 /**
136  * \fn EXPORT char *strrchr(char *str, int character)
137  * \brief Locate the last occurance of a character in a string
138  */
139 EXPORT char *strrchr(char *str, int character)
140 {
141          int    i;
142         i = strlen(str)-1;
143         while(i--)
144         {
145                 if(str[i] == character) return &str[i];
146         }
147         return NULL;
148 }
149
150 /**
151  * \fn EXPORT char *strstr(char *str1, const char *str2)
152  * \brief Search a \a str1 for the first occurance of \a str2
153  */
154 EXPORT char *strstr(char *str1, const char *str2)
155 {
156         const char      *test = str2;
157         
158         while(*str1)
159         {
160                 if(*test == '\0')       return str1;
161                 if(*str1 == *test)      test++;
162                 else    test = str2;
163                 str1 ++;
164         }
165         return NULL;
166 }
167
168 // --- Memory ---
169 /**
170  * \fn EXPORT void *memset(void *dest, int val, size_t num)
171  * \brief Clear memory with the specified value
172  */
173 EXPORT void *memset(void *dest, int val, size_t num)
174 {
175         unsigned char *p = dest;
176         while(num--)    *p++ = val;
177         return dest;
178 }
179
180 /**
181  * \fn EXPORT void *memcpy(void *dest, const void *src, size_t count)
182  * \brief Copy one memory area to another
183  */
184 EXPORT void *memcpy(void *dest, const void *src, size_t count)
185 {
186     char *sp = (char *)src;
187     char *dp = (char *)dest;
188     for(;count--;) *dp++ = *sp++;
189     return dest;
190 }
191
192 /**
193  * \fn EXPORT void *memmove(void *dest, const void *src, size_t count)
194  * \brief Copy data in memory, avoiding overlap problems
195  */
196 EXPORT void *memmove(void *dest, const void *src, size_t count)
197 {
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];
203         else
204         for(;count--;) *dp++ = *sp++;
205     return dest;
206 }
207
208 /**
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
214  */
215 EXPORT int memcmp(const void *mem1, const void *mem2, size_t count)
216 {
217         while(count--)
218         {
219                 if( *(unsigned char*)mem1 != *(unsigned char*)mem2 )
220                         return *(unsigned char*)mem1 - *(unsigned char*)mem2;
221                 mem1 ++;
222                 mem2 ++;
223         }
224         return 0;
225 }
226
227 /**
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
233  */
234 EXPORT void *memchr(void *ptr, int value, size_t num)
235 {
236         while(num--)
237         {
238                 if( *(unsigned char*)ptr == (unsigned char)value )
239                         return ptr;
240                 ptr ++;
241         }
242         return NULL;
243 }

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