[936351c1] | 1 | /*
|
---|
[df4ed85] | 2 | * Copyright (c) 2005 Martin Decky
|
---|
[936351c1] | 3 | * All rights reserved.
|
---|
| 4 | *
|
---|
| 5 | * Redistribution and use in source and binary forms, with or without
|
---|
| 6 | * modification, are permitted provided that the following conditions
|
---|
| 7 | * are met:
|
---|
| 8 | *
|
---|
| 9 | * - Redistributions of source code must retain the above copyright
|
---|
| 10 | * notice, this list of conditions and the following disclaimer.
|
---|
| 11 | * - Redistributions in binary form must reproduce the above copyright
|
---|
| 12 | * notice, this list of conditions and the following disclaimer in the
|
---|
| 13 | * documentation and/or other materials provided with the distribution.
|
---|
| 14 | * - The name of the author may not be used to endorse or promote products
|
---|
| 15 | * derived from this software without specific prior written permission.
|
---|
| 16 | *
|
---|
| 17 | * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
|
---|
| 18 | * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
|
---|
| 19 | * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
|
---|
| 20 | * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
|
---|
| 21 | * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
|
---|
| 22 | * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
|
---|
| 23 | * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
|
---|
| 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
|
---|
| 26 | * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
---|
| 27 | */
|
---|
| 28 |
|
---|
[a46da63] | 29 | /** @addtogroup libc
|
---|
[b2951e2] | 30 | * @{
|
---|
| 31 | */
|
---|
| 32 | /** @file
|
---|
| 33 | */
|
---|
| 34 |
|
---|
[936351c1] | 35 | #include <string.h>
|
---|
[672a24d] | 36 | #include <unistd.h>
|
---|
| 37 | #include <ctype.h>
|
---|
| 38 | #include <limits.h>
|
---|
[7f079d9] | 39 | #include <align.h>
|
---|
[5d4e90f0] | 40 | #include <sys/types.h>
|
---|
[672a24d] | 41 |
|
---|
[936351c1] | 42 |
|
---|
| 43 | /* Dummy implementation of mem/ functions */
|
---|
| 44 |
|
---|
[a46da63] | 45 | void *memset(void *s, int c, size_t n)
|
---|
[936351c1] | 46 | {
|
---|
| 47 | char *os = s;
|
---|
[a46da63] | 48 |
|
---|
[936351c1] | 49 | while (n--)
|
---|
| 50 | *(os++) = c;
|
---|
[a46da63] | 51 |
|
---|
[936351c1] | 52 | return s;
|
---|
| 53 | }
|
---|
| 54 |
|
---|
[a46da63] | 55 | struct along {
|
---|
| 56 | unsigned long n;
|
---|
| 57 | } __attribute__ ((packed));
|
---|
[7f079d9] | 58 |
|
---|
[a46da63] | 59 | static void *unaligned_memcpy(void *dst, const void *src, size_t n)
|
---|
[7f079d9] | 60 | {
|
---|
| 61 | int i, j;
|
---|
| 62 | struct along *adst = dst;
|
---|
| 63 | const struct along *asrc = src;
|
---|
| 64 |
|
---|
[a46da63] | 65 | for (i = 0; i < n / sizeof(unsigned long); i++)
|
---|
[7f079d9] | 66 | adst[i].n = asrc[i].n;
|
---|
| 67 |
|
---|
[a46da63] | 68 | for (j = 0; j < n % sizeof(unsigned long); j++)
|
---|
| 69 | ((unsigned char *) (((unsigned long *) dst) + i))[j] = ((unsigned char *) (((unsigned long *) src) + i))[j];
|
---|
[7f079d9] | 70 |
|
---|
[a46da63] | 71 | return (char *) src;
|
---|
[7f079d9] | 72 | }
|
---|
| 73 |
|
---|
[a46da63] | 74 | void *memcpy(void *dst, const void *src, size_t n)
|
---|
[936351c1] | 75 | {
|
---|
[a2ae4f4] | 76 | int i, j;
|
---|
| 77 |
|
---|
[a46da63] | 78 | if (((long) dst & (sizeof(long) - 1)) || ((long) src & (sizeof(long) - 1)))
|
---|
[7f079d9] | 79 | return unaligned_memcpy(dst, src, n);
|
---|
| 80 |
|
---|
[a46da63] | 81 | for (i = 0; i < n / sizeof(unsigned long); i++)
|
---|
[a2ae4f4] | 82 | ((unsigned long *) dst)[i] = ((unsigned long *) src)[i];
|
---|
| 83 |
|
---|
[a46da63] | 84 | for (j = 0; j < n % sizeof(unsigned long); j++)
|
---|
| 85 | ((unsigned char *) (((unsigned long *) dst) + i))[j] = ((unsigned char *) (((unsigned long *) src) + i))[j];
|
---|
[a2ae4f4] | 86 |
|
---|
[a46da63] | 87 | return (char *) src;
|
---|
[936351c1] | 88 | }
|
---|
[c9857c6] | 89 |
|
---|
[a46da63] | 90 | void *memmove(void *dst, const void *src, size_t n)
|
---|
[a2ae4f4] | 91 | {
|
---|
| 92 | int i, j;
|
---|
| 93 |
|
---|
| 94 | if (src > dst)
|
---|
| 95 | return memcpy(dst, src, n);
|
---|
| 96 |
|
---|
[a46da63] | 97 | for (j = (n % sizeof(unsigned long)) - 1; j >= 0; j--)
|
---|
| 98 | ((unsigned char *) ((unsigned long *) dst))[j] = ((unsigned char *) ((unsigned long *) src))[j];
|
---|
[a2ae4f4] | 99 |
|
---|
[a46da63] | 100 | for (i = n / sizeof(unsigned long) - 1; i >=0 ; i--)
|
---|
[a2ae4f4] | 101 | ((unsigned long *) dst)[i] = ((unsigned long *) src)[i];
|
---|
| 102 |
|
---|
[a46da63] | 103 | return (char *) src;
|
---|
[a2ae4f4] | 104 | }
|
---|
| 105 |
|
---|
[df24ec3] | 106 | /** Compare two memory areas.
|
---|
| 107 | *
|
---|
| 108 | * @param s1 Pointer to the first area to compare.
|
---|
| 109 | * @param s2 Pointer to the second area to compare.
|
---|
| 110 | * @param len Size of the first area in bytes. Both areas must have the same
|
---|
| 111 | * length.
|
---|
| 112 | * @return If len is 0, return zero. If the areas match, return zero.
|
---|
| 113 | * Otherwise return non-zero.
|
---|
| 114 | */
|
---|
| 115 | int bcmp(const char *s1, const char *s2, size_t len)
|
---|
| 116 | {
|
---|
| 117 | for (; len && *s1++ == *s2++; len--)
|
---|
| 118 | ;
|
---|
| 119 | return len;
|
---|
| 120 | }
|
---|
[a2ae4f4] | 121 |
|
---|
[672a24d] | 122 | /** Count the number of characters in the string, not including terminating 0.
|
---|
| 123 | * @param str string
|
---|
| 124 | * @return number of characters in string.
|
---|
| 125 | */
|
---|
[c9857c6] | 126 | size_t strlen(const char *str)
|
---|
| 127 | {
|
---|
[523fad8] | 128 | size_t counter = 0;
|
---|
[c9857c6] | 129 |
|
---|
[a46da63] | 130 | while (str[counter] != 0)
|
---|
[c9857c6] | 131 | counter++;
|
---|
| 132 |
|
---|
| 133 | return counter;
|
---|
| 134 | }
|
---|
[672a24d] | 135 |
|
---|
[a46da63] | 136 | int strcmp(const char *a, const char *b)
|
---|
[c0535f80] | 137 | {
|
---|
[a46da63] | 138 | int c = 0;
|
---|
[c0535f80] | 139 |
|
---|
[a46da63] | 140 | while (a[c] && b[c] && (!(a[c] - b[c])))
|
---|
| 141 | c++;
|
---|
[c0535f80] | 142 |
|
---|
[a46da63] | 143 | return (a[c] - b[c]);
|
---|
[c0535f80] | 144 |
|
---|
| 145 | }
|
---|
| 146 |
|
---|
[5832e9b] | 147 | int strncmp(const char *a, const char *b, size_t n)
|
---|
| 148 | {
|
---|
| 149 | size_t c = 0;
|
---|
| 150 |
|
---|
| 151 | while (c < n && a[c] && b[c] && (!(a[c] - b[c])))
|
---|
| 152 | c++;
|
---|
| 153 |
|
---|
| 154 | return ( c < n ? a[c] - b[c] : 0);
|
---|
| 155 |
|
---|
| 156 | }
|
---|
[c0535f80] | 157 |
|
---|
[672a24d] | 158 | /** Return pointer to the first occurence of character c in string
|
---|
| 159 | * @param str scanned string
|
---|
| 160 | * @param c searched character (taken as one byte)
|
---|
| 161 | * @return pointer to the matched character or NULL if it is not found in given string.
|
---|
| 162 | */
|
---|
| 163 | char *strchr(const char *str, int c)
|
---|
| 164 | {
|
---|
| 165 | while (*str != '\0') {
|
---|
[a46da63] | 166 | if (*str == (char) c)
|
---|
| 167 | return (char *) str;
|
---|
[672a24d] | 168 | str++;
|
---|
| 169 | }
|
---|
| 170 |
|
---|
| 171 | return NULL;
|
---|
| 172 | }
|
---|
| 173 |
|
---|
| 174 | /** Return pointer to the last occurence of character c in string
|
---|
| 175 | * @param str scanned string
|
---|
| 176 | * @param c searched character (taken as one byte)
|
---|
| 177 | * @return pointer to the matched character or NULL if it is not found in given string.
|
---|
| 178 | */
|
---|
| 179 | char *strrchr(const char *str, int c)
|
---|
| 180 | {
|
---|
| 181 | char *retval = NULL;
|
---|
| 182 |
|
---|
| 183 | while (*str != '\0') {
|
---|
[a46da63] | 184 | if (*str == (char) c)
|
---|
| 185 | retval = (char *) str;
|
---|
[672a24d] | 186 | str++;
|
---|
| 187 | }
|
---|
| 188 |
|
---|
[a46da63] | 189 | return (char *) retval;
|
---|
[672a24d] | 190 | }
|
---|
| 191 |
|
---|
| 192 | /** Convert string to a number.
|
---|
| 193 | * Core of strtol and strtoul functions.
|
---|
| 194 | * @param nptr pointer to string
|
---|
| 195 | * @param endptr if not NULL, function stores here pointer to the first invalid character
|
---|
| 196 | * @param base zero or number between 2 and 36 inclusive
|
---|
| 197 | * @param sgn its set to 1 if minus found
|
---|
| 198 | * @return result of conversion.
|
---|
| 199 | */
|
---|
| 200 | static unsigned long _strtoul(const char *nptr, char **endptr, int base, char *sgn)
|
---|
| 201 | {
|
---|
| 202 | unsigned char c;
|
---|
| 203 | unsigned long result = 0;
|
---|
| 204 | unsigned long a, b;
|
---|
| 205 | const char *str = nptr;
|
---|
| 206 | const char *tmpptr;
|
---|
| 207 |
|
---|
| 208 | while (isspace(*str))
|
---|
| 209 | str++;
|
---|
| 210 |
|
---|
| 211 | if (*str == '-') {
|
---|
| 212 | *sgn = 1;
|
---|
| 213 | ++str;
|
---|
| 214 | } else if (*str == '+')
|
---|
| 215 | ++str;
|
---|
| 216 |
|
---|
| 217 | if (base) {
|
---|
| 218 | if ((base == 1) || (base > 36)) {
|
---|
| 219 | /* FIXME: set errno to EINVAL */
|
---|
| 220 | return 0;
|
---|
| 221 | }
|
---|
| 222 | if ((base == 16) && (*str == '0') && ((str[1] == 'x') || (str[1] == 'X'))) {
|
---|
| 223 | str += 2;
|
---|
| 224 | }
|
---|
| 225 | } else {
|
---|
| 226 | base = 10;
|
---|
| 227 |
|
---|
| 228 | if (*str == '0') {
|
---|
| 229 | base = 8;
|
---|
| 230 | if ((str[1] == 'X') || (str[1] == 'x')) {
|
---|
| 231 | base = 16;
|
---|
| 232 | str += 2;
|
---|
| 233 | }
|
---|
| 234 | }
|
---|
| 235 | }
|
---|
| 236 |
|
---|
| 237 | tmpptr = str;
|
---|
| 238 |
|
---|
| 239 | while (*str) {
|
---|
| 240 | c = *str;
|
---|
[a46da63] | 241 | c = (c >= 'a' ? c - 'a' + 10 : (c >= 'A' ? c - 'A' + 10 : (c <= '9' ? c - '0' : 0xff)));
|
---|
[672a24d] | 242 | if (c > base) {
|
---|
| 243 | break;
|
---|
| 244 | }
|
---|
| 245 |
|
---|
| 246 | a = (result & 0xff) * base + c;
|
---|
| 247 | b = (result >> 8) * base + (a >> 8);
|
---|
| 248 |
|
---|
| 249 | if (b > (ULONG_MAX >> 8)) {
|
---|
| 250 | /* overflow */
|
---|
| 251 | /* FIXME: errno = ERANGE*/
|
---|
| 252 | return ULONG_MAX;
|
---|
| 253 | }
|
---|
| 254 |
|
---|
| 255 | result = (b << 8) + (a & 0xff);
|
---|
| 256 | ++str;
|
---|
| 257 | }
|
---|
| 258 |
|
---|
| 259 | if (str == tmpptr) {
|
---|
| 260 | /* no number was found => first invalid character is the first character of the string */
|
---|
| 261 | /* FIXME: set errno to EINVAL */
|
---|
| 262 | str = nptr;
|
---|
| 263 | result = 0;
|
---|
| 264 | }
|
---|
| 265 |
|
---|
| 266 | if (endptr)
|
---|
[a46da63] | 267 | *endptr = (char *) str;
|
---|
[672a24d] | 268 |
|
---|
| 269 | if (nptr == str) {
|
---|
| 270 | /*FIXME: errno = EINVAL*/
|
---|
| 271 | return 0;
|
---|
| 272 | }
|
---|
| 273 |
|
---|
| 274 | return result;
|
---|
| 275 | }
|
---|
| 276 |
|
---|
| 277 | /** Convert initial part of string to long int according to given base.
|
---|
| 278 | * The number may begin with an arbitrary number of whitespaces followed by optional sign (`+' or `-').
|
---|
| 279 | * If the base is 0 or 16, the prefix `0x' may be inserted and the number will be taken as hexadecimal one.
|
---|
| 280 | * If the base is 0 and the number begin with a zero, number will be taken as octal one (as with base 8).
|
---|
| 281 | * Otherwise the base 0 is taken as decimal.
|
---|
| 282 | * @param nptr pointer to string
|
---|
| 283 | * @param endptr if not NULL, function stores here pointer to the first invalid character
|
---|
| 284 | * @param base zero or number between 2 and 36 inclusive
|
---|
| 285 | * @return result of conversion.
|
---|
| 286 | */
|
---|
| 287 | long int strtol(const char *nptr, char **endptr, int base)
|
---|
| 288 | {
|
---|
| 289 | char sgn = 0;
|
---|
| 290 | unsigned long number = 0;
|
---|
| 291 |
|
---|
| 292 | number = _strtoul(nptr, endptr, base, &sgn);
|
---|
| 293 |
|
---|
| 294 | if (number > LONG_MAX) {
|
---|
[a46da63] | 295 | if ((sgn) && (number == (unsigned long) (LONG_MAX) + 1)) {
|
---|
[672a24d] | 296 | /* FIXME: set 0 to errno */
|
---|
| 297 | return number;
|
---|
| 298 | }
|
---|
| 299 | /* FIXME: set ERANGE to errno */
|
---|
[a46da63] | 300 | return (sgn ? LONG_MIN : LONG_MAX);
|
---|
[672a24d] | 301 | }
|
---|
| 302 |
|
---|
[a46da63] | 303 | return (sgn ? -number : number);
|
---|
[672a24d] | 304 | }
|
---|
| 305 |
|
---|
| 306 |
|
---|
| 307 | /** Convert initial part of string to unsigned long according to given base.
|
---|
| 308 | * The number may begin with an arbitrary number of whitespaces followed by optional sign (`+' or `-').
|
---|
| 309 | * If the base is 0 or 16, the prefix `0x' may be inserted and the number will be taken as hexadecimal one.
|
---|
| 310 | * If the base is 0 and the number begin with a zero, number will be taken as octal one (as with base 8).
|
---|
| 311 | * Otherwise the base 0 is taken as decimal.
|
---|
| 312 | * @param nptr pointer to string
|
---|
| 313 | * @param endptr if not NULL, function stores here pointer to the first invalid character
|
---|
| 314 | * @param base zero or number between 2 and 36 inclusive
|
---|
| 315 | * @return result of conversion.
|
---|
| 316 | */
|
---|
| 317 | unsigned long strtoul(const char *nptr, char **endptr, int base)
|
---|
| 318 | {
|
---|
| 319 | char sgn = 0;
|
---|
| 320 | unsigned long number = 0;
|
---|
| 321 |
|
---|
| 322 | number = _strtoul(nptr, endptr, base, &sgn);
|
---|
| 323 |
|
---|
[a46da63] | 324 | return (sgn ? -number : number);
|
---|
[672a24d] | 325 | }
|
---|
[c594489] | 326 |
|
---|
| 327 | char *strcpy(char *dest, const char *src)
|
---|
| 328 | {
|
---|
[a46da63] | 329 | char *orig = dest;
|
---|
| 330 |
|
---|
[1526594c] | 331 | while ((*(dest++) = *(src++)))
|
---|
| 332 | ;
|
---|
[a46da63] | 333 | return orig;
|
---|
[c594489] | 334 | }
|
---|
| 335 |
|
---|
| 336 | char *strncpy(char *dest, const char *src, size_t n)
|
---|
| 337 | {
|
---|
[a46da63] | 338 | char *orig = dest;
|
---|
| 339 |
|
---|
[1526594c] | 340 | while ((*(dest++) = *(src++)) && --n)
|
---|
| 341 | ;
|
---|
| 342 | return orig;
|
---|
| 343 | }
|
---|
| 344 |
|
---|
| 345 | char *strcat(char *dest, const char *src)
|
---|
| 346 | {
|
---|
| 347 | char *orig = dest;
|
---|
| 348 | while (*dest++)
|
---|
| 349 | ;
|
---|
| 350 | --dest;
|
---|
| 351 | while ((*dest++ = *src++))
|
---|
| 352 | ;
|
---|
[a46da63] | 353 | return orig;
|
---|
[c594489] | 354 | }
|
---|
[b2951e2] | 355 |
|
---|
[a46da63] | 356 | /** @}
|
---|
[b2951e2] | 357 | */
|
---|