Rev 1653 | Show entire file | Ignore whitespace | Details | Blame | Last modification | View Log | RSS feed
Rev 1653 | Rev 1719 | ||
---|---|---|---|
Line 24... | Line 24... | ||
24 | * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
24 | * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
25 | * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF |
25 | * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF |
26 | * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
26 | * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
27 | */ |
27 | */ |
28 | 28 | ||
29 | /** @addtogroup libc |
29 | /** @addtogroup libc |
30 | * @{ |
30 | * @{ |
31 | */ |
31 | */ |
32 | /** @file |
32 | /** @file |
33 | */ |
33 | */ |
34 | 34 | ||
Line 39... | Line 39... | ||
39 | #include <align.h> |
39 | #include <align.h> |
40 | 40 | ||
41 | 41 | ||
42 | /* Dummy implementation of mem/ functions */ |
42 | /* Dummy implementation of mem/ functions */ |
43 | 43 | ||
44 | void * memset(void *s, int c, size_t n) |
44 | void *memset(void *s, int c, size_t n) |
45 | { |
45 | { |
46 | char *os = s; |
46 | char *os = s; |
- | 47 | ||
47 | while (n--) |
48 | while (n--) |
48 | *(os++) = c; |
49 | *(os++) = c; |
- | 50 | ||
49 | return s; |
51 | return s; |
50 | } |
52 | } |
51 | 53 | ||
- | 54 | struct along { |
|
- | 55 | unsigned long n; |
|
52 | struct along {unsigned long n; } __attribute__ ((packed)); |
56 | } __attribute__ ((packed)); |
53 | 57 | ||
54 | static void * unaligned_memcpy(void *dst, const void *src, size_t n) |
58 | static void *unaligned_memcpy(void *dst, const void *src, size_t n) |
55 | { |
59 | { |
56 | int i, j; |
60 | int i, j; |
57 | struct along *adst = dst; |
61 | struct along *adst = dst; |
58 | const struct along *asrc = src; |
62 | const struct along *asrc = src; |
59 | 63 | ||
60 | for (i = 0; i < n/sizeof(unsigned long); i++) |
64 | for (i = 0; i < n / sizeof(unsigned long); i++) |
61 | adst[i].n = asrc[i].n; |
65 | adst[i].n = asrc[i].n; |
62 | 66 | ||
63 | for (j = 0; j < n%sizeof(unsigned long); j++) |
67 | for (j = 0; j < n % sizeof(unsigned long); j++) |
64 | ((unsigned char *)(((unsigned long *) dst) + i))[j] = ((unsigned char *)(((unsigned long *) src) + i))[j]; |
68 | ((unsigned char *) (((unsigned long *) dst) + i))[j] = ((unsigned char *) (((unsigned long *) src) + i))[j]; |
65 | 69 | ||
66 | return (char *)src; |
70 | return (char *) src; |
67 | } |
71 | } |
68 | 72 | ||
69 | void * memcpy(void *dst, const void *src, size_t n) |
73 | void *memcpy(void *dst, const void *src, size_t n) |
70 | { |
74 | { |
71 | int i, j; |
75 | int i, j; |
72 | 76 | ||
73 | if (((long)dst & (sizeof(long)-1)) || ((long)src & (sizeof(long)-1))) |
77 | if (((long) dst & (sizeof(long) - 1)) || ((long) src & (sizeof(long) - 1))) |
74 | return unaligned_memcpy(dst, src, n); |
78 | return unaligned_memcpy(dst, src, n); |
75 | 79 | ||
76 | for (i = 0; i < n/sizeof(unsigned long); i++) |
80 | for (i = 0; i < n / sizeof(unsigned long); i++) |
77 | ((unsigned long *) dst)[i] = ((unsigned long *) src)[i]; |
81 | ((unsigned long *) dst)[i] = ((unsigned long *) src)[i]; |
78 | 82 | ||
79 | for (j = 0; j < n%sizeof(unsigned long); j++) |
83 | for (j = 0; j < n % sizeof(unsigned long); j++) |
80 | ((unsigned char *)(((unsigned long *) dst) + i))[j] = ((unsigned char *)(((unsigned long *) src) + i))[j]; |
84 | ((unsigned char *) (((unsigned long *) dst) + i))[j] = ((unsigned char *) (((unsigned long *) src) + i))[j]; |
81 | 85 | ||
82 | return (char *)src; |
86 | return (char *) src; |
83 | } |
87 | } |
84 | 88 | ||
85 | void * memmove(void *dst, const void *src, size_t n) |
89 | void *memmove(void *dst, const void *src, size_t n) |
86 | { |
90 | { |
87 | int i, j; |
91 | int i, j; |
88 | 92 | ||
89 | if (src > dst) |
93 | if (src > dst) |
90 | return memcpy(dst, src, n); |
94 | return memcpy(dst, src, n); |
91 | 95 | ||
92 | for (j = (n%sizeof(unsigned long))-1; j >= 0; j--) |
96 | for (j = (n % sizeof(unsigned long)) - 1; j >= 0; j--) |
93 | ((unsigned char *)(((unsigned long *) dst) + i))[j] = ((unsigned char *)(((unsigned long *) src) + i))[j]; |
97 | ((unsigned char *) ((unsigned long *) dst))[j] = ((unsigned char *) ((unsigned long *) src))[j]; |
94 | 98 | ||
95 | for (i = n/sizeof(unsigned long)-1; i >=0 ; i--) |
99 | for (i = n / sizeof(unsigned long) - 1; i >=0 ; i--) |
96 | ((unsigned long *) dst)[i] = ((unsigned long *) src)[i]; |
100 | ((unsigned long *) dst)[i] = ((unsigned long *) src)[i]; |
97 | 101 | ||
98 | return (char *)src; |
102 | return (char *) src; |
99 | } |
103 | } |
100 | 104 | ||
101 | 105 | ||
102 | /** Count the number of characters in the string, not including terminating 0. |
106 | /** Count the number of characters in the string, not including terminating 0. |
103 | * @param str string |
107 | * @param str string |
Line 105... | Line 109... | ||
105 | */ |
109 | */ |
106 | size_t strlen(const char *str) |
110 | size_t strlen(const char *str) |
107 | { |
111 | { |
108 | size_t counter = 0; |
112 | size_t counter = 0; |
109 | 113 | ||
110 | while (str[counter] != 0) { |
114 | while (str[counter] != 0) |
111 | counter++; |
115 | counter++; |
112 | } |
- | |
113 | 116 | ||
114 | return counter; |
117 | return counter; |
115 | } |
118 | } |
116 | 119 | ||
117 | int strcmp(const char *a,const char *b) |
120 | int strcmp(const char *a, const char *b) |
118 | { |
121 | { |
119 | int c=0; |
122 | int c = 0; |
120 | 123 | ||
121 | while(a[c]&&b[c]&&(!(a[c]-b[c]))) c++; |
124 | while (a[c] && b[c] && (!(a[c] - b[c]))) |
- | 125 | c++; |
|
122 | 126 | ||
123 | return a[c]-b[c]; |
127 | return (a[c] - b[c]); |
124 | 128 | ||
125 | } |
129 | } |
126 | 130 | ||
127 | 131 | ||
128 | - | ||
129 | /** Return pointer to the first occurence of character c in string |
132 | /** Return pointer to the first occurence of character c in string |
130 | * @param str scanned string |
133 | * @param str scanned string |
131 | * @param c searched character (taken as one byte) |
134 | * @param c searched character (taken as one byte) |
132 | * @return pointer to the matched character or NULL if it is not found in given string. |
135 | * @return pointer to the matched character or NULL if it is not found in given string. |
133 | */ |
136 | */ |
134 | char *strchr(const char *str, int c) |
137 | char *strchr(const char *str, int c) |
135 | { |
138 | { |
136 | while (*str != '\0') { |
139 | while (*str != '\0') { |
137 | if (*str == (char)c) |
140 | if (*str == (char) c) |
138 | return (char *)str; |
141 | return (char *) str; |
139 | str++; |
142 | str++; |
140 | } |
143 | } |
141 | 144 | ||
142 | return NULL; |
145 | return NULL; |
143 | } |
146 | } |
Line 150... | Line 153... | ||
150 | char *strrchr(const char *str, int c) |
153 | char *strrchr(const char *str, int c) |
151 | { |
154 | { |
152 | char *retval = NULL; |
155 | char *retval = NULL; |
153 | 156 | ||
154 | while (*str != '\0') { |
157 | while (*str != '\0') { |
155 | if (*str == (char)c) |
158 | if (*str == (char) c) |
156 | retval = (char *)str; |
159 | retval = (char *) str; |
157 | str++; |
160 | str++; |
158 | } |
161 | } |
159 | 162 | ||
160 | return (char *)retval; |
163 | return (char *) retval; |
161 | } |
164 | } |
162 | 165 | ||
163 | /** Convert string to a number. |
166 | /** Convert string to a number. |
164 | * Core of strtol and strtoul functions. |
167 | * Core of strtol and strtoul functions. |
165 | * @param nptr pointer to string |
168 | * @param nptr pointer to string |
Line 207... | Line 210... | ||
207 | 210 | ||
208 | tmpptr = str; |
211 | tmpptr = str; |
209 | 212 | ||
210 | while (*str) { |
213 | while (*str) { |
211 | c = *str; |
214 | c = *str; |
212 | c = ( c >= 'a'? c-'a'+10:(c >= 'A'?c-'A'+10:(c <= '9'?c-'0':0xff))); |
215 | c = (c >= 'a' ? c - 'a' + 10 : (c >= 'A' ? c - 'A' + 10 : (c <= '9' ? c - '0' : 0xff))); |
213 | if (c > base) { |
216 | if (c > base) { |
214 | break; |
217 | break; |
215 | } |
218 | } |
216 | 219 | ||
217 | a = (result & 0xff) * base + c; |
220 | a = (result & 0xff) * base + c; |
Line 233... | Line 236... | ||
233 | str = nptr; |
236 | str = nptr; |
234 | result = 0; |
237 | result = 0; |
235 | } |
238 | } |
236 | 239 | ||
237 | if (endptr) |
240 | if (endptr) |
238 | *endptr = (char *)str; |
241 | *endptr = (char *) str; |
239 | 242 | ||
240 | if (nptr == str) { |
243 | if (nptr == str) { |
241 | /*FIXME: errno = EINVAL*/ |
244 | /*FIXME: errno = EINVAL*/ |
242 | return 0; |
245 | return 0; |
243 | } |
246 | } |
Line 261... | Line 264... | ||
261 | unsigned long number = 0; |
264 | unsigned long number = 0; |
262 | 265 | ||
263 | number = _strtoul(nptr, endptr, base, &sgn); |
266 | number = _strtoul(nptr, endptr, base, &sgn); |
264 | 267 | ||
265 | if (number > LONG_MAX) { |
268 | if (number > LONG_MAX) { |
266 | if ((sgn) && (number == (unsigned long)(LONG_MAX) + 1)) { |
269 | if ((sgn) && (number == (unsigned long) (LONG_MAX) + 1)) { |
267 | /* FIXME: set 0 to errno */ |
270 | /* FIXME: set 0 to errno */ |
268 | return number; |
271 | return number; |
269 | } |
272 | } |
270 | /* FIXME: set ERANGE to errno */ |
273 | /* FIXME: set ERANGE to errno */ |
271 | return (sgn?LONG_MIN:LONG_MAX); |
274 | return (sgn ? LONG_MIN : LONG_MAX); |
272 | } |
275 | } |
273 | 276 | ||
274 | return (sgn?-number:number); |
277 | return (sgn ? -number : number); |
275 | } |
278 | } |
276 | 279 | ||
277 | 280 | ||
278 | /** Convert initial part of string to unsigned long according to given base. |
281 | /** Convert initial part of string to unsigned long according to given base. |
279 | * The number may begin with an arbitrary number of whitespaces followed by optional sign (`+' or `-'). |
282 | * The number may begin with an arbitrary number of whitespaces followed by optional sign (`+' or `-'). |
Line 290... | Line 293... | ||
290 | char sgn = 0; |
293 | char sgn = 0; |
291 | unsigned long number = 0; |
294 | unsigned long number = 0; |
292 | 295 | ||
293 | number = _strtoul(nptr, endptr, base, &sgn); |
296 | number = _strtoul(nptr, endptr, base, &sgn); |
294 | 297 | ||
295 | return (sgn?-number:number); |
298 | return (sgn ? -number : number); |
296 | } |
299 | } |
297 | 300 | ||
298 | char *strcpy(char *dest, const char *src) |
301 | char *strcpy(char *dest, const char *src) |
299 | { |
302 | { |
- | 303 | char *orig = dest; |
|
- | 304 | ||
300 | while (*(dest++) = *(src++)) |
305 | while ((*(dest++) = *(src++))); |
301 | ; |
306 | return orig; |
302 | } |
307 | } |
303 | 308 | ||
304 | char *strncpy(char *dest, const char *src, size_t n) |
309 | char *strncpy(char *dest, const char *src, size_t n) |
305 | { |
310 | { |
- | 311 | char *orig = dest; |
|
- | 312 | ||
306 | while ((*(dest++) = *(src++)) && --n) |
313 | while ((*(dest++) = *(src++)) && --n); |
307 | ; |
314 | return orig; |
308 | } |
315 | } |
309 | 316 | ||
310 | 317 | ||
311 | /** @} |
318 | /** @} |
312 | */ |
319 | */ |
313 | - | ||
314 | - |