You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@mynewt.apache.org by st...@apache.org on 2016/09/29 01:34:40 UTC

[33/49] incubator-mynewt-core git commit: directory re-org

http://git-wip-us.apache.org/repos/asf/incubator-mynewt-core/blob/6a7432f4/kernel/os/test/src/sem_test.c
----------------------------------------------------------------------
diff --git a/kernel/os/test/src/sem_test.c b/kernel/os/test/src/sem_test.c
new file mode 100644
index 0000000..6b40069
--- /dev/null
+++ b/kernel/os/test/src/sem_test.c
@@ -0,0 +1,401 @@
+/**
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License.  You may obtain a copy of the License at
+ * 
+ *  http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied.  See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+#include <stdio.h>
+#include <string.h>
+#include "testutil/testutil.h"
+#include "os/os.h"
+#include "os/os_cfg.h"
+#include "os/os_sem.h"
+#include "os_test_priv.h"
+
+#ifdef ARCH_sim
+#define SEM_TEST_STACK_SIZE     1024
+#else 
+#define SEM_TEST_STACK_SIZE     512
+#endif
+
+struct os_task task1;
+os_stack_t stack1[OS_STACK_ALIGN(SEM_TEST_STACK_SIZE)];
+
+struct os_task task2;
+os_stack_t stack2[OS_STACK_ALIGN(SEM_TEST_STACK_SIZE)];
+
+struct os_task task3;
+os_stack_t stack3[OS_STACK_ALIGN(SEM_TEST_STACK_SIZE)];
+
+struct os_task task4;
+os_stack_t stack4[OS_STACK_ALIGN(SEM_TEST_STACK_SIZE)];
+
+#define TASK1_PRIO (1) 
+#define TASK2_PRIO (2) 
+#define TASK3_PRIO (3) 
+#define TASK4_PRIO (4) 
+
+struct os_sem g_sem1;
+
+/* 
+ * TEST NUMBERS:
+ *  10: In this test we have the highest priority task getting the semaphore
+ *  then sleeping. Two lower priority tasks then wake up and attempt to get
+ *  the semaphore. They are blocked until the higher priority task releases
+ *  the semaphore, at which point the lower priority tasks should wake up in
+ *  order, get the semaphore, then release it and go back to sleep.
+ * 
+ */
+
+/**
+ * sem test disp sem
+ *  
+ * Display semaphore contents 
+ * 
+ * @param sem 
+ */
+static const char *
+sem_test_sem_to_s(const struct os_sem *sem)
+{
+    static char buf[128];
+
+    snprintf(buf, sizeof buf, "\tSemaphore: tokens=%u head=%p",
+             sem->sem_tokens, SLIST_FIRST(&sem->sem_head));
+
+    return buf;
+}
+
+static void 
+sem_test_sleep_task_handler(void *arg)
+{
+    struct os_task *t;
+
+    t = os_sched_get_current_task();
+    TEST_ASSERT(t->t_func == sem_test_sleep_task_handler);
+
+    os_time_delay(2 * OS_TICKS_PER_SEC);
+    os_test_restart();
+}
+
+static void
+sem_test_pend_release_loop(int delay, int timeout, int itvl)
+{
+    os_error_t err;
+
+    os_time_delay(delay);
+
+    while (1) {
+        err = os_sem_pend(&g_sem1, timeout);
+        TEST_ASSERT((err == OS_OK) || (err == OS_TIMEOUT));
+
+        err = os_sem_release(&g_sem1);
+        TEST_ASSERT(err == OS_OK);
+
+        os_time_delay(itvl);
+    }
+}
+
+/**
+ * sem test basic 
+ *  
+ * Basic semaphore tests
+ * 
+ * @return int 
+ */
+static void 
+sem_test_basic_handler(void *arg)
+{
+    struct os_task *t;
+    struct os_sem *sem;
+    os_error_t err;
+
+    sem = &g_sem1;
+    t = os_sched_get_current_task();
+
+    /* Test some error cases */
+    TEST_ASSERT(os_sem_init(NULL, 1)    == OS_INVALID_PARM);
+    TEST_ASSERT(os_sem_release(NULL)    == OS_INVALID_PARM);
+    TEST_ASSERT(os_sem_pend(NULL, 1)    == OS_INVALID_PARM);
+
+    /* Get the semaphore */
+    err = os_sem_pend(sem, 0);
+    TEST_ASSERT(err == 0,
+                "Did not get free semaphore immediately (err=%d)", err);
+
+    /* Check semaphore internals */
+    TEST_ASSERT(sem->sem_tokens == 0 && SLIST_EMPTY(&sem->sem_head),
+                "Semaphore internals wrong after getting semaphore\n"
+                "%s\n"
+                "Task: task=%p prio=%u", sem_test_sem_to_s(sem), t, t->t_prio);
+
+    /* Get the semaphore again; should fail */
+    err = os_sem_pend(sem, 0);
+    TEST_ASSERT(err == OS_TIMEOUT,
+                "Did not time out waiting for semaphore (err=%d)", err);
+
+    /* Check semaphore internals */
+    TEST_ASSERT(sem->sem_tokens == 0 && SLIST_EMPTY(&sem->sem_head),
+                "Semaphore internals wrong after getting semaphore\n"
+                "%s\n"
+                "Task: task=%p prio=%u\n", sem_test_sem_to_s(sem), t,
+                t->t_prio);
+
+    /* Release semaphore */
+    err = os_sem_release(sem);
+    TEST_ASSERT(err == 0,
+                "Could not release semaphore I own (err=%d)", err);
+
+    /* Check semaphore internals */
+    TEST_ASSERT(sem->sem_tokens == 1 && SLIST_EMPTY(&sem->sem_head),
+                "Semaphore internals wrong after releasing semaphore\n"
+                "%s\n"
+                "Task: task=%p prio=%u\n", sem_test_sem_to_s(sem), t,
+                t->t_prio);
+
+    /* Release it again */
+    err = os_sem_release(sem);
+    TEST_ASSERT(err == 0,
+                "Could not release semaphore again (err=%d)\n", err);
+
+    /* Check semaphore internals */
+    TEST_ASSERT(sem->sem_tokens == 2 && SLIST_EMPTY(&sem->sem_head),
+                "Semaphore internals wrong after releasing semaphore\n"
+                "%s\n"
+                "Task: task=%p prio=%u\n", sem_test_sem_to_s(sem), t,
+                t->t_prio);
+
+    os_test_restart();
+}
+
+static void 
+sem_test_1_task1_handler(void *arg)
+{
+    os_error_t err;
+    struct os_task *t;
+    int i;;
+
+    for (i = 0; i < 3; i++) {
+        t = os_sched_get_current_task();
+        TEST_ASSERT(t->t_func == sem_test_1_task1_handler);
+
+
+        err = os_sem_pend(&g_sem1, 0);
+        TEST_ASSERT(err == OS_OK);
+
+        /* Sleep to let other tasks run */
+        os_time_delay(OS_TICKS_PER_SEC / 10);
+
+        /* Release the semaphore */
+        err = os_sem_release(&g_sem1);
+        TEST_ASSERT(err == OS_OK);
+
+        /* Sleep to let other tasks run */
+        os_time_delay(OS_TICKS_PER_SEC / 10);
+    }
+
+    os_test_restart();
+}
+
+TEST_CASE(os_sem_test_basic)
+{
+    os_error_t err;
+
+    os_init();
+
+    err = os_sem_init(&g_sem1, 1);
+    TEST_ASSERT(err == OS_OK);
+
+    os_task_init(&task1, "task1", sem_test_basic_handler, NULL, TASK1_PRIO, 
+            OS_WAIT_FOREVER, stack1, OS_STACK_ALIGN(SEM_TEST_STACK_SIZE));
+
+    os_start();
+}
+
+static void 
+sem_test_1_task2_handler(void *arg) 
+{
+    sem_test_pend_release_loop(0, OS_TICKS_PER_SEC / 10, OS_TICKS_PER_SEC / 10);
+}
+
+static void 
+sem_test_1_task3_handler(void *arg) 
+{
+    sem_test_pend_release_loop(0, OS_TIMEOUT_NEVER, OS_TICKS_PER_SEC * 2);
+}
+
+TEST_CASE(os_sem_test_case_1)
+{
+    os_error_t err;
+
+    os_init();
+
+    err = os_sem_init(&g_sem1, 1);
+    TEST_ASSERT(err == OS_OK);
+
+    os_task_init(&task1, "task1", sem_test_1_task1_handler, NULL,
+                 TASK1_PRIO, OS_WAIT_FOREVER, stack1,
+                 OS_STACK_ALIGN(SEM_TEST_STACK_SIZE));
+
+    os_task_init(&task2, "task2", sem_test_1_task2_handler, NULL,
+                 TASK2_PRIO, OS_WAIT_FOREVER, stack2,
+                 OS_STACK_ALIGN(SEM_TEST_STACK_SIZE));
+
+    os_task_init(&task3, "task3", sem_test_1_task3_handler, NULL, TASK3_PRIO, 
+                 OS_WAIT_FOREVER, stack3, OS_STACK_ALIGN(SEM_TEST_STACK_SIZE));
+
+    os_start();
+}
+
+static void 
+sem_test_2_task2_handler(void *arg) 
+{
+    sem_test_pend_release_loop(0, 2000, 2000);
+}
+
+static void 
+sem_test_2_task3_handler(void *arg) 
+{
+    sem_test_pend_release_loop(0, OS_TIMEOUT_NEVER, 2000);
+}
+
+static void 
+sem_test_2_task4_handler(void *arg) 
+{
+    sem_test_pend_release_loop(0, 2000, 2000);
+}
+
+TEST_CASE(os_sem_test_case_2)
+{
+    os_error_t err;
+
+    os_init();
+
+    err = os_sem_init(&g_sem1, 1);
+    TEST_ASSERT(err == OS_OK);
+
+    os_task_init(&task1, "task1", sem_test_sleep_task_handler, NULL,
+                 TASK1_PRIO, OS_WAIT_FOREVER, stack1,
+                 OS_STACK_ALIGN(SEM_TEST_STACK_SIZE));
+
+    os_task_init(&task2, "task2", sem_test_2_task2_handler, NULL,
+                 TASK2_PRIO, OS_WAIT_FOREVER, stack2,
+                 OS_STACK_ALIGN(SEM_TEST_STACK_SIZE));
+
+    os_task_init(&task3, "task3", sem_test_2_task3_handler, NULL, TASK3_PRIO,
+            OS_WAIT_FOREVER, stack3, OS_STACK_ALIGN(SEM_TEST_STACK_SIZE));
+
+    os_task_init(&task4, "task4", sem_test_2_task4_handler, NULL, TASK4_PRIO,
+            OS_WAIT_FOREVER, stack4, OS_STACK_ALIGN(SEM_TEST_STACK_SIZE));
+
+    os_start();
+}
+
+static void 
+sem_test_3_task2_handler(void *arg) 
+{
+    sem_test_pend_release_loop(100, 2000, 2000);
+}
+
+static void 
+sem_test_3_task3_handler(void *arg) 
+{
+    sem_test_pend_release_loop(150, 2000, 2000);
+}
+
+static void 
+sem_test_3_task4_handler(void *arg) 
+{
+    sem_test_pend_release_loop(0, 2000, 2000);
+}
+
+TEST_CASE(os_sem_test_case_3)
+{
+    os_error_t err;
+
+    os_init();
+
+    err = os_sem_init(&g_sem1, 1);
+    TEST_ASSERT(err == OS_OK);
+
+    os_task_init(&task1, "task1", sem_test_sleep_task_handler, NULL,
+                 TASK1_PRIO, OS_WAIT_FOREVER, stack1,
+                 OS_STACK_ALIGN(SEM_TEST_STACK_SIZE));
+
+    os_task_init(&task2, "task2", sem_test_3_task2_handler, NULL,
+                 TASK2_PRIO, OS_WAIT_FOREVER, stack2,
+                 OS_STACK_ALIGN(SEM_TEST_STACK_SIZE));
+
+    os_task_init(&task3, "task3", sem_test_3_task3_handler, NULL, TASK3_PRIO,
+            OS_WAIT_FOREVER, stack3, OS_STACK_ALIGN(SEM_TEST_STACK_SIZE));
+
+    os_task_init(&task4, "task4", sem_test_3_task4_handler, NULL, TASK4_PRIO,
+            OS_WAIT_FOREVER, stack4, OS_STACK_ALIGN(SEM_TEST_STACK_SIZE));
+
+    os_start();
+}
+
+static void 
+sem_test_4_task2_handler(void *arg) 
+{
+    sem_test_pend_release_loop(60, 2000, 2000);
+}
+
+static void 
+sem_test_4_task3_handler(void *arg) 
+{
+    sem_test_pend_release_loop(60, 2000, 2000);
+}
+
+static void 
+sem_test_4_task4_handler(void *arg) 
+{
+    sem_test_pend_release_loop(0, 2000, 2000);
+}
+
+
+TEST_CASE(os_sem_test_case_4)
+{
+    os_error_t err;
+
+    os_init();
+
+    err = os_sem_init(&g_sem1, 1);
+    TEST_ASSERT(err == OS_OK);
+
+    os_task_init(&task1, "task1", sem_test_sleep_task_handler, NULL,
+                 TASK1_PRIO, OS_WAIT_FOREVER, stack1,
+                 OS_STACK_ALIGN(SEM_TEST_STACK_SIZE));
+
+    os_task_init(&task2, "task2", sem_test_4_task2_handler, NULL,
+                 TASK2_PRIO, OS_WAIT_FOREVER, stack2,
+                 OS_STACK_ALIGN(SEM_TEST_STACK_SIZE));
+
+    os_task_init(&task3, "task3", sem_test_4_task3_handler, NULL, TASK3_PRIO,
+                 OS_WAIT_FOREVER, stack3, OS_STACK_ALIGN(SEM_TEST_STACK_SIZE));
+
+    os_task_init(&task4, "task4", sem_test_4_task4_handler, NULL, TASK4_PRIO,
+                 OS_WAIT_FOREVER, stack4, OS_STACK_ALIGN(SEM_TEST_STACK_SIZE));
+
+    os_start();
+}
+
+TEST_SUITE(os_sem_test_suite)
+{
+    os_sem_test_basic();
+    os_sem_test_case_1();
+    os_sem_test_case_2();
+    os_sem_test_case_3();
+    os_sem_test_case_4();
+}

http://git-wip-us.apache.org/repos/asf/incubator-mynewt-core/blob/6a7432f4/libc/baselibc/LICENSE
----------------------------------------------------------------------
diff --git a/libc/baselibc/LICENSE b/libc/baselibc/LICENSE
new file mode 100644
index 0000000..b791574
--- /dev/null
+++ b/libc/baselibc/LICENSE
@@ -0,0 +1,133 @@
+Baselibc is based on klibc 1.5.23 and tinyprintf modules.
+None of the GPL-licensed parts of klibc are used.
+
+Baselibc is licensed under the BSD license:
+
+Copyright (c) 2012 Petteri Aimonen <jpa at blc.mail.kapsi.fi>
+All rights reserved.
+
+Redistribution and use in source and binary forms, with or without
+modification, are permitted provided that the following conditions are met:
+    * Redistributions of source code must retain the above copyright
+      notice, this list of conditions and the following disclaimer.
+    * 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.
+    * Neither the name of Kustaa Nyholm or SpareTimeLabs 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 COPYRIGHT HOLDERS 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 COPYRIGHT HOLDER 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.
+
+The original licenses of the modules are included below:
+
+------------------ Tinyprintf license ------------------
+
+Copyright (c) 2004,2012 Kustaa Nyholm / SpareTimeLabs
+All rights reserved.
+
+Redistribution and use in source and binary forms, with or without
+modification, are permitted provided that the following conditions are met:
+    * Redistributions of source code must retain the above copyright
+      notice, this list of conditions and the following disclaimer.
+    * 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.
+    * Neither the name of Kustaa Nyholm or SpareTimeLabs 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 COPYRIGHT HOLDERS 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 COPYRIGHT HOLDER 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.
+
+------------------- klibc license -------------------------
+This license applies to all files in directory and its subdirectories,
+unless otherwise noted in individual files.
+
+
+Some files are derived from files derived from the include/ directory
+of the Linux kernel, and are licensed under the terms of the GNU
+General Public License, version 2, as released by the Free Software
+Foundation, Inc.; incorporated herein by reference.
+[These files are not included in the baselibc.]
+
+                                -----
+
+Some files are derived from files copyrighted by the Regents of The
+University of California, and are available under the following
+license:
+
+Note: The advertising clause in the license appearing on BSD Unix
+files was officially rescinded by the Director of the Office of
+Technology Licensing of the University of California on July 22
+1999. He states that clause 3 is "hereby deleted in its entirety."
+
+ * Copyright (c)
+ *      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. All advertising materials mentioning features or use of this software
+ *    must display the following acknowledgement:
+ *      This product includes software developed by the University of
+ *      California, Berkeley and its contributors.
+ * 4. 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.
+
+For all remaining files [of klibc], the following license applies:
+
+ * 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:
+ *
+ * Any copyright notice(s) 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.
+

http://git-wip-us.apache.org/repos/asf/incubator-mynewt-core/blob/6a7432f4/libc/baselibc/Makefile
----------------------------------------------------------------------
diff --git a/libc/baselibc/Makefile b/libc/baselibc/Makefile
new file mode 100644
index 0000000..88c8987
--- /dev/null
+++ b/libc/baselibc/Makefile
@@ -0,0 +1,46 @@
+# You can override the CFLAGS and C compiler externally,
+# e.g. make PLATFORM=cortex-m3
+CFLAGS += -g -Wall -Werror -I include
+
+ifeq ($(PLATFORM),cortex-m3)
+  CC      = arm-none-eabi-gcc
+  AR      = arm-none-eabi-ar
+  CFLAGS += -mcpu=cortex-m3 -mthumb
+  CFLAGS += -fno-common -Os
+  CFLAGS += -ffunction-sections -fdata-sections
+endif
+
+# With this, the makefile should work on Windows also.
+ifdef windir
+  RM = del
+endif
+
+# Just include all the source files in the build.
+CSRC = $(wildcard src/*.c)
+OBJS = $(CSRC:.c=.o)
+
+# And the files for the test suite
+TESTS_CSRC = $(wildcard tests/*_tests.c)
+TESTS_OBJS = $(TESTS_CSRC:.c=)
+
+# Some of the files uses "templates", i.e. common pieces
+# of code included from multiple files.
+CFLAGS += -Isrc/templates
+
+all: libc.a
+
+clean:
+	$(RM) $(OBJS) $(TESTS_OBJS) libc.a
+
+libc.a: $(OBJS)
+	$(RM) $@
+	$(AR) ru $@ $^
+
+run_tests: $(TESTS_OBJS)
+	$(foreach f,$^,$f)
+
+tests/%: tests/%.c tests/tests_glue.c libc.a
+	$(CC) $(CFLAGS) -o $@ $^
+
+%.o: %.c
+	$(CC) $(CFLAGS) -c -o $@ $<

http://git-wip-us.apache.org/repos/asf/incubator-mynewt-core/blob/6a7432f4/libc/baselibc/README.md
----------------------------------------------------------------------
diff --git a/libc/baselibc/README.md b/libc/baselibc/README.md
new file mode 100644
index 0000000..f7e9e49
--- /dev/null
+++ b/libc/baselibc/README.md
@@ -0,0 +1,6 @@
+Baselibc
+========
+This is a very simple libc for embedded systems. Mainly geared for 32-bit microcontrollers in the 10-100kB memory range.
+The library compiles to less than 5kB total on Cortex-M3, and much less if some functions aren't used.
+
+The code is based on klibc and tinyprintf modules, and licensed under the BSD license.

http://git-wip-us.apache.org/repos/asf/incubator-mynewt-core/blob/6a7432f4/libc/baselibc/include/assert.h
----------------------------------------------------------------------
diff --git a/libc/baselibc/include/assert.h b/libc/baselibc/include/assert.h
new file mode 100644
index 0000000..273058f
--- /dev/null
+++ b/libc/baselibc/include/assert.h
@@ -0,0 +1,28 @@
+/*
+ * assert.h
+ */
+
+#ifndef _ASSERT_H
+#define _ASSERT_H
+
+#ifdef NDEBUG
+
+/*
+ * NDEBUG doesn't just suppress the faulting behavior of assert(),
+ * but also all side effects of the assert() argument.  This behavior
+ * is required by the C standard, and allows the argument to reference
+ * variables that are not defined without NDEBUG.
+ */
+#define assert(x) ((void)(0))
+
+#else
+#include <stddef.h>
+
+extern void __assert_func(const char *, int, const char *, const char *)
+    __attribute((noreturn));
+
+#define assert(x) ((x) ? (void)0 : __assert_func(__FILE__, __LINE__, NULL, NULL))
+
+#endif
+
+#endif				/* _ASSERT_H */

http://git-wip-us.apache.org/repos/asf/incubator-mynewt-core/blob/6a7432f4/libc/baselibc/include/ctype.h
----------------------------------------------------------------------
diff --git a/libc/baselibc/include/ctype.h b/libc/baselibc/include/ctype.h
new file mode 100644
index 0000000..4670b6a
--- /dev/null
+++ b/libc/baselibc/include/ctype.h
@@ -0,0 +1,86 @@
+/*
+ * ctype.h
+ *
+ * This assumes ASCII.
+ */
+
+#ifndef _CTYPE_H
+#define _CTYPE_H
+
+#include <klibc/extern.h>
+#include <klibc/inline.h>
+
+__extern_inline int isupper(int __c)
+{
+	return __c >= 'A' && __c <= 'Z';
+}
+
+__extern_inline int islower(int __c)
+{
+	return __c >= 'a' && __c <= 'z';
+}
+
+__extern_inline int isalpha(int __c)
+{
+	return islower(__c) || isupper(__c);
+}
+
+__extern_inline int isdigit(int __c)
+{
+	return ((unsigned)__c - '0') <= 9;
+}
+
+__extern_inline int isalnum(int __c)
+{
+	return isalpha(__c) || isdigit(__c);
+}
+
+__extern_inline int isascii(int __c)
+{
+	return !(__c & ~0x7f);
+}
+
+__extern_inline int isblank(int __c)
+{
+	return (__c == '\t') || (__c == ' ');
+}
+
+__extern_inline int iscntrl(int __c)
+{
+	return __c < 0x20;
+}
+
+__extern_inline int isspace(int __c)
+{
+	return __c == ' ' || __c == '\n' || __c == '\t' || __c == '\r';
+}
+
+__extern_inline int isxdigit(int __c)
+{
+	return isdigit(__c) || (__c >= 'a' && __c <= 'f') || (__c >= 'A' && __c <= 'F');
+}
+
+__extern_inline int ispunct(int __c)
+{
+	return (__c >= '!' && __c <= '/') ||
+	    (__c >= ':' && __c <= '@') ||
+	    (__c >= '[' && __c <= '`') ||
+	    (__c >= '{' && __c <= '~');
+}
+
+__extern_inline int isprint(int __c)
+{
+	return (__c >= 0x20 && __c <= 0x7e);
+}
+
+__extern_inline int toupper(int __c)
+{
+	return islower(__c) ? (__c & ~32) : __c;
+}
+
+__extern_inline int tolower(int __c)
+{
+	return isupper(__c) ? (__c | 32) : __c;
+}
+
+#endif				/* _CTYPE_H */

http://git-wip-us.apache.org/repos/asf/incubator-mynewt-core/blob/6a7432f4/libc/baselibc/include/inttypes.h
----------------------------------------------------------------------
diff --git a/libc/baselibc/include/inttypes.h b/libc/baselibc/include/inttypes.h
new file mode 100644
index 0000000..e9ee426
--- /dev/null
+++ b/libc/baselibc/include/inttypes.h
@@ -0,0 +1,229 @@
+/*
+ * inttypes.h
+ */
+
+#ifndef _INTTYPES_H
+#define _INTTYPES_H
+
+#include <klibc/extern.h>
+#include <stdint.h>
+#include <stddef.h>
+
+static __inline__ intmax_t imaxabs(intmax_t __n)
+{
+	return (__n < (intmax_t) 0) ? -__n : __n;
+}
+
+__extern intmax_t strtoimax(const char *, char **, int);
+__extern uintmax_t strtoumax(const char *, char **, int);
+
+/* extensions */
+__extern intmax_t strntoimax(const char *, char **, int, size_t);
+__extern uintmax_t strntoumax(const char *, char **, int, size_t);
+
+#if !defined(__cplusplus) || defined(__STDC_FORMAT_MACROS)
+
+#define __PRI64_RANK "ll"
+#define __PRI32_RANK "l"
+
+#define PRId8	"d"
+#define PRId16	"d"
+#define PRId32	"d"
+#define PRId64	__PRI64_RANK "d"
+
+#define PRIdLEAST8	"d"
+#define PRIdLEAST16	"d"
+#define PRIdLEAST32	"d"
+#define PRIdLEAST64	__PRI64_RANK "d"
+
+#define PRIdFAST8	"d"
+#define PRIdFAST16	__PRIFAST_RANK "d"
+#define PRIdFAST32	__PRIFAST_RANK "d"
+#define PRIdFAST64	__PRI64_RANK "d"
+
+#define PRIdMAX	 __PRI64_RANK "d"
+#define PRIdPTR  __PRIPTR_RANK "d"
+
+#define PRIi8	"i"
+#define PRIi16	"i"
+#define PRIi32	"i"
+#define PRIi64	__PRI64_RANK "i"
+
+#define PRIiLEAST8	"i"
+#define PRIiLEAST16	"i"
+#define PRIiLEAST32	"i"
+#define PRIiLEAST64	__PRI64_RANK "i"
+
+#define PRIiFAST8	"i"
+#define PRIiFAST16	__PRIFAST_RANK "i"
+#define PRIiFAST32	__PRIFAST_RANK "i"
+#define PRIiFAST64	__PRI64_RANK "i"
+
+#define PRIiMAX	 __PRI64_RANK "i"
+#define PRIiPTR  __PRIPTR_RANK "i"
+
+#define PRIo8	"o"
+#define PRIo16	"o"
+#define PRIo32	"o"
+#define PRIo64	__PRI64_RANK "o"
+
+#define PRIoLEAST8	"o"
+#define PRIoLEAST16	"o"
+#define PRIoLEAST32	"o"
+#define PRIoLEAST64	__PRI64_RANK "o"
+
+#define PRIoFAST8	"o"
+#define PRIoFAST16	__PRIFAST_RANK "o"
+#define PRIoFAST32	__PRIFAST_RANK "o"
+#define PRIoFAST64	__PRI64_RANK "o"
+
+#define PRIoMAX	 __PRI64_RANK "o"
+#define PRIoPTR  __PRIPTR_RANK "o"
+
+#define PRIu8	"u"
+#define PRIu16	"u"
+#define PRIu32	"u"
+#define PRIu64	__PRI64_RANK "u"
+
+#define PRIuLEAST8	"u"
+#define PRIuLEAST16	"u"
+#define PRIuLEAST32	"u"
+#define PRIuLEAST64	__PRI64_RANK "u"
+
+#define PRIuFAST8	"u"
+#define PRIuFAST16	__PRIFAST_RANK "u"
+#define PRIuFAST32	__PRIFAST_RANK "u"
+#define PRIuFAST64	__PRI64_RANK "u"
+
+#define PRIuMAX	 __PRI64_RANK "u"
+#define PRIuPTR  __PRIPTR_RANK "u"
+
+#define PRIx8	"x"
+#define PRIx16	"x"
+#define PRIx32	__PRI32_RANK "x"
+#define PRIx64	__PRI64_RANK "x"
+
+#define PRIxLEAST8	"x"
+#define PRIxLEAST16	"x"
+#define PRIxLEAST32	"x"
+#define PRIxLEAST64	__PRI64_RANK "x"
+
+#define PRIxFAST8	"x"
+#define PRIxFAST16	__PRIFAST_RANK "x"
+#define PRIxFAST32	__PRIFAST_RANK "x"
+#define PRIxFAST64	__PRI64_RANK "x"
+
+#define PRIxMAX	 __PRI64_RANK "x"
+#define PRIxPTR  __PRIPTR_RANK "x"
+
+#define PRIX8	"X"
+#define PRIX16	"X"
+#define PRIX32	__PRI32_RANK "X"
+#define PRIX64	__PRI64_RANK "X"
+
+#define PRIXLEAST8	"X"
+#define PRIXLEAST16	"X"
+#define PRIXLEAST32	"X"
+#define PRIXLEAST64	__PRI64_RANK "X"
+
+#define PRIXFAST8	"X"
+#define PRIXFAST16	__PRIFAST_RANK "X"
+#define PRIXFAST32	__PRIFAST_RANK "X"
+#define PRIXFAST64	__PRI64_RANK "X"
+
+#define PRIXMAX	 __PRI64_RANK "X"
+#define PRIXPTR  __PRIPTR_RANK "X"
+
+#define SCNd8	"hhd"
+#define SCNd16	"hd"
+#define SCNd32	"d"
+#define SCNd64	__PRI64_RANK "d"
+
+#define SCNdLEAST8	"hhd"
+#define SCNdLEAST16	"hd"
+#define SCNdLEAST32	"d"
+#define SCNdLEAST64	__PRI64_RANK "d"
+
+#define SCNdFAST8	"hhd"
+#define SCNdFAST16	__PRIFAST_RANK "d"
+#define SCNdFAST32	__PRIFAST_RANK "d"
+#define SCNdFAST64	__PRI64_RANK "d"
+
+#define SCNdMAX	 __PRI64_RANK "d"
+#define SCNdPTR  __PRIPTR_RANK "d"
+
+#define SCNi8	"hhi"
+#define SCNi16	"hi"
+#define SCNi32	"i"
+#define SCNi64	__PRI64_RANK "i"
+
+#define SCNiLEAST8	"hhi"
+#define SCNiLEAST16	"hi"
+#define SCNiLEAST32	"i"
+#define SCNiLEAST64	__PRI64_RANK "i"
+
+#define SCNiFAST8	"hhi"
+#define SCNiFAST16	__PRIFAST_RANK "i"
+#define SCNiFAST32	__PRIFAST_RANK "i"
+#define SCNiFAST64	__PRI64_RANK "i"
+
+#define SCNiMAX	 __PRI64_RANK "i"
+#define SCNiPTR  __PRIPTR_RANK "i"
+
+#define SCNo8	"hho"
+#define SCNo16	"ho"
+#define SCNo32	"o"
+#define SCNo64	__PRI64_RANK "o"
+
+#define SCNoLEAST8	"hho"
+#define SCNoLEAST16	"ho"
+#define SCNoLEAST32	"o"
+#define SCNoLEAST64	__PRI64_RANK "o"
+
+#define SCNoFAST8	"hho"
+#define SCNoFAST16	__PRIFAST_RANK "o"
+#define SCNoFAST32	__PRIFAST_RANK "o"
+#define SCNoFAST64	__PRI64_RANK "o"
+
+#define SCNoMAX	 __PRI64_RANK "o"
+#define SCNoPTR  __PRIPTR_RANK "o"
+
+#define SCNu8	"hhu"
+#define SCNu16	"hu"
+#define SCNu32	"u"
+#define SCNu64	__PRI64_RANK "u"
+
+#define SCNuLEAST8	"hhu"
+#define SCNuLEAST16	"hu"
+#define SCNuLEAST32	"u"
+#define SCNuLEAST64	__PRI64_RANK "u"
+
+#define SCNuFAST8	"hhu"
+#define SCNuFAST16	__PRIFAST_RANK "u"
+#define SCNuFAST32	__PRIFAST_RANK "u"
+#define SCNuFAST64	__PRI64_RANK "u"
+
+#define SCNuMAX	 __PRI64_RANK "u"
+#define SCNuPTR  __PRIPTR_RANK "u"
+
+#define SCNx8	"hhx"
+#define SCNx16	"hx"
+#define SCNx32	"x"
+#define SCNx64	__PRI64_RANK "x"
+
+#define SCNxLEAST8	"hhx"
+#define SCNxLEAST16	"hx"
+#define SCNxLEAST32	"x"
+#define SCNxLEAST64	__PRI64_RANK "x"
+
+#define SCNxFAST8	"hhx"
+#define SCNxFAST16	__PRIFAST_RANK "x"
+#define SCNxFAST32	__PRIFAST_RANK "x"
+#define SCNxFAST64	__PRI64_RANK "x"
+
+#define SCNxMAX	 __PRI64_RANK "x"
+#define SCNxPTR  __PRIPTR_RANK "x"
+
+#endif
+
+#endif				/* _INTTYPES_H */

http://git-wip-us.apache.org/repos/asf/incubator-mynewt-core/blob/6a7432f4/libc/baselibc/include/klibc/extern.h
----------------------------------------------------------------------
diff --git a/libc/baselibc/include/klibc/extern.h b/libc/baselibc/include/klibc/extern.h
new file mode 100644
index 0000000..7d7c7b8
--- /dev/null
+++ b/libc/baselibc/include/klibc/extern.h
@@ -0,0 +1,16 @@
+/*
+ * klibc/extern.h
+ */
+
+#ifndef _KLIBC_EXTERN_H
+#define _KLIBC_EXTERN_H
+
+#ifdef __cplusplus
+#define __extern extern "C"
+#else
+#define __extern extern
+#endif
+
+#define __alias(x) __attribute__((weak, alias(x)))
+
+#endif				/* _KLIBC_EXTERN_H */

http://git-wip-us.apache.org/repos/asf/incubator-mynewt-core/blob/6a7432f4/libc/baselibc/include/klibc/inline.h
----------------------------------------------------------------------
diff --git a/libc/baselibc/include/klibc/inline.h b/libc/baselibc/include/klibc/inline.h
new file mode 100644
index 0000000..0e54743
--- /dev/null
+++ b/libc/baselibc/include/klibc/inline.h
@@ -0,0 +1,12 @@
+/*
+ * klibc/inline.h
+ */
+
+#ifndef _KLIBC_INLINE_H
+#define _KLIBC_INLINE_H
+
+#ifndef __extern_inline
+#define __extern_inline extern inline __attribute__((gnu_inline))
+#endif
+
+#endif				/* _KLIBC_INLINE_H */

http://git-wip-us.apache.org/repos/asf/incubator-mynewt-core/blob/6a7432f4/libc/baselibc/include/stdio.h
----------------------------------------------------------------------
diff --git a/libc/baselibc/include/stdio.h b/libc/baselibc/include/stdio.h
new file mode 100644
index 0000000..3f93340
--- /dev/null
+++ b/libc/baselibc/include/stdio.h
@@ -0,0 +1,124 @@
+/*
+ * stdio.h
+ */
+
+#ifndef _STDIO_H
+#define _STDIO_H
+
+#include <klibc/extern.h>
+#include <klibc/inline.h>
+#include <stdarg.h>
+#include <stddef.h>
+#include <string.h>
+
+/* The File structure is designed to be compatible with ChibiOS/RT type
+ * BaseSequentialStream.
+ */
+struct File;
+
+typedef struct File FILE;
+
+struct File_methods
+{
+    size_t (*write)(FILE* instance, const char *bp, size_t n);
+    size_t (*read)(FILE* instance, char *bp, size_t n);
+};
+
+struct File
+{
+    const struct File_methods *vmt;
+};
+
+#ifndef EOF
+# define EOF (-1)
+#endif
+
+#ifndef BUFSIZ
+# define BUFSIZ 1
+#endif
+
+/* Standard file descriptors - implement these globals yourself. */
+extern FILE* const stdin;
+extern FILE* const stdout;
+extern FILE* const stderr;
+
+/* Wrappers around stream write and read */
+__extern_inline size_t fread(void *buf, size_t size, size_t nmemb, FILE *stream)
+{
+    if (stream->vmt->read == NULL) return 0;
+    return stream->vmt->read(stream, buf, size*nmemb) / size;
+}
+
+__extern_inline size_t fwrite(const void *buf, size_t size, size_t nmemb, FILE *stream)
+{
+    if (stream->vmt->write == NULL) return 0;
+    return stream->vmt->write(stream, buf, size*nmemb) / size;
+}
+
+__extern_inline int fputs(const char *s, FILE *f)
+{
+	return fwrite(s, 1, strlen(s), f);
+}
+
+__extern_inline int puts(const char *s)
+{
+	return fwrite(s, 1, strlen(s), stdout) + fwrite("\n", 1, 1, stdout);
+}
+
+__extern_inline int fputc(int c, FILE *f)
+{
+	unsigned char ch = c;
+	return fwrite(&ch, 1, 1, f) == 1 ? ch : EOF;
+}
+
+__extern char *fgets(char *, int, FILE *);
+__extern_inline int fgetc(FILE *f)
+{
+	unsigned char ch;
+	return fread(&ch, 1, 1, f) == 1 ? ch : EOF;
+}
+
+__extern int errno;
+__extern_inline char *strerror(int errnum)
+{
+	return "error_str";
+}
+
+#define putc(c,f)  fputc((c),(f))
+#define putchar(c) fputc((c),stdout)
+#define getc(f) fgetc(f)
+#define getchar() fgetc(stdin)
+
+__extern_inline int fflush(FILE *stream)
+{
+	return 0;
+}
+
+__extern int printf(const char *, ...);
+__extern int vprintf(const char *, va_list);
+__extern int fprintf(FILE *, const char *, ...);
+__extern int vfprintf(FILE *, const char *, va_list);
+__extern int sprintf(char *, const char *, ...);
+__extern int vsprintf(char *, const char *, va_list);
+__extern int snprintf(char *, size_t n, const char *, ...);
+__extern int vsnprintf(char *, size_t n, const char *, va_list);
+__extern int asprintf(char **, const char *, ...);
+__extern int vasprintf(char **, const char *, va_list);
+
+__extern int sscanf(const char *, const char *, ...);
+__extern int vsscanf(const char *, const char *, va_list);
+
+/* Open a memory buffer for writing.
+ Note: Does not write null terminator.*/
+struct MemFile
+{
+    struct File file;
+    char *buffer;
+    size_t bytes_written;
+    size_t size;
+};
+
+FILE *fmemopen_w(struct MemFile* storage, char *buffer, size_t size);
+
+
+#endif				/* _STDIO_H */

http://git-wip-us.apache.org/repos/asf/incubator-mynewt-core/blob/6a7432f4/libc/baselibc/include/stdlib.h
----------------------------------------------------------------------
diff --git a/libc/baselibc/include/stdlib.h b/libc/baselibc/include/stdlib.h
new file mode 100644
index 0000000..847cda7
--- /dev/null
+++ b/libc/baselibc/include/stdlib.h
@@ -0,0 +1,101 @@
+/*
+ * stdlib.h
+ */
+
+#ifndef _STDLIB_H
+#define _STDLIB_H
+
+#include <klibc/extern.h>
+#include <klibc/inline.h>
+#include <stddef.h>
+#include <stdbool.h>
+
+__extern_inline int abs(int __n)
+{
+	return (__n < 0) ? -__n : __n;
+}
+
+__extern int atoi(const char *);
+__extern long atol(const char *);
+__extern long long atoll(const char *);
+
+__extern double atof(const char *str);
+__extern double strtod(const char *nptr, char **endptr);
+
+__extern_inline long labs(long __n)
+{
+	return (__n < 0L) ? -__n : __n;
+}
+
+__extern_inline long long llabs(long long __n)
+{
+	return (__n < 0LL) ? -__n : __n;
+}
+
+__extern void free(void *);
+__extern void *malloc(size_t);
+__extern void *calloc(size_t, size_t);
+__extern void *realloc(void *, size_t);
+
+/* Giving malloc some memory from which to allocate */
+__extern void add_malloc_block(void *, size_t);
+__extern void get_malloc_memory_status(size_t *, size_t *);
+
+/* Malloc locking
+ * Until the callbacks are set, malloc doesn't do any locking.
+ * malloc_lock() *may* timeout, in which case malloc() will return NULL.
+ */
+typedef bool (*malloc_lock_t)();
+typedef void (*malloc_unlock_t)();
+__extern void set_malloc_locking(malloc_lock_t, malloc_unlock_t);
+
+__extern long strtol(const char *, char **, int);
+__extern long long strtoll(const char *, char **, int);
+__extern unsigned long strtoul(const char *, char **, int);
+__extern unsigned long long strtoull(const char *, char **, int);
+
+typedef int (*__comparefunc_t) (const void *, const void *);
+__extern void *bsearch(const void *, const void *, size_t, size_t,
+		       __comparefunc_t);
+__extern void qsort(void *, size_t, size_t, __comparefunc_t);
+
+__extern long jrand48(unsigned short *);
+__extern long mrand48(void);
+__extern long nrand48(unsigned short *);
+__extern long lrand48(void);
+__extern unsigned short *seed48(const unsigned short *);
+__extern void srand48(long);
+
+__extern_inline char *getenv(const char *name)
+{
+	return NULL;
+}
+
+#define EXIT_SUCCESS	0
+#define EXIT_FAILURE	1
+__extern_inline void exit(int err)
+{
+	__extern void _exit(int s);
+
+	_exit(err);
+}
+
+#define RAND_MAX 0x7fffffff
+__extern_inline int rand(void)
+{
+	return (int)lrand48();
+}
+__extern_inline void srand(unsigned int __s)
+{
+	srand48(__s);
+}
+__extern_inline long random(void)
+{
+	return lrand48();
+}
+__extern_inline void srandom(unsigned int __s)
+{
+	srand48(__s);
+}
+
+#endif				/* _STDLIB_H */

http://git-wip-us.apache.org/repos/asf/incubator-mynewt-core/blob/6a7432f4/libc/baselibc/include/string.h
----------------------------------------------------------------------
diff --git a/libc/baselibc/include/string.h b/libc/baselibc/include/string.h
new file mode 100644
index 0000000..b3c1988
--- /dev/null
+++ b/libc/baselibc/include/string.h
@@ -0,0 +1,59 @@
+/*
+ * string.h
+ */
+
+#ifndef _STRING_H
+#define _STRING_H
+
+#include <klibc/extern.h>
+#include <stddef.h>
+
+__extern void *memccpy(void *, const void *, int, size_t);
+__extern void *memchr(const void *, int, size_t);
+__extern void *memrchr(const void *, int, size_t);
+__extern int memcmp(const void *, const void *, size_t);
+__extern void *memcpy(void *, const void *, size_t);
+__extern void *memmove(void *, const void *, size_t);
+__extern void *memset(void *, int, size_t);
+__extern void *memmem(const void *, size_t, const void *, size_t);
+__extern void memswap(void *, void *, size_t);
+__extern void bzero(void *, size_t);
+__extern int strcasecmp(const char *, const char *);
+__extern int strncasecmp(const char *, const char *, size_t);
+__extern char *strcat(char *, const char *);
+__extern char *strchr(const char *, int);
+__extern char *index(const char *, int);
+__extern char *strrchr(const char *, int);
+__extern char *rindex(const char *, int);
+__extern int strcmp(const char *, const char *);
+__extern char *strcpy(char *, const char *);
+__extern size_t strcspn(const char *, const char *);
+__extern char *strdup(const char *);
+__extern char *strndup(const char *, size_t);
+__extern size_t strlen(const char *);
+__extern size_t strnlen(const char *, size_t);
+__extern char *strncat(char *, const char *, size_t);
+__extern size_t strlcat(char *, const char *, size_t);
+__extern int strncmp(const char *, const char *, size_t);
+__extern char *strncpy(char *, const char *, size_t);
+__extern size_t strlcpy(char *, const char *, size_t);
+__extern char *strpbrk(const char *, const char *);
+__extern char *strsep(char **, const char *);
+__extern size_t strspn(const char *, const char *);
+__extern char *strstr(const char *, const char *);
+__extern char *strtok(char *, const char *);
+__extern char *strtok_r(char *, const char *, char **);
+
+/* Some dummy functions to avoid errors with C++ cstring */
+inline static int strcoll(const char *s1, const char *s2)
+{
+	return strcmp(s1, s2);
+}
+
+inline static size_t strxfrm(char *dest, const char *src, size_t n)
+{
+	strncpy(dest, src, n);
+	return strlen(src);
+}
+
+#endif				/* _STRING_H */

http://git-wip-us.apache.org/repos/asf/incubator-mynewt-core/blob/6a7432f4/libc/baselibc/pkg.yml
----------------------------------------------------------------------
diff --git a/libc/baselibc/pkg.yml b/libc/baselibc/pkg.yml
new file mode 100644
index 0000000..8ce7945
--- /dev/null
+++ b/libc/baselibc/pkg.yml
@@ -0,0 +1,28 @@
+#
+# Licensed to the Apache Software Foundation (ASF) under one
+# or more contributor license agreements.  See the NOTICE file
+# distributed with this work for additional information
+# regarding copyright ownership.  The ASF licenses this file
+# to you under the Apache License, Version 2.0 (the
+# "License"); you may not use this file except in compliance
+# with the License.  You may obtain a copy of the License at
+# 
+#  http://www.apache.org/licenses/LICENSE-2.0
+#
+# Unless required by applicable law or agreed to in writing,
+# software distributed under the License is distributed on an
+# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+# KIND, either express or implied.  See the License for the
+# specific language governing permissions and limitations
+# under the License.
+#
+
+pkg.name: libc/baselibc
+pkg.description: Simple libc for embedded systems.
+pkg.author: "Petteri Aimonen"
+pkg.homepage: "https://github.com/PetteriAimonen/Baselibc"
+pkg.keywords:
+    - libc
+
+pkg.req_apis:
+    - console

http://git-wip-us.apache.org/repos/asf/incubator-mynewt-core/blob/6a7432f4/libc/baselibc/src/asprintf.c
----------------------------------------------------------------------
diff --git a/libc/baselibc/src/asprintf.c b/libc/baselibc/src/asprintf.c
new file mode 100644
index 0000000..a3f5f00
--- /dev/null
+++ b/libc/baselibc/src/asprintf.c
@@ -0,0 +1,30 @@
+/*
+ * asprintf.c
+ */
+
+#include <stdio.h>
+#include <stdlib.h>
+#include <stdarg.h>
+
+int asprintf(char **bufp, const char *format, ...)
+{
+	va_list ap, ap1;
+	int rv;
+	int bytes;
+	char *p;
+
+	va_start(ap, format);
+	va_copy(ap1, ap);
+
+	bytes = vsnprintf(NULL, 0, format, ap1) + 1;
+	va_end(ap1);
+
+	*bufp = p = malloc(bytes);
+	if (!p)
+		return -1;
+
+	rv = vsnprintf(p, bytes, format, ap);
+	va_end(ap);
+
+	return rv;
+}

http://git-wip-us.apache.org/repos/asf/incubator-mynewt-core/blob/6a7432f4/libc/baselibc/src/atoi.c
----------------------------------------------------------------------
diff --git a/libc/baselibc/src/atoi.c b/libc/baselibc/src/atoi.c
new file mode 100644
index 0000000..a26abee
--- /dev/null
+++ b/libc/baselibc/src/atoi.c
@@ -0,0 +1,3 @@
+#define TYPE int
+#define NAME atoi
+#include "templates/atox.c.template"

http://git-wip-us.apache.org/repos/asf/incubator-mynewt-core/blob/6a7432f4/libc/baselibc/src/atol.c
----------------------------------------------------------------------
diff --git a/libc/baselibc/src/atol.c b/libc/baselibc/src/atol.c
new file mode 100644
index 0000000..1139c52
--- /dev/null
+++ b/libc/baselibc/src/atol.c
@@ -0,0 +1,3 @@
+#define TYPE long
+#define NAME atol
+#include "templates/atox.c.template"

http://git-wip-us.apache.org/repos/asf/incubator-mynewt-core/blob/6a7432f4/libc/baselibc/src/atoll.c
----------------------------------------------------------------------
diff --git a/libc/baselibc/src/atoll.c b/libc/baselibc/src/atoll.c
new file mode 100644
index 0000000..bc8a9fc
--- /dev/null
+++ b/libc/baselibc/src/atoll.c
@@ -0,0 +1,3 @@
+#define TYPE long long
+#define NAME atoll
+#include "templates/atox.c.template"

http://git-wip-us.apache.org/repos/asf/incubator-mynewt-core/blob/6a7432f4/libc/baselibc/src/baselibc_test/printf_tests.c.donotcompile
----------------------------------------------------------------------
diff --git a/libc/baselibc/src/baselibc_test/printf_tests.c.donotcompile b/libc/baselibc/src/baselibc_test/printf_tests.c.donotcompile
new file mode 100644
index 0000000..f9f380f
--- /dev/null
+++ b/libc/baselibc/src/baselibc_test/printf_tests.c.donotcompile
@@ -0,0 +1,22 @@
+#include "unittests.h"
+
+int main()
+{
+    int status = 0;
+    
+    {
+        COMMENT("Testing basic snprintf functionality");
+        char buf[20];
+        
+        snprintf(buf, sizeof(buf), "%08d", 55);
+        TEST(strcmp(buf, "00000055") == 0);
+        
+        TEST(snprintf(buf, sizeof(buf), "01234567890123456789") == 20);
+        TEST(strcmp(buf, "0123456789012345678") == 0);
+    }
+        
+    if (status != 0)
+        fprintf(stdout, "\n\nSome tests FAILED!\n");
+
+    return status;
+}

http://git-wip-us.apache.org/repos/asf/incubator-mynewt-core/blob/6a7432f4/libc/baselibc/src/baselibc_test/tests_glue.c.donotcompile
----------------------------------------------------------------------
diff --git a/libc/baselibc/src/baselibc_test/tests_glue.c.donotcompile b/libc/baselibc/src/baselibc_test/tests_glue.c.donotcompile
new file mode 100644
index 0000000..b18a689
--- /dev/null
+++ b/libc/baselibc/src/baselibc_test/tests_glue.c.donotcompile
@@ -0,0 +1,33 @@
+#include <stdio.h>
+
+#if defined(linux)
+/* Connects the baselibc stdio to normal POSIX stdio */
+size_t write(int fd, const void *buf, size_t count);
+
+static size_t stdio_write(FILE *instance, const char *bp, size_t n)
+{
+    if (instance == stdout)
+        return write(1, bp, n);
+    else
+        return write(2, bp, n);
+}
+#else
+#error No suitable write() implementation.
+#endif
+
+
+static struct File_methods stdio_methods = {
+        &stdio_write, NULL
+};
+
+static struct File _stdout = {
+        &stdio_methods
+};
+
+static struct File _stderr = {
+        &stdio_methods
+};
+
+FILE* const stdout = &_stdout;
+FILE* const stderr = &_stderr;
+

http://git-wip-us.apache.org/repos/asf/incubator-mynewt-core/blob/6a7432f4/libc/baselibc/src/baselibc_test/unittests.h
----------------------------------------------------------------------
diff --git a/libc/baselibc/src/baselibc_test/unittests.h b/libc/baselibc/src/baselibc_test/unittests.h
new file mode 100644
index 0000000..572f595
--- /dev/null
+++ b/libc/baselibc/src/baselibc_test/unittests.h
@@ -0,0 +1,36 @@
+/**
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License.  You may obtain a copy of the License at
+ *
+ *  http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied.  See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+
+#ifndef H_BASELIBC_UNITTESTS_
+#define H_BASELIBC_UNITTESTS_
+
+#include <stdio.h>
+
+#define COMMENT(x) printf("\n----" x "----\n");
+#define STR(x) #x
+#define STR2(x) STR(x)
+#define TEST(x) \
+    if (!(x)) { \
+        fprintf(stderr, "\033[31;1mFAILED:\033[22;39m " __FILE__ ":" STR2(__LINE__) " " #x "\n"); \
+        status = 1; \
+    } else { \
+        printf("\033[32;1mOK:\033[22;39m " #x "\n"); \
+    }
+
+#endif

http://git-wip-us.apache.org/repos/asf/incubator-mynewt-core/blob/6a7432f4/libc/baselibc/src/bsearch.c
----------------------------------------------------------------------
diff --git a/libc/baselibc/src/bsearch.c b/libc/baselibc/src/bsearch.c
new file mode 100644
index 0000000..1c8b07f
--- /dev/null
+++ b/libc/baselibc/src/bsearch.c
@@ -0,0 +1,26 @@
+/*
+ * bsearch.c
+ */
+
+#include <stdlib.h>
+
+void *bsearch(const void *key, const void *base, size_t nmemb,
+	      size_t size, int (*cmp) (const void *, const void *))
+{
+	while (nmemb) {
+		size_t mididx = nmemb / 2;
+		const void *midobj = base + mididx * size;
+		int diff = cmp(key, midobj);
+
+		if (diff == 0)
+			return (void *)midobj;
+
+		if (diff > 0) {
+			base = midobj + size;
+			nmemb -= mididx + 1;
+		} else
+			nmemb = mididx;
+	}
+
+	return NULL;
+}

http://git-wip-us.apache.org/repos/asf/incubator-mynewt-core/blob/6a7432f4/libc/baselibc/src/bzero.c
----------------------------------------------------------------------
diff --git a/libc/baselibc/src/bzero.c b/libc/baselibc/src/bzero.c
new file mode 100644
index 0000000..aa1c1ff
--- /dev/null
+++ b/libc/baselibc/src/bzero.c
@@ -0,0 +1,6 @@
+#include <string.h>
+
+void bzero(void *dst, size_t n)
+{
+	memset(dst, 0, n);
+}

http://git-wip-us.apache.org/repos/asf/incubator-mynewt-core/blob/6a7432f4/libc/baselibc/src/calloc.c
----------------------------------------------------------------------
diff --git a/libc/baselibc/src/calloc.c b/libc/baselibc/src/calloc.c
new file mode 100644
index 0000000..3db7664
--- /dev/null
+++ b/libc/baselibc/src/calloc.c
@@ -0,0 +1,20 @@
+/*
+ * calloc.c
+ */
+
+#include <stdlib.h>
+#include <string.h>
+
+/* FIXME: This should look for multiplication overflow */
+
+void *calloc(size_t nmemb, size_t size)
+{
+	void *ptr;
+
+	size *= nmemb;
+	ptr = malloc(size);
+	if (ptr)
+		memset(ptr, 0, size);
+
+	return ptr;
+}

http://git-wip-us.apache.org/repos/asf/incubator-mynewt-core/blob/6a7432f4/libc/baselibc/src/fgets.c
----------------------------------------------------------------------
diff --git a/libc/baselibc/src/fgets.c b/libc/baselibc/src/fgets.c
new file mode 100644
index 0000000..4e9cf68
--- /dev/null
+++ b/libc/baselibc/src/fgets.c
@@ -0,0 +1,31 @@
+/*
+ * fgets.c
+ *
+ * This will be very slow due to the implementation of getc(),
+ * but we don't have anywhere to put characters we don't need from
+ * the input.
+ */
+
+#include <stdio.h>
+
+char *fgets(char *s, int n, FILE *f)
+{
+	int ch;
+	char *p = s;
+
+	while (n > 1) {
+		ch = getc(f);
+		if (ch == EOF) {
+			*p = '\0';
+			return NULL;
+		}
+		*p++ = ch;
+		n--;
+		if (ch == '\n')
+			break;
+	}
+	if (n)
+		*p = '\0';
+
+	return s;
+}

http://git-wip-us.apache.org/repos/asf/incubator-mynewt-core/blob/6a7432f4/libc/baselibc/src/inline.c
----------------------------------------------------------------------
diff --git a/libc/baselibc/src/inline.c b/libc/baselibc/src/inline.c
new file mode 100644
index 0000000..2d8d013
--- /dev/null
+++ b/libc/baselibc/src/inline.c
@@ -0,0 +1,5 @@
+// Make an externally visible symbol out of inlined functions
+#define __extern_inline
+#include <stdlib.h>
+#include <stdio.h>
+#include <ctype.h>

http://git-wip-us.apache.org/repos/asf/incubator-mynewt-core/blob/6a7432f4/libc/baselibc/src/jrand48.c
----------------------------------------------------------------------
diff --git a/libc/baselibc/src/jrand48.c b/libc/baselibc/src/jrand48.c
new file mode 100644
index 0000000..8e2b3ac
--- /dev/null
+++ b/libc/baselibc/src/jrand48.c
@@ -0,0 +1,24 @@
+/*
+ * jrand48.c
+ */
+
+#include <stdlib.h>
+#include <stdint.h>
+
+long jrand48(unsigned short xsubi[3])
+{
+	uint64_t x;
+
+	/* The xsubi[] array is littleendian by spec */
+	x = (uint64_t) (uint16_t) xsubi[0] +
+	    ((uint64_t) (uint16_t) xsubi[1] << 16) +
+	    ((uint64_t) (uint16_t) xsubi[2] << 32);
+
+	x = (0x5deece66dULL * x) + 0xb;
+
+	xsubi[0] = (unsigned short)(uint16_t) x;
+	xsubi[1] = (unsigned short)(uint16_t) (x >> 16);
+	xsubi[2] = (unsigned short)(uint16_t) (x >> 32);
+
+	return (long)(int32_t) (x >> 16);
+}

http://git-wip-us.apache.org/repos/asf/incubator-mynewt-core/blob/6a7432f4/libc/baselibc/src/lrand48.c
----------------------------------------------------------------------
diff --git a/libc/baselibc/src/lrand48.c b/libc/baselibc/src/lrand48.c
new file mode 100644
index 0000000..a2fc87a
--- /dev/null
+++ b/libc/baselibc/src/lrand48.c
@@ -0,0 +1,13 @@
+/*
+ * lrand48.c
+ */
+
+#include <stdlib.h>
+#include <stdint.h>
+
+extern unsigned short __rand48_seed[3];	/* Common with mrand48.c, srand48.c */
+
+long lrand48(void)
+{
+	return (uint32_t) jrand48(__rand48_seed) >> 1;
+}

http://git-wip-us.apache.org/repos/asf/incubator-mynewt-core/blob/6a7432f4/libc/baselibc/src/malloc.c
----------------------------------------------------------------------
diff --git a/libc/baselibc/src/malloc.c b/libc/baselibc/src/malloc.c
new file mode 100644
index 0000000..3ef5089
--- /dev/null
+++ b/libc/baselibc/src/malloc.c
@@ -0,0 +1,274 @@
+/*
+ * malloc.c
+ *
+ * Very simple linked-list based malloc()/free().
+ */
+
+#include <stdbool.h>
+#include <stdlib.h>
+#include <assert.h>
+#include "malloc.h"
+
+/* Both the arena list and the free memory list are double linked
+   list with head node.  This the head node. Note that the arena list
+   is sorted in order of address. */
+static struct free_arena_header __malloc_head = {
+	{
+		ARENA_TYPE_HEAD,
+		0,
+		&__malloc_head,
+		&__malloc_head,
+	},
+	&__malloc_head,
+	&__malloc_head
+};
+
+static bool malloc_lock_nop() {return true;}
+static void malloc_unlock_nop() {}
+
+static malloc_lock_t malloc_lock = &malloc_lock_nop;
+static malloc_unlock_t malloc_unlock = &malloc_unlock_nop;
+
+static inline void mark_block_dead(struct free_arena_header *ah)
+{
+#ifdef DEBUG_MALLOC
+	ah->a.type = ARENA_TYPE_DEAD;
+#endif
+}
+
+static inline void remove_from_main_chain(struct free_arena_header *ah)
+{
+	struct free_arena_header *ap, *an;
+
+	mark_block_dead(ah);
+
+	ap = ah->a.prev;
+	an = ah->a.next;
+	ap->a.next = an;
+	an->a.prev = ap;
+}
+
+static inline void remove_from_free_chain(struct free_arena_header *ah)
+{
+	struct free_arena_header *ap, *an;
+
+	ap = ah->prev_free;
+	an = ah->next_free;
+	ap->next_free = an;
+	an->prev_free = ap;
+}
+
+static inline void remove_from_chains(struct free_arena_header *ah)
+{
+	remove_from_free_chain(ah);
+	remove_from_main_chain(ah);
+}
+
+static void *__malloc_from_block(struct free_arena_header *fp, size_t size)
+{
+	size_t fsize;
+	struct free_arena_header *nfp, *na, *fpn, *fpp;
+
+	fsize = fp->a.size;
+
+	/* We need the 2* to account for the larger requirements of a
+	   free block */
+	if (fsize >= size + 2 * sizeof(struct arena_header)) {
+		/* Bigger block than required -- split block */
+		nfp = (struct free_arena_header *)((char *)fp + size);
+		na = fp->a.next;
+
+		nfp->a.type = ARENA_TYPE_FREE;
+		nfp->a.size = fsize - size;
+		fp->a.type = ARENA_TYPE_USED;
+		fp->a.size = size;
+
+		/* Insert into all-block chain */
+		nfp->a.prev = fp;
+		nfp->a.next = na;
+		na->a.prev = nfp;
+		fp->a.next = nfp;
+
+		/* Replace current block on free chain */
+		nfp->next_free = fpn = fp->next_free;
+		nfp->prev_free = fpp = fp->prev_free;
+		fpn->prev_free = nfp;
+		fpp->next_free = nfp;
+	} else {
+		fp->a.type = ARENA_TYPE_USED; /* Allocate the whole block */
+		remove_from_free_chain(fp);
+	}
+
+	return (void *)(&fp->a + 1);
+}
+
+static struct free_arena_header *__free_block(struct free_arena_header *ah)
+{
+	struct free_arena_header *pah, *nah;
+
+	pah = ah->a.prev;
+	nah = ah->a.next;
+	if (pah->a.type == ARENA_TYPE_FREE &&
+	    (char *)pah + pah->a.size == (char *)ah) {
+		/* Coalesce into the previous block */
+		pah->a.size += ah->a.size;
+		pah->a.next = nah;
+		nah->a.prev = pah;
+		mark_block_dead(ah);
+
+		ah = pah;
+		pah = ah->a.prev;
+	} else {
+		/* Need to add this block to the free chain */
+		ah->a.type = ARENA_TYPE_FREE;
+
+		ah->next_free = __malloc_head.next_free;
+		ah->prev_free = &__malloc_head;
+		__malloc_head.next_free = ah;
+		ah->next_free->prev_free = ah;
+	}
+
+	/* In either of the previous cases, we might be able to merge
+	   with the subsequent block... */
+	if (nah->a.type == ARENA_TYPE_FREE &&
+	    (char *)ah + ah->a.size == (char *)nah) {
+		ah->a.size += nah->a.size;
+
+		/* Remove the old block from the chains */
+		remove_from_chains(nah);
+	}
+
+	/* Return the block that contains the called block */
+	return ah;
+}
+
+void *malloc(size_t size)
+{
+	struct free_arena_header *fp;
+        void *more_mem;
+        extern void *_sbrk(int incr);
+
+	if (size == 0)
+		return NULL;
+
+	/* Add the obligatory arena header, and round up */
+	size = (size + 2 * sizeof(struct arena_header) - 1) & ARENA_SIZE_MASK;
+
+        if (!malloc_lock())
+                return NULL;
+
+        void *result = NULL;
+retry_alloc:
+	for (fp = __malloc_head.next_free; fp->a.type != ARENA_TYPE_HEAD;
+	     fp = fp->next_free) {
+		if (fp->a.size >= size) {
+			/* Found fit -- allocate out of this block */
+			result = __malloc_from_block(fp, size);
+                        break;
+		}
+	}
+        if (result == NULL) {
+            more_mem = _sbrk(size);
+            if (more_mem != (void *)-1) {
+                add_malloc_block(more_mem, size);
+                goto retry_alloc;
+            }
+        }
+        malloc_unlock();
+	return result;
+}
+
+/* Call this to give malloc some memory to allocate from */
+void add_malloc_block(void *buf, size_t size)
+{
+	struct free_arena_header *fp = buf;
+	struct free_arena_header *pah;
+
+	if (size < sizeof(struct free_arena_header))
+		return; // Too small.
+
+	/* Insert the block into the management chains.  We need to set
+	   up the size and the main block list pointer, the rest of
+	   the work is logically identical to free(). */
+	fp->a.type = ARENA_TYPE_FREE;
+	fp->a.size = size;
+
+        if (!malloc_lock())
+            return;
+
+	/* We need to insert this into the main block list in the proper
+	   place -- this list is required to be sorted.  Since we most likely
+	   get memory assignments in ascending order, search backwards for
+	   the proper place. */
+	for (pah = __malloc_head.a.prev; pah->a.type != ARENA_TYPE_HEAD;
+	     pah = pah->a.prev) {
+		if (pah < fp)
+			break;
+	}
+
+	/* Now pah points to the node that should be the predecessor of
+	   the new node */
+	fp->a.next = pah->a.next;
+	fp->a.prev = pah;
+	pah->a.next = fp;
+	fp->a.next->a.prev = fp;
+
+	/* Insert into the free chain and coalesce with adjacent blocks */
+	fp = __free_block(fp);
+
+        malloc_unlock();
+}
+
+void free(void *ptr)
+{
+	struct free_arena_header *ah;
+
+	if (!ptr)
+		return;
+
+	ah = (struct free_arena_header *)
+	    ((struct arena_header *)ptr - 1);
+
+#ifdef DEBUG_MALLOC
+	assert(ah->a.type == ARENA_TYPE_USED);
+#endif
+
+        if (!malloc_lock())
+            return;
+
+	/* Merge into adjacent free blocks */
+	ah = __free_block(ah);
+        malloc_unlock();
+}
+
+void get_malloc_memory_status(size_t *free_bytes, size_t *largest_block)
+{
+    struct free_arena_header *fp;
+    *free_bytes = 0;
+    *largest_block = 0;
+
+    if (!malloc_lock())
+            return;
+
+    for (fp = __malloc_head.next_free; fp->a.type != ARENA_TYPE_HEAD; fp = fp->next_free) {
+        *free_bytes += fp->a.size;
+        if (fp->a.size >= *largest_block) {
+            *largest_block = fp->a.size;
+        }
+    }
+
+    malloc_unlock();
+}
+
+void set_malloc_locking(malloc_lock_t lock, malloc_unlock_t unlock)
+{
+    if (lock)
+        malloc_lock = lock;
+    else
+        malloc_lock = &malloc_lock_nop;
+
+    if (unlock)
+        malloc_unlock = unlock;
+    else
+        malloc_unlock = &malloc_unlock_nop;
+}

http://git-wip-us.apache.org/repos/asf/incubator-mynewt-core/blob/6a7432f4/libc/baselibc/src/malloc.h
----------------------------------------------------------------------
diff --git a/libc/baselibc/src/malloc.h b/libc/baselibc/src/malloc.h
new file mode 100644
index 0000000..2bed2a6
--- /dev/null
+++ b/libc/baselibc/src/malloc.h
@@ -0,0 +1,43 @@
+/*
+ * malloc.h
+ *
+ * Internals for the memory allocator
+ */
+
+#include <stdint.h>
+#include <stddef.h>
+
+/*
+ * This structure should be a power of two.  This becomes the
+ * alignment unit.
+ */
+struct free_arena_header;
+
+struct arena_header {
+	size_t type;
+	size_t size;
+	struct free_arena_header *next, *prev;
+};
+
+#ifdef DEBUG_MALLOC
+#define ARENA_TYPE_USED 0x64e69c70
+#define ARENA_TYPE_FREE 0x012d610a
+#define ARENA_TYPE_HEAD 0x971676b5
+#define ARENA_TYPE_DEAD 0xeeeeeeee
+#else
+#define ARENA_TYPE_USED 0
+#define ARENA_TYPE_FREE 1
+#define ARENA_TYPE_HEAD 2
+#endif
+
+#define ARENA_SIZE_MASK (~(sizeof(struct arena_header)-1))
+
+/*
+ * This structure should be no more than twice the size of the
+ * previous structure.
+ */
+struct free_arena_header {
+	struct arena_header a;
+	struct free_arena_header *next_free, *prev_free;
+};
+

http://git-wip-us.apache.org/repos/asf/incubator-mynewt-core/blob/6a7432f4/libc/baselibc/src/memccpy.c
----------------------------------------------------------------------
diff --git a/libc/baselibc/src/memccpy.c b/libc/baselibc/src/memccpy.c
new file mode 100644
index 0000000..83d02c9
--- /dev/null
+++ b/libc/baselibc/src/memccpy.c
@@ -0,0 +1,23 @@
+/*
+ * memccpy.c
+ *
+ * memccpy()
+ */
+
+#include <stddef.h>
+#include <string.h>
+
+void *memccpy(void *dst, const void *src, int c, size_t n)
+{
+	char *q = dst;
+	const char *p = src;
+	char ch;
+
+	while (n--) {
+		*q++ = ch = *p++;
+		if (ch == (char)c)
+			return q;
+	}
+
+	return NULL;		/* No instance of "c" found */
+}

http://git-wip-us.apache.org/repos/asf/incubator-mynewt-core/blob/6a7432f4/libc/baselibc/src/memchr.c
----------------------------------------------------------------------
diff --git a/libc/baselibc/src/memchr.c b/libc/baselibc/src/memchr.c
new file mode 100644
index 0000000..f1947fb
--- /dev/null
+++ b/libc/baselibc/src/memchr.c
@@ -0,0 +1,19 @@
+/*
+ * memchr.c
+ */
+
+#include <stddef.h>
+#include <string.h>
+
+void *memchr(const void *s, int c, size_t n)
+{
+	const unsigned char *sp = s;
+
+	while (n--) {
+		if (*sp == (unsigned char)c)
+			return (void *)sp;
+		sp++;
+	}
+
+	return NULL;
+}

http://git-wip-us.apache.org/repos/asf/incubator-mynewt-core/blob/6a7432f4/libc/baselibc/src/memcmp.c
----------------------------------------------------------------------
diff --git a/libc/baselibc/src/memcmp.c b/libc/baselibc/src/memcmp.c
new file mode 100644
index 0000000..3ce9941
--- /dev/null
+++ b/libc/baselibc/src/memcmp.c
@@ -0,0 +1,19 @@
+/*
+ * memcmp.c
+ */
+
+#include <string.h>
+
+int memcmp(const void *s1, const void *s2, size_t n)
+{
+	const unsigned char *c1 = s1, *c2 = s2;
+	int d = 0;
+
+	while (n--) {
+		d = (int)*c1++ - (int)*c2++;
+		if (d)
+			break;
+	}
+
+	return d;
+}

http://git-wip-us.apache.org/repos/asf/incubator-mynewt-core/blob/6a7432f4/libc/baselibc/src/memcpy.c
----------------------------------------------------------------------
diff --git a/libc/baselibc/src/memcpy.c b/libc/baselibc/src/memcpy.c
new file mode 100644
index 0000000..5ce206d
--- /dev/null
+++ b/libc/baselibc/src/memcpy.c
@@ -0,0 +1,29 @@
+/*
+ * memcpy.c
+ */
+
+#include <string.h>
+#include <stdint.h>
+
+void *memcpy(void *dst, const void *src, size_t n)
+{
+	const char *p = src;
+	char *q = dst;
+#if defined(__i386__)
+	size_t nl = n >> 2;
+	asm volatile ("cld ; rep ; movsl ; movl %3,%0 ; rep ; movsb":"+c" (nl),
+		      "+S"(p), "+D"(q)
+		      :"r"(n & 3));
+#elif defined(__x86_64__)
+	size_t nq = n >> 3;
+	asm volatile ("cld ; rep ; movsq ; movl %3,%%ecx ; rep ; movsb":"+c"
+		      (nq), "+S"(p), "+D"(q)
+		      :"r"((uint32_t) (n & 7)));
+#else
+	while (n--) {
+		*q++ = *p++;
+	}
+#endif
+
+	return dst;
+}

http://git-wip-us.apache.org/repos/asf/incubator-mynewt-core/blob/6a7432f4/libc/baselibc/src/memfile.c
----------------------------------------------------------------------
diff --git a/libc/baselibc/src/memfile.c b/libc/baselibc/src/memfile.c
new file mode 100644
index 0000000..c915004
--- /dev/null
+++ b/libc/baselibc/src/memfile.c
@@ -0,0 +1,33 @@
+#include <stdio.h>
+
+size_t memfile_write(FILE *instance, const char *bp, size_t n)
+{
+    struct MemFile *f = (struct MemFile*)instance;
+    size_t i = 0;
+    
+    while (n--)
+    {
+        f->bytes_written++;
+        if (f->bytes_written <= f->size)
+        {
+            *f->buffer++ = *bp++;
+            i++;
+        }
+    }
+    
+    return i;
+}
+
+const struct File_methods MemFile_methods = {
+    &memfile_write,
+    NULL
+};
+
+FILE *fmemopen_w(struct MemFile* storage, char *buffer, size_t size)
+{
+    storage->file.vmt = &MemFile_methods;
+    storage->buffer = buffer;
+    storage->bytes_written = 0;
+    storage->size = size;
+    return (FILE*)storage;
+}

http://git-wip-us.apache.org/repos/asf/incubator-mynewt-core/blob/6a7432f4/libc/baselibc/src/memmem.c
----------------------------------------------------------------------
diff --git a/libc/baselibc/src/memmem.c b/libc/baselibc/src/memmem.c
new file mode 100644
index 0000000..8b5faa0
--- /dev/null
+++ b/libc/baselibc/src/memmem.c
@@ -0,0 +1,52 @@
+/*
+ * memmem.c
+ *
+ * Find a byte string inside a longer byte string
+ *
+ * This uses the "Not So Naive" algorithm, a very simple but
+ * usually effective algorithm, see:
+ *
+ * http://www-igm.univ-mlv.fr/~lecroq/string/
+ */
+
+#include <string.h>
+
+void *memmem(const void *haystack, size_t n, const void *needle, size_t m)
+{
+	const unsigned char *y = (const unsigned char *)haystack;
+	const unsigned char *x = (const unsigned char *)needle;
+
+	size_t j, k, l;
+
+	if (m > n || !m || !n)
+		return NULL;
+
+	if (1 != m) {
+		if (x[0] == x[1]) {
+			k = 2;
+			l = 1;
+		} else {
+			k = 1;
+			l = 2;
+		}
+
+		j = 0;
+		while (j <= n - m) {
+			if (x[1] != y[j + 1]) {
+				j += k;
+			} else {
+				if (!memcmp(x + 2, y + j + 2, m - 2)
+				    && x[0] == y[j])
+					return (void *)&y[j];
+				j += l;
+			}
+		}
+	} else
+		do {
+			if (*y == *x)
+				return (void *)y;
+			y++;
+		} while (--n);
+
+	return NULL;
+}

http://git-wip-us.apache.org/repos/asf/incubator-mynewt-core/blob/6a7432f4/libc/baselibc/src/memmove.c
----------------------------------------------------------------------
diff --git a/libc/baselibc/src/memmove.c b/libc/baselibc/src/memmove.c
new file mode 100644
index 0000000..a398cd8
--- /dev/null
+++ b/libc/baselibc/src/memmove.c
@@ -0,0 +1,36 @@
+/*
+ * memmove.c
+ */
+
+#include <string.h>
+
+void *memmove(void *dst, const void *src, size_t n)
+{
+	const char *p = src;
+	char *q = dst;
+#if defined(__i386__) || defined(__x86_64__)
+	if (q < p) {
+		asm volatile("cld; rep; movsb"
+			     : "+c" (n), "+S"(p), "+D"(q));
+	} else {
+		p += (n - 1);
+		q += (n - 1);
+		asm volatile("std; rep; movsb; cld"
+			     : "+c" (n), "+S"(p), "+D"(q));
+	}
+#else
+	if (q < p) {
+		while (n--) {
+			*q++ = *p++;
+		}
+	} else {
+		p += n;
+		q += n;
+		while (n--) {
+			*--q = *--p;
+		}
+	}
+#endif
+
+	return dst;
+}

http://git-wip-us.apache.org/repos/asf/incubator-mynewt-core/blob/6a7432f4/libc/baselibc/src/memrchr.c
----------------------------------------------------------------------
diff --git a/libc/baselibc/src/memrchr.c b/libc/baselibc/src/memrchr.c
new file mode 100644
index 0000000..ff6d711
--- /dev/null
+++ b/libc/baselibc/src/memrchr.c
@@ -0,0 +1,19 @@
+/*
+ * memrchr.c
+ */
+
+#include <stddef.h>
+#include <string.h>
+
+void *memrchr(const void *s, int c, size_t n)
+{
+	const unsigned char *sp = (const unsigned char *)s + n - 1;
+
+	while (n--) {
+		if (*sp == (unsigned char)c)
+			return (void *)sp;
+		sp--;
+	}
+
+	return NULL;
+}

http://git-wip-us.apache.org/repos/asf/incubator-mynewt-core/blob/6a7432f4/libc/baselibc/src/memset.c
----------------------------------------------------------------------
diff --git a/libc/baselibc/src/memset.c b/libc/baselibc/src/memset.c
new file mode 100644
index 0000000..aa00b5b
--- /dev/null
+++ b/libc/baselibc/src/memset.c
@@ -0,0 +1,30 @@
+/*
+ * memset.c
+ */
+
+#include <string.h>
+#include <stdint.h>
+
+void *memset(void *dst, int c, size_t n)
+{
+	char *q = dst;
+
+#if defined(__i386__)
+	size_t nl = n >> 2;
+	asm volatile ("cld ; rep ; stosl ; movl %3,%0 ; rep ; stosb"
+		      : "+c" (nl), "+D" (q)
+		      : "a" ((unsigned char)c * 0x01010101U), "r" (n & 3));
+#elif defined(__x86_64__)
+	size_t nq = n >> 3;
+	asm volatile ("cld ; rep ; stosq ; movl %3,%%ecx ; rep ; stosb"
+		      :"+c" (nq), "+D" (q)
+		      : "a" ((unsigned char)c * 0x0101010101010101U),
+			"r" ((uint32_t) n & 7));
+#else
+	while (n--) {
+		*q++ = c;
+	}
+#endif
+
+	return dst;
+}

http://git-wip-us.apache.org/repos/asf/incubator-mynewt-core/blob/6a7432f4/libc/baselibc/src/memswap.c
----------------------------------------------------------------------
diff --git a/libc/baselibc/src/memswap.c b/libc/baselibc/src/memswap.c
new file mode 100644
index 0000000..b32315c
--- /dev/null
+++ b/libc/baselibc/src/memswap.c
@@ -0,0 +1,24 @@
+/*
+ * memswap()
+ *
+ * Swaps the contents of two nonoverlapping memory areas.
+ * This really could be done faster...
+ */
+
+#include <string.h>
+
+void memswap(void *m1, void *m2, size_t n)
+{
+	char *p = m1;
+	char *q = m2;
+	char tmp;
+
+	while (n--) {
+		tmp = *p;
+		*p = *q;
+		*q = tmp;
+
+		p++;
+		q++;
+	}
+}

http://git-wip-us.apache.org/repos/asf/incubator-mynewt-core/blob/6a7432f4/libc/baselibc/src/mrand48.c
----------------------------------------------------------------------
diff --git a/libc/baselibc/src/mrand48.c b/libc/baselibc/src/mrand48.c
new file mode 100644
index 0000000..1a2383b
--- /dev/null
+++ b/libc/baselibc/src/mrand48.c
@@ -0,0 +1,13 @@
+/*
+ * mrand48.c
+ */
+
+#include <stdlib.h>
+#include <stdint.h>
+
+extern unsigned short __rand48_seed[3];	/* Common with lrand48.c, srand48.c */
+
+long mrand48(void)
+{
+	return jrand48(__rand48_seed);
+}

http://git-wip-us.apache.org/repos/asf/incubator-mynewt-core/blob/6a7432f4/libc/baselibc/src/mynewt.c
----------------------------------------------------------------------
diff --git a/libc/baselibc/src/mynewt.c b/libc/baselibc/src/mynewt.c
new file mode 100644
index 0000000..e35b39e
--- /dev/null
+++ b/libc/baselibc/src/mynewt.c
@@ -0,0 +1,47 @@
+/**
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License.  You may obtain a copy of the License at
+ * 
+ *  http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied.  See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+
+#include <stdio.h>
+#include <console/console.h>
+
+static size_t
+stdin_read(FILE *fp, char *bp, size_t n)
+{
+    return 0;
+}
+
+static size_t
+stdout_write(FILE *fp, const char *bp, size_t n)
+{
+    console_write(bp, n);
+    return n;
+}
+
+static struct File_methods _stdin_methods = {
+    .write = stdout_write,
+    .read = stdin_read
+};
+
+static struct File _stdin = {
+    .vmt = &_stdin_methods
+};
+
+struct File *const stdin = &_stdin;
+struct File *const stdout = &_stdin;
+struct File *const stderr = &_stdin;

http://git-wip-us.apache.org/repos/asf/incubator-mynewt-core/blob/6a7432f4/libc/baselibc/src/nrand48.c
----------------------------------------------------------------------
diff --git a/libc/baselibc/src/nrand48.c b/libc/baselibc/src/nrand48.c
new file mode 100644
index 0000000..cb3532b
--- /dev/null
+++ b/libc/baselibc/src/nrand48.c
@@ -0,0 +1,11 @@
+/*
+ * nrand48.c
+ */
+
+#include <stdlib.h>
+#include <stdint.h>
+
+long nrand48(unsigned short xsubi[3])
+{
+	return (long)((uint32_t) jrand48(xsubi) >> 1);
+}

http://git-wip-us.apache.org/repos/asf/incubator-mynewt-core/blob/6a7432f4/libc/baselibc/src/qsort.c
----------------------------------------------------------------------
diff --git a/libc/baselibc/src/qsort.c b/libc/baselibc/src/qsort.c
new file mode 100644
index 0000000..4c189fc
--- /dev/null
+++ b/libc/baselibc/src/qsort.c
@@ -0,0 +1,46 @@
+/*
+ * qsort.c
+ *
+ * This is actually combsort.  It's an O(n log n) algorithm with
+ * simplicity/small code size being its main virtue.
+ */
+
+#include <stddef.h>
+#include <stdlib.h>
+#include <string.h>
+
+static inline size_t newgap(size_t gap)
+{
+	gap = (gap * 10) / 13;
+	if (gap == 9 || gap == 10)
+		gap = 11;
+
+	if (gap < 1)
+		gap = 1;
+	return gap;
+}
+
+void qsort(void *base, size_t nmemb, size_t size,
+	   int (*compar) (const void *, const void *))
+{
+	size_t gap = nmemb;
+	size_t i, j;
+	char *p1, *p2;
+	int swapped;
+
+	if (!nmemb)
+		return;
+
+	do {
+		gap = newgap(gap);
+		swapped = 0;
+
+		for (i = 0, p1 = base; i < nmemb - gap; i++, p1 += size) {
+			j = i + gap;
+			if (compar(p1, p2 = (char *)base + j * size) > 0) {
+				memswap(p1, p2, size);
+				swapped = 1;
+			}
+		}
+	} while (gap > 1 || swapped);
+}

http://git-wip-us.apache.org/repos/asf/incubator-mynewt-core/blob/6a7432f4/libc/baselibc/src/realloc.c
----------------------------------------------------------------------
diff --git a/libc/baselibc/src/realloc.c b/libc/baselibc/src/realloc.c
new file mode 100644
index 0000000..77e8acb
--- /dev/null
+++ b/libc/baselibc/src/realloc.c
@@ -0,0 +1,50 @@
+/*
+ * realloc.c
+ */
+
+#include <stdlib.h>
+#include <string.h>
+
+#include "malloc.h"
+
+/* FIXME: This is cheesy, it should be fixed later */
+
+void *realloc(void *ptr, size_t size)
+{
+	struct free_arena_header *ah;
+	void *newptr;
+	size_t oldsize;
+
+	if (!ptr)
+		return malloc(size);
+
+	if (size == 0) {
+		free(ptr);
+		return NULL;
+	}
+
+	/* Add the obligatory arena header, and round up */
+	size = (size + 2 * sizeof(struct arena_header) - 1) & ARENA_SIZE_MASK;
+
+	ah = (struct free_arena_header *)
+	    ((struct arena_header *)ptr - 1);
+
+	if (ah->a.size >= size && size >= (ah->a.size >> 2)) {
+		/* This field is a good size already. */
+		return ptr;
+	} else {
+		/* Make me a new block.  This is kind of bogus; we should
+		   be checking the following block to see if we can do an
+		   in-place adjustment... fix that later. */
+
+		oldsize = ah->a.size - sizeof(struct arena_header);
+
+		newptr = malloc(size);
+                if(newptr) {
+                    memcpy(newptr, ptr, (size < oldsize) ? size : oldsize);
+                }
+		free(ptr);
+
+		return newptr;
+	}
+}

http://git-wip-us.apache.org/repos/asf/incubator-mynewt-core/blob/6a7432f4/libc/baselibc/src/sprintf.c
----------------------------------------------------------------------
diff --git a/libc/baselibc/src/sprintf.c b/libc/baselibc/src/sprintf.c
new file mode 100644
index 0000000..c6d8758
--- /dev/null
+++ b/libc/baselibc/src/sprintf.c
@@ -0,0 +1,18 @@
+/*
+ * sprintf.c
+ */
+
+#include <stdio.h>
+#include <unistd.h>
+
+int sprintf(char *buffer, const char *format, ...)
+{
+	va_list ap;
+	int rv;
+
+	va_start(ap, format);
+	rv = vsnprintf(buffer, ~(size_t) 0, format, ap);
+	va_end(ap);
+
+	return rv;
+}

http://git-wip-us.apache.org/repos/asf/incubator-mynewt-core/blob/6a7432f4/libc/baselibc/src/srand48.c
----------------------------------------------------------------------
diff --git a/libc/baselibc/src/srand48.c b/libc/baselibc/src/srand48.c
new file mode 100644
index 0000000..e1c9567
--- /dev/null
+++ b/libc/baselibc/src/srand48.c
@@ -0,0 +1,15 @@
+/*
+ * srand48.c
+ */
+
+#include <stdlib.h>
+#include <stdint.h>
+
+unsigned short __rand48_seed[3];	/* Common with mrand48.c, lrand48.c */
+
+void srand48(long seedval)
+{
+	__rand48_seed[0] = 0x330e;
+	__rand48_seed[1] = (unsigned short)seedval;
+	__rand48_seed[2] = (unsigned short)((uint32_t) seedval >> 16);
+}

http://git-wip-us.apache.org/repos/asf/incubator-mynewt-core/blob/6a7432f4/libc/baselibc/src/sscanf.c
----------------------------------------------------------------------
diff --git a/libc/baselibc/src/sscanf.c b/libc/baselibc/src/sscanf.c
new file mode 100644
index 0000000..f53b276
--- /dev/null
+++ b/libc/baselibc/src/sscanf.c
@@ -0,0 +1,17 @@
+/*
+ * sscanf()
+ */
+
+#include <stdio.h>
+
+int sscanf(const char *str, const char *format, ...)
+{
+	va_list ap;
+	int rv;
+
+	va_start(ap, format);
+	rv = vsscanf(str, format, ap);
+	va_end(ap);
+
+	return rv;
+}

http://git-wip-us.apache.org/repos/asf/incubator-mynewt-core/blob/6a7432f4/libc/baselibc/src/strcasecmp.c
----------------------------------------------------------------------
diff --git a/libc/baselibc/src/strcasecmp.c b/libc/baselibc/src/strcasecmp.c
new file mode 100644
index 0000000..ee1f28b
--- /dev/null
+++ b/libc/baselibc/src/strcasecmp.c
@@ -0,0 +1,24 @@
+/*
+ * strcasecmp.c
+ */
+
+#include <string.h>
+#include <ctype.h>
+
+int strcasecmp(const char *s1, const char *s2)
+{
+	const unsigned char *c1 = (const unsigned char *)s1;
+	const unsigned char *c2 = (const unsigned char *)s2;
+	unsigned char ch;
+	int d = 0;
+
+	while (1) {
+		/* toupper() expects an unsigned char (implicitly cast to int)
+		   as input, and returns an int, which is exactly what we want. */
+		d = toupper(ch = *c1++) - toupper(*c2++);
+		if (d || !ch)
+			break;
+	}
+
+	return d;
+}

http://git-wip-us.apache.org/repos/asf/incubator-mynewt-core/blob/6a7432f4/libc/baselibc/src/strcat.c
----------------------------------------------------------------------
diff --git a/libc/baselibc/src/strcat.c b/libc/baselibc/src/strcat.c
new file mode 100644
index 0000000..6c5b673
--- /dev/null
+++ b/libc/baselibc/src/strcat.c
@@ -0,0 +1,11 @@
+/*
+ * strcat.c
+ */
+
+#include <string.h>
+
+char *strcat(char *dst, const char *src)
+{
+	strcpy(strchr(dst, '\0'), src);
+	return dst;
+}

http://git-wip-us.apache.org/repos/asf/incubator-mynewt-core/blob/6a7432f4/libc/baselibc/src/strchr.c
----------------------------------------------------------------------
diff --git a/libc/baselibc/src/strchr.c b/libc/baselibc/src/strchr.c
new file mode 100644
index 0000000..6a57313
--- /dev/null
+++ b/libc/baselibc/src/strchr.c
@@ -0,0 +1,17 @@
+/*
+ * strchr.c
+ */
+
+#include <string.h>
+
+char *strchr(const char *s, int c)
+{
+	while (*s != (char)c) {
+		if (!*s)
+			return NULL;
+		s++;
+	}
+
+	return (char *)s;
+}
+

http://git-wip-us.apache.org/repos/asf/incubator-mynewt-core/blob/6a7432f4/libc/baselibc/src/strcmp.c
----------------------------------------------------------------------
diff --git a/libc/baselibc/src/strcmp.c b/libc/baselibc/src/strcmp.c
new file mode 100644
index 0000000..3ab9f5a
--- /dev/null
+++ b/libc/baselibc/src/strcmp.c
@@ -0,0 +1,21 @@
+/*
+ * strcmp.c
+ */
+
+#include <string.h>
+
+int strcmp(const char *s1, const char *s2)
+{
+	const unsigned char *c1 = (const unsigned char *)s1;
+	const unsigned char *c2 = (const unsigned char *)s2;
+	unsigned char ch;
+	int d = 0;
+
+	while (1) {
+		d = (int)(ch = *c1++) - (int)*c2++;
+		if (d || !ch)
+			break;
+	}
+
+	return d;
+}

http://git-wip-us.apache.org/repos/asf/incubator-mynewt-core/blob/6a7432f4/libc/baselibc/src/strcpy.c
----------------------------------------------------------------------
diff --git a/libc/baselibc/src/strcpy.c b/libc/baselibc/src/strcpy.c
new file mode 100644
index 0000000..aa656cf
--- /dev/null
+++ b/libc/baselibc/src/strcpy.c
@@ -0,0 +1,20 @@
+/*
+ * strcpy.c
+ *
+ * strcpy()
+ */
+
+#include <string.h>
+
+char *strcpy(char *dst, const char *src)
+{
+	char *q = dst;
+	const char *p = src;
+	char ch;
+
+	do {
+		*q++ = ch = *p++;
+	} while (ch);
+
+	return dst;
+}

http://git-wip-us.apache.org/repos/asf/incubator-mynewt-core/blob/6a7432f4/libc/baselibc/src/strcspn.c
----------------------------------------------------------------------
diff --git a/libc/baselibc/src/strcspn.c b/libc/baselibc/src/strcspn.c
new file mode 100644
index 0000000..ba9e3be
--- /dev/null
+++ b/libc/baselibc/src/strcspn.c
@@ -0,0 +1,51 @@
+/*
+FUNCTION
+	<<strcspn>>---count characters not in string
+
+INDEX
+	strcspn
+
+ANSI_SYNOPSIS
+	size_t strcspn(const char *<[s1]>, const char *<[s2]>);
+
+TRAD_SYNOPSIS
+	size_t strcspn(<[s1]>, <[s2]>)
+	char *<[s1]>;
+	char *<[s2]>;
+
+DESCRIPTION
+	This function computes the length of the initial part of
+	the string pointed to by <[s1]> which consists entirely of
+	characters <[NOT]> from the string pointed to by <[s2]>
+	(excluding the terminating null character).
+
+RETURNS
+	<<strcspn>> returns the length of the substring found.
+
+PORTABILITY
+<<strcspn>> is ANSI C.
+
+<<strcspn>> requires no supporting OS subroutines.
+ */
+
+#include <string.h>
+
+size_t strcspn(const char *s1, const char *s2)
+{
+  const char *s = s1;
+  const char *c;
+
+  while (*s1)
+    {
+      for (c = s2; *c; c++)
+	{
+	  if (*s1 == *c)
+	    break;
+	}
+      if (*c)
+	break;
+      s1++;
+    }
+
+  return s1 - s;
+}

http://git-wip-us.apache.org/repos/asf/incubator-mynewt-core/blob/6a7432f4/libc/baselibc/src/strdup.c
----------------------------------------------------------------------
diff --git a/libc/baselibc/src/strdup.c b/libc/baselibc/src/strdup.c
new file mode 100644
index 0000000..905b51d
--- /dev/null
+++ b/libc/baselibc/src/strdup.c
@@ -0,0 +1,17 @@
+/*
+ * strdup.c
+ */
+
+#include <string.h>
+#include <stdlib.h>
+
+char *strdup(const char *s)
+{
+	int l = strlen(s) + 1;
+	char *d = malloc(l);
+
+	if (d)
+		memcpy(d, s, l);
+
+	return d;
+}

http://git-wip-us.apache.org/repos/asf/incubator-mynewt-core/blob/6a7432f4/libc/baselibc/src/strlcat.c
----------------------------------------------------------------------
diff --git a/libc/baselibc/src/strlcat.c b/libc/baselibc/src/strlcat.c
new file mode 100644
index 0000000..6d95087
--- /dev/null
+++ b/libc/baselibc/src/strlcat.c
@@ -0,0 +1,30 @@
+/*
+ * strlcat.c
+ */
+
+#include <string.h>
+
+size_t strlcat(char *dst, const char *src, size_t size)
+{
+	size_t bytes = 0;
+	char *q = dst;
+	const char *p = src;
+	char ch;
+
+	while (bytes < size && *q) {
+		q++;
+		bytes++;
+	}
+	if (bytes == size)
+		return (bytes + strlen(src));
+
+	while ((ch = *p++)) {
+		if (bytes + 1 < size)
+			*q++ = ch;
+
+		bytes++;
+	}
+
+	*q = '\0';
+	return bytes;
+}

http://git-wip-us.apache.org/repos/asf/incubator-mynewt-core/blob/6a7432f4/libc/baselibc/src/strlcpy.c
----------------------------------------------------------------------
diff --git a/libc/baselibc/src/strlcpy.c b/libc/baselibc/src/strlcpy.c
new file mode 100644
index 0000000..3ec8fd2
--- /dev/null
+++ b/libc/baselibc/src/strlcpy.c
@@ -0,0 +1,26 @@
+/*
+ * strlcpy.c
+ */
+
+#include <string.h>
+
+size_t strlcpy(char *dst, const char *src, size_t size)
+{
+	size_t bytes = 0;
+	char *q = dst;
+	const char *p = src;
+	char ch;
+
+	while ((ch = *p++)) {
+		if (bytes + 1 < size)
+			*q++ = ch;
+
+		bytes++;
+	}
+
+	/* If size == 0 there is no space for a final null... */
+	if (size)
+		*q = '\0';
+
+	return bytes;
+}

http://git-wip-us.apache.org/repos/asf/incubator-mynewt-core/blob/6a7432f4/libc/baselibc/src/strlen.c
----------------------------------------------------------------------
diff --git a/libc/baselibc/src/strlen.c b/libc/baselibc/src/strlen.c
new file mode 100644
index 0000000..86526a5
--- /dev/null
+++ b/libc/baselibc/src/strlen.c
@@ -0,0 +1,13 @@
+/*
+ * strlen()
+ */
+
+#include <string.h>
+
+size_t strlen(const char *s)
+{
+	const char *ss = s;
+	while (*ss)
+		ss++;
+	return ss - s;
+}

http://git-wip-us.apache.org/repos/asf/incubator-mynewt-core/blob/6a7432f4/libc/baselibc/src/strncasecmp.c
----------------------------------------------------------------------
diff --git a/libc/baselibc/src/strncasecmp.c b/libc/baselibc/src/strncasecmp.c
new file mode 100644
index 0000000..0551935
--- /dev/null
+++ b/libc/baselibc/src/strncasecmp.c
@@ -0,0 +1,24 @@
+/*
+ * strncasecmp.c
+ */
+
+#include <string.h>
+#include <ctype.h>
+
+int strncasecmp(const char *s1, const char *s2, size_t n)
+{
+	const unsigned char *c1 = (const unsigned char *)s1;
+	const unsigned char *c2 = (const unsigned char *)s2;
+	unsigned char ch;
+	int d = 0;
+
+	while (n--) {
+		/* toupper() expects an unsigned char (implicitly cast to int)
+		   as input, and returns an int, which is exactly what we want. */
+		d = toupper(ch = *c1++) - toupper(*c2++);
+		if (d || !ch)
+			break;
+	}
+
+	return d;
+}

http://git-wip-us.apache.org/repos/asf/incubator-mynewt-core/blob/6a7432f4/libc/baselibc/src/strncat.c
----------------------------------------------------------------------
diff --git a/libc/baselibc/src/strncat.c b/libc/baselibc/src/strncat.c
new file mode 100644
index 0000000..5b86216
--- /dev/null
+++ b/libc/baselibc/src/strncat.c
@@ -0,0 +1,21 @@
+/*
+ * strncat.c
+ */
+
+#include <string.h>
+
+char *strncat(char *dst, const char *src, size_t n)
+{
+	char *q = strchr(dst, '\0');
+	const char *p = src;
+	char ch;
+
+	while (n--) {
+		*q++ = ch = *p++;
+		if (!ch)
+			return dst;
+	}
+	*q = '\0';
+
+	return dst;
+}

http://git-wip-us.apache.org/repos/asf/incubator-mynewt-core/blob/6a7432f4/libc/baselibc/src/strncmp.c
----------------------------------------------------------------------
diff --git a/libc/baselibc/src/strncmp.c b/libc/baselibc/src/strncmp.c
new file mode 100644
index 0000000..5235545
--- /dev/null
+++ b/libc/baselibc/src/strncmp.c
@@ -0,0 +1,21 @@
+/*
+ * strncmp.c
+ */
+
+#include <string.h>
+
+int strncmp(const char *s1, const char *s2, size_t n)
+{
+	const unsigned char *c1 = (const unsigned char *)s1;
+	const unsigned char *c2 = (const unsigned char *)s2;
+	unsigned char ch;
+	int d = 0;
+
+	while (n--) {
+		d = (int)(ch = *c1++) - (int)*c2++;
+		if (d || !ch)
+			break;
+	}
+
+	return d;
+}

http://git-wip-us.apache.org/repos/asf/incubator-mynewt-core/blob/6a7432f4/libc/baselibc/src/strncpy.c
----------------------------------------------------------------------
diff --git a/libc/baselibc/src/strncpy.c b/libc/baselibc/src/strncpy.c
new file mode 100644
index 0000000..fffc118
--- /dev/null
+++ b/libc/baselibc/src/strncpy.c
@@ -0,0 +1,24 @@
+/*
+ * strncpy.c
+ */
+
+#include <string.h>
+
+char *strncpy(char *dst, const char *src, size_t n)
+{
+	char *q = dst;
+	const char *p = src;
+	char ch;
+
+	while (n) {
+		n--;
+		*q++ = ch = *p++;
+		if (!ch)
+			break;
+	}
+
+	/* The specs say strncpy() fills the entire buffer with NUL.  Sigh. */
+	memset(q, 0, n);
+
+	return dst;
+}

http://git-wip-us.apache.org/repos/asf/incubator-mynewt-core/blob/6a7432f4/libc/baselibc/src/strndup.c
----------------------------------------------------------------------
diff --git a/libc/baselibc/src/strndup.c b/libc/baselibc/src/strndup.c
new file mode 100644
index 0000000..427162f
--- /dev/null
+++ b/libc/baselibc/src/strndup.c
@@ -0,0 +1,19 @@
+/*
+ * strndup.c
+ */
+
+#include <string.h>
+#include <stdlib.h>
+
+char *strndup(const char *s, size_t n)
+{
+	int l = n > strlen(s) ? strlen(s) + 1 : n + 1;
+	char *d = malloc(l);
+
+	if (!d)
+		return NULL;
+	
+	memcpy(d, s, l);
+	d[n] = '\0';
+	return d;
+}

http://git-wip-us.apache.org/repos/asf/incubator-mynewt-core/blob/6a7432f4/libc/baselibc/src/strnlen.c
----------------------------------------------------------------------
diff --git a/libc/baselibc/src/strnlen.c b/libc/baselibc/src/strnlen.c
new file mode 100644
index 0000000..1678f4b
--- /dev/null
+++ b/libc/baselibc/src/strnlen.c
@@ -0,0 +1,18 @@
+/*
+ * strnlen()
+ */
+
+#include <string.h>
+
+size_t strnlen(const char *s, size_t maxlen)
+{
+	const char *ss = s;
+
+	/* Important: the maxlen test must precede the reference through ss;
+	   since the byte beyond the maximum may segfault */
+	while ((maxlen > 0) && *ss) {
+		ss++;
+		maxlen--;
+	}
+	return ss - s;
+}

http://git-wip-us.apache.org/repos/asf/incubator-mynewt-core/blob/6a7432f4/libc/baselibc/src/strntoimax.c
----------------------------------------------------------------------
diff --git a/libc/baselibc/src/strntoimax.c b/libc/baselibc/src/strntoimax.c
new file mode 100644
index 0000000..179d9e5
--- /dev/null
+++ b/libc/baselibc/src/strntoimax.c
@@ -0,0 +1,13 @@
+/*
+ * strntoimax.c
+ *
+ * strntoimax()
+ */
+
+#include <stddef.h>
+#include <inttypes.h>
+
+intmax_t strntoimax(const char *nptr, char **endptr, int base, size_t n)
+{
+	return (intmax_t) strntoumax(nptr, endptr, base, n);
+}

http://git-wip-us.apache.org/repos/asf/incubator-mynewt-core/blob/6a7432f4/libc/baselibc/src/strntoumax.c
----------------------------------------------------------------------
diff --git a/libc/baselibc/src/strntoumax.c b/libc/baselibc/src/strntoumax.c
new file mode 100644
index 0000000..56dddad
--- /dev/null
+++ b/libc/baselibc/src/strntoumax.c
@@ -0,0 +1,77 @@
+/*
+ * strntoumax.c
+ *
+ * The strntoumax() function and associated
+ */
+
+#include <stddef.h>
+#include <stdint.h>
+#include <ctype.h>
+#include <inttypes.h>
+
+static inline int digitval(int ch)
+{
+	if (ch >= '0' && ch <= '9') {
+		return ch - '0';
+	} else if (ch >= 'A' && ch <= 'Z') {
+		return ch - 'A' + 10;
+	} else if (ch >= 'a' && ch <= 'z') {
+		return ch - 'a' + 10;
+	} else {
+		return -1;
+	}
+}
+
+uintmax_t strntoumax(const char *nptr, char **endptr, int base, size_t n)
+{
+	int minus = 0;
+	uintmax_t v = 0;
+	int d;
+
+	while (n && isspace((unsigned char)*nptr)) {
+		nptr++;
+		n--;
+	}
+
+	/* Single optional + or - */
+	if (n) {
+		char c = *nptr;
+		if (c == '-' || c == '+') {
+			minus = (c == '-');
+			nptr++;
+			n--;
+		}
+	}
+
+	if (base == 0) {
+		if (n >= 2 && nptr[0] == '0' &&
+		    (nptr[1] == 'x' || nptr[1] == 'X')) {
+			n -= 2;
+			nptr += 2;
+			base = 16;
+		} else if (n >= 1 && nptr[0] == '0') {
+			n--;
+			nptr++;
+			base = 8;
+		} else {
+			base = 10;
+		}
+	} else if (base == 16) {
+		if (n >= 2 && nptr[0] == '0' &&
+		    (nptr[1] == 'x' || nptr[1] == 'X')) {
+			n -= 2;
+			nptr += 2;
+		}
+	}
+
+	while (n && (d = digitval(*nptr)) >= 0 && d < base) {
+		v = v * base + d;
+		n--;
+		nptr++;
+	}
+
+	if (endptr)
+		*endptr = (char *)nptr;
+
+	return minus ? -v : v;
+}