diff --git a/Documentation/README.html b/Documentation/README.html
index eff38044d40dc838de778418bf56d379e0081960..1b7304e941ac801ba4e4d727679f6dade0740aee 100755
--- a/Documentation/README.html
+++ b/Documentation/README.html
@@ -9,7 +9,7 @@
   <tr align="center" bgcolor="#e4e4e4">
     <td>
       <h1><big><font color="#3c34ec"><i>NuttX README Files</i></font></big></h1>
-      <p>Last Updated: March 19, 2010</p>
+      <p>Last Updated: April 2, 2010</p>
     </td>
   </tr>
 </table>
@@ -167,8 +167,12 @@
  |   |   `- <a href="nuttx/drivers/README.txt?view=log"><b><i>README.txt</i></b></a>
  |   |- graphics/
  |   |   `- <a href="nuttx/graphics/README.txt?view=log"><b><i>README.txt</i></b></a>
+ |   |- lib/
+ |   |   `- <a href="nuttx/lib/README.txt?view=log"><b><i>README.txt</i></b></a>
  |   |- libxx/
  |   |   `- <a href="nuttx/libxx/README.txt?view=log"><b><i>README.txt</i></b></a>
+ |   |- syscall/
+ |   |   `- <a href="nuttx/syscall/README.txt?view=log"><b><i>README.txt</i></b></a>
  |   `- tools/
  |       `- <a href="nuttx/tols/README.txt?view=log"><b><i>README.txt</i></b></a>
  `- apps/
diff --git a/README.txt b/README.txt
index 1c6f935821a8010a1cb44e0a27f1dff9ef13c48d..7b149a06f0bcdc3a4c522e5f9ac8473d0fca3d9e 100755
--- a/README.txt
+++ b/README.txt
@@ -382,7 +382,11 @@ Below is a guide to the available README files in the NuttX source tree:
  |   `- README.txt
  |- graphics/
  |   `- README.txt
+ |- lib/
+ |   `- README.txt
  |- libxx/
  |   `- README.txt
+ |- syscall/
+ |   `- README.txt
  `- tools/
      `- README.txt
diff --git a/fs/Makefile b/fs/Makefile
index d6301ab995a249525f4e7f68f62a4b595464aeb4..3926cac2e55f554685985c1bda04b741572f9f80 100644
--- a/fs/Makefile
+++ b/fs/Makefile
@@ -1,7 +1,7 @@
 ############################################################################
 # fs/Makefile
 #
-#   Copyright (C) 2007, 2008 Gregory Nutt. All rights reserved.
+#   Copyright (C) 2007, 2008, 2011 Gregory Nutt. All rights reserved.
 #   Author: Gregory Nutt <spudmonkey@racsa.co.cr>
 #
 # Redistribution and use in source and binary forms, with or without
@@ -47,11 +47,10 @@ else
 CSRCS		+= fs_open.c fs_close.c fs_read.c fs_write.c fs_ioctl.c \
 		   fs_poll.c fs_select.c fs_lseek.c fs_dup.c fs_filedup.c \
 		   fs_dup2.c fs_fcntl.c fs_filedup2.c fs_mmap.c fs_opendir.c \
-		   fs_closedir.c fs_stat.c fs_readdir.c fs_readdirr.c \
-		   fs_seekdir.c fs_telldir.c fs_rewinddir.c fs_files.c fs_inode.c \
-		   fs_inodefind.c fs_inodereserve.c fs_statfs.c fs_inoderemove.c \
-		   fs_registerdriver.c fs_unregisterdriver.c fs_inodeaddref.c \
-		   fs_inoderelease.c
+		   fs_closedir.c fs_stat.c fs_readdir.c fs_seekdir.c \
+		   fs_rewinddir.c fs_files.c fs_inode.c fs_inodefind.c fs_inodereserve.c  \
+		   fs_statfs.c fs_inoderemove.c fs_registerdriver.c fs_unregisterdriver.c \
+		   fs_inodeaddref.c fs_inoderelease.c
 CSRCS		+= fs_registerblockdriver.c fs_unregisterblockdriver.c \
 		   fs_findblockdriver.c fs_openblockdriver.c fs_closeblockdriver.c
 ifneq ($(CONFIG_DISABLE_MOUNTPOINT),y)
diff --git a/include/sched.h b/include/sched.h
index f7ebd9c821c3cd35134b8ff84af5e0b698155376..72c860f0de856209ff9c3a257c099946223f11a0 100644
--- a/include/sched.h
+++ b/include/sched.h
@@ -139,7 +139,7 @@ EXTERN int    sched_rr_get_interval(pid_t pid, struct timespec *interval);
 
 EXTERN int    sched_lock(void);
 EXTERN int    sched_unlock(void);
-EXTERN int32_t sched_lockcount(void);
+EXTERN int    sched_lockcount(void);
 
 /* If instrumentation of the scheduler is enabled, then some outboard logic
  * must provide the following interfaces.
diff --git a/include/sys/syscall.h b/include/sys/syscall.h
index f44587b3cfa4d66eb3b8340a93df2e3d5457ea27..77aff0530a3a5108bb03a3a2452ed4d61a0d7e9c 100644
--- a/include/sys/syscall.h
+++ b/include/sys/syscall.h
@@ -57,15 +57,15 @@
 
 #define SYS__exit                     (CONFIG_SYS_RESERVED+0)
 #define SYS_accept                    (CONFIG_SYS_RESERVED+1)
-#define SYS_bind                      (CONFIG_SYS_RESERVED+2)
-#define SYS_clearenv                  (CONFIG_SYS_RESERVED+3)
-#define SYS_clock_getres              (CONFIG_SYS_RESERVED+4)
-#define SYS_clock_gettime             (CONFIG_SYS_RESERVED+5)
-#define SYS_clock_settime             (CONFIG_SYS_RESERVED+6)
-#define SYS_close                     (CONFIG_SYS_RESERVED+7)
-#define SYS_closedir                  (CONFIG_SYS_RESERVED+8)
-#define SYS_connect                   (CONFIG_SYS_RESERVED+9)
-#define SYS_creat                     (CONFIG_SYS_RESERVED+10)
+#define SYS_atexit                    (CONFIG_SYS_RESERVED+2)
+#define SYS_bind                      (CONFIG_SYS_RESERVED+3)
+#define SYS_clearenv                  (CONFIG_SYS_RESERVED+4)
+#define SYS_clock_getres              (CONFIG_SYS_RESERVED+5)
+#define SYS_clock_gettime             (CONFIG_SYS_RESERVED+6)
+#define SYS_clock_settime             (CONFIG_SYS_RESERVED+7)
+#define SYS_close                     (CONFIG_SYS_RESERVED+8)
+#define SYS_closedir                  (CONFIG_SYS_RESERVED+9)
+#define SYS_connect                   (CONFIG_SYS_RESERVED+10)
 #define SYS_dup                       (CONFIG_SYS_RESERVED+11)
 #define SYS_dup2                      (CONFIG_SYS_RESERVED+12)
 #define SYS_exit                      (CONFIG_SYS_RESERVED+13)
@@ -86,9 +86,9 @@
 #define SYS_mkfifo                    (CONFIG_SYS_RESERVED+28)
 #define SYS_mmap                      (CONFIG_SYS_RESERVED+29)
 #define SYS_mount                     (CONFIG_SYS_RESERVED+30)
-#define SYS_mq_notify                 (CONFIG_SYS_RESERVED+31)
-#define SYS_mq_open                   (CONFIG_SYS_RESERVED+32)
-#define SYS_mq_close                  (CONFIG_SYS_RESERVED+33)
+#define SYS_mq_close                  (CONFIG_SYS_RESERVED+31)
+#define SYS_mq_notify                 (CONFIG_SYS_RESERVED+32)
+#define SYS_mq_open                   (CONFIG_SYS_RESERVED+33)
 #define SYS_mq_receive                (CONFIG_SYS_RESERVED+34)
 #define SYS_mq_send                   (CONFIG_SYS_RESERVED+35)
 #define SYS_mq_timedreceive           (CONFIG_SYS_RESERVED+36)
@@ -133,45 +133,66 @@
 #define SYS_putenv                    (CONFIG_SYS_RESERVED+75)
 #define SYS_read                      (CONFIG_SYS_RESERVED+76)
 #define SYS_readdir                   (CONFIG_SYS_RESERVED+77)
-#define SYS_reboot                    (CONFIG_SYS_RESERVED+78)
+#define SYS_recv                      (CONFIG_SYS_RESERVED+78)
 #define SYS_recvfrom                  (CONFIG_SYS_RESERVED+79)
 #define SYS_rename                    (CONFIG_SYS_RESERVED+80)
-#define SYS_rmdir                     (CONFIG_SYS_RESERVED+81)
-#define SYS_sched_getparam            (CONFIG_SYS_RESERVED+82)
-#define SYS_sched_get_priority_max    (CONFIG_SYS_RESERVED+83)
-#define SYS_sched_get_priority_min    (CONFIG_SYS_RESERVED+84)
-#define SYS_sched_getscheduler        (CONFIG_SYS_RESERVED+85)
-#define SYS_sched_rr_get_interval     (CONFIG_SYS_RESERVED+86)
-#define SYS_sched_setparam            (CONFIG_SYS_RESERVED+87)
-#define SYS_sched_setscheduler        (CONFIG_SYS_RESERVED+88)
-#define SYS_sched_yield               (CONFIG_SYS_RESERVED+89)
-#define SYS_select                    (CONFIG_SYS_RESERVED+90)
-#define SYS_sendto                    (CONFIG_SYS_RESERVED+91)
-#define SYS_set_errno                 (CONFIG_SYS_RESERVED+92)
-#define SYS_setenv                    (CONFIG_SYS_RESERVED+93)
-#define SYS_setsockopt                (CONFIG_SYS_RESERVED+94)
-#define SYS_sigaction                 (CONFIG_SYS_RESERVED+95)
-#define SYS_signal                    (CONFIG_SYS_RESERVED+96)
-#define SYS_sigpending                (CONFIG_SYS_RESERVED+97)
-#define SYS_sigprocmask               (CONFIG_SYS_RESERVED+98)
-#define SYS_sigsuspend                (CONFIG_SYS_RESERVED+99)
-#define SYS_socket                    (CONFIG_SYS_RESERVED+100)
-#define SYS_stat                      (CONFIG_SYS_RESERVED+101)
-#define SYS_statfs                    (CONFIG_SYS_RESERVED+102)
-#define SYS_task_create               (CONFIG_SYS_RESERVED+103)
-#define SYS_task_delete               (CONFIG_SYS_RESERVED+104)
-#define SYS_task_init                 (CONFIG_SYS_RESERVED+105)
-#define SYS_task_restart              (CONFIG_SYS_RESERVED+106)
-#define SYS_timer_create              (CONFIG_SYS_RESERVED+107)
-#define SYS_timer_delete              (CONFIG_SYS_RESERVED+108)
-#define SYS_timer_getoverrun          (CONFIG_SYS_RESERVED+109)
-#define SYS_timer_gettime             (CONFIG_SYS_RESERVED+110)
-#define SYS_timer_settime             (CONFIG_SYS_RESERVED+111)
-#define SYS_umount                    (CONFIG_SYS_RESERVED+112)
-#define SYS_unlink                    (CONFIG_SYS_RESERVED+113)
-#define SYS_waitid                    (CONFIG_SYS_RESERVED+114)
-#define SYS_waitpid                   (CONFIG_SYS_RESERVED+115)
-#define SYS_write                     (CONFIG_SYS_RESERVED+116)
+#define SYS_rewinddir                 (CONFIG_SYS_RESERVED+81)
+#define SYS_rmdir                     (CONFIG_SYS_RESERVED+82)
+#define SYS_sched_getparam            (CONFIG_SYS_RESERVED+83)
+#define SYS_sched_getscheduler        (CONFIG_SYS_RESERVED+84)
+#define SYS_sched_lock                (CONFIG_SYS_RESERVED+85)
+#define SYS_sched_lockcount           (CONFIG_SYS_RESERVED+86)
+#define SYS_sched_rr_get_interval     (CONFIG_SYS_RESERVED+87)
+#define SYS_sched_setparam            (CONFIG_SYS_RESERVED+88)
+#define SYS_sched_setscheduler        (CONFIG_SYS_RESERVED+89)
+#define SYS_sched_unlock              (CONFIG_SYS_RESERVED+90)
+#define SYS_sched_yield               (CONFIG_SYS_RESERVED+91)
+#define SYS_seekdir                   (CONFIG_SYS_RESERVED+92)
+#define SYS_select                    (CONFIG_SYS_RESERVED+93)
+#define SYS_sem_close                 (CONFIG_SYS_RESERVED+94)
+#define SYS_sem_destroy               (CONFIG_SYS_RESERVED+95)
+#define SYS_sem_open                  (CONFIG_SYS_RESERVED+96)
+#define SYS_sem_post                  (CONFIG_SYS_RESERVED+97)
+#define SYS_sem_trywait               (CONFIG_SYS_RESERVED+98)
+#define SYS_sem_unlink                (CONFIG_SYS_RESERVED+99)
+#define SYS_sem_wait                  (CONFIG_SYS_RESERVED+100)
+#define SYS_sem_close                 (CONFIG_SYS_RESERVED+101)
+#define SYS_sem_close                 (CONFIG_SYS_RESERVED+102)
+#define SYS_sem_close                 (CONFIG_SYS_RESERVED+103)
+#define SYS_sem_close                 (CONFIG_SYS_RESERVED+104)
+#define SYS_send                      (CONFIG_SYS_RESERVED+105)
+#define SYS_sendto                    (CONFIG_SYS_RESERVED+106)
+#define SYS_set_errno                 (CONFIG_SYS_RESERVED+107)
+#define SYS_setenv                    (CONFIG_SYS_RESERVED+108)
+#define SYS_setsockopt                (CONFIG_SYS_RESERVED+109)
+#define SYS_sigaction                 (CONFIG_SYS_RESERVED+110)
+#define SYS_sigpending                (CONFIG_SYS_RESERVED+111)
+#define SYS_sigprocmask               (CONFIG_SYS_RESERVED+112)
+#define SYS_sigqueue                  (CONFIG_SYS_RESERVED+113)
+#define SYS_sigsuspend                (CONFIG_SYS_RESERVED+114)
+#define SYS_sigtimedwait              (CONFIG_SYS_RESERVED+115)
+#define SYS_sigwaitinfo               (CONFIG_SYS_RESERVED+116)
+#define SYS_sleep                     (CONFIG_SYS_RESERVED+117)
+#define SYS_socket                    (CONFIG_SYS_RESERVED+118)
+#define SYS_stat                      (CONFIG_SYS_RESERVED+119)
+#define SYS_statfs                    (CONFIG_SYS_RESERVED+120)
+#define SYS_task_create               (CONFIG_SYS_RESERVED+121)
+#define SYS_task_delete               (CONFIG_SYS_RESERVED+122)
+#define SYS_task_restart              (CONFIG_SYS_RESERVED+123)
+#define SYS_telldir                   (CONFIG_SYS_RESERVED+124)
+#define SYS_timer_create              (CONFIG_SYS_RESERVED+125)
+#define SYS_timer_delete              (CONFIG_SYS_RESERVED+126)
+#define SYS_timer_getoverrun          (CONFIG_SYS_RESERVED+127)
+#define SYS_timer_gettime             (CONFIG_SYS_RESERVED+128)
+#define SYS_timer_settime             (CONFIG_SYS_RESERVED+129)
+#define SYS_umount                    (CONFIG_SYS_RESERVED+130)
+#define SYS_unlink                    (CONFIG_SYS_RESERVED+131)
+#define SYS_unsetenv                  (CONFIG_SYS_RESERVED+132)
+#define SYS_up_assert                 (CONFIG_SYS_RESERVED+133)
+#define SYS_up_assert_code            (CONFIG_SYS_RESERVED+134)
+#define SYS_usleep                    (CONFIG_SYS_RESERVED+135)
+#define SYS_waitpid                   (CONFIG_SYS_RESERVED+136)
+#define SYS_write                     (CONFIG_SYS_RESERVED+137)
 
 /****************************************************************************
  * Public Type Definitions
diff --git a/lib/Makefile b/lib/Makefile
index df7a9240f6165769ebb703cf16883f8cfb233090..54ba764ff2247337c38b293d3ea4a95493f62103 100644
--- a/lib/Makefile
+++ b/lib/Makefile
@@ -41,6 +41,7 @@ include sched/Make.defs
 include string/Make.defs
 include pthread/Make.defs
 include semaphore/Make.defs
+include signal/Make.defs
 include mqueue/Make.defs
 include math/Make.defs
 include net/Make.defs
@@ -53,7 +54,7 @@ ASRCS =
 AOBJS = $(ASRCS:.S=$(OBJEXT))
 
 CSRCS = $(STDIO_SRCS) $(STDLIB_SRCS) $(UNISTD_SRCS)  $(SCHED_SRCS) \
-		  $(STRING_SRCS) $(PTHREAD_SRCS) $(SEM_SRCS) $(MQUEUE_SRCS) \
+		  $(STRING_SRCS) $(PTHREAD_SRCS) $(SEM_SRCS) $(SIG_SRCS) $(MQUEUE_SRCS) \
 		  $(MATH_SRCS) $(NET_SRCS) $(TIME_SRCS) $(LIBGEN_SRCS) \
 		  $(QUEUE_SRCS) $(MISC_SRCS) $(REGEX_SRCS) $(CRC_SRCS) $(DBG_SRCS) 
 COBJS = $(CSRCS:.c=$(OBJEXT))
@@ -69,6 +70,7 @@ SCHEDDEPPATH = --dep-path sched
 STRINGDEPPATH = --dep-path string
 PTHREADDEPPATH = --dep-path pthread
 SEMDEPPATH = --dep-path semaphore
+SIGDEPPATH = --dep-path signal
 MQDEPPATH = --dep-path mqueue
 MATHDEPPATH = --dep-path math
 NETDEPPATH = --dep-path net
@@ -76,7 +78,7 @@ TIMEDEPPATH = --dep-path time
 LIBGENDEPPATH = --dep-path libgen
 QUEUEDEPPATH = --dep-path queue
 MISCDEPPATH = --dep-path misc
-VPATH = stdio:stdlib:unistd:sched:string:pthread:semaphore:mqueue:math:net:time:libgen:queue:misc
+VPATH = stdio:stdlib:unistd:sched:string:pthread:semaphore:signal:mqueue:math:net:time:libgen:queue:misc
 
 BIN = liblib$(LIBEXT)
 
@@ -96,8 +98,8 @@ $(BIN):	$(OBJS)
 .depend: Makefile $(SRCS)
 	@$(MKDEP) $(ROOTDEPPATH) $(STDIODEPPATH) $(STDLIBDEPPATH) \
 	  $(UNISTDDEPPATH) $(SCHEDDEPPATH) $(STRINGDEPPATH) $(PTHREADDEPPATH) \
-	  $(SEMDEPPATH) $(MQDEPPATH) $(MATHDEPPATH) $(NETDEPPATH) $(TIMEDEPPATH) \
-	  $(LIBGENDEPPATH) $(QUEUEDEPPATH) $(MISCDEPPATH) \
+	  $(SEMDEPPATH) $(SIGDEPPATH) $(MQDEPPATH) $(MATHDEPPATH) $(NETDEPPATH)  \
+	  $(TIMEDEPPATH) $(LIBGENDEPPATH) $(QUEUEDEPPATH) $(MISCDEPPATH) \
 	  $(CC) -- $(CFLAGS) -- $(SRCS) >Make.dep
 	@touch $@
 
diff --git a/lib/README.txt b/lib/README.txt
new file mode 100644
index 0000000000000000000000000000000000000000..62e2fcce2e5f81e4ae7e0ac495a0359e8ecad9ea
--- /dev/null
+++ b/lib/README.txt
@@ -0,0 +1,47 @@
+lib
+===
+
+This directory contains numerous, small functions typically associated with
+what you would expect to find in a standard C library.  The sub-directories
+in this directory contain standard interface that can be executed by user-
+mode programs.
+
+Normally, NuttX is built with no protection and all threads running in kerne-
+mode.  In that model, there is no real architectural distinction between
+what is a kernel-mode program and what is a user-mode program; the system is
+more like on multi-threaded program that all runs in kernel-mode.
+
+But if the CONFIG_NUTTX_KERNEL option is selected, NuttX will be built into
+distinct user-mode and kernel-mode sections.  In that case, most of the
+code in the nuttx/ directory will run in kernel-mode with with exceptions
+of (1) the user-mode "proxies" found in syscall/proxies, and (2) the
+standard C library functions found in this directory.  In this build model,
+it is critical to separate the user-mode OS interfaces in this way.
+
+Sub-Directories
+===============
+
+The files in the lib/ directory are organized (mostly) according which file
+in the include/ directory provides the prototype for library functions.  So
+we have:
+
+  libgen    - libgen.h
+  math      - math.h and fixedmath.h
+  mqueue    - pthread.h
+  net       - Various network-related header files: netinet/ether.h, arpa/inet.h
+  pthread   - pthread.h
+  queue     - queue.h
+  sched     - sched.h
+  semaphore - semaphore.h
+  stdio     - stdio.h
+  stdlib    - stdlib.h
+  string    - string.h
+  time      - time.h
+  unistd    - unistd.h
+
+There is also a misc/ subdirectory that contains various internal functions
+and interfaces from header files that are too few to warrant their own sub-
+directory:
+
+ misc       - Nonstandard "glue" logic, debug.h, crc32.h, dirent.h
+
diff --git a/lib/misc/Make.defs b/lib/misc/Make.defs
index 680180a951fcf745fbf2b129bac45d9c95c37b97..62443c338bd5f887334abc77fd5f317b01518333 100644
--- a/lib/misc/Make.defs
+++ b/lib/misc/Make.defs
@@ -34,9 +34,13 @@
 ############################################################################
 
 MISC_SRCS = lib_init.c lib_filesem.c
+
+ifneq ($(CONFIG_NFILE_DESCRIPTORS),0)
+MISC_SRCS += lib_readdirr.c lib_telldir.c
 ifneq ($(CONFIG_NFILE_STREAMS),0)
 MISC_SRCS += lib_streamsem.c
 endif
+endif
 
 REGEX_SRCS = lib_match.c
 
diff --git a/lib/misc/lib_filesem.c b/lib/misc/lib_filesem.c
index 1f2f80b3c9cdd67ce3f96615c5270b74731c0240..e6a3fdb671e8b09bfb7d227ccdb00c9bbd7171a1 100644
--- a/lib/misc/lib_filesem.c
+++ b/lib/misc/lib_filesem.c
@@ -103,7 +103,7 @@ void lib_take_semaphore(FAR struct file_struct *stream)
 	   * the wait was awakened by a signal.
 	   */
 
-	  ASSERT(*get_errno_ptr() == EINTR);
+	  ASSERT(get_errno() == EINTR);
 	}
 
       /* We have it.  Claim the stak and return */
diff --git a/fs/fs_readdirr.c b/lib/misc/lib_readdirr.c
similarity index 95%
rename from fs/fs_readdirr.c
rename to lib/misc/lib_readdirr.c
index 953b43d5acff914c817001c7266e68a95d837b1e..6a9af906816c24747308c338960ad65a726c67c5 100644
--- a/fs/fs_readdirr.c
+++ b/lib/misc/lib_readdirr.c
@@ -1,7 +1,7 @@
 /****************************************************************************
- * fs/fs_readdirr.c
+ * lib/misc/lib_readdirr.c
  *
- *   Copyright (C) 2007-2009 Gregory Nutt. All rights reserved.
+ *   Copyright (C) 2007-2009, 2011 Gregory Nutt. All rights reserved.
  *   Author: Gregory Nutt <spudmonkey@racsa.co.cr>
  *
  * Redistribution and use in source and binary forms, with or without
@@ -42,8 +42,8 @@
 #include <string.h>
 #include <dirent.h>
 #include <errno.h>
+
 #include <nuttx/fs.h>
-#include "fs_internal.h"
 
 /****************************************************************************
  * Private Functions
@@ -87,11 +87,13 @@ int readdir_r(FAR DIR *dirp, FAR struct dirent *entry,
 {
   struct dirent *tmp;
 
-  *get_errno_ptr() = 0;
+  /* NOTE: The following use or errno is *not* thread-safe */
+
+  set_errno(0);
   tmp = readdir(dirp);
   if (!tmp)
     {
-       int error = *get_errno_ptr();
+       int error = get_errno();
        if (!error)
           {
             if (result)
diff --git a/lib/misc/lib_streamsem.c b/lib/misc/lib_streamsem.c
index 5ab4135bb19beceb18ecedd238103837b83fbf18..a8610382eba5000a6bab412b12f4ff2691d027c7 100644
--- a/lib/misc/lib_streamsem.c
+++ b/lib/misc/lib_streamsem.c
@@ -78,7 +78,7 @@ void stream_semtake(FAR struct streamlist *list)
        * the wait was awakened by a signal.
        */
 
-      ASSERT(*get_errno_ptr() == EINTR);
+      ASSERT(get_errno() == EINTR);
     }
 }
 
diff --git a/fs/fs_telldir.c b/lib/misc/lib_telldir.c
similarity index 99%
rename from fs/fs_telldir.c
rename to lib/misc/lib_telldir.c
index f8fd3b92cd4feca9516b24898e59fefa93e68bc9..2fc5384bbcb2913c5415cde9400e8820799bf8f0 100644
--- a/fs/fs_telldir.c
+++ b/lib/misc/lib_telldir.c
@@ -46,8 +46,6 @@
 #include <nuttx/fs.h>
 #include <nuttx/dirent.h>
 
-#include "fs_internal.h"
-
 /****************************************************************************
  * Private Functions
  ****************************************************************************/
diff --git a/lib/semaphore/sem_getvalue.c b/lib/semaphore/sem_getvalue.c
index bc9971b201d20cfdc6aebfb211ab7b9b9b9fea17..137eb90010f9bcf1c439125811fd5081a5c4c558 100644
--- a/lib/semaphore/sem_getvalue.c
+++ b/lib/semaphore/sem_getvalue.c
@@ -102,7 +102,7 @@ int sem_getvalue(FAR sem_t *sem, FAR int *sval)
     }
   else
     {
-	  errno = -EINVAL;
+      set_errno(EINVAL);
 	  return ERROR;
     }
 }
diff --git a/lib/semaphore/sem_init.c b/lib/semaphore/sem_init.c
index f4c9a0d8b566d594b6ffbbe4be7cb69202c92852..f90cc54700d6d4947e224f956a4b32ecc10d27a4 100644
--- a/lib/semaphore/sem_init.c
+++ b/lib/semaphore/sem_init.c
@@ -109,7 +109,7 @@ int sem_init (FAR sem_t *sem, int pshared, unsigned int value)
     }
   else
     {
-	  errno = -EINVAL;
+      set_errno(EINVAL);
 	  return ERROR;
     }
 }
diff --git a/lib/signal/Make.defs b/lib/signal/Make.defs
new file mode 100644
index 0000000000000000000000000000000000000000..0c66efa73f70ae8f3854b9c1ca32b74b96a17d3b
--- /dev/null
+++ b/lib/signal/Make.defs
@@ -0,0 +1,40 @@
+############################################################################
+# lib/signal/Make.defs
+#
+#   Copyright (C) 2011 Gregory Nutt. All rights reserved.
+#   Author: Gregory Nutt <spudmonkey@racsa.co.cr>
+#
+# 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 NuttX 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 THE
+# COPYRIGHT OWNER 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.
+#
+############################################################################
+
+ifneq ($(CONFIG_DISABLE_SIGNALS),y)
+SIG_SRCS = sig_emptyset.c sig_fillset.c sig_addset.c sig_delset.c sig_ismember.c
+endif
+
+
diff --git a/sched/sig_addset.c b/lib/signal/sig_addset.c
similarity index 97%
rename from sched/sig_addset.c
rename to lib/signal/sig_addset.c
index 678a7fcaffaf009a2db7ee259d4426936219f1b3..af9463aac8b0a925c4d566333fb136e33509805b 100644
--- a/sched/sig_addset.c
+++ b/lib/signal/sig_addset.c
@@ -1,7 +1,7 @@
 /****************************************************************************
- * sched/sig_addset.c
+ * lib/signal/sig_addset.c
  *
- *   Copyright (C) 2007, 2008 Gregory Nutt. All rights reserved.
+ *   Copyright (C) 2007, 2008, 2011 Gregory Nutt. All rights reserved.
  *   Author: Gregory Nutt <spudmonkey@racsa.co.cr>
  *
  * Redistribution and use in source and binary forms, with or without
@@ -97,3 +97,4 @@ int sigaddset(FAR sigset_t *set, int signo)
 
   return ret;
 }
+
diff --git a/sched/sig_delset.c b/lib/signal/sig_delset.c
similarity index 97%
rename from sched/sig_delset.c
rename to lib/signal/sig_delset.c
index 78f8dc177020e7aaed33b1fa9345f9bb592bba16..489c59afacd856957dc436b3e2b96de9214b9e7d 100644
--- a/sched/sig_delset.c
+++ b/lib/signal/sig_delset.c
@@ -1,7 +1,7 @@
 /****************************************************************************
- * sched/sig_delset.c
+ * lib/signal/sig_delset.c
  *
- *   Copyright (C) 2007, 2008 Gregory Nutt. All rights reserved.
+ *   Copyright (C) 2007, 2008, 2011 Gregory Nutt. All rights reserved.
  *   Author: Gregory Nutt <spudmonkey@racsa.co.cr>
  *
  * Redistribution and use in source and binary forms, with or without
@@ -97,3 +97,4 @@ int sigdelset(FAR sigset_t *set, int signo)
 
   return ret;
 }
+
diff --git a/sched/sig_emptyset.c b/lib/signal/sig_emptyset.c
similarity index 97%
rename from sched/sig_emptyset.c
rename to lib/signal/sig_emptyset.c
index c8fc4ef40a2374282d4630b6b1d38fd8586c8e34..bfee58832379870df15eee0fe2dfaec946097e83 100644
--- a/sched/sig_emptyset.c
+++ b/lib/signal/sig_emptyset.c
@@ -1,7 +1,7 @@
 /****************************************************************************
- * sched/sig_emptyset.c
+ * lib/signal/sig_emptyset.c
  *
- *   Copyright (C) 2007, 2008 Gregory Nutt. All rights reserved.
+ *   Copyright (C) 2007, 2008, 2011 Gregory Nutt. All rights reserved.
  *   Author: Gregory Nutt <spudmonkey@racsa.co.cr>
  *
  * Redistribution and use in source and binary forms, with or without
@@ -85,3 +85,4 @@ int sigemptyset(FAR sigset_t *set)
   *set = NULL_SIGNAL_SET;
   return OK;
 }
+
diff --git a/sched/sig_fillset.c b/lib/signal/sig_fillset.c
similarity index 96%
rename from sched/sig_fillset.c
rename to lib/signal/sig_fillset.c
index d315c0d6365ec5c38ca4bb74095e7191f0146f2f..c660da0ad83a5e9adc0482e88b852eb0e842445c 100644
--- a/sched/sig_fillset.c
+++ b/lib/signal/sig_fillset.c
@@ -1,7 +1,7 @@
 /****************************************************************************
- * sched/sig_fillset.c
+ * lib/signal/sig_fillset.c
  *
- *   Copyright (C) 2007, 2008 Gregory Nutt. All rights reserved.
+ *   Copyright (C) 2007, 2008, 2011 Gregory Nutt. All rights reserved.
  *   Author: Gregory Nutt <spudmonkey@racsa.co.cr>
  *
  * Redistribution and use in source and binary forms, with or without
@@ -60,7 +60,7 @@
  ****************************************************************************/
 
 /****************************************************************************
- * Publics Functioins
+ * Publics Functions
  ****************************************************************************/
 
 /****************************************************************************
@@ -85,3 +85,4 @@ int sigfillset(FAR sigset_t *set)
   *set = ALL_SIGNAL_SET;
   return OK;
 }
+
diff --git a/sched/sig_ismember.c b/lib/signal/sig_ismember.c
similarity index 97%
rename from sched/sig_ismember.c
rename to lib/signal/sig_ismember.c
index 670ef7bd207252e157e8697279a7a3e3767e331f..88dc8d819b89cc867ee68a873b0ff46046f700ff 100644
--- a/sched/sig_ismember.c
+++ b/lib/signal/sig_ismember.c
@@ -1,7 +1,7 @@
 /****************************************************************************
- * sched/sig_ismember.c
+ * lib/signal/sig_ismember.c
  *
- *   Copyright (C) 2007, 2008 Gregory Nutt. All rights reserved.
+ *   Copyright (C) 2007, 2008, 2011 Gregory Nutt. All rights reserved.
  *   Author: Gregory Nutt <spudmonkey@racsa.co.cr>
  *
  * Redistribution and use in source and binary forms, with or without
@@ -98,3 +98,4 @@ int sigismember(FAR const sigset_t *set, int signo)
 
   return ret;
 }
+
diff --git a/lib/stdio/lib_fgetpos.c b/lib/stdio/lib_fgetpos.c
index a641db4814ce11781e0298eb41a17ea37275b763..af07f7387e9ca016fbef13fc183c844444224c8f 100644
--- a/lib/stdio/lib_fgetpos.c
+++ b/lib/stdio/lib_fgetpos.c
@@ -107,7 +107,7 @@ int fgetpos(FAR FILE *stream, FAR fpos_t *pos)
 #if CONFIG_DEBUG
   if (!stream || !pos)
     {
-      errno = EINVAL;
+      set_errno(EINVAL);
       return ERROR;
     }
 #endif
diff --git a/lib/stdio/lib_fileno.c b/lib/stdio/lib_fileno.c
index 2fe3d9c995af2350342055755856960c39662a9a..7366194ba8bf55e88f1a44d34922f98660d222e5 100644
--- a/lib/stdio/lib_fileno.c
+++ b/lib/stdio/lib_fileno.c
@@ -60,7 +60,7 @@ int fileno(FAR FILE *stream)
 
   if (ret < 0)
     {
-      errno = EBADF;
+      set_errno(EBADF);
       return ERROR;
     }
   return ret;
diff --git a/lib/stdio/lib_fopen.c b/lib/stdio/lib_fopen.c
index 07437161c96d0075d49abfe1b20ca375439cfc2d..42ac19d1b301be2c8bda460f8f6b22b63a997b96 100644
--- a/lib/stdio/lib_fopen.c
+++ b/lib/stdio/lib_fopen.c
@@ -230,7 +230,7 @@ errout_with_sem:
   stream_semgive(slist);
 
 errout:
-  *get_errno_ptr() = err;
+  set_errno(err);
   return NULL;
 }
 
diff --git a/lib/stdio/lib_fputs.c b/lib/stdio/lib_fputs.c
index 870c5c42ea0222e0d84ddacb59ff7e881455af26..54801ca50d7ce7dcfef60c140e864e3a65c7b4bb 100644
--- a/lib/stdio/lib_fputs.c
+++ b/lib/stdio/lib_fputs.c
@@ -100,7 +100,7 @@ int fputs(FAR const char *s, FAR FILE *stream)
 
   if (!s)
     {
-      *get_errno_ptr() = EINVAL;
+      set_errno(EINVAL);
     }
   else
     {
diff --git a/lib/stdio/lib_fseek.c b/lib/stdio/lib_fseek.c
index e56645ab6d3a2055002f1c7d38443098ac2d2343..a68de79fe208772f616567c5ae9ded48c85661f5 100644
--- a/lib/stdio/lib_fseek.c
+++ b/lib/stdio/lib_fseek.c
@@ -119,7 +119,7 @@ int fseek(FAR FILE *stream, long int offset, int whence)
 
   if (!stream)
     {
-      errno = EBADF;
+      set_errno(EBADF);
       return ERROR;
     }
 #endif
diff --git a/lib/stdio/lib_fsetpos.c b/lib/stdio/lib_fsetpos.c
index 637c627b50db1489f484249150f31bc0658e2ba7..ad3e67230053266710aab99c48ad8eccbf2e66de 100644
--- a/lib/stdio/lib_fsetpos.c
+++ b/lib/stdio/lib_fsetpos.c
@@ -107,7 +107,7 @@ int fsetpos(FAR FILE *stream, FAR fpos_t *pos)
 #if CONFIG_DEBUG
   if (!stream || !pos)
     {
-      errno = EINVAL;
+      set_errno(EINVAL);
       return ERROR;
     }
 #endif
diff --git a/lib/stdio/lib_ftell.c b/lib/stdio/lib_ftell.c
index b0b0a78e58599e1b24da43f340b4caee131e2845..cd0e4836872409cf9f0007e53a92bf87654dc1fd 100644
--- a/lib/stdio/lib_ftell.c
+++ b/lib/stdio/lib_ftell.c
@@ -107,7 +107,7 @@ long ftell(FAR FILE *stream)
 
   if (!stream)
     {
-      errno = EBADF;
+      set_errno(EBADF);
       return ERROR;
     }
 
diff --git a/lib/stdio/lib_libfflush.c b/lib/stdio/lib_libfflush.c
index 68a1bc39fcaa6659e13e0db1cd84d7fa00f772a9..3e9de538ab6ae503e67a3995db9a40e17b8971ad 100644
--- a/lib/stdio/lib_libfflush.c
+++ b/lib/stdio/lib_libfflush.c
@@ -114,7 +114,7 @@ ssize_t lib_fflush(FAR FILE *stream, bool bforce)
 
   if (stream->fs_filedes < 0 || (stream->fs_oflags & O_WROK) == 0)
     {
-      *get_errno_ptr() = EBADF;
+      set_errno(EBADF);
       return ERROR;
     }
 
diff --git a/lib/stdio/lib_libflushall.c b/lib/stdio/lib_libflushall.c
index de2853df817474763446b97c615c55da5f07b849..0b95fc22d709b82dc6dcde0f4359fd1e137fc0cd 100644
--- a/lib/stdio/lib_libflushall.c
+++ b/lib/stdio/lib_libflushall.c
@@ -121,7 +121,7 @@ int lib_flushall(FAR struct streamlist *list)
                     * to flush all of the buffered write data.  Return EOF on failure.
                     */
 
-                   lasterrno = *get_errno_ptr();
+                   lasterrno = get_errno();
                    ret = ERROR;
                  }
              }
@@ -131,6 +131,9 @@ int lib_flushall(FAR struct streamlist *list)
 
   /* If any flush failed, return that last failed flush */
 
-  *get_errno_ptr() = lasterrno;
+  if (ret != OK)
+    {
+      set_errno(lasterrno);
+    }
   return ret;
 }
diff --git a/lib/stdio/lib_libfread.c b/lib/stdio/lib_libfread.c
index abeb34deb69465a296f270b78b9f3dffe31e9a5d..4d402a42b48f7845bc4d0972f69f81b8ce8b9a3c 100644
--- a/lib/stdio/lib_libfread.c
+++ b/lib/stdio/lib_libfread.c
@@ -93,7 +93,7 @@ ssize_t lib_fread(FAR void *ptr, size_t count, FAR FILE *stream)
 
   if (!stream || (stream->fs_oflags & O_RDOK) == 0)
     {
-      *get_errno_ptr() = EBADF;
+      set_errno(EBADF);
       bytes_read = -1;
     }
   else
diff --git a/lib/stdio/lib_libfwrite.c b/lib/stdio/lib_libfwrite.c
index 3b7253cdd0fc3fc57ef0a94ca3b7338d65e66d95..e2c35e3e992cd00cb9f9198cea4234ee6e77f467 100644
--- a/lib/stdio/lib_libfwrite.c
+++ b/lib/stdio/lib_libfwrite.c
@@ -96,7 +96,7 @@ ssize_t lib_fwrite(FAR const void *ptr, size_t count, FAR FILE *stream)
 
   if (!stream || (stream->fs_oflags & O_WROK) == 0)
     {
-      *get_errno_ptr() = EBADF;
+      set_errno(EBADF);
       goto errout;
     }
 
diff --git a/lib/stdio/lib_rawinstream.c b/lib/stdio/lib_rawinstream.c
index b54305ce64f261280149127f2f5b43f13d7a2eec..ead3116b4f81f5a7b5f9f68650de6d82a360abb7 100644
--- a/lib/stdio/lib_rawinstream.c
+++ b/lib/stdio/lib_rawinstream.c
@@ -66,7 +66,7 @@ static int rawinstream_getc(FAR struct lib_instream_s *this)
               return ch;
             }
         }
-      while (nwritten < 0 && errno == EINTR);
+      while (nwritten < 0 && get_errno() == EINTR);
     }
 
   return EOF;
diff --git a/lib/stdio/lib_rawoutstream.c b/lib/stdio/lib_rawoutstream.c
index bf9a682068f7ad635d544610ecf9251d65f0acaa..05cb5853ff29ca74fab4a2c164cf313adf25bcb5 100644
--- a/lib/stdio/lib_rawoutstream.c
+++ b/lib/stdio/lib_rawoutstream.c
@@ -64,7 +64,7 @@ static void rawoutstream_putc(FAR struct lib_outstream_s *this, int ch)
               this->nput++;
             }
         }
-      while (nwritten < 0 && *get_errno_ptr() == EINTR);
+      while (nwritten < 0 && get_errno() == EINTR);
     }
 }
 
diff --git a/lib/stdio/lib_rdflush.c b/lib/stdio/lib_rdflush.c
index 592cc418392c66045776cae8aca8c34bf99811ca..948dcce5dad2ca2accc36718bc95e36a6722384d 100644
--- a/lib/stdio/lib_rdflush.c
+++ b/lib/stdio/lib_rdflush.c
@@ -96,7 +96,7 @@ int lib_rdflush(FAR FILE *stream)
 {
   if (!stream)
     {
-      *get_errno_ptr() = EBADF;
+      set_errno(EBADF);
       return ERROR;
     }
 
diff --git a/lib/stdio/lib_ungetc.c b/lib/stdio/lib_ungetc.c
index 9ad72b394e4f5c769cced5a916bfe879e7722000..e27428fa441e88fb1bc989e913abbd12f8f674e7 100644
--- a/lib/stdio/lib_ungetc.c
+++ b/lib/stdio/lib_ungetc.c
@@ -99,7 +99,7 @@ int ungetc(int c, FAR FILE *stream)
   if ((stream && stream->fs_filedes < 0) ||
       ((stream->fs_oflags & O_RDOK) == 0))
     {
-      errno = EBADF;
+      set_errno(EBADF);
       return EOF;
     }
 
@@ -114,7 +114,7 @@ int ungetc(int c, FAR FILE *stream)
   else
 #endif
     {
-      errno = ENOMEM;
+      set_errno(ENOMEM);
       return EOF;
     }
 }
diff --git a/lib/string/lib_strtod.c b/lib/string/lib_strtod.c
index 26480f9255dc9bef0cc9aa066054eec0d8ba2638..86719c5105b249af9e03dc7c0c17edfe63c5c08b 100755
--- a/lib/string/lib_strtod.c
+++ b/lib/string/lib_strtod.c
@@ -145,7 +145,7 @@ double_t strtod(const char *str, char **endptr)
 
   if (num_digits == 0)
     {
-      errno = ERANGE;
+      set_errno(ERANGE);
       return 0.0;
     }
 
@@ -193,7 +193,7 @@ double_t strtod(const char *str, char **endptr)
   if (exponent < __DBL_MIN_EXP__ ||
       exponent > __DBL_MAX_EXP__)
     {
-      errno = ERANGE;
+      set_errno(ERANGE);
       return infinite;
     }
 
@@ -219,8 +219,15 @@ double_t strtod(const char *str, char **endptr)
       p10 *= p10;
     }
 
-  if (!is_real(number)) errno = ERANGE;
-  if (endptr) *endptr = p;
+  if (!is_real(number))
+    {
+      set_errno(ERANGE);
+    }
+
+  if (endptr)
+    {
+      *endptr = p;
+    }
 
   return number;
 }
diff --git a/lib/unistd/lib_chdir.c b/lib/unistd/lib_chdir.c
index 15dc643835a9dd273c330a04a1e7a5d10d4b29f6..c054f2b23496844a895f7af191b8e309757435cd 100644
--- a/lib/unistd/lib_chdir.c
+++ b/lib/unistd/lib_chdir.c
@@ -173,7 +173,7 @@ int chdir(FAR const char *path)
   return OK;
 
 errout:
-  errno = err;
+  set_errno(err);
   return ERROR;
 }
 #endif /* CONFIG_NFILE_DESCRIPTORS && !CONFIG_DISABLE_ENVIRON */
diff --git a/lib/unistd/lib_getcwd.c b/lib/unistd/lib_getcwd.c
index 6888470ac44d8fca791c6d248dfe239fa4733084..9bfa102fb633a87085a683f7c9ecd6e17518569b 100644
--- a/lib/unistd/lib_getcwd.c
+++ b/lib/unistd/lib_getcwd.c
@@ -99,11 +99,13 @@ FAR char *getcwd(FAR char *buf, size_t size)
 
   /* Verify input parameters */
 
+#ifdef CONFIG_DEBUG
   if (!buf || !size)
     {
-      errno = EINVAL;
+      set_errno(EINVAL);
       return NULL;
     }
+#endif
 
   /* If no working directory is defined, then default to the home directory */
 
@@ -117,7 +119,7 @@ FAR char *getcwd(FAR char *buf, size_t size)
 
   if (strlen(pwd) + 1 > size)
     {
-      errno = ERANGE;
+      set_errno(ERANGE);
       return NULL;
     }
 
diff --git a/sched/Makefile b/sched/Makefile
index 8b8b33a5878166bed4ff464d6796fa30e17295ec..86e7c0b76e4f9f4bec9151a45243d9a7d24bd5c7 100644
--- a/sched/Makefile
+++ b/sched/Makefile
@@ -38,10 +38,10 @@
 ASRCS		= 
 AOBJS		= $(ASRCS:.S=$(OBJEXT))
 
-MISC_SRCS	= os_start.c os_bringup.c get_errno_ptr.c sched_garbage.c \
-		  sched_setupstreams.c sched_getfiles.c sched_getsockets.c sched_getstreams.c \
-		  sched_setupidlefiles.c sched_setuptaskfiles.c sched_setuppthreadfiles.c \
-		  sched_releasefiles.c
+MISC_SRCS	= os_start.c os_bringup.c errno_getptr.c errno_get.c errno_set.c \
+		  sched_garbage.c sched_setupstreams.c sched_getfiles.c sched_getsockets.c \
+		  sched_getstreams.c sched_setupidlefiles.c sched_setuptaskfiles.c \
+		  sched_setuppthreadfiles.c sched_releasefiles.c
 
 TSK_SRCS	= task_create.c task_init.c task_setup.c task_activate.c \
 		  task_start.c task_delete.c task_deletecurrent.c task_restart.c \
@@ -80,12 +80,10 @@ CLOCK_SRCS	= clock_initialize.c clock_settime.c clock_gettime.c clock_getres.c \
 SIGNAL_SRCS	= sig_initialize.c \
 		  sig_action.c sig_procmask.c sig_pending.c sig_suspend.c \
 		  sig_kill.c sig_queue.c sig_waitinfo.c sig_timedwait.c \
-		  sig_emptyset.c sig_fillset.c sig_addset.c sig_delset.c \
-		  sig_ismember.c sig_findaction.c \
-		  sig_allocatependingsigaction.c sig_releasependingsigaction.c \
-                  sig_unmaskpendingsignal.c sig_removependingsignal.c \
-		  sig_releasependingsignal.c sig_lowest.c sig_mqnotempty.c \
-		  sig_cleanup.c sig_received.c sig_deliver.c
+		  sig_findaction.c sig_allocatependingsigaction.c \
+		  sig_releasependingsigaction.c sig_unmaskpendingsignal.c \
+		  sig_removependingsignal.c sig_releasependingsignal.c sig_lowest.c \
+		  sig_mqnotempty.c sig_cleanup.c sig_received.c sig_deliver.c
 
 MQUEUE_SRCS	= mq_open.c mq_close.c mq_unlink.c mq_send.c mq_timedsend.c\
 		  mq_sndinternal.c mq_receive.c mq_timedreceive.c mq_rcvinternal.c \
diff --git a/sched/errno_get.c b/sched/errno_get.c
new file mode 100644
index 0000000000000000000000000000000000000000..7461a42c708f752d2e2b07088275dd15d693f867
--- /dev/null
+++ b/sched/errno_get.c
@@ -0,0 +1,84 @@
+/****************************************************************************
+ * sched/errno_get.c
+ *
+ *   Copyright (C) 2011 Gregory Nutt. All rights reserved.
+ *   Author: Gregory Nutt <spudmonkey@racsa.co.cr>
+ *
+ * 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 NuttX 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 THE
+ * COPYRIGHT OWNER 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.
+ *
+ ****************************************************************************/
+
+/****************************************************************************
+ * Included Files
+ ****************************************************************************/
+
+#include <nuttx/config.h>
+
+#include <errno.h>
+
+/****************************************************************************
+ * Pre-processor Definitions
+ ****************************************************************************/
+
+#undef get_errno_ptr
+#undef get_errno
+#undef errno
+
+/****************************************************************************
+ * Private Data
+ ****************************************************************************/
+
+/****************************************************************************
+ * Public Functions
+ ****************************************************************************/
+
+/****************************************************************************
+ * Function:  get_errno
+ *
+ * Description:
+ *   Return the value of the thread specific errno.  This function is only
+ *   intended to provide a mechanism for user-mode programs to get the
+ *   thread-specific errno value.  It is #define'd to the symbol errno in
+ *   include/errno.h.
+ *
+ * Parameters:
+ *   None
+ *
+ * Return Value:
+ *   The current value of the thread specific errno.
+ *
+ * Assumptions:
+ *
+ ****************************************************************************/
+
+int get_errno(void)
+{
+  return *get_errno_ptr();
+}
+
+
diff --git a/sched/get_errno_ptr.c b/sched/errno_getptr.c
similarity index 93%
rename from sched/get_errno_ptr.c
rename to sched/errno_getptr.c
index ebf2e105c341d7a3555540337acae3802fa0b179..8daf5b2cc9df77ebe3537956fe5244d0efc2d70b 100644
--- a/sched/get_errno_ptr.c
+++ b/sched/errno_getptr.c
@@ -1,7 +1,7 @@
 /****************************************************************************
- * sched/get_errno_ptr.c
+ * sched/errno_getptr.c
  *
- *   Copyright (C) 2007, 2008 Gregory Nutt. All rights reserved.
+ *   Copyright (C) 2007, 2008, 2011 Gregory Nutt. All rights reserved.
  *   Author: Gregory Nutt <spudmonkey@racsa.co.cr>
  *
  * Redistribution and use in source and binary forms, with or without
@@ -43,6 +43,10 @@
 #include <nuttx/arch.h>
 #include "os_internal.h"
 
+/****************************************************************************
+ * Pre-processor Definitions
+ ****************************************************************************/
+
 #undef get_errno_ptr
 #undef errno
 
diff --git a/sched/errno_set.c b/sched/errno_set.c
new file mode 100644
index 0000000000000000000000000000000000000000..2bf01d9b1c378850edaba280ae6b42a7c0f1c378
--- /dev/null
+++ b/sched/errno_set.c
@@ -0,0 +1,83 @@
+/****************************************************************************
+ * sched/errno_set.c
+ *
+ *   Copyright (C) 2011 Gregory Nutt. All rights reserved.
+ *   Author: Gregory Nutt <spudmonkey@racsa.co.cr>
+ *
+ * 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 NuttX 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 THE
+ * COPYRIGHT OWNER 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.
+ *
+ ****************************************************************************/
+
+/****************************************************************************
+ * Included Files
+ ****************************************************************************/
+
+#include <nuttx/config.h>
+
+#include <errno.h>
+
+/****************************************************************************
+ * Pre-processor Definitions
+ ****************************************************************************/
+
+#undef get_errno_ptr
+#undef set_errno
+#undef errno
+
+/****************************************************************************
+ * Private Data
+ ****************************************************************************/
+
+/****************************************************************************
+ * Public Functions
+ ****************************************************************************/
+
+/****************************************************************************
+ * Function:  set_errno
+ *
+ * Description:
+ *   Set the value of the thread specific errno.  This function is only
+ *   intended to provide a mechanism for user-mode programs to set the
+ *   thread-specific errno value.
+ *
+ * Parameters:
+ *   errcode - The thread specific errno will be set to this error code value.
+ *
+ * Return Value:
+ *   None
+ *
+ * Assumptions:
+ *
+ ****************************************************************************/
+
+void set_errno(int errcode)
+{
+  *get_errno_ptr() = errcode;
+}
+
+
diff --git a/sched/sched_lockcount.c b/sched/sched_lockcount.c
index 21998f6b3e024006237ad3b3166ea03b5e4221f0..6ac3a9deed7ac31470ed849d2f4bc77a59aad4ed 100644
--- a/sched/sched_lockcount.c
+++ b/sched/sched_lockcount.c
@@ -1,7 +1,7 @@
 /************************************************************************
  * sched/sched_lockcount.c
  *
- *   Copyright (C) 2007, 2009 Gregory Nutt. All rights reserved.
+ *   Copyright (C) 2007, 2009, 2011 Gregory Nutt. All rights reserved.
  *   Author: Gregory Nutt <spudmonkey@racsa.co.cr>
  *
  * Redistribution and use in source and binary forms, with or without
@@ -75,10 +75,11 @@
  * Name: sched_lockcount
  *
  * Description:
- *   This function returns the current value of the lockcount.
- *   If zero, preemption is enabled; if non-zero, this value
- *   indicates the number of times that osTask() has been
- *   called on this thread of execution.
+ *   This function returns the current value of the lockcount. If zero,
+ *   pre-emption is enabled; if non-zero, this value indicates the number
+ *   of times that sched_lock() has been called on this thread of
+ *   execution.  sched_unlock() will have to called that many times from
+ *   this thread in order to re-enable pre-emption.
  *
  * Inputs:
  *   None
@@ -88,8 +89,9 @@
  *
  ************************************************************************/
 
-int32_t sched_lockcount(void)
+int sched_lockcount(void)
 {
    _TCB *rtcb = (_TCB*)g_readytorun.head;
-   return (int32_t)rtcb->lockcount;
+   return (int)rtcb->lockcount;
 }
+
diff --git a/syscall/Makefile b/syscall/Makefile
index b38518bfbc64250f87943d778df33df1745c750e..89d079e5ce29a9a8560f5082719e1b4d2377962a 100644
--- a/syscall/Makefile
+++ b/syscall/Makefile
@@ -97,7 +97,7 @@ context: .context
 
 clean:
 	@rm -f $(BIN1) $(BIN2) *~ .*.swp
-ifneq ($(OBJECT),)
+ifneq ($(OBJEXT),)
 	@rm -f proxies/*$(OBJEXT) stubs/*$(OBJEXT)
 endif
 	$(call CLEAN)
diff --git a/syscall/README.txt b/syscall/README.txt
index 08d0413466499626e9c74a30f293e68638e666c7..7c45207f868901bcdcf10a6f6ad974ac9b6c4598 100644
--- a/syscall/README.txt
+++ b/syscall/README.txt
@@ -90,7 +90,23 @@ The format of the CVS file for each line is:
   Field 2: The header file that contains the function prototype
   Field 3: Condition for compilation
   Field 4: The type of function return value.
-  Field 5 - N+5: The type of each of the N formal parameters of the function
+  Field 5 - N+5: The type of each of the N formal parameters of the function
+
+Each type field has a format as follows:
+
+  type name:
+        For all simpler types
+  formal type | actual type: 
+        For array types where the form of the formal (eg. int parm[2])
+        differs from the type of actual passed parameter (eg. int*).  This
+        is necessary because you cannot do simple casts to array types.
+  formal type | union member actual type | union member fieldname:
+        A similar situation exists for unions.  For example, the formal
+        parameter type union sigval -- You cannot cast a uintptr_t to
+        a union sigval, but you can cast to the type of one of the union
+        member types when passing the actua paramter.  Similarly, we
+        cannot cast a union sigval to a uinptr_t either.  Rather, we need
+        to cast a specific union member fieldname to uintptr_t.
 
 Auto-Generated Files
 ====================
@@ -104,7 +120,7 @@ database.  Here the following definition is used:
           call into a syscall, marshalling all of the system call parameters
           as necessary.
 
-  STUB  - Another tiny bit of code that executes within the NuttX kernel
+  Stub  - Another tiny bit of code that executes within the NuttX kernel
           that is used to map a software interrupt received by the kernel to
           a kernel function call. The stubs receive the marshalled system
           call data, and perform the actually kernel function call (in
diff --git a/syscall/syscall.csv b/syscall/syscall.csv
index d0cf3eb1694a522eacb354aa8791b5e6e5037a5c..44920d015d73647076f0187450a4beae2555bdd1 100644
--- a/syscall/syscall.csv
+++ b/syscall/syscall.csv
@@ -9,7 +9,6 @@
 "close","unistd.h","CONFIG_NSOCKET_DESCRIPTORS > 0 || CONFIG_NFILE_DESCRIPTORS > 0","int","int"
 "closedir","dirent.h","CONFIG_NFILE_DESCRIPTORS > 0","int","FAR DIR*"
 "connect","sys/socket.h","CONFIG_NSOCKET_DESCRIPTORS > 0 && defined(CONFIG_NET)","int","int","FAR const struct sockaddr*","socklen_t"
-"creat","fcntl.h","CONFIG_NSOCKET_DESCRIPTORS > 0 && defined(CONFIG_NET)","int","const char*","mode_t"
 "dup","unistd.h","CONFIG_NFILE_DESCRIPTORS > 0","int","int"
 "dup2","unistd.h","CONFIG_NFILE_DESCRIPTORS > 0","int","int","int"
 "exit","stdlib.h","","void","int"
@@ -79,7 +78,6 @@
 "putenv","stdlib.h","!defined(CONFIG_DISABLE_ENVIRON)","int","FAR const char*"
 "read","unistd.h","CONFIG_NSOCKET_DESCRIPTORS > 0 || CONFIG_NFILE_DESCRIPTORS > 0","ssize_t","int","FAR void*","size_t"
 "readdir","dirent.h","CONFIG_NFILE_DESCRIPTORS > 0","FAR struct dirent*","FAR DIR*"
-"readdir_r","dirent.h","CONFIG_NFILE_DESCRIPTORS > 0","int","FAR DIR*","FAR struct dirent*","FAR struct dirent**"
 "recv","sys/socket.h","CONFIG_NSOCKET_DESCRIPTORS > 0 && defined(CONFIG_NET)","ssize_t","int","FAR void*","size_t","int"
 "recvfrom","sys/socket.h","CONFIG_NSOCKET_DESCRIPTORS > 0 && defined(CONFIG_NET)","ssize_t","int","FAR void*","size_t","int","FAR struct sockaddr*","FAR socklen_t*"
 "rename","stdio.h","CONFIG_NFILE_DESCRIPTORS > 0 && !defined(CONFIG_DISABLE_MOUNTPOINT)","int","FAR const char*","FAR const char*"
@@ -89,9 +87,6 @@
 "sched_getscheduler","sched.h","","int","pid_t"
 "sched_lock","sched.h","","int"
 "sched_lockcount","sched.h","","int32_t"
-"sched_note_start","sched.h","","void","FAR _TCB* "
-"sched_note_stop","sched.h","","void","FAR _TCB* "
-"sched_note_switch","sched.h","","void","FAR _TCB*","FAR _TCB*"
 "sched_rr_get_interval","sched.h","","int","pid_t","struct timespec*"
 "sched_setparam","sched.h","","int","pid_t","const struct sched_param*"
 "sched_setscheduler","sched.h","","int","pid_t","int","const struct sched_param*"
@@ -112,30 +107,20 @@
 "setenv","stdlib.h","","int","const char*","const char*","int"
 "setsockopt","sys/socket.h","CONFIG_NSOCKET_DESCRIPTORS > 0 && defined(CONFIG_NET)","int","int","int","int","FAR const void*","socklen_t"
 "sigaction","signal.h","!defined(CONFIG_DISABLE_SIGNALS)","int","int","FAR const struct sigaction*","FAR struct sigaction*"
-"sigaddset","signal.h","!defined(CONFIG_DISABLE_SIGNALS)","int","FAR sigset_t*","int"
-"sigdelset","signal.h","!defined(CONFIG_DISABLE_SIGNALS)","int","FAR sigset_t*","int"
-"sigemptyset","signal.h","!defined(CONFIG_DISABLE_SIGNALS)","int","FAR sigset_t*"
-"sigfillset","signal.h","!defined(CONFIG_DISABLE_SIGNALS)","int","FAR sigset_t*"
-"sigismember","signal.h","!defined(CONFIG_DISABLE_SIGNALS)","int","FAR const sigset_t*","int"
 "sigpending","signal.h","!defined(CONFIG_DISABLE_SIGNALS)","int","FAR sigset_t*"
 "sigprocmask","signal.h","!defined(CONFIG_DISABLE_SIGNALS)","int","int","FAR const sigset_t*","FAR sigset_t*"
-"sigqueue","signal.h","!defined(CONFIG_DISABLE_SIGNALS)","int","int","int","FAR void*"
-"sigqueue","signal.h","!defined(CONFIG_DISABLE_SIGNALS)","int","int","int","union sigval|FAR void *"
+"sigqueue","signal.h","!defined(CONFIG_DISABLE_SIGNALS)","int","int","int","union sigval|FAR void *|sival_ptr"
 "sigsuspend","signal.h","!defined(CONFIG_DISABLE_SIGNALS)","int","FAR const sigset_t*"
 "sigtimedwait","signal.h","!defined(CONFIG_DISABLE_SIGNALS)","int","FAR const sigset_t*","FAR struct siginfo*","FAR const struct timespec*"
 "sigwaitinfo","signal.h","!defined(CONFIG_DISABLE_SIGNALS)","int","FAR const sigset_t*","FAR struct siginfo*"
 "sleep","unistd.h","!defined(CONFIG_DISABLE_SIGNALS)","unsigned int","unsigned int"
-"snprintf","stdio.h","","int","FAR char*","size_t","const char*","..."
 "socket","sys/socket.h","","int","int","int","int"
 "stat","sys/stat.h","CONFIG_NFILE_DESCRIPTORS > 0","int","const char*","FAR struct stat*"
 #"statfs","stdio.h","","int","FAR const char*","FAR struct statfs*"
 "statfs","sys/statfs.h","CONFIG_NFILE_DESCRIPTORS > 0","int","const char*","struct statfs*"
-"task_activate","sched.h","","int","FAR _TCB*"
 "task_create","sched.h","","int","const char*","int","int","main_t","const char* []|const char**"
 #"task_create","sched.h","","int","const char*","int","main_t","const char* []|const char**"
 "task_delete","sched.h","","int","pid_t"
-"task_init","sched.h","","int","FAR _TCB*","const char*","int","FAR uint32_t*","uint32_t","main_t","const char* []|const char**"
-#"task_init","sched.h","","int","FAR _TCB*","const char*","int","main_t","const char* []|const char**"
 "task_restart","sched.h","","int","pid_t"
 "telldir","dirent.h","CONFIG_NFILE_DESCRIPTORS > 0","off_t","FAR DIR*"
 "timer_create","time.h","!defined(CONFIG_DISABLE_POSIX_TIMERS)","int","clockid_t","FAR struct sigevent*","FAR timer_t*"
diff --git a/tools/mksyscall.c b/tools/mksyscall.c
index f65a4f4891f4a302fbca6940d9f2b017de8c13f5..a8f2cf99b704e60f92597937dd1a7acd0cfe1664 100644
--- a/tools/mksyscall.c
+++ b/tools/mksyscall.c
@@ -274,35 +274,69 @@ static void print_formalparm(FILE *stream, const char *argtype, int parmno)
 
 static void get_formalparmtype(const char *arg, char *formal)
 {
-  char *ptr = strchr(arg,'|');
-  if (ptr)
-    {
-      /* The formal parm type is a pointer to everything up to the '|' */
+  /* The formal parm type is a pointer to everything up to the first'|' (or
+   * the end of the string if there is no '|' in the type description).
+   */
 
-      while (*arg != '|')
-        {
-          *formal++ = *arg++;
-        }
-      *formal   = '\0';
-    }
-  else
+  while (*arg != '|' && *arg != '\0')
     {
-      strncpy(formal, arg, MAX_PARMSIZE);
+      *formal++ = *arg++;
     }
+  *formal   = '\0';
 }
 
 static void get_actualparmtype(const char *arg, char *actual)
 {
-  char *ptr = strchr(arg,'|');
-  if (ptr)
+  const char *pstart = strchr(arg,'|');
+  if (pstart)
     {
-      ptr++;
-      strncpy(actual, ptr, MAX_PARMSIZE);
+      /* The actual parameter type starts after the '|' */
+
+      pstart++;
     }
   else
     {
-      strncpy(actual, arg, MAX_PARMSIZE);
+      /* The actual parameter is the same as the formal parameter
+       * at starts at the beginning of the type string.
+       */
+
+      pstart = arg;
     }
+
+  /* The actual parm type is a pointer to everything up to the next '|' (or
+   * the end of the string if there is no '|' in the type description).
+   */
+
+  while (*pstart != '|' && *pstart != '\0')
+    {
+      *actual++ = *pstart++;
+    }
+  *actual   = '\0';
+}
+
+static void get_fieldname(const char *arg, char *fieldname)
+{
+  char *pactual = strchr(arg,'|');
+  char *pstart;
+
+  if (pactual)
+    {
+      /* The actual parameter type starts after the '|' */
+
+      pactual++;
+      pstart = strchr(pactual,'|');
+      if (pstart)
+        {
+          /* The fieldname is everything past the second '|' to the end of the string */
+
+          pstart++;
+          strncpy(fieldname, pstart, MAX_PARMSIZE);
+          return;
+        }
+    }
+
+  fprintf(stderr, "%d: Missing union fieldname: %s\n", g_lineno, arg);
+  exit(15);
 }
 
 static FILE *open_proxy(void)
@@ -326,6 +360,7 @@ static void generate_proxy(int nparms)
 {
   FILE *stream = open_proxy();
   char formal[MAX_PARMSIZE];
+  char fieldname[MAX_PARMSIZE];
   bool bvarargs = false;
   int nformal;
   int nactual;
@@ -444,7 +479,22 @@ static void generate_proxy(int nparms)
 
   for (i = 0; i < nactual; i++)
     {
-      fprintf(stream, ", (uintptr_t)parm%d", i+1);
+      /* Is the parameter a union member */
+
+      if (i < nparms && is_union(g_parm[PARM1_INDEX+i]))
+        {
+          /* Then we will have to pick a field name that can be cast to a
+           * uintptr_t.  There probably should be some error handling here
+           * to catch the case where the fieldname was not supplied.
+           */
+
+          get_fieldname(g_parm[PARM1_INDEX+i], fieldname);
+          fprintf(stream, ", (uintptr_t)parm%d.%s", i+1, fieldname);          
+        }
+      else
+        {
+          fprintf(stream, ", (uintptr_t)parm%d", i+1);
+        }
     }
 
   /* Handle the tail end of the function. */