[Feature]add MT2731_MP2_MR2_SVN388 baseline version
Change-Id: Ief04314834b31e27effab435d3ca8ba33b499059
diff --git a/src/bsp/lk/lib/libc/atexit.c b/src/bsp/lk/lib/libc/atexit.c
new file mode 100644
index 0000000..9be083f
--- /dev/null
+++ b/src/bsp/lk/lib/libc/atexit.c
@@ -0,0 +1,29 @@
+/*
+ * Copyright (c) 2008 Travis Geiselbrecht
+ *
+ * Permission is hereby granted, free of charge, to any person obtaining
+ * a copy of this software and associated documentation files
+ * (the "Software"), to deal in the Software without restriction,
+ * including without limitation the rights to use, copy, modify, merge,
+ * publish, distribute, sublicense, and/or sell copies of the Software,
+ * and to permit persons to whom the Software is furnished to do so,
+ * subject to the following conditions:
+ *
+ * The above copyright notice and this permission notice shall be
+ * included in all copies or substantial portions of the Software.
+ *
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
+ * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
+ * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
+ * IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
+ * CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
+ * TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
+ * SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
+ */
+
+/* nulled out atexit. static object constructors call this */
+int atexit(void (*func)(void))
+{
+ return 0;
+}
+
diff --git a/src/bsp/lk/lib/libc/atoi.c b/src/bsp/lk/lib/libc/atoi.c
new file mode 100644
index 0000000..d641e00
--- /dev/null
+++ b/src/bsp/lk/lib/libc/atoi.c
@@ -0,0 +1,191 @@
+/*
+** Copyright 2001, Travis Geiselbrecht. All rights reserved.
+** Distributed under the terms of the NewOS License.
+*/
+/*
+ * Copyright (c) 2008 Travis Geiselbrecht
+ *
+ * Permission is hereby granted, free of charge, to any person obtaining
+ * a copy of this software and associated documentation files
+ * (the "Software"), to deal in the Software without restriction,
+ * including without limitation the rights to use, copy, modify, merge,
+ * publish, distribute, sublicense, and/or sell copies of the Software,
+ * and to permit persons to whom the Software is furnished to do so,
+ * subject to the following conditions:
+ *
+ * The above copyright notice and this permission notice shall be
+ * included in all copies or substantial portions of the Software.
+ *
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
+ * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
+ * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
+ * IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
+ * CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
+ * TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
+ * SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
+ */
+
+#include <stdlib.h>
+#include <ctype.h>
+#include <errno.h>
+
+#define LONG_IS_INT 1
+
+static int hexval(char c)
+{
+ if (c >= '0' && c <= '9')
+ return c - '0';
+ else if (c >= 'a' && c <= 'f')
+ return c - 'a' + 10;
+ else if (c >= 'A' && c <= 'F')
+ return c - 'A' + 10;
+
+ return 0;
+}
+
+int atoi(const char *num)
+{
+#if !LONG_IS_INT
+ // XXX fail
+#else
+ return atol(num);
+#endif
+}
+
+unsigned int atoui(const char *num)
+{
+#if !LONG_IS_INT
+ // XXX fail
+#else
+ return atoul(num);
+#endif
+}
+
+long atol(const char *num)
+{
+ long value = 0;
+ int neg = 0;
+
+ if (num[0] == '0' && num[1] == 'x') {
+ // hex
+ num += 2;
+ while (*num && isxdigit(*num))
+ value = value * 16 + hexval(*num++);
+ } else {
+ // decimal
+ if (num[0] == '-') {
+ neg = 1;
+ num++;
+ }
+ while (*num && isdigit(*num))
+ value = value * 10 + *num++ - '0';
+ }
+
+ if (neg)
+ value = -value;
+
+ return value;
+}
+
+unsigned long atoul(const char *num)
+{
+ unsigned long value = 0;
+ if (num[0] == '0' && num[1] == 'x') {
+ // hex
+ num += 2;
+ while (*num && isxdigit(*num))
+ value = value * 16 + hexval(*num++);
+ } else {
+ // decimal
+ while (*num && isdigit(*num))
+ value = value * 10 + *num++ - '0';
+ }
+
+ return value;
+}
+
+unsigned long long atoull(const char *num)
+{
+ unsigned long long value = 0;
+ if (num[0] == '0' && num[1] == 'x') {
+ // hex
+ num += 2;
+ while (*num && isxdigit(*num))
+ value = value * 16 + hexval(*num++);
+ } else {
+ // decimal
+ while (*num && isdigit(*num))
+ value = value * 10 + *num++ - '0';
+ }
+
+ return value;
+}
+
+unsigned long strtoul(const char *nptr, char **endptr, int base)
+{
+ int neg = 0;
+ unsigned long ret = 0;
+
+ if (base < 0 || base == 1 || base > 36) {
+ errno = EINVAL;
+ return 0;
+ }
+
+ while (isspace(*nptr)) {
+ nptr++;
+ }
+
+ if (*nptr == '+') {
+ nptr++;
+ } else if (*nptr == '-') {
+ neg = 1;
+ nptr++;
+ }
+
+ if ((base == 0 || base == 16) && nptr[0] == '0' && nptr[1] == 'x') {
+ base = 16;
+ nptr += 2;
+ } else if (base == 0 && nptr[0] == '0') {
+ base = 8;
+ nptr++;
+ } else if (base == 0) {
+ base = 10;
+ }
+
+ for (;;) {
+ char c = *nptr;
+ int v = -1;
+ unsigned long new_ret;
+
+ if (c >= 'A' && c <= 'Z') {
+ v = c - 'A' + 10;
+ } else if (c >= 'a' && c <= 'z') {
+ v = c - 'a' + 10;
+ } else if (c >= '0' && c <= '9') {
+ v = c - '0';
+ }
+
+ if (v < 0 || v >= base) {
+ *endptr = (char *) nptr;
+ break;
+ }
+
+ new_ret = ret * base;
+ if (new_ret / base != ret ||
+ new_ret + v < new_ret ||
+ ret == ULONG_MAX) {
+ ret = ULONG_MAX;
+ errno = ERANGE;
+ } else {
+ ret = new_ret + v;
+ }
+
+ nptr++;
+ }
+
+ if (neg && ret != ULONG_MAX) {
+ ret = -ret;
+ }
+
+ return ret;
+}
diff --git a/src/bsp/lk/lib/libc/bsearch.c b/src/bsp/lk/lib/libc/bsearch.c
new file mode 100644
index 0000000..a50c576
--- /dev/null
+++ b/src/bsp/lk/lib/libc/bsearch.c
@@ -0,0 +1,45 @@
+/* Copyright (c) 2014, Google Inc.
+ *
+ * Permission to use, copy, modify, and/or distribute this software for any
+ * purpose with or without fee is hereby granted, provided that the above
+ * copyright notice and this permission notice appear in all copies.
+ *
+ * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
+ * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
+ * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY
+ * SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
+ * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION
+ * OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN
+ * CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. */
+
+#include <stdlib.h>
+
+void *bsearch(const void *key, const void *base, size_t num_elems, size_t size,
+ int (*compare)(const void *, const void *))
+{
+ size_t low = 0, high = num_elems - 1;
+
+ if (num_elems == 0) {
+ return NULL;
+ }
+
+ for (;;) {
+ size_t mid = low + ((high - low) / 2);
+ const void *mid_elem = ((unsigned char*) base) + mid*size;
+ int r = compare(key, mid_elem);
+
+ if (r < 0) {
+ if (mid == 0) {
+ return NULL;
+ }
+ high = mid - 1;
+ } else if (r > 0) {
+ low = mid + 1;
+ if (low < mid || low > high) {
+ return NULL;
+ }
+ } else {
+ return (void*) mid_elem;
+ }
+ }
+}
diff --git a/src/bsp/lk/lib/libc/ctype.c b/src/bsp/lk/lib/libc/ctype.c
new file mode 100644
index 0000000..e5600bc
--- /dev/null
+++ b/src/bsp/lk/lib/libc/ctype.c
@@ -0,0 +1,98 @@
+/*
+ * Copyright (c) 2008 Travis Geiselbrecht
+ *
+ * Permission is hereby granted, free of charge, to any person obtaining
+ * a copy of this software and associated documentation files
+ * (the "Software"), to deal in the Software without restriction,
+ * including without limitation the rights to use, copy, modify, merge,
+ * publish, distribute, sublicense, and/or sell copies of the Software,
+ * and to permit persons to whom the Software is furnished to do so,
+ * subject to the following conditions:
+ *
+ * The above copyright notice and this permission notice shall be
+ * included in all copies or substantial portions of the Software.
+ *
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
+ * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
+ * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
+ * IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
+ * CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
+ * TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
+ * SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
+ */
+#include <ctype.h>
+
+int isblank(int c)
+{
+ return (c == ' ' || c == '\t');
+}
+
+int isspace(int c)
+{
+ return (c == ' ' || c == '\f' || c == '\n' || c == '\r' || c == '\t' || c == '\v');
+}
+
+int islower(int c)
+{
+ return ((c >= 'a') && (c <= 'z'));
+}
+
+int isupper(int c)
+{
+ return ((c >= 'A') && (c <= 'Z'));
+}
+
+int isdigit(int c)
+{
+ return ((c >= '0') && (c <= '9'));
+}
+
+int isalpha(int c)
+{
+ return isupper(c) || islower(c);
+}
+
+int isalnum(int c)
+{
+ return isalpha(c) || isdigit(c);
+}
+
+int isxdigit(int c)
+{
+ return isdigit(c) || ((c >= 'a') && (c <= 'f')) || ((c >= 'A') && (c <= 'F'));
+}
+
+int isgraph(int c)
+{
+ return ((c > ' ') && (c < 0x7f));
+}
+
+int iscntrl(int c)
+{
+ return ((c < ' ') || (c == 0x7f));
+}
+
+int isprint(int c)
+{
+ return ((c >= 0x20) && (c < 0x7f));
+}
+
+int ispunct(int c)
+{
+ return isgraph(c) && (!isalnum(c));
+}
+
+int tolower(int c)
+{
+ if ((c >= 'A') && (c <= 'Z'))
+ return c + ('a' - 'A');
+ return c;
+}
+
+int toupper(int c)
+{
+ if ((c >= 'a') && (c <= 'z'))
+ return c + ('A' - 'a');
+ return c;
+}
+
diff --git a/src/bsp/lk/lib/libc/eabi.c b/src/bsp/lk/lib/libc/eabi.c
new file mode 100644
index 0000000..8cf1381
--- /dev/null
+++ b/src/bsp/lk/lib/libc/eabi.c
@@ -0,0 +1,68 @@
+/* Copyright (c) 2008 Travis Geiselbrecht
+ *
+ * Permission is hereby granted, free of charge, to any person obtaining
+ * a copy of this software and associated documentation files
+ * (the "Software"), to deal in the Software without restriction,
+ * including without limitation the rights to use, copy, modify, merge,
+ * publish, distribute, sublicense, and/or sell copies of the Software,
+ * and to permit persons to whom the Software is furnished to do so,
+ * subject to the following conditions:
+ *
+ * The above copyright notice and this permission notice shall be
+ * included in all copies or substantial portions of the Software.
+ *
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
+ * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
+ * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
+ * IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
+ * CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
+ * TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
+ * SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
+ */
+
+/* some cruft we have to define when using the linux toolchain */
+#include <unwind.h>
+
+#if defined(__ARM_EABI_UNWINDER__) && __ARM_EABI_UNWINDER__
+
+/* Our toolchain has eabi functionality built in, but they're not really used.
+ * so we stub them out here. */
+_Unwind_Reason_Code __aeabi_unwind_cpp_pr0(_Unwind_State state, _Unwind_Control_Block *ucbp, _Unwind_Context *context)
+{
+ return _URC_FAILURE;
+}
+
+_Unwind_Reason_Code __aeabi_unwind_cpp_pr1(_Unwind_State state, _Unwind_Control_Block *ucbp, _Unwind_Context *context)
+{
+ return _URC_FAILURE;
+}
+
+_Unwind_Reason_Code __aeabi_unwind_cpp_pr2(_Unwind_State state, _Unwind_Control_Block *ucbp, _Unwind_Context *context)
+{
+ return _URC_FAILURE;
+}
+
+#elif defined(__clang__) && defined(__arm__)
+
+_Unwind_Reason_Code __aeabi_unwind_cpp_pr0(_Unwind_State state, void *ucbp, struct _Unwind_Context *context)
+{
+ return _URC_FAILURE;
+}
+
+_Unwind_Reason_Code __aeabi_unwind_cpp_pr1(_Unwind_State state, void *ucbp, struct _Unwind_Context *context)
+{
+ return _URC_FAILURE;
+}
+
+_Unwind_Reason_Code __aeabi_unwind_cpp_pr2(_Unwind_State state, void *ucbp, struct _Unwind_Context *context)
+{
+ return _URC_FAILURE;
+}
+
+#endif
+
+/* needed by some piece of EABI */
+void raise(void)
+{
+}
+
diff --git a/src/bsp/lk/lib/libc/errno.c b/src/bsp/lk/lib/libc/errno.c
new file mode 100644
index 0000000..d9b76cc
--- /dev/null
+++ b/src/bsp/lk/lib/libc/errno.c
@@ -0,0 +1,34 @@
+/*
+ * Copyright (c) 2015 Travis Geiselbrecht
+ *
+ * Permission is hereby granted, free of charge, to any person obtaining
+ * a copy of this software and associated documentation files
+ * (the "Software"), to deal in the Software without restriction,
+ * including without limitation the rights to use, copy, modify, merge,
+ * publish, distribute, sublicense, and/or sell copies of the Software,
+ * and to permit persons to whom the Software is furnished to do so,
+ * subject to the following conditions:
+ *
+ * The above copyright notice and this permission notice shall be
+ * included in all copies or substantial portions of the Software.
+ *
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
+ * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
+ * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
+ * IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
+ * CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
+ * TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
+ * SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
+ */
+
+#include <errno.h>
+
+/* completely un-threadsafe implementation of errno */
+/* TODO: pull from kernel TLS or some other thread local storage */
+static int _errno;
+
+int *__geterrno(void)
+{
+ return &_errno;
+}
+
diff --git a/src/bsp/lk/lib/libc/printf.c b/src/bsp/lk/lib/libc/printf.c
new file mode 100644
index 0000000..14e7cd1
--- /dev/null
+++ b/src/bsp/lk/lib/libc/printf.c
@@ -0,0 +1,638 @@
+/*
+ * Copyright (c) 2008-2014 Travis Geiselbrecht
+ *
+ * Permission is hereby granted, free of charge, to any person obtaining
+ * a copy of this software and associated documentation files
+ * (the "Software"), to deal in the Software without restriction,
+ * including without limitation the rights to use, copy, modify, merge,
+ * publish, distribute, sublicense, and/or sell copies of the Software,
+ * and to permit persons to whom the Software is furnished to do so,
+ * subject to the following conditions:
+ *
+ * The above copyright notice and this permission notice shall be
+ * included in all copies or substantial portions of the Software.
+ *
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
+ * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
+ * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
+ * IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
+ * CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
+ * TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
+ * SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
+ */
+#include <debug.h>
+#include <assert.h>
+#include <limits.h>
+#include <printf.h>
+#include <stdarg.h>
+#include <sys/types.h>
+#include <stdio.h>
+#include <string.h>
+#include <platform/debug.h>
+
+#if WITH_NO_FP
+#define FLOAT_PRINTF 0
+#else
+#define FLOAT_PRINTF 1
+#endif
+
+int sprintf(char *str, const char *fmt, ...)
+{
+ int err;
+
+ va_list ap;
+ va_start(ap, fmt);
+ err = vsprintf(str, fmt, ap);
+ va_end(ap);
+
+ return err;
+}
+
+int snprintf(char *str, size_t len, const char *fmt, ...)
+{
+ int err;
+
+ va_list ap;
+ va_start(ap, fmt);
+ err = vsnprintf(str, len, fmt, ap);
+ va_end(ap);
+
+ return err;
+}
+
+
+#define LONGFLAG 0x00000001
+#define LONGLONGFLAG 0x00000002
+#define HALFFLAG 0x00000004
+#define HALFHALFFLAG 0x00000008
+#define SIZETFLAG 0x00000010
+#define INTMAXFLAG 0x00000020
+#define PTRDIFFFLAG 0x00000040
+#define ALTFLAG 0x00000080
+#define CAPSFLAG 0x00000100
+#define SHOWSIGNFLAG 0x00000200
+#define SIGNEDFLAG 0x00000400
+#define LEFTFORMATFLAG 0x00000800
+#define LEADZEROFLAG 0x00001000
+#define BLANKPOSFLAG 0x00002000
+
+__NO_INLINE static char *longlong_to_string(char *buf, unsigned long long n, size_t len, uint flag, char *signchar)
+{
+ size_t pos = len;
+ int negative = 0;
+
+ if ((flag & SIGNEDFLAG) && (long long)n < 0) {
+ negative = 1;
+ n = -n;
+ }
+
+ buf[--pos] = 0;
+
+ /* only do the math if the number is >= 10 */
+ while (n >= 10) {
+ int digit = n % 10;
+
+ n /= 10;
+
+ buf[--pos] = digit + '0';
+ }
+ buf[--pos] = n + '0';
+
+ if (negative)
+ *signchar = '-';
+ else if ((flag & SHOWSIGNFLAG))
+ *signchar = '+';
+ else if ((flag & BLANKPOSFLAG))
+ *signchar = ' ';
+ else
+ *signchar = '\0';
+
+ return &buf[pos];
+}
+
+static const char hextable[] = { '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'a', 'b', 'c', 'd', 'e', 'f' };
+static const char hextable_caps[] = { '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'A', 'B', 'C', 'D', 'E', 'F' };
+
+__NO_INLINE static char *longlong_to_hexstring(char *buf, unsigned long long u, size_t len, uint flag)
+{
+ size_t pos = len;
+ const char *table = (flag & CAPSFLAG) ? hextable_caps : hextable;
+
+ buf[--pos] = 0;
+ do {
+ unsigned int digit = u % 16;
+ u /= 16;
+
+ buf[--pos] = table[digit];
+ } while (u != 0);
+
+ return &buf[pos];
+}
+
+#if FLOAT_PRINTF
+union double_int {
+ double d;
+ uint64_t i;
+};
+
+#define OUT(c) buf[pos++] = (c)
+#define OUTSTR(str) do { for (size_t i = 0; (str)[i] != 0; i++) OUT((str)[i]); } while (0)
+
+/* print up to a 4 digit exponent as string, with sign */
+__NO_INLINE static size_t exponent_to_string(char *buf, int32_t exponent)
+{
+ size_t pos = 0;
+
+ /* handle sign */
+ if (exponent < 0) {
+ OUT('-');
+ exponent = -exponent;
+ } else {
+ OUT('+');
+ }
+
+ /* see how far we need to bump into the string to print from the right */
+ if (exponent >= 1000) pos += 4;
+ else if (exponent >= 100) pos += 3;
+ else if (exponent >= 10) pos += 2;
+ else pos++;
+
+ /* print decimal string, from the right */
+ uint i = pos;
+ do {
+ uint digit = (uint32_t)exponent % 10;
+
+ buf[--i] = digit + '0';
+
+ exponent /= 10;
+ } while (exponent != 0);
+
+ /* return number of characters printed */
+ return pos;
+}
+
+__NO_INLINE static char *double_to_string(char *buf, size_t len, double d, uint flag)
+{
+ size_t pos = 0;
+ union double_int u = { d };
+
+ uint32_t exponent = (u.i >> 52) & 0x7ff;
+ uint64_t fraction = (u.i & ((1ULL << 52) - 1));
+ bool neg = !!(u.i & (1ULL << 63));
+
+ /* start constructing the string */
+ if (neg) {
+ OUT('-');
+ d = -d;
+ }
+
+ /* longest:
+ * 179769313486231570814527423731704356798070567525844996598917476803157260780028538760589558632766878171540458953514382464234321326889464182768467546703537516986049910576551282076245490090389328944075868508455133942304583236903222948165808559332123348274797826204144723168738177180919299881250404026184124858368.000000o
+ */
+
+ /* look for special cases */
+ if (exponent == 0x7ff) {
+ if (fraction == 0) {
+ /* infinity */
+ if (flag & CAPSFLAG) OUTSTR("INF");
+ else OUTSTR("inf");
+ } else {
+ /* NaN */
+ if (flag & CAPSFLAG) OUTSTR("NAN");
+ else OUTSTR("nan");
+ }
+ } else if (exponent == 0) {
+ if (fraction == 0) {
+ /* zero */
+ OUTSTR("0.000000");
+ } else {
+ /* denormalized */
+ /* XXX does not handle */
+ if (flag & CAPSFLAG) OUTSTR("DEN");
+ else OUTSTR("den");
+ }
+ } else {
+ /* see if it's in the range of floats we can easily print */
+ int exponent_signed = exponent - 1023;
+ if (exponent_signed < -52 || exponent_signed > 52) {
+ OUTSTR("<range>");
+ } else {
+ /* start by walking backwards through the string */
+#define OUTREV(c) do { if (&buf[pos] == buf) goto done; else buf[--pos] = (c); } while (0)
+ pos = len;
+ OUTREV(0);
+
+ /* reserve space for the fractional component first */
+ for (int i = 0; i <= 6; i++)
+ OUTREV('0');
+ size_t decimal_spot = pos;
+
+ /* print the integer portion */
+ uint64_t u;
+ if (exponent_signed >= 0) {
+ u = fraction;
+ u |= (1ULL<<52);
+ u >>= (52 - exponent_signed);
+
+ char *s = longlong_to_string(buf, u, pos + 1, flag, &(char) {0});
+
+ pos = s - buf;
+ } else {
+ /* exponent is negative */
+ u = 0;
+ OUTREV('0');
+ }
+
+ buf[decimal_spot] = '.';
+
+ /* handle the fractional part */
+ uint32_t frac = ((d - u) * 1000000) + .5;
+
+ uint i = decimal_spot + 6 + 1;
+ while (frac != 0) {
+ uint digit = frac % 10;
+
+ buf[--i] = digit + '0';
+
+ frac /= 10;
+ }
+
+ if (neg)
+ OUTREV('-');
+
+done:
+ /* separate return path, since we've been walking backwards through the string */
+ return &buf[pos];
+ }
+#undef OUTREV
+ }
+
+ buf[pos] = 0;
+ return buf;
+}
+
+__NO_INLINE static char *double_to_hexstring(char *buf, size_t len, double d, uint flag)
+{
+ size_t pos = 0;
+ union double_int u = { d };
+
+ uint32_t exponent = (u.i >> 52) & 0x7ff;
+ uint64_t fraction = (u.i & ((1ULL << 52) - 1));
+ bool neg = !!(u.i & (1ULL << 63));
+
+ /* start constructing the string */
+ if (neg) {
+ OUT('-');
+ }
+
+ /* look for special cases */
+ if (exponent == 0x7ff) {
+ if (fraction == 0) {
+ /* infinity */
+ if (flag & CAPSFLAG) OUTSTR("INF");
+ else OUTSTR("inf");
+ } else {
+ /* NaN */
+ if (flag & CAPSFLAG) OUTSTR("NAN");
+ else OUTSTR("nan");
+ }
+ } else if (exponent == 0) {
+ if (fraction == 0) {
+ /* zero */
+ if (flag & CAPSFLAG) OUTSTR("0X0P+0");
+ else OUTSTR("0x0p+0");
+ } else {
+ /* denormalized */
+ /* XXX does not handle */
+ if (flag & CAPSFLAG) OUTSTR("DEN");
+ else OUTSTR("den");
+ }
+ } else {
+ /* regular normalized numbers:
+ * 0x1p+1
+ * 0x1.0000000000001p+1
+ * 0X1.FFFFFFFFFFFFFP+1023
+ * 0x1.FFFFFFFFFFFFFP+1023
+ */
+ int exponent_signed = exponent - 1023;
+
+ /* implicit 1. */
+ if (flag & CAPSFLAG) OUTSTR("0X1");
+ else OUTSTR("0x1");
+
+ /* select the appropriate hex case table */
+ const char *table = (flag & CAPSFLAG) ? hextable_caps : hextable;
+
+ int zero_count = 0;
+ bool output_dot = false;
+ for (int i = 52 - 4; i >= 0; i -= 4) {
+ uint digit = (fraction >> i) & 0xf;
+
+ if (digit == 0) {
+ zero_count++;
+ } else {
+ /* output a . the first time we output a char */
+ if (!output_dot) {
+ OUT('.');
+ output_dot = true;
+ }
+ /* if we have a non zero digit, see if we need to output a string of zeros */
+ while (zero_count > 0) {
+ OUT('0');
+ zero_count--;
+ }
+ buf[pos++] = table[digit];
+ }
+ }
+
+ /* handle the exponent */
+ buf[pos++] = (flag & CAPSFLAG) ? 'P' : 'p';
+ pos += exponent_to_string(&buf[pos], exponent_signed);
+ }
+
+ buf[pos] = 0;
+ return buf;
+}
+
+#undef OUT
+#undef OUTSTR
+
+#endif // FLOAT_PRINTF
+
+int vsprintf(char *str, const char *fmt, va_list ap)
+{
+ return vsnprintf(str, INT_MAX, fmt, ap);
+}
+
+struct _output_args {
+ char *outstr;
+ size_t len;
+ size_t pos;
+};
+
+static int _vsnprintf_output(const char *str, size_t len, void *state)
+{
+ struct _output_args *args = state;
+
+ size_t count = 0;
+ while (count < len) {
+ if (args->pos < args->len) {
+ args->outstr[args->pos++] = *str;
+ }
+
+ str++;
+ count++;
+ }
+
+ return count;
+}
+
+int vsnprintf(char *str, size_t len, const char *fmt, va_list ap)
+{
+ struct _output_args args;
+ int wlen;
+
+ args.outstr = str;
+ args.len = len;
+ args.pos = 0;
+
+ wlen = _printf_engine(&_vsnprintf_output, (void *)&args, fmt, ap);
+ if (args.pos >= len)
+ str[len-1] = '\0';
+ else
+ str[wlen] = '\0';
+ return wlen;
+}
+
+int _printf_engine(_printf_engine_output_func out, void *state, const char *fmt, va_list ap)
+{
+ int err = 0;
+ char c;
+ unsigned char uc;
+ const char *s;
+ size_t string_len;
+ unsigned long long n;
+ void *ptr;
+ int flags;
+ unsigned int format_num;
+ char signchar;
+ size_t chars_written = 0;
+ char num_buffer[32];
+
+#define OUTPUT_STRING(str, len) do { err = out(str, len, state); if (err < 0) { goto exit; } else { chars_written += err; } } while(0)
+#define OUTPUT_CHAR(c) do { char __temp[1] = { c }; OUTPUT_STRING(__temp, 1); } while (0)
+
+ for (;;) {
+ /* reset the format state */
+ flags = 0;
+ format_num = 0;
+ signchar = '\0';
+
+ /* handle regular chars that aren't format related */
+ s = fmt;
+ string_len = 0;
+ while ((c = *fmt++) != 0) {
+ if (c == '%')
+ break; /* we saw a '%', break and start parsing format */
+ string_len++;
+ }
+
+ /* output the string we've accumulated */
+ OUTPUT_STRING(s, string_len);
+
+ /* make sure we haven't just hit the end of the string */
+ if (c == 0)
+ break;
+
+next_format:
+ /* grab the next format character */
+ c = *fmt++;
+ if (c == 0)
+ break;
+
+ switch (c) {
+ case '0'...'9':
+ if (c == '0' && format_num == 0)
+ flags |= LEADZEROFLAG;
+ format_num *= 10;
+ format_num += c - '0';
+ goto next_format;
+ case '.':
+ /* XXX for now eat numeric formatting */
+ goto next_format;
+ case '%':
+ OUTPUT_CHAR('%');
+ break;
+ case 'c':
+ uc = va_arg(ap, unsigned int);
+ OUTPUT_CHAR(uc);
+ break;
+ case 's':
+ s = va_arg(ap, const char *);
+ if (s == 0)
+ s = "<null>";
+ flags &= ~LEADZEROFLAG; /* doesn't make sense for strings */
+ goto _output_string;
+ case '-':
+ flags |= LEFTFORMATFLAG;
+ goto next_format;
+ case '+':
+ flags |= SHOWSIGNFLAG;
+ goto next_format;
+ case ' ':
+ flags |= BLANKPOSFLAG;
+ goto next_format;
+ case '#':
+ flags |= ALTFLAG;
+ goto next_format;
+ case 'l':
+ if (flags & LONGFLAG)
+ flags |= LONGLONGFLAG;
+ flags |= LONGFLAG;
+ goto next_format;
+ case 'h':
+ if (flags & HALFFLAG)
+ flags |= HALFHALFFLAG;
+ flags |= HALFFLAG;
+ goto next_format;
+ case 'z':
+ flags |= SIZETFLAG;
+ goto next_format;
+ case 'j':
+ flags |= INTMAXFLAG;
+ goto next_format;
+ case 't':
+ flags |= PTRDIFFFLAG;
+ goto next_format;
+ case 'i':
+ case 'd':
+ n = (flags & LONGLONGFLAG) ? va_arg(ap, long long) :
+ (flags & LONGFLAG) ? va_arg(ap, long) :
+ (flags & HALFHALFFLAG) ? (signed char)va_arg(ap, int) :
+ (flags & HALFFLAG) ? (short)va_arg(ap, int) :
+ (flags & SIZETFLAG) ? va_arg(ap, ssize_t) :
+ (flags & INTMAXFLAG) ? va_arg(ap, intmax_t) :
+ (flags & PTRDIFFFLAG) ? va_arg(ap, ptrdiff_t) :
+ va_arg(ap, int);
+ flags |= SIGNEDFLAG;
+ s = longlong_to_string(num_buffer, n, sizeof(num_buffer), flags, &signchar);
+ goto _output_string;
+ case 'u':
+ n = (flags & LONGLONGFLAG) ? va_arg(ap, unsigned long long) :
+ (flags & LONGFLAG) ? va_arg(ap, unsigned long) :
+ (flags & HALFHALFFLAG) ? (unsigned char)va_arg(ap, unsigned int) :
+ (flags & HALFFLAG) ? (unsigned short)va_arg(ap, unsigned int) :
+ (flags & SIZETFLAG) ? va_arg(ap, size_t) :
+ (flags & INTMAXFLAG) ? va_arg(ap, uintmax_t) :
+ (flags & PTRDIFFFLAG) ? (uintptr_t)va_arg(ap, ptrdiff_t) :
+ va_arg(ap, unsigned int);
+ s = longlong_to_string(num_buffer, n, sizeof(num_buffer), flags, &signchar);
+ goto _output_string;
+ case 'p':
+ flags |= LONGFLAG | ALTFLAG;
+ goto hex;
+ case 'X':
+ flags |= CAPSFLAG;
+ /* fallthrough */
+hex:
+ case 'x':
+ n = (flags & LONGLONGFLAG) ? va_arg(ap, unsigned long long) :
+ (flags & LONGFLAG) ? va_arg(ap, unsigned long) :
+ (flags & HALFHALFFLAG) ? (unsigned char)va_arg(ap, unsigned int) :
+ (flags & HALFFLAG) ? (unsigned short)va_arg(ap, unsigned int) :
+ (flags & SIZETFLAG) ? va_arg(ap, size_t) :
+ (flags & INTMAXFLAG) ? va_arg(ap, uintmax_t) :
+ (flags & PTRDIFFFLAG) ? (uintptr_t)va_arg(ap, ptrdiff_t) :
+ va_arg(ap, unsigned int);
+ s = longlong_to_hexstring(num_buffer, n, sizeof(num_buffer), flags);
+ if (flags & ALTFLAG) {
+ OUTPUT_CHAR('0');
+ OUTPUT_CHAR((flags & CAPSFLAG) ? 'X': 'x');
+ }
+ goto _output_string;
+ case 'n':
+ ptr = va_arg(ap, void *);
+ if (flags & LONGLONGFLAG)
+ *(long long *)ptr = chars_written;
+ else if (flags & LONGFLAG)
+ *(long *)ptr = chars_written;
+ else if (flags & HALFHALFFLAG)
+ *(signed char *)ptr = chars_written;
+ else if (flags & HALFFLAG)
+ *(short *)ptr = chars_written;
+ else if (flags & SIZETFLAG)
+ *(size_t *)ptr = chars_written;
+ else
+ *(int *)ptr = chars_written;
+ break;
+#if FLOAT_PRINTF
+ case 'F':
+ flags |= CAPSFLAG;
+ /* fallthrough */
+ case 'f': {
+ double d = va_arg(ap, double);
+ s = double_to_string(num_buffer, sizeof(num_buffer), d, flags);
+ goto _output_string;
+ }
+ case 'A':
+ flags |= CAPSFLAG;
+ /* fallthrough */
+ case 'a': {
+ double d = va_arg(ap, double);
+ s = double_to_hexstring(num_buffer, sizeof(num_buffer), d, flags);
+ goto _output_string;
+ }
+#endif
+ default:
+ OUTPUT_CHAR('%');
+ OUTPUT_CHAR(c);
+ break;
+ }
+
+ /* move on to the next field */
+ continue;
+
+ /* shared output code */
+_output_string:
+ string_len = strlen(s);
+
+ if (flags & LEFTFORMATFLAG) {
+ /* left justify the text */
+ OUTPUT_STRING(s, string_len);
+ uint written = err;
+
+ /* pad to the right (if necessary) */
+ for (; format_num > written; format_num--)
+ OUTPUT_CHAR(' ');
+ } else {
+ /* right justify the text (digits) */
+
+ /* if we're going to print a sign digit,
+ it'll chew up one byte of the format size */
+ if (signchar != '\0' && format_num > 0)
+ format_num--;
+
+ /* output the sign char before the leading zeros */
+ if (flags & LEADZEROFLAG && signchar != '\0')
+ OUTPUT_CHAR(signchar);
+
+ /* pad according to the format string */
+ for (; format_num > string_len; format_num--)
+ OUTPUT_CHAR(flags & LEADZEROFLAG ? '0' : ' ');
+
+ /* if not leading zeros, output the sign char just before the number */
+ if (!(flags & LEADZEROFLAG) && signchar != '\0')
+ OUTPUT_CHAR(signchar);
+
+ /* output the string */
+ OUTPUT_STRING(s, string_len);
+ }
+ continue;
+ }
+
+#undef OUTPUT_STRING
+#undef OUTPUT_CHAR
+
+exit:
+ return (err < 0) ? err : (int)chars_written;
+}
diff --git a/src/bsp/lk/lib/libc/pure_virtual.cpp b/src/bsp/lk/lib/libc/pure_virtual.cpp
new file mode 100644
index 0000000..5d3fc0a
--- /dev/null
+++ b/src/bsp/lk/lib/libc/pure_virtual.cpp
@@ -0,0 +1,29 @@
+/*
+ * Copyright (c) 2006 Travis Geiselbrecht
+ *
+ * Permission is hereby granted, free of charge, to any person obtaining
+ * a copy of this software and associated documentation files
+ * (the "Software"), to deal in the Software without restriction,
+ * including without limitation the rights to use, copy, modify, merge,
+ * publish, distribute, sublicense, and/or sell copies of the Software,
+ * and to permit persons to whom the Software is furnished to do so,
+ * subject to the following conditions:
+ *
+ * The above copyright notice and this permission notice shall be
+ * included in all copies or substantial portions of the Software.
+ *
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
+ * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
+ * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
+ * IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
+ * CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
+ * TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
+ * SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
+ */
+#include <debug.h>
+
+extern "C" void __cxa_pure_virtual(void)
+{
+ panic("pure virtual called\n");
+}
+
diff --git a/src/bsp/lk/lib/libc/qsort.c b/src/bsp/lk/lib/libc/qsort.c
new file mode 100644
index 0000000..ddd1c45
--- /dev/null
+++ b/src/bsp/lk/lib/libc/qsort.c
@@ -0,0 +1,162 @@
+/* $OpenBSD: qsort.c,v 1.10 2005/08/08 08:05:37 espie Exp $ */
+/*-
+ * Copyright (c) 1992, 1993
+ * The Regents of the University of California. All rights reserved.
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions
+ * are met:
+ * 1. Redistributions of source code must retain the above copyright
+ * notice, this list of conditions and the following disclaimer.
+ * 2. Redistributions in binary form must reproduce the above copyright
+ * notice, this list of conditions and the following disclaimer in the
+ * documentation and/or other materials provided with the distribution.
+ * 3. Neither the name of the University nor the names of its contributors
+ * may be used to endorse or promote products derived from this software
+ * without specific prior written permission.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
+ * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
+ * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
+ * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
+ * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
+ * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
+ * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
+ * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
+ * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
+ * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
+ * SUCH DAMAGE.
+ */
+
+#include <sys/types.h>
+#include <stdlib.h>
+
+static __inline char *med3(char *, char *, char *, int (*)(const void *, const void *));
+static __inline void swapfunc(char *, char *, int, int);
+
+#define min(a, b) (a) < (b) ? a : b
+
+/*
+ * Qsort routine from Bentley & McIlroy's "Engineering a Sort Function".
+ */
+#define swapcode(TYPE, parmi, parmj, n) { \
+ long i = (n) / sizeof (TYPE); \
+ TYPE *pi = (TYPE *) (parmi); \
+ TYPE *pj = (TYPE *) (parmj); \
+ do { \
+ TYPE t = *pi; \
+ *pi++ = *pj; \
+ *pj++ = t; \
+ } while (--i > 0); \
+}
+
+#define SWAPINIT(a, es) swaptype = ((char *)a - (char *)0) % sizeof(long) || \
+ es % sizeof(long) ? 2 : es == sizeof(long)? 0 : 1;
+
+static __inline void
+swapfunc(char *a, char *b, int n, int swaptype)
+{
+ if (swaptype <= 1)
+ swapcode(long, a, b, n)
+ else
+ swapcode(char, a, b, n)
+ }
+
+#define swap(a, b) \
+ if (swaptype == 0) { \
+ long t = *(long *)(a); \
+ *(long *)(a) = *(long *)(b); \
+ *(long *)(b) = t; \
+ } else \
+ swapfunc(a, b, es, swaptype)
+
+#define vecswap(a, b, n) if ((n) > 0) swapfunc(a, b, n, swaptype)
+
+static __inline char *
+med3(char *a, char *b, char *c, int (*cmp)(const void *, const void *))
+{
+ return cmp(a, b) < 0 ?
+ (cmp(b, c) < 0 ? b : (cmp(a, c) < 0 ? c : a ))
+ :(cmp(b, c) > 0 ? b : (cmp(a, c) < 0 ? a : c ));
+}
+
+void
+qsort(void *aa, size_t n, size_t es, int (*cmp)(const void *, const void *))
+{
+ char *pa, *pb, *pc, *pd, *pl, *pm, *pn;
+ int d, r, swaptype, swap_cnt;
+ char *a = aa;
+
+loop:
+ SWAPINIT(a, es);
+ swap_cnt = 0;
+ if (n < 7) {
+ for (pm = (char *)a + es; pm < (char *) a + n * es; pm += es)
+ for (pl = pm; pl > (char *) a && cmp(pl - es, pl) > 0;
+ pl -= es)
+ swap(pl, pl - es);
+ return;
+ }
+ pm = (char *)a + (n / 2) * es;
+ if (n > 7) {
+ pl = (char *)a;
+ pn = (char *)a + (n - 1) * es;
+ if (n > 40) {
+ d = (n / 8) * es;
+ pl = med3(pl, pl + d, pl + 2 * d, cmp);
+ pm = med3(pm - d, pm, pm + d, cmp);
+ pn = med3(pn - 2 * d, pn - d, pn, cmp);
+ }
+ pm = med3(pl, pm, pn, cmp);
+ }
+ swap(a, pm);
+ pa = pb = (char *)a + es;
+
+ pc = pd = (char *)a + (n - 1) * es;
+ for (;;) {
+ while (pb <= pc && (r = cmp(pb, a)) <= 0) {
+ if (r == 0) {
+ swap_cnt = 1;
+ swap(pa, pb);
+ pa += es;
+ }
+ pb += es;
+ }
+ while (pb <= pc && (r = cmp(pc, a)) >= 0) {
+ if (r == 0) {
+ swap_cnt = 1;
+ swap(pc, pd);
+ pd -= es;
+ }
+ pc -= es;
+ }
+ if (pb > pc)
+ break;
+ swap(pb, pc);
+ swap_cnt = 1;
+ pb += es;
+ pc -= es;
+ }
+ if (swap_cnt == 0) { /* Switch to insertion sort */
+ for (pm = (char *) a + es; pm < (char *) a + n * es; pm += es)
+ for (pl = pm; pl > (char *) a && cmp(pl - es, pl) > 0;
+ pl -= es)
+ swap(pl, pl - es);
+ return;
+ }
+
+ pn = (char *)a + n * es;
+ r = min(pa - (char *)a, pb - pa);
+ vecswap(a, pb - r, r);
+ r = min(pd - pc, pn - pd - (int)es);
+ vecswap(pb, pn - r, r);
+ if ((r = pb - pa) > (int)es)
+ qsort(a, r / es, es, cmp);
+ if ((r = pd - pc) > (int)es) {
+ /* Iterate rather than recurse to save stack space */
+ a = pn - r;
+ n = r / es;
+ goto loop;
+ }
+ /* qsort(pn - r, r / es, es, cmp); */
+}
diff --git a/src/bsp/lk/lib/libc/rand.c b/src/bsp/lk/lib/libc/rand.c
new file mode 100644
index 0000000..fe502c1
--- /dev/null
+++ b/src/bsp/lk/lib/libc/rand.c
@@ -0,0 +1,49 @@
+/*
+ * Copyright (c) 2008-2014 Travis Geiselbrecht
+ *
+ * Permission is hereby granted, free of charge, to any person obtaining
+ * a copy of this software and associated documentation files
+ * (the "Software"), to deal in the Software without restriction,
+ * including without limitation the rights to use, copy, modify, merge,
+ * publish, distribute, sublicense, and/or sell copies of the Software,
+ * and to permit persons to whom the Software is furnished to do so,
+ * subject to the following conditions:
+ *
+ * The above copyright notice and this permission notice shall be
+ * included in all copies or substantial portions of the Software.
+ *
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
+ * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
+ * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
+ * IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
+ * CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
+ * TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
+ * SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
+ */
+#include <rand.h>
+#include <sys/types.h>
+
+static unsigned int randseed = 12345;
+
+void srand(unsigned int seed)
+{
+ randseed = seed;
+}
+
+void rand_add_entropy(const void *buf, size_t len)
+{
+ if (len == 0)
+ return;
+
+ uint32_t enp = 0;
+ for (size_t i = 0; i < len; i++) {
+ enp ^= ((enp << 8) | (enp >> 24)) ^ ((const uint8_t *)buf)[i];
+ }
+
+ randseed ^= enp;
+}
+
+int rand(void)
+{
+ return (randseed = randseed * 1664525 + 1013904223);
+}
diff --git a/src/bsp/lk/lib/libc/rules.mk b/src/bsp/lk/lib/libc/rules.mk
new file mode 100644
index 0000000..8b488a1
--- /dev/null
+++ b/src/bsp/lk/lib/libc/rules.mk
@@ -0,0 +1,30 @@
+LOCAL_DIR := $(GET_LOCAL_DIR)
+
+MODULE := $(LOCAL_DIR)
+
+MODULE_DEPS := \
+ lib/heap
+
+MODULE_SRCS += \
+ $(LOCAL_DIR)/atoi.c \
+ $(LOCAL_DIR)/bsearch.c \
+ $(LOCAL_DIR)/ctype.c \
+ $(LOCAL_DIR)/errno.c \
+ $(LOCAL_DIR)/printf.c \
+ $(LOCAL_DIR)/scanf.c \
+ $(LOCAL_DIR)/rand.c \
+ $(LOCAL_DIR)/strtol.c \
+ $(LOCAL_DIR)/strtoll.c \
+ $(LOCAL_DIR)/stdio.c \
+ $(LOCAL_DIR)/qsort.c \
+ $(LOCAL_DIR)/eabi.c
+
+ifeq ($(WITH_CPP_SUPPORT),true)
+MODULE_SRCS += \
+ $(LOCAL_DIR)/atexit.c \
+ $(LOCAL_DIR)/pure_virtual.cpp
+endif
+
+include $(LOCAL_DIR)/string/rules.mk
+
+include make/module.mk
diff --git a/src/bsp/lk/lib/libc/scanf.c b/src/bsp/lk/lib/libc/scanf.c
new file mode 100644
index 0000000..b3d4cd7
--- /dev/null
+++ b/src/bsp/lk/lib/libc/scanf.c
@@ -0,0 +1,243 @@
+/*
+ * Copyright (c) 2020 MediaTek Inc.
+ *
+ * Use of this source code is governed by a MIT-style
+ * license that can be found in the LICENSE file or at
+ * https://opensource.org/licenses/MIT
+ */
+
+#include <assert.h>
+#include <ctype.h>
+#include <debug.h>
+#include <limits.h>
+#include <platform/debug.h>
+#include <printf.h>
+#include <stdarg.h>
+#include <stdio.h>
+#include <string.h>
+#include <sys/types.h>
+#include <trace.h>
+
+#define LOCAL_TRACE 0
+
+#define FLAG_SHORT (0x1 << 2)
+#define FLAG_INT (0x1 << 3)
+
+static int xchar_to_int(char c)
+{
+ if (isdigit(c))
+ return c - '0';
+ else if (c >= 'a' && c <= 'f')
+ return 10 + c - 'a';
+ else if (c >= 'A' && c <= 'F')
+ return 10 + c - 'A';
+ else
+ return 0; // not xdigit: return 0!
+}
+
+static int get_int(const char **buf, int *val)
+{
+ const char *p = *buf;
+ int i = 0;
+ int err = -1;
+ int sign = 0;
+
+ // ignore blank
+ while (isspace(*p)) {
+ ++p;
+ }
+
+ if (*p == '+')
+ ++p;
+ else if (*p == '-') {
+ sign = 1;
+ ++p;
+ }
+
+ while (1) {
+ char c = *p;
+
+ if (isdigit(c)) {
+ i = i * 10 + (c - '0');
+ err = 0; // find at least one digit
+
+ LTRACEF("buf:%s, i:%d, c:%c\n", p, i, c);
+
+ ++p;
+ continue;
+ } else {
+ if (err == -1)
+ return -1;
+
+ *buf = p;
+
+ *val = sign ? -i : i;
+ break;
+ }
+ }
+
+ return 0;
+}
+
+static int get_hex_int(const char **buf)
+{
+ const char *p = *buf;
+ int i = 0;
+ int err = -1;
+ int found_hex_prefix = 0, ignore_hex_prefix = 0;
+
+ // ignore blank
+ while (isspace(*p)) {
+ ++p;
+ }
+
+ while (1) {
+ char c = *p;
+
+ if (isspace(c)) {
+ *buf = p;
+ break;
+ }
+
+ // hex prefix
+ if (!ignore_hex_prefix && !found_hex_prefix && c == '0') {
+ c = *++p;
+ if (c == 'x' || c == 'X') {
+ found_hex_prefix = 1;
+ ignore_hex_prefix = 1;
+ ++p;
+ continue;
+ } else if (isxdigit(c)) {
+ i = i * 16 + (xchar_to_int(c));
+ ignore_hex_prefix = 1;
+ ++p;
+ } else
+ goto error;
+ }
+
+ if (isxdigit(c)) {
+ i = i * 16 + xchar_to_int(c);
+ ignore_hex_prefix = 1;
+ err = 0; // find at least one digit
+
+ LTRACEF("buf:%s, i:0x%x, c:%c\n", p, i, c);
+
+ ++p;
+ continue;
+ } else if (err == 0)
+ break;
+ else
+ goto error;
+ }
+
+ if (err == 0)
+ *buf = p;
+ return i;
+
+error:
+ return -1;
+}
+
+/* support interger scanf */
+static int vsscanf(const char *buf, const char *fmt, va_list ap)
+{
+ int err = 0;
+ char c;
+ size_t item;
+ int n;
+ int *p;
+ char *pch;
+ short *psh;
+ int flag;
+
+ item = 0;
+ flag = FLAG_INT;
+
+ for (;;) {
+ while ((c = *fmt++) != 0) {
+ if (isspace(c))
+ continue;
+
+ if (c == '%')
+ break;
+ else {
+ while (isspace(*buf)) {
+ ++buf;
+ }
+ if (c != *buf)
+ return 0;
+
+ ++buf;
+ }
+ }
+
+ /* make sure we haven't just hit the end of the string */
+ if (c == 0)
+ break;
+
+ c = *fmt++;
+ if (c == 0)
+ break;
+
+format_continue:
+ switch (c) {
+ case 'h': /* short interger: %hd */
+ flag = FLAG_SHORT;
+ c = *fmt++;
+ goto format_continue;
+ case 'd': /* interger: %d */
+ if (flag == FLAG_SHORT)
+ psh = va_arg(ap, short *);
+ else
+ p = va_arg(ap, int *);
+
+ err = get_int(&buf, &n);
+
+ if (err < 0)
+ goto exit;
+
+ if (flag == FLAG_SHORT)
+ *psh = n;
+ else
+ *p = n;
+ item++;
+ break;
+
+ case 'x': /* hex interger */
+ case 'X':
+ p = va_arg(ap, int *);
+
+ n = get_hex_int(&buf);
+
+ if (n < 0)
+ goto exit;
+
+ *p = n;
+ item++;
+ break;
+
+ default:
+ err = -1;
+ break;
+ }
+
+ continue;
+ }
+
+exit:
+ return (err < 0) ? 0 : (int)item;
+}
+
+// a simple sscanf: %d, %x
+int sscanf(const char *buf, const char *fmt, ...)
+{
+ va_list ap;
+ int err;
+
+ va_start(ap, fmt);
+ err = vsscanf(buf, fmt, ap);
+ va_end(ap);
+
+ return err;
+}
+
diff --git a/src/bsp/lk/lib/libc/stdio.c b/src/bsp/lk/lib/libc/stdio.c
new file mode 100644
index 0000000..2c96e94
--- /dev/null
+++ b/src/bsp/lk/lib/libc/stdio.c
@@ -0,0 +1,88 @@
+/*
+ * Copyright (c) 2013 Travis Geiselbrecht
+ *
+ * Permission is hereby granted, free of charge, to any person obtaining
+ * a copy of this software and associated documentation files
+ * (the "Software"), to deal in the Software without restriction,
+ * including without limitation the rights to use, copy, modify, merge,
+ * publish, distribute, sublicense, and/or sell copies of the Software,
+ * and to permit persons to whom the Software is furnished to do so,
+ * subject to the following conditions:
+ *
+ * The above copyright notice and this permission notice shall be
+ * included in all copies or substantial portions of the Software.
+ *
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
+ * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
+ * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
+ * IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
+ * CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
+ * TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
+ * SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
+ */
+#include <debug.h>
+#include <stdio.h>
+#include <stdarg.h>
+#include <sys/types.h>
+#include <platform/debug.h>
+
+int fputc(int c, FILE *fp)
+{
+ return fp->fputc(fp->ctx, c);
+}
+
+int putchar(int c)
+{
+ return fputc(c, stdout);
+}
+
+int puts(const char *str)
+{
+ int err = fputs(str, stdout);
+ if (err >= 0)
+ err = fputc('\n', stdout);
+ return err;
+}
+
+int fputs(const char *s, FILE *fp)
+{
+ return fp->fputs(fp->ctx, s);
+}
+
+int getc(FILE *fp)
+{
+ return fp->fgetc(fp->ctx);
+}
+
+int getchar(void)
+{
+ return getc(stdin);
+}
+
+int vfprintf(FILE *fp, const char *fmt, va_list ap)
+{
+ return fp->vfprintf(fp->ctx, fmt, ap);
+}
+
+int fprintf(FILE *fp, const char *fmt, ...)
+{
+ va_list ap;
+ int err;
+
+ va_start(ap, fmt);
+ err = vfprintf(fp, fmt, ap);
+ va_end(ap);
+ return err;
+}
+
+int _printf(const char *fmt, ...)
+{
+ va_list ap;
+ int err;
+
+ va_start(ap, fmt);
+ err = vfprintf(stdout, fmt, ap);
+ va_end(ap);
+
+ return err;
+}
diff --git a/src/bsp/lk/lib/libc/string/arch/arm/arm-m/memcpy.S b/src/bsp/lk/lib/libc/string/arch/arm/arm-m/memcpy.S
new file mode 100644
index 0000000..46ce8cb
--- /dev/null
+++ b/src/bsp/lk/lib/libc/string/arch/arm/arm-m/memcpy.S
@@ -0,0 +1,135 @@
+/*
+ * Copyright (c) 2014 Travis Geiselbrecht
+ *
+ * Permission is hereby granted, free of charge, to any person obtaining
+ * a copy of this software and associated documentation files
+ * (the "Software"), to deal in the Software without restriction,
+ * including without limitation the rights to use, copy, modify, merge,
+ * publish, distribute, sublicense, and/or sell copies of the Software,
+ * and to permit persons to whom the Software is furnished to do so,
+ * subject to the following conditions:
+ *
+ * The above copyright notice and this permission notice shall be
+ * included in all copies or substantial portions of the Software.
+ *
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
+ * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
+ * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
+ * IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
+ * CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
+ * TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
+ * SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
+ */
+#include <asm.h>
+#include <arch/arm/cores.h>
+
+.text
+.syntax unified
+.thumb
+.align 2
+
+/* void bcopy(const void *src, void *dest, size_t n); */
+FUNCTION(bcopy)
+ // swap args for bcopy
+ mov r12, r0
+ mov r0, r1
+ mov r1, r12
+
+/* void *memcpy(void *dest, const void *src, size_t count) */
+FUNCTION(memcpy)
+ push { r0, r14 }
+
+ // test for zero length or pointers being equivalent
+ cbz r2, .L_done
+ cmp r0, r1
+ beq .L_done
+
+ // check for a short copy len
+ cmp r2, #16
+ blt .L_bytewise
+
+ // check to see if the pointers are similarly dword aligned
+ eors r3, r0, r1
+ ands r3, #7
+ beq .L_prepare_dword
+
+ // see how many bytes we need to move to align dest to word boundary
+ and r3, r0, #3
+ cbz r3, .L_prepare_wordwise
+ rsb r3, #4
+ subs r2, r3
+
+ .align 2
+.L_bytewise_align:
+ // bytewise to align memcpy
+ ldrb r12, [r1], #1
+ subs r3, r3, #1
+ strb r12, [r0], #1
+ bgt .L_bytewise_align
+
+.L_prepare_wordwise:
+ // load the number of words left
+ lsrs r3, r2, #2
+
+ .align 2
+.L_wordwise:
+ // wordwise copy
+ ldr r12, [r1], #4
+ subs r3, r3, #1
+ str r12, [r0], #4
+ bgt .L_wordwise
+
+ // remaining bytes
+ ands r2, #3
+ beq .L_done
+
+ .align 2
+.L_bytewise:
+ // simple bytewise copy
+ ldrb r12, [r1], #1
+ subs r2, r2, #1
+ strb r12, [r0], #1
+ bgt .L_bytewise
+
+.L_done:
+ pop { r0, pc }
+
+// Handle copying by dword (8 bytes at a time) increments
+.L_prepare_dword:
+ // see how many bytes we need to move to align dest to dword boundary
+ and r3, r0, #7
+ cbz r3, .L_prepare_dwordwise
+ rsb r3, #8
+ subs r2, r3
+
+ .align 2
+.L_bytewise_align_dword:
+ // bytewise to align memcpy
+ ldrb r12, [r1], #1
+ subs r3, r3, #1
+ strb r12, [r0], #1
+ bgt .L_bytewise_align_dword
+
+.L_prepare_dwordwise:
+ // load the number of dwords left
+ lsrs r3, r2, #3
+
+ push { r5 }
+
+ .align 2
+.L_dwordwise:
+ // dwordwise copy
+ ldrd r5, r12, [r1], #8
+ subs r3, r3, #1
+ strd r5, r12, [r0], #8
+ bgt .L_dwordwise
+
+ pop { r5 }
+
+ // remaining bytes
+ ands r2, #7
+ beq .L_done
+
+ // finish the remaining bytes and exit
+ b .L_bytewise
+
diff --git a/src/bsp/lk/lib/libc/string/arch/arm/arm-m/memset.S b/src/bsp/lk/lib/libc/string/arch/arm/arm-m/memset.S
new file mode 100644
index 0000000..0b54e3c
--- /dev/null
+++ b/src/bsp/lk/lib/libc/string/arch/arm/arm-m/memset.S
@@ -0,0 +1,90 @@
+/*
+ * Copyright (c) 2014 Travis Geiselbrecht
+ *
+ * Permission is hereby granted, free of charge, to any person obtaining
+ * a copy of this software and associated documentation files
+ * (the "Software"), to deal in the Software without restriction,
+ * including without limitation the rights to use, copy, modify, merge,
+ * publish, distribute, sublicense, and/or sell copies of the Software,
+ * and to permit persons to whom the Software is furnished to do so,
+ * subject to the following conditions:
+ *
+ * The above copyright notice and this permission notice shall be
+ * included in all copies or substantial portions of the Software.
+ *
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
+ * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
+ * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
+ * IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
+ * CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
+ * TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
+ * SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
+ */
+#include <asm.h>
+#include <arch/arm/cores.h>
+
+.text
+.syntax unified
+.thumb
+.align 2
+
+/* void bzero(void *s, size_t n); */
+FUNCTION(bzero)
+ mov r2, r1
+ movs r1, #0
+
+/* void *memset(void *s, int c, size_t n); */
+FUNCTION(memset)
+ // save the original pointer
+ push { r0, lr }
+
+ // check for zero length
+ cbz r2, .L_done
+
+ // short memsets aren't worth optimizing and make sure we have
+ // enough headroom to try to do dwordwise move optimization
+ cmp r2, #16
+ blt .L_bytewise
+
+ // see how many bytes we need to move to align to dword boundaries
+ and r3, r0, #7
+ cbz r3, .L_prepare_dwordwise
+ rsb r3, #8
+ subs r2, r3
+
+.L_bytewise_align:
+ // bytewise to align memset
+ subs r3, r3, #1
+ strb r1, [r0], #1
+ bgt .L_bytewise_align
+
+.L_prepare_dwordwise:
+ // fill a pair of 32 bit registers with the 8 bit value
+ uxtb r1, r1
+ orr r1, r1, r1, lsl #8
+ orr r1, r1, r1, lsl #16
+ mov r12, r1
+
+ // load the number of dwords left
+ lsrs r3, r2, #3
+
+.L_dwordwise:
+ // dwordwise memset
+ subs r3, r3, #1
+ strd r1, r12, [r0], #8
+ bgt .L_dwordwise
+
+ // remaining bytes
+ ands r2, #7
+ beq .L_done
+
+.L_bytewise:
+ // bytewise memset
+ subs r2, r2, #1
+ strb r1, [r0], #1
+ bgt .L_bytewise
+
+.L_done:
+ // restore the base pointer as return value
+ pop { r0, pc }
+
diff --git a/src/bsp/lk/lib/libc/string/arch/arm/arm/memcpy.S b/src/bsp/lk/lib/libc/string/arch/arm/arm/memcpy.S
new file mode 100644
index 0000000..efe46df
--- /dev/null
+++ b/src/bsp/lk/lib/libc/string/arch/arm/arm/memcpy.S
@@ -0,0 +1,178 @@
+/*
+ * Copyright (c) 2008 Travis Geiselbrecht
+ *
+ * Permission is hereby granted, free of charge, to any person obtaining
+ * a copy of this software and associated documentation files
+ * (the "Software"), to deal in the Software without restriction,
+ * including without limitation the rights to use, copy, modify, merge,
+ * publish, distribute, sublicense, and/or sell copies of the Software,
+ * and to permit persons to whom the Software is furnished to do so,
+ * subject to the following conditions:
+ *
+ * The above copyright notice and this permission notice shall be
+ * included in all copies or substantial portions of the Software.
+ *
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
+ * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
+ * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
+ * IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
+ * CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
+ * TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
+ * SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
+ */
+#include <asm.h>
+#include <arch/arm/cores.h>
+
+.text
+.align 2
+
+/* void bcopy(const void *src, void *dest, size_t n); */
+FUNCTION(bcopy)
+ // swap args for bcopy
+ mov r12, r0
+ mov r0, r1
+ mov r1, r12
+
+/* void *memcpy(void *dest, const void *src, size_t n); */
+FUNCTION(memmove)
+FUNCTION(memcpy)
+ // check for zero length copy or the same pointer
+ cmp r2, #0
+ cmpne r1, r0
+ bxeq lr
+
+ // save a few registers for use and the return code (input dst)
+ stmfd sp!, {r0, r4, r5, lr}
+
+ // check for forwards overlap (src > dst, distance < len)
+ subs r3, r0, r1
+ cmphi r2, r3
+ bhi .L_forwardoverlap
+
+ // check for a short copy len.
+ // 20 bytes is enough so that if a 16 byte alignment needs to happen there is at least a
+ // wordwise copy worth of work to be done.
+ cmp r2, #(16+4)
+ blo .L_bytewise
+
+ // see if they are similarly aligned on 4 byte boundaries
+ eor r3, r0, r1
+ tst r3, #3
+ bne .L_bytewise // dissimilarly aligned, nothing we can do (for now)
+
+ // check for 16 byte alignment on dst.
+ // this will also catch src being not 4 byte aligned, since it is similarly 4 byte
+ // aligned with dst at this point.
+ tst r0, #15
+ bne .L_not16bytealigned
+
+ // check to see if we have at least 32 bytes of data to copy.
+ // if not, just revert to wordwise copy
+ cmp r2, #32
+ blo .L_wordwise
+
+.L_bigcopy:
+ // copy 32 bytes at a time. src & dst need to be at least 4 byte aligned,
+ // and we need at least 32 bytes remaining to copy
+
+ // save r6-r7 for use in the big copy
+ stmfd sp!, {r6-r11}
+
+ sub r2, r2, #32 // subtract an extra 32 to the len so we can avoid an extra compare
+
+.L_bigcopy_loop:
+ pld [r1, #64]
+ ldmia r1!, {r4-r11}
+ subs r2, r2, #32
+ stmia r0!, {r4-r11}
+ bhs .L_bigcopy_loop
+
+ // restore r6-r7
+ ldmfd sp!, {r6-r11}
+
+ // see if we are done
+ adds r2, r2, #32
+ beq .L_done
+
+ // less then 4 bytes left?
+ cmp r2, #4
+ blo .L_bytewise
+
+.L_wordwise:
+ // copy 4 bytes at a time.
+ // src & dst are guaranteed to be word aligned, and at least 4 bytes are left to copy.
+ subs r2, r2, #4
+
+.L_wordwise_loop:
+ ldr r3, [r1], #4
+ subs r2, r2, #4
+ str r3, [r0], #4
+ bhs .L_wordwise_loop
+
+ // correct the remaining len and test for completion
+ adds r2, r2, #4
+ beq .L_done
+
+.L_bytewise:
+ // simple bytewise copy
+ ldrb r3, [r1], #1
+ subs r2, r2, #1
+ strb r3, [r0], #1
+ bhi .L_bytewise
+
+.L_done:
+ // load dst for return and restore r4,r5
+#if ARM_ARCH_LEVEL >= 5
+ ldmfd sp!, {r0, r4, r5, pc}
+#else
+ ldmfd sp!, {r0, r4, r5, lr}
+ bx lr
+#endif
+
+.L_not16bytealigned:
+ // dst is not 16 byte aligned, so we will copy up to 15 bytes to get it aligned.
+ // src is guaranteed to be similarly word aligned with dst.
+
+ // set the condition flags based on the alignment.
+ lsl r12, r0, #28
+ rsb r12, r12, #0
+ msr CPSR_f, r12 // move into NZCV fields in CPSR
+
+ // move as many bytes as necessary to get the dst aligned
+ ldrvsb r3, [r1], #1 // V set
+ ldrcsh r4, [r1], #2 // C set
+ ldreq r5, [r1], #4 // Z set
+
+ strvsb r3, [r0], #1
+ strcsh r4, [r0], #2
+ streq r5, [r0], #4
+
+ ldmmiia r1!, {r3-r4} // N set
+ stmmiia r0!, {r3-r4}
+
+ // fix the remaining len
+ sub r2, r2, r12, lsr #28
+
+ // test to see what we should do now
+ cmp r2, #32
+ bhs .L_bigcopy
+ b .L_wordwise
+
+ // src and dest overlap 'forwards' or dst > src
+.L_forwardoverlap:
+
+ // do a bytewise reverse copy for now
+ add r1, r1, r2
+ add r0, r0, r2
+ sub r1, r1, #1
+ sub r0, r0, #1
+
+.L_bytewisereverse:
+ // simple bytewise reverse copy
+ ldrb r3, [r1], #-1
+ subs r2, r2, #1
+ strb r3, [r0], #-1
+ bhi .L_bytewisereverse
+
+ b .L_done
+
diff --git a/src/bsp/lk/lib/libc/string/arch/arm/arm/memset.S b/src/bsp/lk/lib/libc/string/arch/arm/arm/memset.S
new file mode 100644
index 0000000..1307e1c
--- /dev/null
+++ b/src/bsp/lk/lib/libc/string/arch/arm/arm/memset.S
@@ -0,0 +1,113 @@
+/*
+ * Copyright (c) 2008 Travis Geiselbrecht
+ *
+ * Permission is hereby granted, free of charge, to any person obtaining
+ * a copy of this software and associated documentation files
+ * (the "Software"), to deal in the Software without restriction,
+ * including without limitation the rights to use, copy, modify, merge,
+ * publish, distribute, sublicense, and/or sell copies of the Software,
+ * and to permit persons to whom the Software is furnished to do so,
+ * subject to the following conditions:
+ *
+ * The above copyright notice and this permission notice shall be
+ * included in all copies or substantial portions of the Software.
+ *
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
+ * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
+ * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
+ * IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
+ * CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
+ * TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
+ * SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
+ */
+#include <asm.h>
+#include <arch/arm/cores.h>
+
+.text
+.align 2
+
+/* void bzero(void *s, size_t n); */
+FUNCTION(bzero)
+ mov r2, r1
+ mov r1, #0
+
+/* void *memset(void *s, int c, size_t n); */
+FUNCTION(memset)
+ // check for zero length
+ cmp r2, #0
+ bxeq lr
+
+ // save the original pointer
+ mov r12, r0
+
+ // short memsets aren't worth optimizing
+ cmp r2, #(32 + 16)
+ blt .L_bytewise
+
+ // fill a 32 bit register with the 8 bit value
+ and r1, r1, #0xff
+ orr r1, r1, r1, lsl #8
+ orr r1, r1, r1, lsl #16
+
+ // check for 16 byte alignment
+ tst r0, #15
+ bne .L_not16bytealigned
+
+.L_bigset:
+ // dump some registers to make space for our values
+ stmfd sp!, { r4-r5 }
+
+ // fill a bunch of registers with the set value
+ mov r3, r1
+ mov r4, r1
+ mov r5, r1
+
+ // prepare the count register so we can avoid an extra compare
+ sub r2, r2, #32
+
+ // 32 bytes at a time
+.L_bigset_loop:
+ stmia r0!, { r1, r3, r4, r5 }
+ subs r2, r2, #32
+ stmia r0!, { r1, r3, r4, r5 }
+ bge .L_bigset_loop
+
+ // restore our dumped registers
+ ldmfd sp!, { r4-r5 }
+
+ // see if we're done
+ adds r2, r2, #32
+ beq .L_done
+
+.L_bytewise:
+ // bytewise memset
+ subs r2, r2, #1
+ strb r1, [r0], #1
+ bgt .L_bytewise
+
+.L_done:
+ // restore the base pointer as return value
+ mov r0, r12
+ bx lr
+
+.L_not16bytealigned:
+ // dst is not 16 byte aligned, so we will set up to 15 bytes to get it aligned.
+
+ // set the condition flags based on the alignment.
+ lsl r3, r0, #28
+ rsb r3, r3, #0
+ msr CPSR_f, r3 // move into NZCV fields in CPSR
+
+ // move as many bytes as necessary to get the dst aligned
+ strvsb r1, [r0], #1 // V set
+ strcsh r1, [r0], #2 // C set
+ streq r1, [r0], #4 // Z set
+ strmi r1, [r0], #4 // N set
+ strmi r1, [r0], #4 // N set
+
+ // fix the remaining len
+ sub r2, r2, r3, lsr #28
+
+ // do the large memset
+ b .L_bigset
+
diff --git a/src/bsp/lk/lib/libc/string/arch/arm/rules.mk b/src/bsp/lk/lib/libc/string/arch/arm/rules.mk
new file mode 100644
index 0000000..8674a9d
--- /dev/null
+++ b/src/bsp/lk/lib/libc/string/arch/arm/rules.mk
@@ -0,0 +1,41 @@
+LOCAL_DIR := $(GET_LOCAL_DIR)
+
+ifeq ($(SUBARCH),arm)
+
+ASM_STRING_OPS := bcopy bzero memcpy memmove memset
+
+MODULE_SRCS += \
+ $(LOCAL_DIR)/arm/memcpy.S \
+ $(LOCAL_DIR)/arm/memset.S
+
+# filter out the C implementation
+C_STRING_OPS := $(filter-out $(ASM_STRING_OPS),$(C_STRING_OPS))
+endif
+
+ifeq ($(SUBARCH),arm-m)
+ifeq ($(ARM_CPU),cortex-m0)
+
+ASM_STRING_OPS :=
+
+MODULE_SRCS += \
+
+else ifeq ($(ARM_CPU),cortex-m0plus)
+
+ASM_STRING_OPS :=
+
+MODULE_SRCS += \
+
+else
+
+ASM_STRING_OPS := bcopy bzero memcpy memset
+
+MODULE_SRCS += \
+ $(LOCAL_DIR)/arm-m/memcpy.S \
+ $(LOCAL_DIR)/arm-m/memset.S
+endif
+
+# filter out the C implementation
+C_STRING_OPS := $(filter-out $(ASM_STRING_OPS),$(C_STRING_OPS))
+endif
+
+
diff --git a/src/bsp/lk/lib/libc/string/arch/arm64/rules.mk b/src/bsp/lk/lib/libc/string/arch/arm64/rules.mk
new file mode 100644
index 0000000..7a97451
--- /dev/null
+++ b/src/bsp/lk/lib/libc/string/arch/arm64/rules.mk
@@ -0,0 +1,2 @@
+LOCAL_DIR := $(GET_LOCAL_DIR)
+
diff --git a/src/bsp/lk/lib/libc/string/arch/microblaze/rules.mk b/src/bsp/lk/lib/libc/string/arch/microblaze/rules.mk
new file mode 100644
index 0000000..3dc080b
--- /dev/null
+++ b/src/bsp/lk/lib/libc/string/arch/microblaze/rules.mk
@@ -0,0 +1,11 @@
+LOCAL_DIR := $(GET_LOCAL_DIR)
+
+ASM_STRING_OPS := #bcopy bzero memcpy memmove memset
+
+MODULE_SRCS += \
+ #$(LOCAL_DIR)/memcpy.S \
+ #$(LOCAL_DIR)/memset.S
+
+# filter out the C implementation
+C_STRING_OPS := $(filter-out $(ASM_STRING_OPS),$(C_STRING_OPS))
+
diff --git a/src/bsp/lk/lib/libc/string/arch/mips/rules.mk b/src/bsp/lk/lib/libc/string/arch/mips/rules.mk
new file mode 100644
index 0000000..3dc080b
--- /dev/null
+++ b/src/bsp/lk/lib/libc/string/arch/mips/rules.mk
@@ -0,0 +1,11 @@
+LOCAL_DIR := $(GET_LOCAL_DIR)
+
+ASM_STRING_OPS := #bcopy bzero memcpy memmove memset
+
+MODULE_SRCS += \
+ #$(LOCAL_DIR)/memcpy.S \
+ #$(LOCAL_DIR)/memset.S
+
+# filter out the C implementation
+C_STRING_OPS := $(filter-out $(ASM_STRING_OPS),$(C_STRING_OPS))
+
diff --git a/src/bsp/lk/lib/libc/string/arch/or1k/rules.mk b/src/bsp/lk/lib/libc/string/arch/or1k/rules.mk
new file mode 100644
index 0000000..e69de29
--- /dev/null
+++ b/src/bsp/lk/lib/libc/string/arch/or1k/rules.mk
diff --git a/src/bsp/lk/lib/libc/string/arch/x86-64/memcpy.S b/src/bsp/lk/lib/libc/string/arch/x86-64/memcpy.S
new file mode 100644
index 0000000..84aec44
--- /dev/null
+++ b/src/bsp/lk/lib/libc/string/arch/x86-64/memcpy.S
@@ -0,0 +1,38 @@
+/*
+ * Copyright (c) 2009 Corey Tabaka
+ *
+ * Permission is hereby granted, free of charge, to any person obtaining
+ * a copy of this software and associated documentation files
+ * (the "Software"), to deal in the Software without restriction,
+ * including without limitation the rights to use, copy, modify, merge,
+ * publish, distribute, sublicense, and/or sell copies of the Software,
+ * and to permit persons to whom the Software is furnished to do so,
+ * subject to the following conditions:
+ *
+ * The above copyright notice and this permission notice shall be
+ * included in all copies or substantial portions of the Software.
+ *
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
+ * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
+ * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
+ * IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
+ * CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
+ * TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
+ * SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
+ */
+#include <asm.h>
+
+/* TODO: */
+
+.text
+.align 2
+
+/* void bcopy(const void *src, void *dest, size_t n); */
+FUNCTION(bcopy)
+ ret
+
+/* void *memcpy(void *dest, const void *src, size_t n); */
+FUNCTION(memmove)
+FUNCTION(memcpy)
+ ret
+
diff --git a/src/bsp/lk/lib/libc/string/arch/x86-64/memset.S b/src/bsp/lk/lib/libc/string/arch/x86-64/memset.S
new file mode 100644
index 0000000..43367f6
--- /dev/null
+++ b/src/bsp/lk/lib/libc/string/arch/x86-64/memset.S
@@ -0,0 +1,37 @@
+/*
+ * Copyright (c) 2009 Corey Tabaka
+ *
+ * Permission is hereby granted, free of charge, to any person obtaining
+ * a copy of this software and associated documentation files
+ * (the "Software"), to deal in the Software without restriction,
+ * including without limitation the rights to use, copy, modify, merge,
+ * publish, distribute, sublicense, and/or sell copies of the Software,
+ * and to permit persons to whom the Software is furnished to do so,
+ * subject to the following conditions:
+ *
+ * The above copyright notice and this permission notice shall be
+ * included in all copies or substantial portions of the Software.
+ *
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
+ * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
+ * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
+ * IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
+ * CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
+ * TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
+ * SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
+ */
+#include <asm.h>
+
+/* TODO: */
+
+.text
+.align 2
+
+/* void bzero(void *s, size_t n); */
+FUNCTION(bzero)
+ ret
+
+/* void *memset(void *s, int c, size_t n); */
+FUNCTION(memset)
+ ret
+
diff --git a/src/bsp/lk/lib/libc/string/arch/x86-64/rules.mk b/src/bsp/lk/lib/libc/string/arch/x86-64/rules.mk
new file mode 100644
index 0000000..3dc080b
--- /dev/null
+++ b/src/bsp/lk/lib/libc/string/arch/x86-64/rules.mk
@@ -0,0 +1,11 @@
+LOCAL_DIR := $(GET_LOCAL_DIR)
+
+ASM_STRING_OPS := #bcopy bzero memcpy memmove memset
+
+MODULE_SRCS += \
+ #$(LOCAL_DIR)/memcpy.S \
+ #$(LOCAL_DIR)/memset.S
+
+# filter out the C implementation
+C_STRING_OPS := $(filter-out $(ASM_STRING_OPS),$(C_STRING_OPS))
+
diff --git a/src/bsp/lk/lib/libc/string/arch/x86/memcpy.S b/src/bsp/lk/lib/libc/string/arch/x86/memcpy.S
new file mode 100644
index 0000000..84aec44
--- /dev/null
+++ b/src/bsp/lk/lib/libc/string/arch/x86/memcpy.S
@@ -0,0 +1,38 @@
+/*
+ * Copyright (c) 2009 Corey Tabaka
+ *
+ * Permission is hereby granted, free of charge, to any person obtaining
+ * a copy of this software and associated documentation files
+ * (the "Software"), to deal in the Software without restriction,
+ * including without limitation the rights to use, copy, modify, merge,
+ * publish, distribute, sublicense, and/or sell copies of the Software,
+ * and to permit persons to whom the Software is furnished to do so,
+ * subject to the following conditions:
+ *
+ * The above copyright notice and this permission notice shall be
+ * included in all copies or substantial portions of the Software.
+ *
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
+ * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
+ * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
+ * IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
+ * CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
+ * TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
+ * SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
+ */
+#include <asm.h>
+
+/* TODO: */
+
+.text
+.align 2
+
+/* void bcopy(const void *src, void *dest, size_t n); */
+FUNCTION(bcopy)
+ ret
+
+/* void *memcpy(void *dest, const void *src, size_t n); */
+FUNCTION(memmove)
+FUNCTION(memcpy)
+ ret
+
diff --git a/src/bsp/lk/lib/libc/string/arch/x86/memset.S b/src/bsp/lk/lib/libc/string/arch/x86/memset.S
new file mode 100644
index 0000000..43367f6
--- /dev/null
+++ b/src/bsp/lk/lib/libc/string/arch/x86/memset.S
@@ -0,0 +1,37 @@
+/*
+ * Copyright (c) 2009 Corey Tabaka
+ *
+ * Permission is hereby granted, free of charge, to any person obtaining
+ * a copy of this software and associated documentation files
+ * (the "Software"), to deal in the Software without restriction,
+ * including without limitation the rights to use, copy, modify, merge,
+ * publish, distribute, sublicense, and/or sell copies of the Software,
+ * and to permit persons to whom the Software is furnished to do so,
+ * subject to the following conditions:
+ *
+ * The above copyright notice and this permission notice shall be
+ * included in all copies or substantial portions of the Software.
+ *
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
+ * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
+ * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
+ * IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
+ * CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
+ * TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
+ * SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
+ */
+#include <asm.h>
+
+/* TODO: */
+
+.text
+.align 2
+
+/* void bzero(void *s, size_t n); */
+FUNCTION(bzero)
+ ret
+
+/* void *memset(void *s, int c, size_t n); */
+FUNCTION(memset)
+ ret
+
diff --git a/src/bsp/lk/lib/libc/string/arch/x86/rules.mk b/src/bsp/lk/lib/libc/string/arch/x86/rules.mk
new file mode 100644
index 0000000..3dc080b
--- /dev/null
+++ b/src/bsp/lk/lib/libc/string/arch/x86/rules.mk
@@ -0,0 +1,11 @@
+LOCAL_DIR := $(GET_LOCAL_DIR)
+
+ASM_STRING_OPS := #bcopy bzero memcpy memmove memset
+
+MODULE_SRCS += \
+ #$(LOCAL_DIR)/memcpy.S \
+ #$(LOCAL_DIR)/memset.S
+
+# filter out the C implementation
+C_STRING_OPS := $(filter-out $(ASM_STRING_OPS),$(C_STRING_OPS))
+
diff --git a/src/bsp/lk/lib/libc/string/bcopy.c b/src/bsp/lk/lib/libc/string/bcopy.c
new file mode 100644
index 0000000..cc025ff
--- /dev/null
+++ b/src/bsp/lk/lib/libc/string/bcopy.c
@@ -0,0 +1,34 @@
+/*
+** Copyright 2001, Travis Geiselbrecht. All rights reserved.
+** Distributed under the terms of the NewOS License.
+*/
+/*
+ * Copyright (c) 2008 Travis Geiselbrecht
+ *
+ * Permission is hereby granted, free of charge, to any person obtaining
+ * a copy of this software and associated documentation files
+ * (the "Software"), to deal in the Software without restriction,
+ * including without limitation the rights to use, copy, modify, merge,
+ * publish, distribute, sublicense, and/or sell copies of the Software,
+ * and to permit persons to whom the Software is furnished to do so,
+ * subject to the following conditions:
+ *
+ * The above copyright notice and this permission notice shall be
+ * included in all copies or substantial portions of the Software.
+ *
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
+ * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
+ * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
+ * IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
+ * CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
+ * TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
+ * SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
+ */
+#include <string.h>
+#include <sys/types.h>
+
+void bcopy(void const *src, void *dest, size_t count)
+{
+ memcpy(dest, src, count);
+}
+
diff --git a/src/bsp/lk/lib/libc/string/bzero.c b/src/bsp/lk/lib/libc/string/bzero.c
new file mode 100644
index 0000000..b787d4e
--- /dev/null
+++ b/src/bsp/lk/lib/libc/string/bzero.c
@@ -0,0 +1,35 @@
+/*
+** Copyright 2001, Travis Geiselbrecht. All rights reserved.
+** Distributed under the terms of the NewOS License.
+*/
+/*
+ * Copyright (c) 2008 Travis Geiselbrecht
+ *
+ * Permission is hereby granted, free of charge, to any person obtaining
+ * a copy of this software and associated documentation files
+ * (the "Software"), to deal in the Software without restriction,
+ * including without limitation the rights to use, copy, modify, merge,
+ * publish, distribute, sublicense, and/or sell copies of the Software,
+ * and to permit persons to whom the Software is furnished to do so,
+ * subject to the following conditions:
+ *
+ * The above copyright notice and this permission notice shall be
+ * included in all copies or substantial portions of the Software.
+ *
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
+ * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
+ * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
+ * IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
+ * CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
+ * TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
+ * SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
+ */
+#include <string.h>
+#include <sys/types.h>
+
+void
+bzero(void *dst, size_t count)
+{
+ memset(dst, 0, count);
+}
+
diff --git a/src/bsp/lk/lib/libc/string/memchr.c b/src/bsp/lk/lib/libc/string/memchr.c
new file mode 100644
index 0000000..cb3b4bf
--- /dev/null
+++ b/src/bsp/lk/lib/libc/string/memchr.c
@@ -0,0 +1,45 @@
+/*
+** Copyright 2001, Manuel J. Petit. All rights reserved.
+** Distributed under the terms of the NewOS License.
+*/
+/*
+ * Copyright (c) 2008 Travis Geiselbrecht
+ *
+ * Permission is hereby granted, free of charge, to any person obtaining
+ * a copy of this software and associated documentation files
+ * (the "Software"), to deal in the Software without restriction,
+ * including without limitation the rights to use, copy, modify, merge,
+ * publish, distribute, sublicense, and/or sell copies of the Software,
+ * and to permit persons to whom the Software is furnished to do so,
+ * subject to the following conditions:
+ *
+ * The above copyright notice and this permission notice shall be
+ * included in all copies or substantial portions of the Software.
+ *
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
+ * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
+ * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
+ * IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
+ * CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
+ * TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
+ * SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
+ */
+#include <string.h>
+#include <sys/types.h>
+
+void *
+memchr(void const *buf, int c, size_t len)
+{
+ size_t i;
+ unsigned char const *b= buf;
+ unsigned char x= (c&0xff);
+
+ for (i= 0; i< len; i++) {
+ if (b[i]== x) {
+ return (void*)(b+i);
+ }
+ }
+
+ return NULL;
+}
+
diff --git a/src/bsp/lk/lib/libc/string/memcmp.c b/src/bsp/lk/lib/libc/string/memcmp.c
new file mode 100644
index 0000000..d6c0abd
--- /dev/null
+++ b/src/bsp/lk/lib/libc/string/memcmp.c
@@ -0,0 +1,40 @@
+/*
+** Copyright 2001, Travis Geiselbrecht. All rights reserved.
+** Distributed under the terms of the NewOS License.
+*/
+/*
+ * Copyright (c) 2008 Travis Geiselbrecht
+ *
+ * Permission is hereby granted, free of charge, to any person obtaining
+ * a copy of this software and associated documentation files
+ * (the "Software"), to deal in the Software without restriction,
+ * including without limitation the rights to use, copy, modify, merge,
+ * publish, distribute, sublicense, and/or sell copies of the Software,
+ * and to permit persons to whom the Software is furnished to do so,
+ * subject to the following conditions:
+ *
+ * The above copyright notice and this permission notice shall be
+ * included in all copies or substantial portions of the Software.
+ *
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
+ * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
+ * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
+ * IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
+ * CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
+ * TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
+ * SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
+ */
+#include <string.h>
+#include <sys/types.h>
+
+int
+memcmp(const void *cs, const void *ct, size_t count)
+{
+ const unsigned char *su1, *su2;
+ signed char res = 0;
+
+ for (su1 = cs, su2 = ct; 0 < count; ++su1, ++su2, count--)
+ if ((res = *su1 - *su2) != 0)
+ break;
+ return res;
+}
diff --git a/src/bsp/lk/lib/libc/string/memcpy.c b/src/bsp/lk/lib/libc/string/memcpy.c
new file mode 100644
index 0000000..f0e439c
--- /dev/null
+++ b/src/bsp/lk/lib/libc/string/memcpy.c
@@ -0,0 +1,69 @@
+/*
+** Copyright 2001, Travis Geiselbrecht. All rights reserved.
+** Distributed under the terms of the NewOS License.
+*/
+/*
+ * Copyright (c) 2008 Travis Geiselbrecht
+ *
+ * Permission is hereby granted, free of charge, to any person obtaining
+ * a copy of this software and associated documentation files
+ * (the "Software"), to deal in the Software without restriction,
+ * including without limitation the rights to use, copy, modify, merge,
+ * publish, distribute, sublicense, and/or sell copies of the Software,
+ * and to permit persons to whom the Software is furnished to do so,
+ * subject to the following conditions:
+ *
+ * The above copyright notice and this permission notice shall be
+ * included in all copies or substantial portions of the Software.
+ *
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
+ * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
+ * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
+ * IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
+ * CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
+ * TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
+ * SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
+ */
+#include <string.h>
+#include <sys/types.h>
+
+
+#if !_ASM_MEMCPY
+
+typedef long word;
+
+#define lsize sizeof(word)
+#define lmask (lsize - 1)
+
+void *memcpy(void *dest, const void *src, size_t count)
+{
+ char *d = (char *)dest;
+ const char *s = (const char *)src;
+ int len;
+
+ if (count == 0 || dest == src)
+ return dest;
+
+ if (((long)d | (long)s) & lmask) {
+ // src and/or dest do not align on word boundary
+ if ((((long)d ^ (long)s) & lmask) || (count < lsize))
+ len = count; // copy the rest of the buffer with the byte mover
+ else
+ len = lsize - ((long)d & lmask); // move the ptrs up to a word boundary
+
+ count -= len;
+ for (; len > 0; len--)
+ *d++ = *s++;
+ }
+ for (len = count / lsize; len > 0; len--) {
+ *(word *)d = *(word *)s;
+ d += lsize;
+ s += lsize;
+ }
+ for (len = count & lmask; len > 0; len--)
+ *d++ = *s++;
+
+ return dest;
+}
+
+#endif
diff --git a/src/bsp/lk/lib/libc/string/memmove.c b/src/bsp/lk/lib/libc/string/memmove.c
new file mode 100644
index 0000000..c7da350
--- /dev/null
+++ b/src/bsp/lk/lib/libc/string/memmove.c
@@ -0,0 +1,93 @@
+/*
+** Copyright 2001, Travis Geiselbrecht. All rights reserved.
+** Distributed under the terms of the NewOS License.
+*/
+/*
+ * Copyright (c) 2008 Travis Geiselbrecht
+ *
+ * Permission is hereby granted, free of charge, to any person obtaining
+ * a copy of this software and associated documentation files
+ * (the "Software"), to deal in the Software without restriction,
+ * including without limitation the rights to use, copy, modify, merge,
+ * publish, distribute, sublicense, and/or sell copies of the Software,
+ * and to permit persons to whom the Software is furnished to do so,
+ * subject to the following conditions:
+ *
+ * The above copyright notice and this permission notice shall be
+ * included in all copies or substantial portions of the Software.
+ *
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
+ * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
+ * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
+ * IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
+ * CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
+ * TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
+ * SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
+ */
+#include <string.h>
+#include <sys/types.h>
+
+#if !_ASM_MEMMOVE
+
+typedef long word;
+
+#define lsize sizeof(word)
+#define lmask (lsize - 1)
+
+void *
+memmove(void *dest, void const *src, size_t count)
+{
+ char *d = (char *)dest;
+ const char *s = (const char *)src;
+ int len;
+
+ if (count == 0 || dest == src)
+ return dest;
+
+ if ((long)d < (long)s) {
+ if (((long)d | (long)s) & lmask) {
+ // src and/or dest do not align on word boundary
+ if ((((long)d ^ (long)s) & lmask) || (count < lsize))
+ len = count; // copy the rest of the buffer with the byte mover
+ else
+ len = lsize - ((long)d & lmask); // move the ptrs up to a word boundary
+
+ count -= len;
+ for (; len > 0; len--)
+ *d++ = *s++;
+ }
+ for (len = count / lsize; len > 0; len--) {
+ *(word *)d = *(word *)s;
+ d += lsize;
+ s += lsize;
+ }
+ for (len = count & lmask; len > 0; len--)
+ *d++ = *s++;
+ } else {
+ d += count;
+ s += count;
+ if (((long)d | (long)s) & lmask) {
+ // src and/or dest do not align on word boundary
+ if ((((long)d ^ (long)s) & lmask) || (count <= lsize))
+ len = count;
+ else
+ len = ((long)d & lmask);
+
+ count -= len;
+ for (; len > 0; len--)
+ *--d = *--s;
+ }
+ for (len = count / lsize; len > 0; len--) {
+ d -= lsize;
+ s -= lsize;
+ *(word *)d = *(word *)s;
+ }
+ for (len = count & lmask; len > 0; len--)
+ *--d = *--s;
+ }
+
+ return dest;
+}
+
+#endif
+
diff --git a/src/bsp/lk/lib/libc/string/memscan.c b/src/bsp/lk/lib/libc/string/memscan.c
new file mode 100644
index 0000000..babe3d3
--- /dev/null
+++ b/src/bsp/lk/lib/libc/string/memscan.c
@@ -0,0 +1,41 @@
+/*
+** Copyright 2001, Travis Geiselbrecht. All rights reserved.
+** Distributed under the terms of the NewOS License.
+*/
+/*
+ * Copyright (c) 2008 Travis Geiselbrecht
+ *
+ * Permission is hereby granted, free of charge, to any person obtaining
+ * a copy of this software and associated documentation files
+ * (the "Software"), to deal in the Software without restriction,
+ * including without limitation the rights to use, copy, modify, merge,
+ * publish, distribute, sublicense, and/or sell copies of the Software,
+ * and to permit persons to whom the Software is furnished to do so,
+ * subject to the following conditions:
+ *
+ * The above copyright notice and this permission notice shall be
+ * included in all copies or substantial portions of the Software.
+ *
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
+ * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
+ * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
+ * IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
+ * CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
+ * TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
+ * SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
+ */
+#include <libc/string.h>
+#include <libc/ctype.h>
+
+void *memscan(void *addr, int c, size_t size)
+{
+ unsigned char *p = (unsigned char *)addr;
+
+ while (size) {
+ if (*p == c)
+ return (void *)p;
+ p++;
+ size--;
+ }
+ return (void *)p;
+}
diff --git a/src/bsp/lk/lib/libc/string/memset.c b/src/bsp/lk/lib/libc/string/memset.c
new file mode 100644
index 0000000..9a19e16
--- /dev/null
+++ b/src/bsp/lk/lib/libc/string/memset.c
@@ -0,0 +1,63 @@
+/*
+** Copyright 2005, Michael Noisternig. All rights reserved.
+** Copyright 2001, Travis Geiselbrecht. All rights reserved.
+** Distributed under the terms of the NewOS License.
+*/
+/*
+ * Copyright (c) 2008 Travis Geiselbrecht
+ *
+ * Permission is hereby granted, free of charge, to any person obtaining
+ * a copy of this software and associated documentation files
+ * (the "Software"), to deal in the Software without restriction,
+ * including without limitation the rights to use, copy, modify, merge,
+ * publish, distribute, sublicense, and/or sell copies of the Software,
+ * and to permit persons to whom the Software is furnished to do so,
+ * subject to the following conditions:
+ *
+ * The above copyright notice and this permission notice shall be
+ * included in all copies or substantial portions of the Software.
+ *
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
+ * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
+ * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
+ * IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
+ * CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
+ * TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
+ * SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
+ */
+#include <string.h>
+#include <sys/types.h>
+
+void *
+memset(void *s, int c, size_t count)
+{
+ char *xs = (char *) s;
+ size_t len = (-(size_t)s) & (sizeof(size_t)-1);
+ size_t cc = c & 0xff;
+
+ if ( count > len ) {
+ count -= len;
+ cc |= cc << 8;
+ cc |= cc << 16;
+ if (sizeof(size_t) == 8)
+ cc |= (uint64_t)cc << 32; // should be optimized out on 32 bit machines
+
+ // write to non-aligned memory byte-wise
+ for ( ; len > 0; len-- )
+ *xs++ = c;
+
+ // write to aligned memory dword-wise
+ for ( len = count/sizeof(size_t); len > 0; len-- ) {
+ *((size_t *)xs) = (size_t)cc;
+ xs += sizeof(size_t);
+ }
+
+ count &= sizeof(size_t)-1;
+ }
+
+ // write remaining bytes
+ for ( ; count > 0; count-- )
+ *xs++ = c;
+
+ return s;
+}
diff --git a/src/bsp/lk/lib/libc/string/rules.mk b/src/bsp/lk/lib/libc/string/rules.mk
new file mode 100644
index 0000000..4e28e3e
--- /dev/null
+++ b/src/bsp/lk/lib/libc/string/rules.mk
@@ -0,0 +1,42 @@
+LOCAL_DIR := $(GET_LOCAL_DIR)
+
+C_STRING_OPS := \
+ bcopy \
+ bzero \
+ memchr \
+ memcmp \
+ memcpy \
+ memmove \
+ memset \
+ strcat \
+ strchr \
+ strcmp \
+ strcoll \
+ strcpy \
+ strdup \
+ strerror \
+ strlcat \
+ strlcpy \
+ strlen \
+ strncat \
+ strncpy \
+ strncmp \
+ strnicmp \
+ strnlen \
+ strpbrk \
+ strrchr \
+ strspn \
+ strstr \
+ strtok \
+ strxfrm
+
+LIBC_STRING_C_DIR := $(LOCAL_DIR)
+
+# include the arch specific string routines
+#
+# the makefile may filter out implemented versions from the C_STRING_OPS variable
+include $(LOCAL_DIR)/arch/$(ARCH)/rules.mk
+
+MODULE_SRCS += \
+ $(addprefix $(LIBC_STRING_C_DIR)/,$(addsuffix .c,$(C_STRING_OPS)))
+
diff --git a/src/bsp/lk/lib/libc/string/strcat.c b/src/bsp/lk/lib/libc/string/strcat.c
new file mode 100644
index 0000000..c665519
--- /dev/null
+++ b/src/bsp/lk/lib/libc/string/strcat.c
@@ -0,0 +1,42 @@
+/*
+** Copyright 2001, Travis Geiselbrecht. All rights reserved.
+** Distributed under the terms of the NewOS License.
+*/
+/*
+ * Copyright (c) 2008 Travis Geiselbrecht
+ *
+ * Permission is hereby granted, free of charge, to any person obtaining
+ * a copy of this software and associated documentation files
+ * (the "Software"), to deal in the Software without restriction,
+ * including without limitation the rights to use, copy, modify, merge,
+ * publish, distribute, sublicense, and/or sell copies of the Software,
+ * and to permit persons to whom the Software is furnished to do so,
+ * subject to the following conditions:
+ *
+ * The above copyright notice and this permission notice shall be
+ * included in all copies or substantial portions of the Software.
+ *
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
+ * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
+ * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
+ * IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
+ * CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
+ * TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
+ * SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
+ */
+#include <string.h>
+#include <sys/types.h>
+
+char *
+strcat(char *dest, char const*src)
+{
+ char *tmp = dest;
+
+ while (*dest)
+ dest++;
+ while ((*dest++ = *src++) != '\0')
+ ;
+
+ return tmp;
+}
+
diff --git a/src/bsp/lk/lib/libc/string/strchr.c b/src/bsp/lk/lib/libc/string/strchr.c
new file mode 100644
index 0000000..f57e5d1
--- /dev/null
+++ b/src/bsp/lk/lib/libc/string/strchr.c
@@ -0,0 +1,37 @@
+/*
+** Copyright 2001, Travis Geiselbrecht. All rights reserved.
+** Distributed under the terms of the NewOS License.
+*/
+/*
+ * Copyright (c) 2008 Travis Geiselbrecht
+ *
+ * Permission is hereby granted, free of charge, to any person obtaining
+ * a copy of this software and associated documentation files
+ * (the "Software"), to deal in the Software without restriction,
+ * including without limitation the rights to use, copy, modify, merge,
+ * publish, distribute, sublicense, and/or sell copies of the Software,
+ * and to permit persons to whom the Software is furnished to do so,
+ * subject to the following conditions:
+ *
+ * The above copyright notice and this permission notice shall be
+ * included in all copies or substantial portions of the Software.
+ *
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
+ * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
+ * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
+ * IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
+ * CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
+ * TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
+ * SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
+ */
+#include <string.h>
+#include <sys/types.h>
+
+char *
+strchr(const char *s, int c)
+{
+ for (; *s != (char) c; ++s)
+ if (*s == '\0')
+ return NULL;
+ return (char *) s;
+}
diff --git a/src/bsp/lk/lib/libc/string/strcmp.c b/src/bsp/lk/lib/libc/string/strcmp.c
new file mode 100644
index 0000000..a89d57c
--- /dev/null
+++ b/src/bsp/lk/lib/libc/string/strcmp.c
@@ -0,0 +1,41 @@
+/*
+** Copyright 2001, Travis Geiselbrecht. All rights reserved.
+** Distributed under the terms of the NewOS License.
+*/
+/*
+ * Copyright (c) 2008 Travis Geiselbrecht
+ *
+ * Permission is hereby granted, free of charge, to any person obtaining
+ * a copy of this software and associated documentation files
+ * (the "Software"), to deal in the Software without restriction,
+ * including without limitation the rights to use, copy, modify, merge,
+ * publish, distribute, sublicense, and/or sell copies of the Software,
+ * and to permit persons to whom the Software is furnished to do so,
+ * subject to the following conditions:
+ *
+ * The above copyright notice and this permission notice shall be
+ * included in all copies or substantial portions of the Software.
+ *
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
+ * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
+ * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
+ * IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
+ * CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
+ * TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
+ * SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
+ */
+#include <string.h>
+#include <sys/types.h>
+
+int
+strcmp(char const *cs, char const *ct)
+{
+ signed char __res;
+
+ while (1) {
+ if ((__res = *cs - *ct++) != 0 || !*cs++)
+ break;
+ }
+
+ return __res;
+}
diff --git a/src/bsp/lk/lib/libc/string/strcoll.c b/src/bsp/lk/lib/libc/string/strcoll.c
new file mode 100644
index 0000000..549cb89
--- /dev/null
+++ b/src/bsp/lk/lib/libc/string/strcoll.c
@@ -0,0 +1,34 @@
+/*
+** Copyright 2004, Travis Geiselbrecht. All rights reserved.
+** Distributed under the terms of the NewOS License.
+*/
+/*
+ * Copyright (c) 2008 Travis Geiselbrecht
+ *
+ * Permission is hereby granted, free of charge, to any person obtaining
+ * a copy of this software and associated documentation files
+ * (the "Software"), to deal in the Software without restriction,
+ * including without limitation the rights to use, copy, modify, merge,
+ * publish, distribute, sublicense, and/or sell copies of the Software,
+ * and to permit persons to whom the Software is furnished to do so,
+ * subject to the following conditions:
+ *
+ * The above copyright notice and this permission notice shall be
+ * included in all copies or substantial portions of the Software.
+ *
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
+ * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
+ * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
+ * IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
+ * CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
+ * TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
+ * SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
+ */
+#include <string.h>
+
+int
+strcoll(const char *s1, const char *s2)
+{
+ return strcmp(s1, s2);
+}
+
diff --git a/src/bsp/lk/lib/libc/string/strcpy.c b/src/bsp/lk/lib/libc/string/strcpy.c
new file mode 100644
index 0000000..f8f43e1
--- /dev/null
+++ b/src/bsp/lk/lib/libc/string/strcpy.c
@@ -0,0 +1,39 @@
+/*
+** Copyright 2001, Travis Geiselbrecht. All rights reserved.
+** Distributed under the terms of the NewOS License.
+*/
+/*
+ * Copyright (c) 2008 Travis Geiselbrecht
+ *
+ * Permission is hereby granted, free of charge, to any person obtaining
+ * a copy of this software and associated documentation files
+ * (the "Software"), to deal in the Software without restriction,
+ * including without limitation the rights to use, copy, modify, merge,
+ * publish, distribute, sublicense, and/or sell copies of the Software,
+ * and to permit persons to whom the Software is furnished to do so,
+ * subject to the following conditions:
+ *
+ * The above copyright notice and this permission notice shall be
+ * included in all copies or substantial portions of the Software.
+ *
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
+ * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
+ * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
+ * IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
+ * CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
+ * TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
+ * SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
+ */
+#include <string.h>
+#include <sys/types.h>
+
+char *
+strcpy(char *dest, char const *src)
+{
+ char *tmp = dest;
+
+ while ((*dest++ = *src++) != '\0')
+ ;
+ return tmp;
+}
+
diff --git a/src/bsp/lk/lib/libc/string/strdup.c b/src/bsp/lk/lib/libc/string/strdup.c
new file mode 100644
index 0000000..ee292c4
--- /dev/null
+++ b/src/bsp/lk/lib/libc/string/strdup.c
@@ -0,0 +1,43 @@
+/*
+** Copyright 2004, Travis Geiselbrecht. All rights reserved.
+** Distributed under the terms of the NewOS License.
+*/
+/*
+ * Copyright (c) 2008 Travis Geiselbrecht
+ *
+ * Permission is hereby granted, free of charge, to any person obtaining
+ * a copy of this software and associated documentation files
+ * (the "Software"), to deal in the Software without restriction,
+ * including without limitation the rights to use, copy, modify, merge,
+ * publish, distribute, sublicense, and/or sell copies of the Software,
+ * and to permit persons to whom the Software is furnished to do so,
+ * subject to the following conditions:
+ *
+ * The above copyright notice and this permission notice shall be
+ * included in all copies or substantial portions of the Software.
+ *
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
+ * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
+ * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
+ * IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
+ * CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
+ * TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
+ * SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
+ */
+#include <stdlib.h>
+#include <string.h>
+
+char *
+strdup(const char *str)
+{
+ size_t len;
+ char *copy;
+
+ len = strlen(str) + 1;
+ copy = malloc(len);
+ if (copy == NULL)
+ return NULL;
+ memcpy(copy, str, len);
+ return copy;
+}
+
diff --git a/src/bsp/lk/lib/libc/string/strerror.c b/src/bsp/lk/lib/libc/string/strerror.c
new file mode 100644
index 0000000..49941ac
--- /dev/null
+++ b/src/bsp/lk/lib/libc/string/strerror.c
@@ -0,0 +1,36 @@
+/*
+ * Copyright (c) 2008 Travis Geiselbrecht
+ *
+ * Permission is hereby granted, free of charge, to any person obtaining
+ * a copy of this software and associated documentation files
+ * (the "Software"), to deal in the Software without restriction,
+ * including without limitation the rights to use, copy, modify, merge,
+ * publish, distribute, sublicense, and/or sell copies of the Software,
+ * and to permit persons to whom the Software is furnished to do so,
+ * subject to the following conditions:
+ *
+ * The above copyright notice and this permission notice shall be
+ * included in all copies or substantial portions of the Software.
+ *
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
+ * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
+ * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
+ * IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
+ * CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
+ * TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
+ * SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
+ */
+
+#include <string.h>
+#include <sys/types.h>
+
+char *
+strerror(int errnum)
+{
+ if (errnum < 0) {
+ return (char *)"General Error";
+ } else {
+ return (char *)"No Error";
+ }
+}
+
diff --git a/src/bsp/lk/lib/libc/string/strlcat.c b/src/bsp/lk/lib/libc/string/strlcat.c
new file mode 100644
index 0000000..4760e91
--- /dev/null
+++ b/src/bsp/lk/lib/libc/string/strlcat.c
@@ -0,0 +1,50 @@
+/*
+** Copyright 2002, Manuel J. Petit. All rights reserved.
+** Distributed under the terms of the NewOS License.
+*/
+/*
+ * Copyright (c) 2008 Travis Geiselbrecht
+ *
+ * Permission is hereby granted, free of charge, to any person obtaining
+ * a copy of this software and associated documentation files
+ * (the "Software"), to deal in the Software without restriction,
+ * including without limitation the rights to use, copy, modify, merge,
+ * publish, distribute, sublicense, and/or sell copies of the Software,
+ * and to permit persons to whom the Software is furnished to do so,
+ * subject to the following conditions:
+ *
+ * The above copyright notice and this permission notice shall be
+ * included in all copies or substantial portions of the Software.
+ *
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
+ * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
+ * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
+ * IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
+ * CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
+ * TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
+ * SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
+ */
+
+#include <string.h>
+#include <sys/types.h>
+
+size_t
+strlcat(char *dst, char const *src, size_t s)
+{
+ size_t i;
+ size_t j= strnlen(dst, s);
+
+ if (!s) {
+ return j+strlen(src);
+ }
+
+ dst+= j;
+
+ for (i= 0; ((i< s-1) && src[i]); i++) {
+ dst[i]= src[i];
+ }
+
+ dst[i]= 0;
+
+ return j + i + strlen(src+i);
+}
diff --git a/src/bsp/lk/lib/libc/string/strlcpy.c b/src/bsp/lk/lib/libc/string/strlcpy.c
new file mode 100644
index 0000000..7c76be6
--- /dev/null
+++ b/src/bsp/lk/lib/libc/string/strlcpy.c
@@ -0,0 +1,47 @@
+/*
+** Copyright 2002, Manuel J. Petit. All rights reserved.
+** Distributed under the terms of the NewOS License.
+*/
+/*
+ * Copyright (c) 2008 Travis Geiselbrecht
+ *
+ * Permission is hereby granted, free of charge, to any person obtaining
+ * a copy of this software and associated documentation files
+ * (the "Software"), to deal in the Software without restriction,
+ * including without limitation the rights to use, copy, modify, merge,
+ * publish, distribute, sublicense, and/or sell copies of the Software,
+ * and to permit persons to whom the Software is furnished to do so,
+ * subject to the following conditions:
+ *
+ * The above copyright notice and this permission notice shall be
+ * included in all copies or substantial portions of the Software.
+ *
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
+ * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
+ * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
+ * IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
+ * CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
+ * TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
+ * SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
+ */
+
+#include <string.h>
+#include <sys/types.h>
+
+size_t
+strlcpy(char *dst, char const *src, size_t s)
+{
+ size_t i= 0;
+
+ if (!s) {
+ return strlen(src);
+ }
+
+ for (i= 0; ((i< s-1) && src[i]); i++) {
+ dst[i]= src[i];
+ }
+
+ dst[i]= 0;
+
+ return i + strlen(src+i);
+}
diff --git a/src/bsp/lk/lib/libc/string/strlen.c b/src/bsp/lk/lib/libc/string/strlen.c
new file mode 100644
index 0000000..1659f78
--- /dev/null
+++ b/src/bsp/lk/lib/libc/string/strlen.c
@@ -0,0 +1,41 @@
+/*
+** Copyright 2001, Travis Geiselbrecht. All rights reserved.
+** Distributed under the terms of the NewOS License.
+*/
+/*
+ * Copyright (c) 2008 Travis Geiselbrecht
+ *
+ * Permission is hereby granted, free of charge, to any person obtaining
+ * a copy of this software and associated documentation files
+ * (the "Software"), to deal in the Software without restriction,
+ * including without limitation the rights to use, copy, modify, merge,
+ * publish, distribute, sublicense, and/or sell copies of the Software,
+ * and to permit persons to whom the Software is furnished to do so,
+ * subject to the following conditions:
+ *
+ * The above copyright notice and this permission notice shall be
+ * included in all copies or substantial portions of the Software.
+ *
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
+ * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
+ * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
+ * IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
+ * CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
+ * TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
+ * SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
+ */
+#include <string.h>
+#include <sys/types.h>
+
+size_t
+strlen(char const *s)
+{
+ size_t i;
+
+ i= 0;
+ while (s[i]) {
+ i+= 1;
+ }
+
+ return i;
+}
diff --git a/src/bsp/lk/lib/libc/string/strncat.c b/src/bsp/lk/lib/libc/string/strncat.c
new file mode 100644
index 0000000..078c33d
--- /dev/null
+++ b/src/bsp/lk/lib/libc/string/strncat.c
@@ -0,0 +1,48 @@
+/*
+** Copyright 2001, Travis Geiselbrecht. All rights reserved.
+** Distributed under the terms of the NewOS License.
+*/
+/*
+ * Copyright (c) 2008 Travis Geiselbrecht
+ *
+ * Permission is hereby granted, free of charge, to any person obtaining
+ * a copy of this software and associated documentation files
+ * (the "Software"), to deal in the Software without restriction,
+ * including without limitation the rights to use, copy, modify, merge,
+ * publish, distribute, sublicense, and/or sell copies of the Software,
+ * and to permit persons to whom the Software is furnished to do so,
+ * subject to the following conditions:
+ *
+ * The above copyright notice and this permission notice shall be
+ * included in all copies or substantial portions of the Software.
+ *
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
+ * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
+ * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
+ * IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
+ * CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
+ * TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
+ * SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
+ */
+#include <string.h>
+#include <sys/types.h>
+
+char *
+strncat(char *dest, char const *src, size_t count)
+{
+ char *tmp = dest;
+
+ if (count > 0) {
+ while (*dest)
+ dest++;
+ while ((*dest++ = *src++)) {
+ if (--count == 0) {
+ *dest = '\0';
+ break;
+ }
+ }
+ }
+
+ return tmp;
+}
+
diff --git a/src/bsp/lk/lib/libc/string/strncmp.c b/src/bsp/lk/lib/libc/string/strncmp.c
new file mode 100644
index 0000000..4185482
--- /dev/null
+++ b/src/bsp/lk/lib/libc/string/strncmp.c
@@ -0,0 +1,42 @@
+/*
+** Copyright 2001, Travis Geiselbrecht. All rights reserved.
+** Distributed under the terms of the NewOS License.
+*/
+/*
+ * Copyright (c) 2008 Travis Geiselbrecht
+ *
+ * Permission is hereby granted, free of charge, to any person obtaining
+ * a copy of this software and associated documentation files
+ * (the "Software"), to deal in the Software without restriction,
+ * including without limitation the rights to use, copy, modify, merge,
+ * publish, distribute, sublicense, and/or sell copies of the Software,
+ * and to permit persons to whom the Software is furnished to do so,
+ * subject to the following conditions:
+ *
+ * The above copyright notice and this permission notice shall be
+ * included in all copies or substantial portions of the Software.
+ *
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
+ * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
+ * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
+ * IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
+ * CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
+ * TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
+ * SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
+ */
+#include <string.h>
+#include <sys/types.h>
+
+int
+strncmp(char const *cs, char const *ct, size_t count)
+{
+ signed char __res = 0;
+
+ while (count > 0) {
+ if ((__res = *cs - *ct++) != 0 || !*cs++)
+ break;
+ count--;
+ }
+
+ return __res;
+}
diff --git a/src/bsp/lk/lib/libc/string/strncpy.c b/src/bsp/lk/lib/libc/string/strncpy.c
new file mode 100644
index 0000000..70ea19b
--- /dev/null
+++ b/src/bsp/lk/lib/libc/string/strncpy.c
@@ -0,0 +1,40 @@
+/*
+** Copyright 2001, Travis Geiselbrecht. All rights reserved.
+** Distributed under the terms of the NewOS License.
+*/
+/*
+ * Copyright (c) 2008 Travis Geiselbrecht
+ *
+ * Permission is hereby granted, free of charge, to any person obtaining
+ * a copy of this software and associated documentation files
+ * (the "Software"), to deal in the Software without restriction,
+ * including without limitation the rights to use, copy, modify, merge,
+ * publish, distribute, sublicense, and/or sell copies of the Software,
+ * and to permit persons to whom the Software is furnished to do so,
+ * subject to the following conditions:
+ *
+ * The above copyright notice and this permission notice shall be
+ * included in all copies or substantial portions of the Software.
+ *
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
+ * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
+ * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
+ * IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
+ * CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
+ * TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
+ * SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
+ */
+#include <string.h>
+#include <sys/types.h>
+
+char *
+strncpy(char *dest, char const *src, size_t count)
+{
+ char *tmp = dest;
+
+ while (count-- && (*dest++ = *src++) != '\0')
+ ;
+
+ return tmp;
+}
+
diff --git a/src/bsp/lk/lib/libc/string/strnicmp.c b/src/bsp/lk/lib/libc/string/strnicmp.c
new file mode 100644
index 0000000..67c7b9f
--- /dev/null
+++ b/src/bsp/lk/lib/libc/string/strnicmp.c
@@ -0,0 +1,57 @@
+/*
+** Copyright 2001, Travis Geiselbrecht. All rights reserved.
+** Distributed under the terms of the NewOS License.
+*/
+/*
+ * Copyright (c) 2008 Travis Geiselbrecht
+ *
+ * Permission is hereby granted, free of charge, to any person obtaining
+ * a copy of this software and associated documentation files
+ * (the "Software"), to deal in the Software without restriction,
+ * including without limitation the rights to use, copy, modify, merge,
+ * publish, distribute, sublicense, and/or sell copies of the Software,
+ * and to permit persons to whom the Software is furnished to do so,
+ * subject to the following conditions:
+ *
+ * The above copyright notice and this permission notice shall be
+ * included in all copies or substantial portions of the Software.
+ *
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
+ * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
+ * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
+ * IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
+ * CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
+ * TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
+ * SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
+ */
+#include <string.h>
+#include <ctype.h>
+#include <sys/types.h>
+
+int
+strnicmp(char const *s1, char const *s2, size_t len)
+{
+ unsigned char c1 = '\0';
+ unsigned char c2 = '\0';
+
+ if (len > 0) {
+ do {
+ c1 = *s1;
+ c2 = *s2;
+ s1++;
+ s2++;
+ if (!c1)
+ break;
+ if (!c2)
+ break;
+ if (c1 == c2)
+ continue;
+ c1 = tolower(c1);
+ c2 = tolower(c2);
+ if (c1 != c2)
+ break;
+ } while (--len);
+ }
+ return (int)c1 - (int)c2;
+}
+#pragma weak strncasecmp=strnicmp
diff --git a/src/bsp/lk/lib/libc/string/strnlen.c b/src/bsp/lk/lib/libc/string/strnlen.c
new file mode 100644
index 0000000..07e56e8
--- /dev/null
+++ b/src/bsp/lk/lib/libc/string/strnlen.c
@@ -0,0 +1,38 @@
+/*
+** Copyright 2001, Travis Geiselbrecht. All rights reserved.
+** Distributed under the terms of the NewOS License.
+*/
+/*
+ * Copyright (c) 2008 Travis Geiselbrecht
+ *
+ * Permission is hereby granted, free of charge, to any person obtaining
+ * a copy of this software and associated documentation files
+ * (the "Software"), to deal in the Software without restriction,
+ * including without limitation the rights to use, copy, modify, merge,
+ * publish, distribute, sublicense, and/or sell copies of the Software,
+ * and to permit persons to whom the Software is furnished to do so,
+ * subject to the following conditions:
+ *
+ * The above copyright notice and this permission notice shall be
+ * included in all copies or substantial portions of the Software.
+ *
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
+ * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
+ * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
+ * IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
+ * CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
+ * TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
+ * SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
+ */
+#include <string.h>
+#include <sys/types.h>
+
+size_t
+strnlen(char const *s, size_t count)
+{
+ const char *sc;
+
+ for (sc = s; count-- && *sc != '\0'; ++sc)
+ ;
+ return sc - s;
+}
diff --git a/src/bsp/lk/lib/libc/string/strpbrk.c b/src/bsp/lk/lib/libc/string/strpbrk.c
new file mode 100644
index 0000000..0812037
--- /dev/null
+++ b/src/bsp/lk/lib/libc/string/strpbrk.c
@@ -0,0 +1,44 @@
+/*
+** Copyright 2001, Travis Geiselbrecht. All rights reserved.
+** Distributed under the terms of the NewOS License.
+*/
+/*
+ * Copyright (c) 2008 Travis Geiselbrecht
+ *
+ * Permission is hereby granted, free of charge, to any person obtaining
+ * a copy of this software and associated documentation files
+ * (the "Software"), to deal in the Software without restriction,
+ * including without limitation the rights to use, copy, modify, merge,
+ * publish, distribute, sublicense, and/or sell copies of the Software,
+ * and to permit persons to whom the Software is furnished to do so,
+ * subject to the following conditions:
+ *
+ * The above copyright notice and this permission notice shall be
+ * included in all copies or substantial portions of the Software.
+ *
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
+ * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
+ * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
+ * IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
+ * CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
+ * TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
+ * SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
+ */
+#include <string.h>
+#include <sys/types.h>
+
+char *
+strpbrk(char const *cs, char const *ct)
+{
+ const char *sc1;
+ const char *sc2;
+
+ for (sc1 = cs; *sc1 != '\0'; ++sc1) {
+ for (sc2 = ct; *sc2 != '\0'; ++sc2) {
+ if (*sc1 == *sc2)
+ return (char *)sc1;
+ }
+ }
+
+ return NULL;
+}
diff --git a/src/bsp/lk/lib/libc/string/strrchr.c b/src/bsp/lk/lib/libc/string/strrchr.c
new file mode 100644
index 0000000..f0dc21c
--- /dev/null
+++ b/src/bsp/lk/lib/libc/string/strrchr.c
@@ -0,0 +1,45 @@
+/*
+** Copyright 2001, Manuel J. Petit. All rights reserved.
+** Distributed under the terms of the NewOS License.
+*/
+/*
+ * Copyright (c) 2008 Travis Geiselbrecht
+ *
+ * Permission is hereby granted, free of charge, to any person obtaining
+ * a copy of this software and associated documentation files
+ * (the "Software"), to deal in the Software without restriction,
+ * including without limitation the rights to use, copy, modify, merge,
+ * publish, distribute, sublicense, and/or sell copies of the Software,
+ * and to permit persons to whom the Software is furnished to do so,
+ * subject to the following conditions:
+ *
+ * The above copyright notice and this permission notice shall be
+ * included in all copies or substantial portions of the Software.
+ *
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
+ * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
+ * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
+ * IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
+ * CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
+ * TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
+ * SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
+ */
+#include <string.h>
+#include <sys/types.h>
+
+char *
+strrchr(char const *s, int c)
+{
+ char const *last= c?0:s;
+
+
+ while (*s) {
+ if (*s== c) {
+ last= s;
+ }
+
+ s+= 1;
+ }
+
+ return (char *)last;
+}
diff --git a/src/bsp/lk/lib/libc/string/strspn.c b/src/bsp/lk/lib/libc/string/strspn.c
new file mode 100644
index 0000000..bfeb7ac
--- /dev/null
+++ b/src/bsp/lk/lib/libc/string/strspn.c
@@ -0,0 +1,48 @@
+/*
+** Copyright 2001, Travis Geiselbrecht. All rights reserved.
+** Distributed under the terms of the NewOS License.
+*/
+/*
+ * Copyright (c) 2008 Travis Geiselbrecht
+ *
+ * Permission is hereby granted, free of charge, to any person obtaining
+ * a copy of this software and associated documentation files
+ * (the "Software"), to deal in the Software without restriction,
+ * including without limitation the rights to use, copy, modify, merge,
+ * publish, distribute, sublicense, and/or sell copies of the Software,
+ * and to permit persons to whom the Software is furnished to do so,
+ * subject to the following conditions:
+ *
+ * The above copyright notice and this permission notice shall be
+ * included in all copies or substantial portions of the Software.
+ *
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
+ * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
+ * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
+ * IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
+ * CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
+ * TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
+ * SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
+ */
+#include <string.h>
+#include <sys/types.h>
+
+size_t
+strspn(char const *s, char const *accept)
+{
+ const char *p;
+ const char *a;
+ size_t count = 0;
+
+ for (p = s; *p != '\0'; ++p) {
+ for (a = accept; *a != '\0'; ++a) {
+ if (*p == *a)
+ break;
+ }
+ if (*a == '\0')
+ return count;
+ ++count;
+ }
+
+ return count;
+}
diff --git a/src/bsp/lk/lib/libc/string/strstr.c b/src/bsp/lk/lib/libc/string/strstr.c
new file mode 100644
index 0000000..15dc6af
--- /dev/null
+++ b/src/bsp/lk/lib/libc/string/strstr.c
@@ -0,0 +1,46 @@
+/*
+** Copyright 2001, Travis Geiselbrecht. All rights reserved.
+** Distributed under the terms of the NewOS License.
+*/
+/*
+ * Copyright (c) 2008 Travis Geiselbrecht
+ *
+ * Permission is hereby granted, free of charge, to any person obtaining
+ * a copy of this software and associated documentation files
+ * (the "Software"), to deal in the Software without restriction,
+ * including without limitation the rights to use, copy, modify, merge,
+ * publish, distribute, sublicense, and/or sell copies of the Software,
+ * and to permit persons to whom the Software is furnished to do so,
+ * subject to the following conditions:
+ *
+ * The above copyright notice and this permission notice shall be
+ * included in all copies or substantial portions of the Software.
+ *
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
+ * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
+ * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
+ * IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
+ * CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
+ * TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
+ * SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
+ */
+#include <string.h>
+#include <sys/types.h>
+
+char *
+strstr(char const *s1, char const *s2)
+{
+ int l1, l2;
+
+ l2 = strlen(s2);
+ if (!l2)
+ return (char *)s1;
+ l1 = strlen(s1);
+ while (l1 >= l2) {
+ l1--;
+ if (!memcmp(s1,s2,l2))
+ return (char *)s1;
+ s1++;
+ }
+ return NULL;
+}
diff --git a/src/bsp/lk/lib/libc/string/strtok.c b/src/bsp/lk/lib/libc/string/strtok.c
new file mode 100644
index 0000000..4839614
--- /dev/null
+++ b/src/bsp/lk/lib/libc/string/strtok.c
@@ -0,0 +1,51 @@
+/*
+** Copyright 2001, Travis Geiselbrecht. All rights reserved.
+** Distributed under the terms of the NewOS License.
+*/
+/*
+ * Copyright (c) 2008 Travis Geiselbrecht
+ *
+ * Permission is hereby granted, free of charge, to any person obtaining
+ * a copy of this software and associated documentation files
+ * (the "Software"), to deal in the Software without restriction,
+ * including without limitation the rights to use, copy, modify, merge,
+ * publish, distribute, sublicense, and/or sell copies of the Software,
+ * and to permit persons to whom the Software is furnished to do so,
+ * subject to the following conditions:
+ *
+ * The above copyright notice and this permission notice shall be
+ * included in all copies or substantial portions of the Software.
+ *
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
+ * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
+ * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
+ * IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
+ * CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
+ * TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
+ * SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
+ */
+#include <string.h>
+#include <sys/types.h>
+
+static char *___strtok = NULL;
+
+char *
+strtok(char *s, char const *ct)
+{
+ char *sbegin, *send;
+
+ sbegin = s ? s : ___strtok;
+ if (!sbegin) {
+ return NULL;
+ }
+ sbegin += strspn(sbegin,ct);
+ if (*sbegin == '\0') {
+ ___strtok = NULL;
+ return ( NULL );
+ }
+ send = strpbrk( sbegin, ct);
+ if (send && *send != '\0')
+ *send++ = '\0';
+ ___strtok = send;
+ return (sbegin);
+}
diff --git a/src/bsp/lk/lib/libc/string/strxfrm.c b/src/bsp/lk/lib/libc/string/strxfrm.c
new file mode 100644
index 0000000..0eff870
--- /dev/null
+++ b/src/bsp/lk/lib/libc/string/strxfrm.c
@@ -0,0 +1,42 @@
+/*
+** Copyright 2004, Travis Geiselbrecht. All rights reserved.
+** Distributed under the terms of the NewOS License.
+*/
+/*
+ * Copyright (c) 2008 Travis Geiselbrecht
+ *
+ * Permission is hereby granted, free of charge, to any person obtaining
+ * a copy of this software and associated documentation files
+ * (the "Software"), to deal in the Software without restriction,
+ * including without limitation the rights to use, copy, modify, merge,
+ * publish, distribute, sublicense, and/or sell copies of the Software,
+ * and to permit persons to whom the Software is furnished to do so,
+ * subject to the following conditions:
+ *
+ * The above copyright notice and this permission notice shall be
+ * included in all copies or substantial portions of the Software.
+ *
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
+ * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
+ * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
+ * IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
+ * CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
+ * TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
+ * SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
+ */
+#include <string.h>
+#include <sys/types.h>
+
+size_t
+strxfrm(char *dest, const char *src, size_t n)
+{
+ size_t len = strlen(src);
+
+ if (n) {
+ size_t copy_len = len < n ? len : n - 1;
+ memcpy(dest, src, copy_len);
+ dest[copy_len] = 0;
+ }
+ return len;
+}
+
diff --git a/src/bsp/lk/lib/libc/strtol.c b/src/bsp/lk/lib/libc/strtol.c
new file mode 100644
index 0000000..d87bde0
--- /dev/null
+++ b/src/bsp/lk/lib/libc/strtol.c
@@ -0,0 +1,140 @@
+/* $OpenBSD: strtol.c,v 1.7 2005/08/08 08:05:37 espie Exp $ */
+/*-
+ * Copyright (c) 1990 The Regents of the University of California.
+ * All rights reserved.
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions
+ * are met:
+ * 1. Redistributions of source code must retain the above copyright
+ * notice, this list of conditions and the following disclaimer.
+ * 2. Redistributions in binary form must reproduce the above copyright
+ * notice, this list of conditions and the following disclaimer in the
+ * documentation and/or other materials provided with the distribution.
+ * 3. Neither the name of the University nor the names of its contributors
+ * may be used to endorse or promote products derived from this software
+ * without specific prior written permission.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
+ * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
+ * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
+ * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
+ * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
+ * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
+ * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
+ * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
+ * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
+ * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
+ * SUCH DAMAGE.
+ */
+
+#include <ctype.h>
+#include <errno.h>
+#include <limits.h>
+#include <stdlib.h>
+
+
+/*
+ * Convert a string to a long integer.
+ *
+ * Ignores `locale' stuff. Assumes that the upper and lower case
+ * alphabets and digits are each contiguous.
+ */
+long
+strtol(const char *nptr, char **endptr, int base)
+{
+ const char *s;
+ long acc, cutoff;
+ int c;
+ int neg, any, cutlim;
+
+ /*
+ * Skip white space and pick up leading +/- sign if any.
+ * If base is 0, allow 0x for hex and 0 for octal, else
+ * assume decimal; if base is already 16, allow 0x.
+ */
+ s = nptr;
+ do {
+ c = (unsigned char) *s++;
+ } while (isspace(c));
+ if (c == '-') {
+ neg = 1;
+ c = *s++;
+ } else {
+ neg = 0;
+ if (c == '+')
+ c = *s++;
+ }
+ if ((base == 0 || base == 16) &&
+ c == '0' && (*s == 'x' || *s == 'X')) {
+ c = s[1];
+ s += 2;
+ base = 16;
+ }
+ if (base == 0)
+ base = c == '0' ? 8 : 10;
+
+ /*
+ * Compute the cutoff value between legal numbers and illegal
+ * numbers. That is the largest legal value, divided by the
+ * base. An input number that is greater than this value, if
+ * followed by a legal input character, is too big. One that
+ * is equal to this value may be valid or not; the limit
+ * between valid and invalid numbers is then based on the last
+ * digit. For instance, if the range for longs is
+ * [-2147483648..2147483647] and the input base is 10,
+ * cutoff will be set to 214748364 and cutlim to either
+ * 7 (neg==0) or 8 (neg==1), meaning that if we have accumulated
+ * a value > 214748364, or equal but the next digit is > 7 (or 8),
+ * the number is too big, and we will return a range error.
+ *
+ * Set any if any `digits' consumed; make it negative to indicate
+ * overflow.
+ */
+ cutoff = neg ? LONG_MIN : LONG_MAX;
+ cutlim = cutoff % base;
+ cutoff /= base;
+ if (neg) {
+ if (cutlim > 0) {
+ cutlim -= base;
+ cutoff += 1;
+ }
+ cutlim = -cutlim;
+ }
+ for (acc = 0, any = 0;; c = (unsigned char) *s++) {
+ if (isdigit(c))
+ c -= '0';
+ else if (isalpha(c))
+ c -= isupper(c) ? 'A' - 10 : 'a' - 10;
+ else
+ break;
+ if (c >= base)
+ break;
+ if (any < 0)
+ continue;
+ if (neg) {
+ if (acc < cutoff || (acc == cutoff && c > cutlim)) {
+ any = -1;
+ acc = LONG_MIN;
+ errno = ERANGE;
+ } else {
+ any = 1;
+ acc *= base;
+ acc -= c;
+ }
+ } else {
+ if (acc > cutoff || (acc == cutoff && c > cutlim)) {
+ any = -1;
+ acc = LONG_MAX;
+ errno = ERANGE;
+ } else {
+ any = 1;
+ acc *= base;
+ acc += c;
+ }
+ }
+ }
+ if (endptr != 0)
+ *endptr = (char *) (any ? s - 1 : nptr);
+ return (acc);
+}
diff --git a/src/bsp/lk/lib/libc/strtoll.c b/src/bsp/lk/lib/libc/strtoll.c
new file mode 100644
index 0000000..e63c66a
--- /dev/null
+++ b/src/bsp/lk/lib/libc/strtoll.c
@@ -0,0 +1,142 @@
+/* $OpenBSD: strtoll.c,v 1.6 2005/11/10 10:00:17 espie Exp $ */
+/*-
+ * Copyright (c) 1992 The Regents of the University of California.
+ * All rights reserved.
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions
+ * are met:
+ * 1. Redistributions of source code must retain the above copyright
+ * notice, this list of conditions and the following disclaimer.
+ * 2. Redistributions in binary form must reproduce the above copyright
+ * notice, this list of conditions and the following disclaimer in the
+ * documentation and/or other materials provided with the distribution.
+ * 3. Neither the name of the University nor the names of its contributors
+ * may be used to endorse or promote products derived from this software
+ * without specific prior written permission.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
+ * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
+ * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
+ * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
+ * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
+ * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
+ * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
+ * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
+ * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
+ * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
+ * SUCH DAMAGE.
+ */
+
+#include <sys/types.h>
+
+#include <ctype.h>
+#include <errno.h>
+#include <limits.h>
+#include <stdlib.h>
+
+/*
+ * Convert a string to a long long.
+ *
+ * Ignores `locale' stuff. Assumes that the upper and lower case
+ * alphabets and digits are each contiguous.
+ */
+long long
+strtoll(const char *nptr, char **endptr, int base)
+{
+ const char *s;
+ long long acc, cutoff;
+ int c;
+ int neg, any, cutlim;
+
+ /*
+ * Skip white space and pick up leading +/- sign if any.
+ * If base is 0, allow 0x for hex and 0 for octal, else
+ * assume decimal; if base is already 16, allow 0x.
+ */
+ s = nptr;
+ do {
+ c = (unsigned char) *s++;
+ } while (isspace(c));
+ if (c == '-') {
+ neg = 1;
+ c = *s++;
+ } else {
+ neg = 0;
+ if (c == '+')
+ c = *s++;
+ }
+ if ((base == 0 || base == 16) &&
+ c == '0' && (*s == 'x' || *s == 'X')) {
+ c = s[1];
+ s += 2;
+ base = 16;
+ }
+ if (base == 0)
+ base = c == '0' ? 8 : 10;
+
+ /*
+ * Compute the cutoff value between legal numbers and illegal
+ * numbers. That is the largest legal value, divided by the
+ * base. An input number that is greater than this value, if
+ * followed by a legal input character, is too big. One that
+ * is equal to this value may be valid or not; the limit
+ * between valid and invalid numbers is then based on the last
+ * digit. For instance, if the range for long longs is
+ * [-9223372036854775808..9223372036854775807] and the input base
+ * is 10, cutoff will be set to 922337203685477580 and cutlim to
+ * either 7 (neg==0) or 8 (neg==1), meaning that if we have
+ * accumulated a value > 922337203685477580, or equal but the
+ * next digit is > 7 (or 8), the number is too big, and we will
+ * return a range error.
+ *
+ * Set any if any `digits' consumed; make it negative to indicate
+ * overflow.
+ */
+ cutoff = neg ? LLONG_MIN : LLONG_MAX;
+ cutlim = cutoff % base;
+ cutoff /= base;
+ if (neg) {
+ if (cutlim > 0) {
+ cutlim -= base;
+ cutoff += 1;
+ }
+ cutlim = -cutlim;
+ }
+ for (acc = 0, any = 0;; c = (unsigned char) *s++) {
+ if (isdigit(c))
+ c -= '0';
+ else if (isalpha(c))
+ c -= isupper(c) ? 'A' - 10 : 'a' - 10;
+ else
+ break;
+ if (c >= base)
+ break;
+ if (any < 0)
+ continue;
+ if (neg) {
+ if (acc < cutoff || (acc == cutoff && c > cutlim)) {
+ any = -1;
+ acc = LLONG_MIN;
+ errno = ERANGE;
+ } else {
+ any = 1;
+ acc *= base;
+ acc -= c;
+ }
+ } else {
+ if (acc > cutoff || (acc == cutoff && c > cutlim)) {
+ any = -1;
+ acc = LLONG_MAX;
+ errno = ERANGE;
+ } else {
+ any = 1;
+ acc *= base;
+ acc += c;
+ }
+ }
+ }
+ if (endptr != 0)
+ *endptr = (char *) (any ? s - 1 : nptr);
+ return (acc);
+}