Index: VERSION
===================================================================
--- VERSION	(revision 6c980128774e4359b32d7325a987283c0635869f)
+++ VERSION	(revision cca44008d81209e9fa992157637d9de0384e0536)
@@ -3,3 +3,3 @@
 #
 
-RTEMS Version 19981203
+RTEMS Version 4.0.0
Index: c/src/exec/include/rtems/libio_.h
===================================================================
--- c/src/exec/include/rtems/libio_.h	(revision 6c980128774e4359b32d7325a987283c0635869f)
+++ c/src/exec/include/rtems/libio_.h	(revision cca44008d81209e9fa992157637d9de0384e0536)
@@ -66,4 +66,5 @@
 extern rtems_libio_t  *rtems_libio_iops;
 extern rtems_libio_t  *rtems_libio_last_iop;
+extern rtems_libio_t *rtems_libio_iop_freelist;
 
 /*
Index: c/src/exec/libcsupport/include/rtems/libio.h
===================================================================
--- c/src/exec/libcsupport/include/rtems/libio.h	(revision 6c980128774e4359b32d7325a987283c0635869f)
+++ c/src/exec/libcsupport/include/rtems/libio.h	(revision cca44008d81209e9fa992157637d9de0384e0536)
@@ -394,9 +394,13 @@
 #define LIBIO_FLAGS_READ          0x0002  /* reading */
 #define LIBIO_FLAGS_WRITE         0x0004  /* writing */
-#define LIBIO_FLAGS_LINE_BUFFERED 0x0008  /* line buffered io (^h, ^u, etc) */
 #define LIBIO_FLAGS_OPEN          0x0100  /* device is open */
 #define LIBIO_FLAGS_APPEND        0x0200  /* all writes append */
 #define LIBIO_FLAGS_CREATE        0x0400  /* create file */
 #define LIBIO_FLAGS_CLOSE_ON_EXEC 0x0800  /* close on process exec() */
+
+#define LIBIO_FLAGS_HANDLER_SHIFT 12
+#define LIBIO_FLAGS_HANDLER_MASK  0xF000  /* mask for external handler type */
+#define LIBIO_FLAGS_HANDLER_RTEMS 0x0000  /* `traditional' RTEMS I/O */
+#define LIBIO_FLAGS_HANDLER_SOCK  0x1000  /* BSD socket */
 
 #define LIBIO_FLAGS_READ_WRITE    (LIBIO_FLAGS_READ | LIBIO_FLAGS_WRITE)
Index: c/src/exec/libcsupport/include/rtems/libio_.h
===================================================================
--- c/src/exec/libcsupport/include/rtems/libio_.h	(revision 6c980128774e4359b32d7325a987283c0635869f)
+++ c/src/exec/libcsupport/include/rtems/libio_.h	(revision cca44008d81209e9fa992157637d9de0384e0536)
@@ -66,4 +66,5 @@
 extern rtems_libio_t  *rtems_libio_iops;
 extern rtems_libio_t  *rtems_libio_last_iop;
+extern rtems_libio_t *rtems_libio_iop_freelist;
 
 /*
Index: c/src/exec/libcsupport/include/sys/sockio.h
===================================================================
--- c/src/exec/libcsupport/include/sys/sockio.h	(revision 6c980128774e4359b32d7325a987283c0635869f)
+++ c/src/exec/libcsupport/include/sys/sockio.h	(revision cca44008d81209e9fa992157637d9de0384e0536)
@@ -84,10 +84,9 @@
 #define	SIOCGIFMEDIA	_IOWR('i', 56, struct ifmediareq) /* get net media */
 
-
 /*
  * RTEMS additions for setting/getting `tap' function on incoming packets.
  */
-#define SIOCSIFTAP	 _IOW('i', 80, struct ifreq)	/* set tap function */
-#define SIOCGIFTAP	_IOWR('i', 81, struct ifreq)	/* get tap function */
+#define SIOCSIFTAP	_IOW('i', 80, struct ifreq)	/* set tap function */
+#define SIOCGIFTAP	_IOW('i', 81, struct ifreq)	/* get tap function */
 
 #endif /* !_SYS_SOCKIO_H_ */
Index: c/src/exec/libcsupport/src/close.c
===================================================================
--- c/src/exec/libcsupport/src/close.c	(revision 6c980128774e4359b32d7325a987283c0635869f)
+++ c/src/exec/libcsupport/src/close.c	(revision cca44008d81209e9fa992157637d9de0384e0536)
@@ -23,8 +23,11 @@
   int                 status;
 
-  if ( rtems_file_descriptor_type( fd ) ) {
+  rtems_libio_check_fd(fd);
+  iop = rtems_libio_iop(fd);
+  if ( iop->flags & LIBIO_FLAGS_HANDLER_MASK ) {
     int (*fp)(int  fd);
 
-    fp = rtems_libio_handlers[rtems_file_descriptor_type_index(fd)].close;
+    fp = rtems_libio_handlers[
+            (iop->flags >> LIBIO_FLAGS_HANDLER_SHIFT) - 1].close;
     if ( fp == NULL )
       set_errno_and_return_minus_one( EBADF );
@@ -32,6 +35,4 @@
     return status;
   }
-  iop = rtems_libio_iop(fd);
-  rtems_libio_check_fd(fd);
 
   if ( !iop->handlers )
Index: c/src/exec/libcsupport/src/fchmod.c
===================================================================
--- c/src/exec/libcsupport/src/fchmod.c	(revision 6c980128774e4359b32d7325a987283c0635869f)
+++ c/src/exec/libcsupport/src/fchmod.c	(revision cca44008d81209e9fa992157637d9de0384e0536)
@@ -28,9 +28,12 @@
   rtems_libio_t *iop;
   
+  rtems_libio_check_fd( fd );
+  iop = rtems_libio_iop( fd );
+
   /*
    *  If this is not a file system based entity, it is an error.
    */
 
-  if ( rtems_file_descriptor_type( fd ) )
+  if ( iop->flags & LIBIO_FLAGS_HANDLER_MASK )
     set_errno_and_return_minus_one( EBADF );
 
@@ -39,6 +42,4 @@
    */
 
-  iop = rtems_libio_iop( fd );
-  rtems_libio_check_fd( fd );
   rtems_libio_check_permissions( iop, LIBIO_FLAGS_WRITE );
 
Index: c/src/exec/libcsupport/src/fcntl.c
===================================================================
--- c/src/exec/libcsupport/src/fcntl.c	(revision 6c980128774e4359b32d7325a987283c0635869f)
+++ c/src/exec/libcsupport/src/fcntl.c	(revision cca44008d81209e9fa992157637d9de0384e0536)
@@ -33,9 +33,12 @@
   va_start( ap, cmd );
 
+  rtems_libio_check_fd( fd );
+  iop = rtems_libio_iop( fd );
+
   /*
    *  If this is not a file system based entity, it is an error.
    */
 
-  if ( rtems_file_descriptor_type( fd ) )
+  if ( iop->flags & LIBIO_FLAGS_HANDLER_MASK )
     set_errno_and_return_minus_one( EBADF );
 
@@ -43,7 +46,4 @@
    *  Now process the fcntl().
    */
-
-  iop = rtems_libio_iop( fd );
-  rtems_libio_check_fd( fd );
 
   /*
Index: c/src/exec/libcsupport/src/fdatasync.c
===================================================================
--- c/src/exec/libcsupport/src/fdatasync.c	(revision 6c980128774e4359b32d7325a987283c0635869f)
+++ c/src/exec/libcsupport/src/fdatasync.c	(revision cca44008d81209e9fa992157637d9de0384e0536)
@@ -23,4 +23,8 @@
   rtems_libio_t *iop;
 
+  rtems_libio_check_fd( fd );
+  iop = rtems_libio_iop( fd );
+  rtems_libio_check_permissions( iop, LIBIO_FLAGS_WRITE );
+
   /*
    *  If this file descriptor is mapped to an external set of handlers,
@@ -28,5 +32,5 @@
    */
 
-  if ( rtems_file_descriptor_type( fd ) )
+  if ( iop->flags & LIBIO_FLAGS_HANDLER_MASK )
     set_errno_and_return_minus_one( EBADF );
 
@@ -35,8 +39,4 @@
    */
 
-  iop = rtems_libio_iop( fd );
-  rtems_libio_check_fd( fd );
-  rtems_libio_check_permissions( iop, LIBIO_FLAGS_WRITE );
-
   if ( !iop->handlers->fdatasync )
     set_errno_and_return_minus_one( ENOTSUP );
Index: c/src/exec/libcsupport/src/fpathconf.c
===================================================================
--- c/src/exec/libcsupport/src/fpathconf.c	(revision 6c980128774e4359b32d7325a987283c0635869f)
+++ c/src/exec/libcsupport/src/fpathconf.c	(revision cca44008d81209e9fa992157637d9de0384e0536)
@@ -27,4 +27,8 @@
   rtems_filesystem_limits_and_options_t  *the_limits;
 
+  rtems_libio_check_fd(fd);
+  iop = rtems_libio_iop(fd);
+  rtems_libio_check_permissions(iop, LIBIO_FLAGS_READ);
+
   /*
    *  If this file descriptor is mapped to an external set of handlers,
@@ -33,5 +37,5 @@
    */
 
-  if ( rtems_file_descriptor_type( fd ) )
+  if ( iop->flags & LIBIO_FLAGS_HANDLER_MASK )
     set_errno_and_return_minus_one( EBADF );
 
@@ -39,8 +43,4 @@
    *  Now process the information request.
    */
-
-  iop = rtems_libio_iop(fd);
-  rtems_libio_check_fd(fd);
-  rtems_libio_check_permissions(iop, LIBIO_FLAGS_READ);
 
   the_limits = &iop->pathinfo.mt_entry->pathconf_limits_and_options;
Index: c/src/exec/libcsupport/src/fsync.c
===================================================================
--- c/src/exec/libcsupport/src/fsync.c	(revision 6c980128774e4359b32d7325a987283c0635869f)
+++ c/src/exec/libcsupport/src/fsync.c	(revision cca44008d81209e9fa992157637d9de0384e0536)
@@ -23,4 +23,8 @@
   rtems_libio_t *iop;
 
+  rtems_libio_check_fd( fd );
+  iop = rtems_libio_iop( fd );
+  rtems_libio_check_permissions( iop, LIBIO_FLAGS_WRITE );
+
   /*
    *  If this file descriptor is mapped to an external set of handlers,
@@ -28,5 +32,5 @@
    */
 
-  if ( rtems_file_descriptor_type( fd ) )
+  if ( iop->flags & LIBIO_FLAGS_HANDLER_MASK )
     set_errno_and_return_minus_one( EBADF );
 
@@ -35,8 +39,4 @@
    */
 
-  iop = rtems_libio_iop( fd );
-  rtems_libio_check_fd( fd );
-  rtems_libio_check_permissions( iop, LIBIO_FLAGS_WRITE );
-
   if ( !iop->handlers->fsync )
     set_errno_and_return_minus_one( ENOTSUP );
Index: c/src/exec/libcsupport/src/ftruncate.c
===================================================================
--- c/src/exec/libcsupport/src/ftruncate.c	(revision 6c980128774e4359b32d7325a987283c0635869f)
+++ c/src/exec/libcsupport/src/ftruncate.c	(revision cca44008d81209e9fa992157637d9de0384e0536)
@@ -26,9 +26,12 @@
   rtems_filesystem_location_info_t  loc;
   
+  rtems_libio_check_fd( fd );
+  iop = rtems_libio_iop( fd );
+
   /*
    *  If this is not a file system based entity, it is an error.
    */
 
-  if ( rtems_file_descriptor_type( fd ) )
+  if ( iop->flags & LIBIO_FLAGS_HANDLER_MASK )
     set_errno_and_return_minus_one( EBADF );
 
@@ -37,6 +40,4 @@
    */
   
-  iop = rtems_libio_iop( fd );
-
   /*
    *  Make sure we are not working on a directory
@@ -50,5 +51,4 @@
     set_errno_and_return_minus_one( EISDIR );
 
-  rtems_libio_check_fd( fd );
   rtems_libio_check_permissions( iop, LIBIO_FLAGS_WRITE );
 
Index: c/src/exec/libcsupport/src/ioctl.c
===================================================================
--- c/src/exec/libcsupport/src/ioctl.c	(revision 6c980128774e4359b32d7325a987283c0635869f)
+++ c/src/exec/libcsupport/src/ioctl.c	(revision cca44008d81209e9fa992157637d9de0384e0536)
@@ -27,4 +27,7 @@
   rtems_libio_t     *iop;
 
+  rtems_libio_check_fd( fd );
+  iop = rtems_libio_iop( fd );
+
   /*
    *  If this file descriptor is mapped to an external set of handlers,
@@ -32,8 +35,9 @@
    */ 
 
-  if ( rtems_file_descriptor_type( fd ) ) {
+  if ( iop->flags & LIBIO_FLAGS_HANDLER_MASK ) {
     rtems_libio_ioctl_t fp;
 
-    fp = rtems_libio_handlers[rtems_file_descriptor_type_index(fd)].ioctl;
+    fp = rtems_libio_handlers[
+           (iop->flags >> LIBIO_FLAGS_HANDLER_SHIFT) - 1].ioctl;
     if ( fp == NULL )
       set_errno_and_return_minus_one( EBADF );
@@ -46,7 +50,4 @@
    */
 
-  iop = rtems_libio_iop( fd );
-  rtems_libio_check_fd( fd );
-
   if ( !iop->handlers->ioctl )
     set_errno_and_return_minus_one( ENOTSUP );
Index: c/src/exec/libcsupport/src/libio.c
===================================================================
--- c/src/exec/libcsupport/src/libio.c	(revision 6c980128774e4359b32d7325a987283c0635869f)
+++ c/src/exec/libcsupport/src/libio.c	(revision cca44008d81209e9fa992157637d9de0384e0536)
@@ -4,7 +4,4 @@
  *  POSIX system calls like open(), read, fstat(), etc.
  *
- *  This provides the foundation for POSIX compliant IO system calls
- *  for RTEMS.
- *
  *  COPYRIGHT (c) 1989-1998.
  *  On-Line Applications Research Corporation (OAR).
@@ -19,14 +16,52 @@
 
 #include "libio_.h"                   /* libio_.h pulls in rtems */
-
-/*
- *  Global variables used to manage the File Descriptor Table.
- *  IOP = IO Pointer.
- */
-
-rtems_id                rtems_libio_semaphore;
-rtems_libio_t          *rtems_libio_iops;
-rtems_libio_t          *rtems_libio_last_iop;
+#include <rtems.h>
+#include <rtems/assoc.h>                /* assoc.h not included by rtems.h */
+
+#include <stdio.h>                      /* O_RDONLY, et.al. */
+#include <fcntl.h>                      /* O_RDONLY, et.al. */
+#include <assert.h>
+#include <errno.h>
+
+#if ! defined(O_NDELAY)
+# if defined(solaris2)
+#  define O_NDELAY O_NONBLOCK
+# elif defined(RTEMS_NEWLIB)
+#  define O_NDELAY _FNBIO
+# endif
+#endif
+
+
+#include <errno.h>
+#include <string.h>                     /* strcmp */
+#include <unistd.h>
+#include <stdlib.h>                     /* calloc() */
+
+#include "libio.h"                      /* libio.h not pulled in by rtems */
+
+/*                            
+ *  File descriptor Table Information
+ */         
+
+extern unsigned32  rtems_libio_number_iops;
+rtems_id           rtems_libio_semaphore;
+rtems_libio_t     *rtems_libio_iops;
+rtems_libio_t     *rtems_libio_last_iop;
+rtems_libio_t     *rtems_libio_iop_freelist;
+
+/*
+ *  External I/O Handlers Table
+ *
+ *  Space for all possible handlers is preallocated
+ *  to speed up dispatch to external handlers.
+ */
+
 rtems_libio_handler_t   rtems_libio_handlers[15];
+
+/*
+ *  Default mode for all files.
+ */
+
+mode_t    rtems_filesystem_umask;
 
 /*
@@ -45,6 +80,5 @@
 )
 {
-  int handler_index = rtems_file_descriptor_type_index( handler_flag );
-
+  int handler_index = (handler_flag >> LIBIO_FLAGS_HANDLER_SHIFT) - 1;
 
   if ((handler_index < 0) || (handler_index >= 15))
@@ -62,19 +96,20 @@
 void rtems_libio_init( void )
 {
-  rtems_status_code rc;
-
-  /*
-   *  Allocate memory for the IOP Table
-   */
-
-  if ( rtems_libio_number_iops > 0 ) {
-    rtems_libio_iops =
-      (rtems_libio_t *) calloc(rtems_libio_number_iops, sizeof(rtems_libio_t));
-
-    if (rtems_libio_iops == NULL)
-        rtems_fatal_error_occurred( RTEMS_NO_MEMORY );
-
-    rtems_libio_last_iop = rtems_libio_iops + (rtems_libio_number_iops - 1);
-  }
+    rtems_status_code rc;
+    int i;
+    rtems_libio_t *iop;
+
+    if (rtems_libio_number_iops > 0)
+    {
+        rtems_libio_iops = (rtems_libio_t *) calloc(rtems_libio_number_iops,
+                                                    sizeof(rtems_libio_t));
+        if (rtems_libio_iops == NULL)
+            rtems_fatal_error_occurred(RTEMS_NO_MEMORY);
+
+        iop = rtems_libio_iop_freelist = rtems_libio_iops;
+	for (i = 0 ; i < (rtems_libio_number_iops - 1) ; i++, iop++)
+		iop->data1 = iop + 1;
+	iop->data1 = NULL;
+    }
 
   /*
@@ -158,22 +193,19 @@
   rtems_semaphore_obtain( rtems_libio_semaphore, RTEMS_WAIT, RTEMS_NO_TIMEOUT );
 
-  for (iop = rtems_libio_iops; iop <= rtems_libio_last_iop; iop++)
-    if ((iop->flags & LIBIO_FLAGS_OPEN) == 0) {
-      /*
-       *  Got an IOP -- create a semaphore for it.
-       */
-
-      rc = rtems_semaphore_create(
-        RTEMS_LIBIO_IOP_SEM(iop - rtems_libio_iops),
-        1,
-        RTEMS_BINARY_SEMAPHORE | RTEMS_INHERIT_PRIORITY | RTEMS_PRIORITY,
-        RTEMS_NO_PRIORITY,
-        &iop->sem
-      );
-      if ( rc != RTEMS_SUCCESSFUL )
-        goto failed;
-    
-      iop->flags = LIBIO_FLAGS_OPEN;
-      goto done;
+  if (rtems_libio_iop_freelist) {
+    iop = rtems_libio_iop_freelist;
+    rc = rtems_semaphore_create(
+      RTEMS_LIBIO_IOP_SEM(iop - rtems_libio_iops),
+      1,
+      RTEMS_BINARY_SEMAPHORE | RTEMS_INHERIT_PRIORITY | RTEMS_PRIORITY,
+      RTEMS_NO_PRIORITY,
+      &iop->sem
+    );
+    if (rc != RTEMS_SUCCESSFUL)
+      goto failed;
+    rtems_libio_iop_freelist = iop->data1;
+    iop->data1 = 0;
+    iop->flags = LIBIO_FLAGS_OPEN;
+    goto done;
   }
   
@@ -199,10 +231,11 @@
   rtems_semaphore_obtain( rtems_libio_semaphore, RTEMS_WAIT, RTEMS_NO_TIMEOUT );
 
-  if (iop->sem)
-    rtems_semaphore_delete(iop->sem);
-
-  (void) memset(iop, 0, sizeof(*iop));
-
-  rtems_semaphore_release( rtems_libio_semaphore );
+    if (iop->sem)
+      rtems_semaphore_delete(iop->sem);
+
+    iop->data1 = rtems_libio_iop_freelist;
+    rtems_libio_iop_freelist = iop;
+
+  rtems_semaphore_release(rtems_libio_semaphore);
 }
 
@@ -211,5 +244,5 @@
  *
  *  This routine scans the entire file descriptor table to determine if the
- *  are any active file descriptors that refer to the atleast one node in the
+ *  are any active file descriptors that refer to the at least one node in the
  *  file system that we are trying to dismount.
  *
@@ -226,17 +259,17 @@
 
   rtems_semaphore_obtain( rtems_libio_semaphore, RTEMS_WAIT, RTEMS_NO_TIMEOUT );
-
+              
   /*
    *  Look for any active file descriptor entry.
-   */
-
+   */         
+     
   for ( iop=rtems_libio_iops ; iop <= rtems_libio_last_iop ; iop++ ) {
-
+        
     if ((iop->flags & LIBIO_FLAGS_OPEN) != 0) {
-
+        
        /*
         *  Check if this node is under the file system that we
         *  are trying to dismount.
-        */ 
+        */
 
        if ( iop->pathinfo.mt_entry == fs_mt_entry ) {
@@ -248,5 +281,5 @@
 
   rtems_semaphore_release( rtems_libio_semaphore );
-
+ 
   return result;
 }
@@ -257,5 +290,5 @@
  *  This routine scans the entire file descriptor table to determine if the
  *  given file refers to an active file descriptor.
- *
+ * 
  *  If the given file is open a 1 is returned, otherwise a 0 is returned.
  */
@@ -266,5 +299,5 @@
 {
   rtems_libio_t     *iop;
-  int                result=0; 
+  int                result=0;
 
   rtems_semaphore_obtain( rtems_libio_semaphore, RTEMS_WAIT, RTEMS_NO_TIMEOUT );
@@ -273,13 +306,13 @@
    *  Look for any active file descriptor entry.
    */
-
+  
   for ( iop=rtems_libio_iops ; iop <= rtems_libio_last_iop ; iop++ ) {
-
+  
     if ((iop->flags & LIBIO_FLAGS_OPEN) != 0) {
-
+  
        /*
         *  Check if this node is under the file system that we
         *  are trying to dismount.
-        */ 
+        */
 
        if ( iop->pathinfo.node_access == node_access ) {
@@ -294,2 +327,4 @@
   return result;
 }
+
+
Index: c/src/exec/libcsupport/src/lseek.c
===================================================================
--- c/src/exec/libcsupport/src/lseek.c	(revision 6c980128774e4359b32d7325a987283c0635869f)
+++ c/src/exec/libcsupport/src/lseek.c	(revision cca44008d81209e9fa992157637d9de0384e0536)
@@ -25,4 +25,7 @@
   rtems_libio_t *iop;
 
+  rtems_libio_check_fd( fd );
+  iop = rtems_libio_iop( fd );
+
   /*
    *  If this file descriptor is mapped to an external set of handlers,
@@ -30,8 +33,9 @@
    */
 
-  if ( rtems_file_descriptor_type( fd ) ) {
+  if ( iop->flags & LIBIO_FLAGS_HANDLER_MASK ) {
     rtems_libio_lseek_t fp;
 
-    fp = rtems_libio_handlers[rtems_file_descriptor_type_index(fd)].lseek;
+    fp = rtems_libio_handlers[
+           (iop->flags >> LIBIO_FLAGS_HANDLER_SHIFT) - 1].lseek;
     if ( fp == NULL )
       set_errno_and_return_minus_one( EBADF );
@@ -43,7 +47,4 @@
    *  Now process the lseek().
    */
-
-  iop = rtems_libio_iop( fd );
-  rtems_libio_check_fd( fd );
 
   switch ( whence ) {
Index: c/src/exec/libcsupport/src/read.c
===================================================================
--- c/src/exec/libcsupport/src/read.c	(revision 6c980128774e4359b32d7325a987283c0635869f)
+++ c/src/exec/libcsupport/src/read.c	(revision cca44008d81209e9fa992157637d9de0384e0536)
@@ -27,4 +27,10 @@
   rtems_libio_t *iop;
 
+  rtems_libio_check_fd( fd );
+  iop = rtems_libio_iop( fd );
+  rtems_libio_check_buffer( buffer );
+  rtems_libio_check_count( count );
+  rtems_libio_check_permissions( iop, LIBIO_FLAGS_READ );
+
   /*
    *  If this file descriptor is mapped to an external set of handlers,
@@ -32,8 +38,9 @@
    */ 
 
-  if ( rtems_file_descriptor_type( fd ) ) {
+  if ( iop->flags & LIBIO_FLAGS_HANDLER_MASK ) {
     rtems_libio_read_t fp;
 
-    fp = rtems_libio_handlers[rtems_file_descriptor_type_index(fd)].read;
+    fp = rtems_libio_handlers[
+           (iop->flags >> LIBIO_FLAGS_HANDLER_SHIFT) - 1].read;
     if ( fp == NULL )
       set_errno_and_return_minus_one( EBADF );
@@ -45,10 +52,4 @@
    *  Now process the read().
    */
-
-  iop = rtems_libio_iop( fd );
-  rtems_libio_check_fd( fd );
-  rtems_libio_check_buffer( buffer );
-  rtems_libio_check_count( count );
-  rtems_libio_check_permissions( iop, LIBIO_FLAGS_READ );
 
   if ( !iop->handlers->read )
Index: c/src/exec/libcsupport/src/write.c
===================================================================
--- c/src/exec/libcsupport/src/write.c	(revision 6c980128774e4359b32d7325a987283c0635869f)
+++ c/src/exec/libcsupport/src/write.c	(revision cca44008d81209e9fa992157637d9de0384e0536)
@@ -35,4 +35,10 @@
   rtems_libio_t     *iop;
 
+  rtems_libio_check_fd( fd );
+  iop = rtems_libio_iop( fd );
+  rtems_libio_check_buffer( buffer );
+  rtems_libio_check_count( count );
+  rtems_libio_check_permissions( iop, LIBIO_FLAGS_WRITE );
+
   /*
    *  If this file descriptor is mapped to an external set of handlers,
@@ -40,8 +46,9 @@
    */
 
-  if ( rtems_file_descriptor_type( fd ) ) {
+  if ( iop->flags & LIBIO_FLAGS_HANDLER_MASK ) {
     rtems_libio_write_t fp;
 
-    fp = rtems_libio_handlers[rtems_file_descriptor_type_index(fd)].write;
+    fp = rtems_libio_handlers[
+           (iop->flags >> LIBIO_FLAGS_HANDLER_SHIFT) - 1].write;
     if ( fp == NULL )
       set_errno_and_return_minus_one( EBADF );
@@ -53,10 +60,4 @@
    *  Now process the write() request.
    */
-
-  iop = rtems_libio_iop( fd );
-  rtems_libio_check_fd( fd );
-  rtems_libio_check_buffer( buffer );
-  rtems_libio_check_count( count );
-  rtems_libio_check_permissions( iop, LIBIO_FLAGS_WRITE );
 
   if ( !iop->handlers->write )
Index: c/src/exec/libnetworking/rtems/rtems_glue.c
===================================================================
--- c/src/exec/libnetworking/rtems/rtems_glue.c	(revision 6c980128774e4359b32d7325a987283c0635869f)
+++ c/src/exec/libnetworking/rtems/rtems_glue.c	(revision cca44008d81209e9fa992157637d9de0384e0536)
@@ -30,17 +30,4 @@
 #include <net/netisr.h>
 #include <net/route.h>
-
-/*
- * Events used by networking routines.
- * Everything will break if the application
- * tries to use these events or if the `sleep'
- * events are equal to any of the NETISR * events.
- */
-#define SBWAIT_EVENT	RTEMS_EVENT_24
-#define SOSLEEP_EVENT	RTEMS_EVENT_25
-#define NETISR_IP_EVENT		(1 << NETISR_IP)
-#define NETISR_ARP_EVENT	(1 << NETISR_ARP)
-#define NETISR_EVENTS	(NETISR_IP_EVENT|NETISR_ARP_EVENT)
-
 
 /*
@@ -247,5 +234,5 @@
 	 * Register as an external I/O handler
 	 */
-	rtems_register_libio_handler (RTEMS_FILE_DESCRIPTOR_TYPE_SOCKET,
+	rtems_register_libio_handler (LIBIO_FLAGS_HANDLER_SOCK,
 						&rtems_bsdnet_io_handler);
 
@@ -306,15 +293,8 @@
 	 * Set this task as the target of the wakeup operation.
 	 */
-	if (sb->sb_sel.si_pid)
-		rtems_panic ("Another task is already sleeping on that socket buffer");
 	rtems_task_ident (RTEMS_SELF, 0, &tid);
 	sb->sb_sel.si_pid = tid;
 
 	/*
-	 * Mark the socket buffer as waiting.
-	 */
-	sb->sb_flags |= SB_WAIT;
-
-	/*
 	 * Release the network semaphore.
 	 */
@@ -330,10 +310,4 @@
 	 */
 	rtems_bsdnet_semaphore_obtain ();
-
-	/*
-	 * Relinquish ownership of the socket buffer
-	 */
-	sb->sb_flags &= ~SB_WAIT;
-	sb->sb_sel.si_pid = 0;
 
 	/*
@@ -356,7 +330,9 @@
 	register struct sockbuf *sb;
 {
-	if (sb->sb_flags & SB_WAIT) {
-		sb->sb_flags &= ~SB_WAIT;
-		rtems_event_send (sb->sb_sel.si_pid, SBWAIT_EVENT);
+	rtems_id tid;
+
+	if ((tid = sb->sb_sel.si_pid) != 0) {
+		sb->sb_sel.si_pid = 0;
+		rtems_event_send (tid, SBWAIT_EVENT);
 	}
 }
Index: c/src/exec/libnetworking/rtems/rtems_syscall.c
===================================================================
--- c/src/exec/libnetworking/rtems/rtems_syscall.c	(revision 6c980128774e4359b32d7325a987283c0635869f)
+++ c/src/exec/libnetworking/rtems/rtems_syscall.c	(revision cca44008d81209e9fa992157637d9de0384e0536)
@@ -25,75 +25,4 @@
 #include <net/if.h>
 #include <net/route.h>
-
-/*
- *********************************************************************
- *              Map RTEMS file descriptor to BSD socket              *
- *********************************************************************
- */
-struct fdsock {
-	int		indexFreeNext;
-	struct socket	*sock;
-};
-static struct fdsock *fdsock;
-static int fdsockCount;
-static int indexFreeHead = -1;
-
-/*
- * Convert an RTEMS file descriptor to a BSD socket pointer.
- */
-static struct socket *
-fdToSocket (int fd)
-{
-	int i;
-	struct socket *s;
-
-	if ((fd < 0)
-	 || (rtems_file_descriptor_type(fd) != RTEMS_FILE_DESCRIPTOR_TYPE_SOCKET)
-	 || ((i = rtems_file_descriptor_base(fd)) >= fdsockCount)
-	 || ((s = fdsock[i].sock) == NULL)) {
-		errno = EBADF;
-		return NULL;
-	}
-	return s;
-}
-
-/*
- * Enlarge the size of the file-descritor/socket pointer map.
- */
-static int
-enlargeFdMap (void)
-{
-	struct fdsock *nfdsock;
-	int i;
-
-	nfdsock = realloc (fdsock, sizeof *fdsock * (fdsockCount + 20));
-	if (nfdsock == NULL) {
-		errno = ENFILE;
-		return 0;
-	}
-	fdsock = nfdsock;
-	for (i = fdsockCount, fdsockCount += 20 ; i < fdsockCount ; i++) {
-		fdsock[i].sock = NULL;
-		fdsock[i].indexFreeNext = indexFreeHead;
-		indexFreeHead = i;
-	}
-	return 1;
-}
-
-/*
- * Create a file descriptor for a new socket
- */
-static int
-makeFd (struct socket *s)
-{
-	int i;
-
-	if ((indexFreeHead < 0) && !enlargeFdMap ())
-		return -1;
-	i = indexFreeHead;
-	indexFreeHead = fdsock[i].indexFreeNext;
-	fdsock[i].sock = s;
-	return rtems_make_file_descriptor(i,RTEMS_FILE_DESCRIPTOR_TYPE_SOCKET);
-}
 
 /*
@@ -129,5 +58,5 @@
 socket (int domain, int type, int protocol)
 {
-	int fd = -1;
+	int fd;
 	int error;
 	struct socket *so;
@@ -136,5 +65,5 @@
 	error = socreate(domain, &so, type, protocol, NULL);
 	if (error == 0) {
-		fd = makeFd (so);
+		fd = rtems_bsdnet_makeFdForSocket (so);
 		if (fd < 0)
 			soclose (so);
@@ -157,5 +86,5 @@
 
 	rtems_bsdnet_semaphore_obtain ();
-	if ((so = fdToSocket (s)) != NULL) {
+	if ((so = rtems_bsdnet_fdToSocket (s)) != NULL) {
 		error = sockargstombuf (&nam, name, namelen, MT_SONAME);
 		if (error == 0) {
@@ -184,5 +113,5 @@
 
 	rtems_bsdnet_semaphore_obtain ();
-	if ((so = fdToSocket (s)) == NULL) {
+	if ((so = rtems_bsdnet_fdToSocket (s)) == NULL) {
 		rtems_bsdnet_semaphore_release ();
 		return -1;
@@ -234,5 +163,5 @@
 
 	rtems_bsdnet_semaphore_obtain ();
-	if ((so = fdToSocket (s)) != NULL) {
+	if ((so = rtems_bsdnet_fdToSocket (s)) != NULL) {
 		error = solisten (so, backlog);
 		if (error == 0)
@@ -253,5 +182,5 @@
 
 	rtems_bsdnet_semaphore_obtain ();
-	if ((head = fdToSocket (s)) == NULL) {
+	if ((head = rtems_bsdnet_fdToSocket (s)) == NULL) {
 		rtems_bsdnet_semaphore_release ();
 		return -1;
@@ -285,5 +214,5 @@
 	head->so_qlen--;
 
-	fd = makeFd (so);
+	fd = rtems_bsdnet_makeFdForSocket (so);
 	if (fd < 0) {
 		TAILQ_INSERT_HEAD(&head->so_comp, so, so_list);
@@ -326,5 +255,5 @@
 
 	rtems_bsdnet_semaphore_obtain ();
-	if ((so = fdToSocket (s)) == NULL) {
+	if ((so = rtems_bsdnet_fdToSocket (s)) == NULL) {
 		rtems_bsdnet_semaphore_release ();
 		return -1;
@@ -429,5 +358,5 @@
 
 	rtems_bsdnet_semaphore_obtain ();
-	if ((so = fdToSocket (s)) == NULL) {
+	if ((so = rtems_bsdnet_fdToSocket (s)) == NULL) {
 		rtems_bsdnet_semaphore_release ();
 		return -1;
@@ -548,5 +477,5 @@
 
 	rtems_bsdnet_semaphore_obtain ();
-	if ((so = fdToSocket (s)) == NULL) {
+	if ((so = rtems_bsdnet_fdToSocket (s)) == NULL) {
 		rtems_bsdnet_semaphore_release ();
 		return -1;
@@ -585,5 +514,5 @@
 
 	rtems_bsdnet_semaphore_obtain ();
-	if ((so = fdToSocket (s)) == NULL) {
+	if ((so = rtems_bsdnet_fdToSocket (s)) == NULL) {
 		rtems_bsdnet_semaphore_release ();
 		return -1;
@@ -627,5 +556,5 @@
 
 	rtems_bsdnet_semaphore_obtain ();
-	if ((so = fdToSocket (s)) == NULL) {
+	if ((so = rtems_bsdnet_fdToSocket (s)) == NULL) {
 		rtems_bsdnet_semaphore_release ();
 		return -1;
@@ -677,14 +606,10 @@
 	struct socket *so;
 	int error;
-	int i;
-
-	rtems_bsdnet_semaphore_obtain ();
-	if ((so = fdToSocket (fd)) == NULL) {
-		rtems_bsdnet_semaphore_release ();
-		return -1;
-	}
-	i = rtems_file_descriptor_base(fd);
-	fdsock[i].indexFreeNext = indexFreeHead;;
-	indexFreeHead = i;
+
+	rtems_bsdnet_semaphore_obtain ();
+	if ((so = rtems_bsdnet_fdToSocket (fd)) == NULL) {
+		rtems_bsdnet_semaphore_release ();
+		return -1;
+	}
 	error = soclose (so);
 	rtems_bsdnet_semaphore_release ();
@@ -738,5 +663,5 @@
 
 	rtems_bsdnet_semaphore_obtain ();
-	if ((so = fdToSocket (fd)) == NULL) {
+	if ((so = rtems_bsdnet_fdToSocket (fd)) == NULL) {
 		rtems_bsdnet_semaphore_release ();
 		return -1;
Index: c/src/lib/include/rtems/libio.h
===================================================================
--- c/src/lib/include/rtems/libio.h	(revision 6c980128774e4359b32d7325a987283c0635869f)
+++ c/src/lib/include/rtems/libio.h	(revision cca44008d81209e9fa992157637d9de0384e0536)
@@ -394,9 +394,13 @@
 #define LIBIO_FLAGS_READ          0x0002  /* reading */
 #define LIBIO_FLAGS_WRITE         0x0004  /* writing */
-#define LIBIO_FLAGS_LINE_BUFFERED 0x0008  /* line buffered io (^h, ^u, etc) */
 #define LIBIO_FLAGS_OPEN          0x0100  /* device is open */
 #define LIBIO_FLAGS_APPEND        0x0200  /* all writes append */
 #define LIBIO_FLAGS_CREATE        0x0400  /* create file */
 #define LIBIO_FLAGS_CLOSE_ON_EXEC 0x0800  /* close on process exec() */
+
+#define LIBIO_FLAGS_HANDLER_SHIFT 12
+#define LIBIO_FLAGS_HANDLER_MASK  0xF000  /* mask for external handler type */
+#define LIBIO_FLAGS_HANDLER_RTEMS 0x0000  /* `traditional' RTEMS I/O */
+#define LIBIO_FLAGS_HANDLER_SOCK  0x1000  /* BSD socket */
 
 #define LIBIO_FLAGS_READ_WRITE    (LIBIO_FLAGS_READ | LIBIO_FLAGS_WRITE)
Index: c/src/lib/include/rtems/libio_.h
===================================================================
--- c/src/lib/include/rtems/libio_.h	(revision 6c980128774e4359b32d7325a987283c0635869f)
+++ c/src/lib/include/rtems/libio_.h	(revision cca44008d81209e9fa992157637d9de0384e0536)
@@ -66,4 +66,5 @@
 extern rtems_libio_t  *rtems_libio_iops;
 extern rtems_libio_t  *rtems_libio_last_iop;
+extern rtems_libio_t *rtems_libio_iop_freelist;
 
 /*
Index: c/src/lib/include/sys/sockio.h
===================================================================
--- c/src/lib/include/sys/sockio.h	(revision 6c980128774e4359b32d7325a987283c0635869f)
+++ c/src/lib/include/sys/sockio.h	(revision cca44008d81209e9fa992157637d9de0384e0536)
@@ -84,10 +84,9 @@
 #define	SIOCGIFMEDIA	_IOWR('i', 56, struct ifmediareq) /* get net media */
 
-
 /*
  * RTEMS additions for setting/getting `tap' function on incoming packets.
  */
-#define SIOCSIFTAP	 _IOW('i', 80, struct ifreq)	/* set tap function */
-#define SIOCGIFTAP	_IOWR('i', 81, struct ifreq)	/* get tap function */
+#define SIOCSIFTAP	_IOW('i', 80, struct ifreq)	/* set tap function */
+#define SIOCGIFTAP	_IOW('i', 81, struct ifreq)	/* get tap function */
 
 #endif /* !_SYS_SOCKIO_H_ */
Index: c/src/lib/libc/close.c
===================================================================
--- c/src/lib/libc/close.c	(revision 6c980128774e4359b32d7325a987283c0635869f)
+++ c/src/lib/libc/close.c	(revision cca44008d81209e9fa992157637d9de0384e0536)
@@ -23,8 +23,11 @@
   int                 status;
 
-  if ( rtems_file_descriptor_type( fd ) ) {
+  rtems_libio_check_fd(fd);
+  iop = rtems_libio_iop(fd);
+  if ( iop->flags & LIBIO_FLAGS_HANDLER_MASK ) {
     int (*fp)(int  fd);
 
-    fp = rtems_libio_handlers[rtems_file_descriptor_type_index(fd)].close;
+    fp = rtems_libio_handlers[
+            (iop->flags >> LIBIO_FLAGS_HANDLER_SHIFT) - 1].close;
     if ( fp == NULL )
       set_errno_and_return_minus_one( EBADF );
@@ -32,6 +35,4 @@
     return status;
   }
-  iop = rtems_libio_iop(fd);
-  rtems_libio_check_fd(fd);
 
   if ( !iop->handlers )
Index: c/src/lib/libc/fchmod.c
===================================================================
--- c/src/lib/libc/fchmod.c	(revision 6c980128774e4359b32d7325a987283c0635869f)
+++ c/src/lib/libc/fchmod.c	(revision cca44008d81209e9fa992157637d9de0384e0536)
@@ -28,9 +28,12 @@
   rtems_libio_t *iop;
   
+  rtems_libio_check_fd( fd );
+  iop = rtems_libio_iop( fd );
+
   /*
    *  If this is not a file system based entity, it is an error.
    */
 
-  if ( rtems_file_descriptor_type( fd ) )
+  if ( iop->flags & LIBIO_FLAGS_HANDLER_MASK )
     set_errno_and_return_minus_one( EBADF );
 
@@ -39,6 +42,4 @@
    */
 
-  iop = rtems_libio_iop( fd );
-  rtems_libio_check_fd( fd );
   rtems_libio_check_permissions( iop, LIBIO_FLAGS_WRITE );
 
Index: c/src/lib/libc/fcntl.c
===================================================================
--- c/src/lib/libc/fcntl.c	(revision 6c980128774e4359b32d7325a987283c0635869f)
+++ c/src/lib/libc/fcntl.c	(revision cca44008d81209e9fa992157637d9de0384e0536)
@@ -33,9 +33,12 @@
   va_start( ap, cmd );
 
+  rtems_libio_check_fd( fd );
+  iop = rtems_libio_iop( fd );
+
   /*
    *  If this is not a file system based entity, it is an error.
    */
 
-  if ( rtems_file_descriptor_type( fd ) )
+  if ( iop->flags & LIBIO_FLAGS_HANDLER_MASK )
     set_errno_and_return_minus_one( EBADF );
 
@@ -43,7 +46,4 @@
    *  Now process the fcntl().
    */
-
-  iop = rtems_libio_iop( fd );
-  rtems_libio_check_fd( fd );
 
   /*
Index: c/src/lib/libc/fdatasync.c
===================================================================
--- c/src/lib/libc/fdatasync.c	(revision 6c980128774e4359b32d7325a987283c0635869f)
+++ c/src/lib/libc/fdatasync.c	(revision cca44008d81209e9fa992157637d9de0384e0536)
@@ -23,4 +23,8 @@
   rtems_libio_t *iop;
 
+  rtems_libio_check_fd( fd );
+  iop = rtems_libio_iop( fd );
+  rtems_libio_check_permissions( iop, LIBIO_FLAGS_WRITE );
+
   /*
    *  If this file descriptor is mapped to an external set of handlers,
@@ -28,5 +32,5 @@
    */
 
-  if ( rtems_file_descriptor_type( fd ) )
+  if ( iop->flags & LIBIO_FLAGS_HANDLER_MASK )
     set_errno_and_return_minus_one( EBADF );
 
@@ -35,8 +39,4 @@
    */
 
-  iop = rtems_libio_iop( fd );
-  rtems_libio_check_fd( fd );
-  rtems_libio_check_permissions( iop, LIBIO_FLAGS_WRITE );
-
   if ( !iop->handlers->fdatasync )
     set_errno_and_return_minus_one( ENOTSUP );
Index: c/src/lib/libc/fpathconf.c
===================================================================
--- c/src/lib/libc/fpathconf.c	(revision 6c980128774e4359b32d7325a987283c0635869f)
+++ c/src/lib/libc/fpathconf.c	(revision cca44008d81209e9fa992157637d9de0384e0536)
@@ -27,4 +27,8 @@
   rtems_filesystem_limits_and_options_t  *the_limits;
 
+  rtems_libio_check_fd(fd);
+  iop = rtems_libio_iop(fd);
+  rtems_libio_check_permissions(iop, LIBIO_FLAGS_READ);
+
   /*
    *  If this file descriptor is mapped to an external set of handlers,
@@ -33,5 +37,5 @@
    */
 
-  if ( rtems_file_descriptor_type( fd ) )
+  if ( iop->flags & LIBIO_FLAGS_HANDLER_MASK )
     set_errno_and_return_minus_one( EBADF );
 
@@ -39,8 +43,4 @@
    *  Now process the information request.
    */
-
-  iop = rtems_libio_iop(fd);
-  rtems_libio_check_fd(fd);
-  rtems_libio_check_permissions(iop, LIBIO_FLAGS_READ);
 
   the_limits = &iop->pathinfo.mt_entry->pathconf_limits_and_options;
Index: c/src/lib/libc/fsync.c
===================================================================
--- c/src/lib/libc/fsync.c	(revision 6c980128774e4359b32d7325a987283c0635869f)
+++ c/src/lib/libc/fsync.c	(revision cca44008d81209e9fa992157637d9de0384e0536)
@@ -23,4 +23,8 @@
   rtems_libio_t *iop;
 
+  rtems_libio_check_fd( fd );
+  iop = rtems_libio_iop( fd );
+  rtems_libio_check_permissions( iop, LIBIO_FLAGS_WRITE );
+
   /*
    *  If this file descriptor is mapped to an external set of handlers,
@@ -28,5 +32,5 @@
    */
 
-  if ( rtems_file_descriptor_type( fd ) )
+  if ( iop->flags & LIBIO_FLAGS_HANDLER_MASK )
     set_errno_and_return_minus_one( EBADF );
 
@@ -35,8 +39,4 @@
    */
 
-  iop = rtems_libio_iop( fd );
-  rtems_libio_check_fd( fd );
-  rtems_libio_check_permissions( iop, LIBIO_FLAGS_WRITE );
-
   if ( !iop->handlers->fsync )
     set_errno_and_return_minus_one( ENOTSUP );
Index: c/src/lib/libc/ftruncate.c
===================================================================
--- c/src/lib/libc/ftruncate.c	(revision 6c980128774e4359b32d7325a987283c0635869f)
+++ c/src/lib/libc/ftruncate.c	(revision cca44008d81209e9fa992157637d9de0384e0536)
@@ -26,9 +26,12 @@
   rtems_filesystem_location_info_t  loc;
   
+  rtems_libio_check_fd( fd );
+  iop = rtems_libio_iop( fd );
+
   /*
    *  If this is not a file system based entity, it is an error.
    */
 
-  if ( rtems_file_descriptor_type( fd ) )
+  if ( iop->flags & LIBIO_FLAGS_HANDLER_MASK )
     set_errno_and_return_minus_one( EBADF );
 
@@ -37,6 +40,4 @@
    */
   
-  iop = rtems_libio_iop( fd );
-
   /*
    *  Make sure we are not working on a directory
@@ -50,5 +51,4 @@
     set_errno_and_return_minus_one( EISDIR );
 
-  rtems_libio_check_fd( fd );
   rtems_libio_check_permissions( iop, LIBIO_FLAGS_WRITE );
 
Index: c/src/lib/libc/ioctl.c
===================================================================
--- c/src/lib/libc/ioctl.c	(revision 6c980128774e4359b32d7325a987283c0635869f)
+++ c/src/lib/libc/ioctl.c	(revision cca44008d81209e9fa992157637d9de0384e0536)
@@ -27,4 +27,7 @@
   rtems_libio_t     *iop;
 
+  rtems_libio_check_fd( fd );
+  iop = rtems_libio_iop( fd );
+
   /*
    *  If this file descriptor is mapped to an external set of handlers,
@@ -32,8 +35,9 @@
    */ 
 
-  if ( rtems_file_descriptor_type( fd ) ) {
+  if ( iop->flags & LIBIO_FLAGS_HANDLER_MASK ) {
     rtems_libio_ioctl_t fp;
 
-    fp = rtems_libio_handlers[rtems_file_descriptor_type_index(fd)].ioctl;
+    fp = rtems_libio_handlers[
+           (iop->flags >> LIBIO_FLAGS_HANDLER_SHIFT) - 1].ioctl;
     if ( fp == NULL )
       set_errno_and_return_minus_one( EBADF );
@@ -46,7 +50,4 @@
    */
 
-  iop = rtems_libio_iop( fd );
-  rtems_libio_check_fd( fd );
-
   if ( !iop->handlers->ioctl )
     set_errno_and_return_minus_one( ENOTSUP );
Index: c/src/lib/libc/libio.c
===================================================================
--- c/src/lib/libc/libio.c	(revision 6c980128774e4359b32d7325a987283c0635869f)
+++ c/src/lib/libc/libio.c	(revision cca44008d81209e9fa992157637d9de0384e0536)
@@ -4,7 +4,4 @@
  *  POSIX system calls like open(), read, fstat(), etc.
  *
- *  This provides the foundation for POSIX compliant IO system calls
- *  for RTEMS.
- *
  *  COPYRIGHT (c) 1989-1998.
  *  On-Line Applications Research Corporation (OAR).
@@ -19,14 +16,52 @@
 
 #include "libio_.h"                   /* libio_.h pulls in rtems */
-
-/*
- *  Global variables used to manage the File Descriptor Table.
- *  IOP = IO Pointer.
- */
-
-rtems_id                rtems_libio_semaphore;
-rtems_libio_t          *rtems_libio_iops;
-rtems_libio_t          *rtems_libio_last_iop;
+#include <rtems.h>
+#include <rtems/assoc.h>                /* assoc.h not included by rtems.h */
+
+#include <stdio.h>                      /* O_RDONLY, et.al. */
+#include <fcntl.h>                      /* O_RDONLY, et.al. */
+#include <assert.h>
+#include <errno.h>
+
+#if ! defined(O_NDELAY)
+# if defined(solaris2)
+#  define O_NDELAY O_NONBLOCK
+# elif defined(RTEMS_NEWLIB)
+#  define O_NDELAY _FNBIO
+# endif
+#endif
+
+
+#include <errno.h>
+#include <string.h>                     /* strcmp */
+#include <unistd.h>
+#include <stdlib.h>                     /* calloc() */
+
+#include "libio.h"                      /* libio.h not pulled in by rtems */
+
+/*                            
+ *  File descriptor Table Information
+ */         
+
+extern unsigned32  rtems_libio_number_iops;
+rtems_id           rtems_libio_semaphore;
+rtems_libio_t     *rtems_libio_iops;
+rtems_libio_t     *rtems_libio_last_iop;
+rtems_libio_t     *rtems_libio_iop_freelist;
+
+/*
+ *  External I/O Handlers Table
+ *
+ *  Space for all possible handlers is preallocated
+ *  to speed up dispatch to external handlers.
+ */
+
 rtems_libio_handler_t   rtems_libio_handlers[15];
+
+/*
+ *  Default mode for all files.
+ */
+
+mode_t    rtems_filesystem_umask;
 
 /*
@@ -45,6 +80,5 @@
 )
 {
-  int handler_index = rtems_file_descriptor_type_index( handler_flag );
-
+  int handler_index = (handler_flag >> LIBIO_FLAGS_HANDLER_SHIFT) - 1;
 
   if ((handler_index < 0) || (handler_index >= 15))
@@ -62,19 +96,20 @@
 void rtems_libio_init( void )
 {
-  rtems_status_code rc;
-
-  /*
-   *  Allocate memory for the IOP Table
-   */
-
-  if ( rtems_libio_number_iops > 0 ) {
-    rtems_libio_iops =
-      (rtems_libio_t *) calloc(rtems_libio_number_iops, sizeof(rtems_libio_t));
-
-    if (rtems_libio_iops == NULL)
-        rtems_fatal_error_occurred( RTEMS_NO_MEMORY );
-
-    rtems_libio_last_iop = rtems_libio_iops + (rtems_libio_number_iops - 1);
-  }
+    rtems_status_code rc;
+    int i;
+    rtems_libio_t *iop;
+
+    if (rtems_libio_number_iops > 0)
+    {
+        rtems_libio_iops = (rtems_libio_t *) calloc(rtems_libio_number_iops,
+                                                    sizeof(rtems_libio_t));
+        if (rtems_libio_iops == NULL)
+            rtems_fatal_error_occurred(RTEMS_NO_MEMORY);
+
+        iop = rtems_libio_iop_freelist = rtems_libio_iops;
+	for (i = 0 ; i < (rtems_libio_number_iops - 1) ; i++, iop++)
+		iop->data1 = iop + 1;
+	iop->data1 = NULL;
+    }
 
   /*
@@ -158,22 +193,19 @@
   rtems_semaphore_obtain( rtems_libio_semaphore, RTEMS_WAIT, RTEMS_NO_TIMEOUT );
 
-  for (iop = rtems_libio_iops; iop <= rtems_libio_last_iop; iop++)
-    if ((iop->flags & LIBIO_FLAGS_OPEN) == 0) {
-      /*
-       *  Got an IOP -- create a semaphore for it.
-       */
-
-      rc = rtems_semaphore_create(
-        RTEMS_LIBIO_IOP_SEM(iop - rtems_libio_iops),
-        1,
-        RTEMS_BINARY_SEMAPHORE | RTEMS_INHERIT_PRIORITY | RTEMS_PRIORITY,
-        RTEMS_NO_PRIORITY,
-        &iop->sem
-      );
-      if ( rc != RTEMS_SUCCESSFUL )
-        goto failed;
-    
-      iop->flags = LIBIO_FLAGS_OPEN;
-      goto done;
+  if (rtems_libio_iop_freelist) {
+    iop = rtems_libio_iop_freelist;
+    rc = rtems_semaphore_create(
+      RTEMS_LIBIO_IOP_SEM(iop - rtems_libio_iops),
+      1,
+      RTEMS_BINARY_SEMAPHORE | RTEMS_INHERIT_PRIORITY | RTEMS_PRIORITY,
+      RTEMS_NO_PRIORITY,
+      &iop->sem
+    );
+    if (rc != RTEMS_SUCCESSFUL)
+      goto failed;
+    rtems_libio_iop_freelist = iop->data1;
+    iop->data1 = 0;
+    iop->flags = LIBIO_FLAGS_OPEN;
+    goto done;
   }
   
@@ -199,10 +231,11 @@
   rtems_semaphore_obtain( rtems_libio_semaphore, RTEMS_WAIT, RTEMS_NO_TIMEOUT );
 
-  if (iop->sem)
-    rtems_semaphore_delete(iop->sem);
-
-  (void) memset(iop, 0, sizeof(*iop));
-
-  rtems_semaphore_release( rtems_libio_semaphore );
+    if (iop->sem)
+      rtems_semaphore_delete(iop->sem);
+
+    iop->data1 = rtems_libio_iop_freelist;
+    rtems_libio_iop_freelist = iop;
+
+  rtems_semaphore_release(rtems_libio_semaphore);
 }
 
@@ -211,5 +244,5 @@
  *
  *  This routine scans the entire file descriptor table to determine if the
- *  are any active file descriptors that refer to the atleast one node in the
+ *  are any active file descriptors that refer to the at least one node in the
  *  file system that we are trying to dismount.
  *
@@ -226,17 +259,17 @@
 
   rtems_semaphore_obtain( rtems_libio_semaphore, RTEMS_WAIT, RTEMS_NO_TIMEOUT );
-
+              
   /*
    *  Look for any active file descriptor entry.
-   */
-
+   */         
+     
   for ( iop=rtems_libio_iops ; iop <= rtems_libio_last_iop ; iop++ ) {
-
+        
     if ((iop->flags & LIBIO_FLAGS_OPEN) != 0) {
-
+        
        /*
         *  Check if this node is under the file system that we
         *  are trying to dismount.
-        */ 
+        */
 
        if ( iop->pathinfo.mt_entry == fs_mt_entry ) {
@@ -248,5 +281,5 @@
 
   rtems_semaphore_release( rtems_libio_semaphore );
-
+ 
   return result;
 }
@@ -257,5 +290,5 @@
  *  This routine scans the entire file descriptor table to determine if the
  *  given file refers to an active file descriptor.
- *
+ * 
  *  If the given file is open a 1 is returned, otherwise a 0 is returned.
  */
@@ -266,5 +299,5 @@
 {
   rtems_libio_t     *iop;
-  int                result=0; 
+  int                result=0;
 
   rtems_semaphore_obtain( rtems_libio_semaphore, RTEMS_WAIT, RTEMS_NO_TIMEOUT );
@@ -273,13 +306,13 @@
    *  Look for any active file descriptor entry.
    */
-
+  
   for ( iop=rtems_libio_iops ; iop <= rtems_libio_last_iop ; iop++ ) {
-
+  
     if ((iop->flags & LIBIO_FLAGS_OPEN) != 0) {
-
+  
        /*
         *  Check if this node is under the file system that we
         *  are trying to dismount.
-        */ 
+        */
 
        if ( iop->pathinfo.node_access == node_access ) {
@@ -294,2 +327,4 @@
   return result;
 }
+
+
Index: c/src/lib/libc/libio.h
===================================================================
--- c/src/lib/libc/libio.h	(revision 6c980128774e4359b32d7325a987283c0635869f)
+++ c/src/lib/libc/libio.h	(revision cca44008d81209e9fa992157637d9de0384e0536)
@@ -394,9 +394,13 @@
 #define LIBIO_FLAGS_READ          0x0002  /* reading */
 #define LIBIO_FLAGS_WRITE         0x0004  /* writing */
-#define LIBIO_FLAGS_LINE_BUFFERED 0x0008  /* line buffered io (^h, ^u, etc) */
 #define LIBIO_FLAGS_OPEN          0x0100  /* device is open */
 #define LIBIO_FLAGS_APPEND        0x0200  /* all writes append */
 #define LIBIO_FLAGS_CREATE        0x0400  /* create file */
 #define LIBIO_FLAGS_CLOSE_ON_EXEC 0x0800  /* close on process exec() */
+
+#define LIBIO_FLAGS_HANDLER_SHIFT 12
+#define LIBIO_FLAGS_HANDLER_MASK  0xF000  /* mask for external handler type */
+#define LIBIO_FLAGS_HANDLER_RTEMS 0x0000  /* `traditional' RTEMS I/O */
+#define LIBIO_FLAGS_HANDLER_SOCK  0x1000  /* BSD socket */
 
 #define LIBIO_FLAGS_READ_WRITE    (LIBIO_FLAGS_READ | LIBIO_FLAGS_WRITE)
Index: c/src/lib/libc/libio_.h
===================================================================
--- c/src/lib/libc/libio_.h	(revision 6c980128774e4359b32d7325a987283c0635869f)
+++ c/src/lib/libc/libio_.h	(revision cca44008d81209e9fa992157637d9de0384e0536)
@@ -66,4 +66,5 @@
 extern rtems_libio_t  *rtems_libio_iops;
 extern rtems_libio_t  *rtems_libio_last_iop;
+extern rtems_libio_t *rtems_libio_iop_freelist;
 
 /*
Index: c/src/lib/libc/lseek.c
===================================================================
--- c/src/lib/libc/lseek.c	(revision 6c980128774e4359b32d7325a987283c0635869f)
+++ c/src/lib/libc/lseek.c	(revision cca44008d81209e9fa992157637d9de0384e0536)
@@ -25,4 +25,7 @@
   rtems_libio_t *iop;
 
+  rtems_libio_check_fd( fd );
+  iop = rtems_libio_iop( fd );
+
   /*
    *  If this file descriptor is mapped to an external set of handlers,
@@ -30,8 +33,9 @@
    */
 
-  if ( rtems_file_descriptor_type( fd ) ) {
+  if ( iop->flags & LIBIO_FLAGS_HANDLER_MASK ) {
     rtems_libio_lseek_t fp;
 
-    fp = rtems_libio_handlers[rtems_file_descriptor_type_index(fd)].lseek;
+    fp = rtems_libio_handlers[
+           (iop->flags >> LIBIO_FLAGS_HANDLER_SHIFT) - 1].lseek;
     if ( fp == NULL )
       set_errno_and_return_minus_one( EBADF );
@@ -43,7 +47,4 @@
    *  Now process the lseek().
    */
-
-  iop = rtems_libio_iop( fd );
-  rtems_libio_check_fd( fd );
 
   switch ( whence ) {
Index: c/src/lib/libc/read.c
===================================================================
--- c/src/lib/libc/read.c	(revision 6c980128774e4359b32d7325a987283c0635869f)
+++ c/src/lib/libc/read.c	(revision cca44008d81209e9fa992157637d9de0384e0536)
@@ -27,4 +27,10 @@
   rtems_libio_t *iop;
 
+  rtems_libio_check_fd( fd );
+  iop = rtems_libio_iop( fd );
+  rtems_libio_check_buffer( buffer );
+  rtems_libio_check_count( count );
+  rtems_libio_check_permissions( iop, LIBIO_FLAGS_READ );
+
   /*
    *  If this file descriptor is mapped to an external set of handlers,
@@ -32,8 +38,9 @@
    */ 
 
-  if ( rtems_file_descriptor_type( fd ) ) {
+  if ( iop->flags & LIBIO_FLAGS_HANDLER_MASK ) {
     rtems_libio_read_t fp;
 
-    fp = rtems_libio_handlers[rtems_file_descriptor_type_index(fd)].read;
+    fp = rtems_libio_handlers[
+           (iop->flags >> LIBIO_FLAGS_HANDLER_SHIFT) - 1].read;
     if ( fp == NULL )
       set_errno_and_return_minus_one( EBADF );
@@ -45,10 +52,4 @@
    *  Now process the read().
    */
-
-  iop = rtems_libio_iop( fd );
-  rtems_libio_check_fd( fd );
-  rtems_libio_check_buffer( buffer );
-  rtems_libio_check_count( count );
-  rtems_libio_check_permissions( iop, LIBIO_FLAGS_READ );
 
   if ( !iop->handlers->read )
Index: c/src/lib/libc/write.c
===================================================================
--- c/src/lib/libc/write.c	(revision 6c980128774e4359b32d7325a987283c0635869f)
+++ c/src/lib/libc/write.c	(revision cca44008d81209e9fa992157637d9de0384e0536)
@@ -35,4 +35,10 @@
   rtems_libio_t     *iop;
 
+  rtems_libio_check_fd( fd );
+  iop = rtems_libio_iop( fd );
+  rtems_libio_check_buffer( buffer );
+  rtems_libio_check_count( count );
+  rtems_libio_check_permissions( iop, LIBIO_FLAGS_WRITE );
+
   /*
    *  If this file descriptor is mapped to an external set of handlers,
@@ -40,8 +46,9 @@
    */
 
-  if ( rtems_file_descriptor_type( fd ) ) {
+  if ( iop->flags & LIBIO_FLAGS_HANDLER_MASK ) {
     rtems_libio_write_t fp;
 
-    fp = rtems_libio_handlers[rtems_file_descriptor_type_index(fd)].write;
+    fp = rtems_libio_handlers[
+           (iop->flags >> LIBIO_FLAGS_HANDLER_SHIFT) - 1].write;
     if ( fp == NULL )
       set_errno_and_return_minus_one( EBADF );
@@ -53,10 +60,4 @@
    *  Now process the write() request.
    */
-
-  iop = rtems_libio_iop( fd );
-  rtems_libio_check_fd( fd );
-  rtems_libio_check_buffer( buffer );
-  rtems_libio_check_count( count );
-  rtems_libio_check_permissions( iop, LIBIO_FLAGS_WRITE );
 
   if ( !iop->handlers->write )
Index: c/src/lib/libnetworking/rtems/Makefile.in
===================================================================
--- c/src/lib/libnetworking/rtems/Makefile.in	(revision 6c980128774e4359b32d7325a987283c0635869f)
+++ c/src/lib/libnetworking/rtems/Makefile.in	(revision cca44008d81209e9fa992157637d9de0384e0536)
@@ -17,5 +17,5 @@
         rtems_showmbuf rtems_showroute \
         rtems_showifstat rtems_showipstat rtems_showicmpstat \
-        rtems_showtcpstat rtems_showudpstat
+        rtems_showtcpstat rtems_showudpstat rtems_select
 C_FILES=$(C_PIECES:%=%.c)
 C_O_FILES=$(C_PIECES:%=${ARCH}/%.o)
Index: c/src/lib/libnetworking/rtems/rtems_glue.c
===================================================================
--- c/src/lib/libnetworking/rtems/rtems_glue.c	(revision 6c980128774e4359b32d7325a987283c0635869f)
+++ c/src/lib/libnetworking/rtems/rtems_glue.c	(revision cca44008d81209e9fa992157637d9de0384e0536)
@@ -30,17 +30,4 @@
 #include <net/netisr.h>
 #include <net/route.h>
-
-/*
- * Events used by networking routines.
- * Everything will break if the application
- * tries to use these events or if the `sleep'
- * events are equal to any of the NETISR * events.
- */
-#define SBWAIT_EVENT	RTEMS_EVENT_24
-#define SOSLEEP_EVENT	RTEMS_EVENT_25
-#define NETISR_IP_EVENT		(1 << NETISR_IP)
-#define NETISR_ARP_EVENT	(1 << NETISR_ARP)
-#define NETISR_EVENTS	(NETISR_IP_EVENT|NETISR_ARP_EVENT)
-
 
 /*
@@ -247,5 +234,5 @@
 	 * Register as an external I/O handler
 	 */
-	rtems_register_libio_handler (RTEMS_FILE_DESCRIPTOR_TYPE_SOCKET,
+	rtems_register_libio_handler (LIBIO_FLAGS_HANDLER_SOCK,
 						&rtems_bsdnet_io_handler);
 
@@ -306,15 +293,8 @@
 	 * Set this task as the target of the wakeup operation.
 	 */
-	if (sb->sb_sel.si_pid)
-		rtems_panic ("Another task is already sleeping on that socket buffer");
 	rtems_task_ident (RTEMS_SELF, 0, &tid);
 	sb->sb_sel.si_pid = tid;
 
 	/*
-	 * Mark the socket buffer as waiting.
-	 */
-	sb->sb_flags |= SB_WAIT;
-
-	/*
 	 * Release the network semaphore.
 	 */
@@ -330,10 +310,4 @@
 	 */
 	rtems_bsdnet_semaphore_obtain ();
-
-	/*
-	 * Relinquish ownership of the socket buffer
-	 */
-	sb->sb_flags &= ~SB_WAIT;
-	sb->sb_sel.si_pid = 0;
 
 	/*
@@ -356,7 +330,9 @@
 	register struct sockbuf *sb;
 {
-	if (sb->sb_flags & SB_WAIT) {
-		sb->sb_flags &= ~SB_WAIT;
-		rtems_event_send (sb->sb_sel.si_pid, SBWAIT_EVENT);
+	rtems_id tid;
+
+	if ((tid = sb->sb_sel.si_pid) != 0) {
+		sb->sb_sel.si_pid = 0;
+		rtems_event_send (tid, SBWAIT_EVENT);
 	}
 }
Index: c/src/lib/libnetworking/rtems/rtems_syscall.c
===================================================================
--- c/src/lib/libnetworking/rtems/rtems_syscall.c	(revision 6c980128774e4359b32d7325a987283c0635869f)
+++ c/src/lib/libnetworking/rtems/rtems_syscall.c	(revision cca44008d81209e9fa992157637d9de0384e0536)
@@ -25,75 +25,4 @@
 #include <net/if.h>
 #include <net/route.h>
-
-/*
- *********************************************************************
- *              Map RTEMS file descriptor to BSD socket              *
- *********************************************************************
- */
-struct fdsock {
-	int		indexFreeNext;
-	struct socket	*sock;
-};
-static struct fdsock *fdsock;
-static int fdsockCount;
-static int indexFreeHead = -1;
-
-/*
- * Convert an RTEMS file descriptor to a BSD socket pointer.
- */
-static struct socket *
-fdToSocket (int fd)
-{
-	int i;
-	struct socket *s;
-
-	if ((fd < 0)
-	 || (rtems_file_descriptor_type(fd) != RTEMS_FILE_DESCRIPTOR_TYPE_SOCKET)
-	 || ((i = rtems_file_descriptor_base(fd)) >= fdsockCount)
-	 || ((s = fdsock[i].sock) == NULL)) {
-		errno = EBADF;
-		return NULL;
-	}
-	return s;
-}
-
-/*
- * Enlarge the size of the file-descritor/socket pointer map.
- */
-static int
-enlargeFdMap (void)
-{
-	struct fdsock *nfdsock;
-	int i;
-
-	nfdsock = realloc (fdsock, sizeof *fdsock * (fdsockCount + 20));
-	if (nfdsock == NULL) {
-		errno = ENFILE;
-		return 0;
-	}
-	fdsock = nfdsock;
-	for (i = fdsockCount, fdsockCount += 20 ; i < fdsockCount ; i++) {
-		fdsock[i].sock = NULL;
-		fdsock[i].indexFreeNext = indexFreeHead;
-		indexFreeHead = i;
-	}
-	return 1;
-}
-
-/*
- * Create a file descriptor for a new socket
- */
-static int
-makeFd (struct socket *s)
-{
-	int i;
-
-	if ((indexFreeHead < 0) && !enlargeFdMap ())
-		return -1;
-	i = indexFreeHead;
-	indexFreeHead = fdsock[i].indexFreeNext;
-	fdsock[i].sock = s;
-	return rtems_make_file_descriptor(i,RTEMS_FILE_DESCRIPTOR_TYPE_SOCKET);
-}
 
 /*
@@ -129,5 +58,5 @@
 socket (int domain, int type, int protocol)
 {
-	int fd = -1;
+	int fd;
 	int error;
 	struct socket *so;
@@ -136,5 +65,5 @@
 	error = socreate(domain, &so, type, protocol, NULL);
 	if (error == 0) {
-		fd = makeFd (so);
+		fd = rtems_bsdnet_makeFdForSocket (so);
 		if (fd < 0)
 			soclose (so);
@@ -157,5 +86,5 @@
 
 	rtems_bsdnet_semaphore_obtain ();
-	if ((so = fdToSocket (s)) != NULL) {
+	if ((so = rtems_bsdnet_fdToSocket (s)) != NULL) {
 		error = sockargstombuf (&nam, name, namelen, MT_SONAME);
 		if (error == 0) {
@@ -184,5 +113,5 @@
 
 	rtems_bsdnet_semaphore_obtain ();
-	if ((so = fdToSocket (s)) == NULL) {
+	if ((so = rtems_bsdnet_fdToSocket (s)) == NULL) {
 		rtems_bsdnet_semaphore_release ();
 		return -1;
@@ -234,5 +163,5 @@
 
 	rtems_bsdnet_semaphore_obtain ();
-	if ((so = fdToSocket (s)) != NULL) {
+	if ((so = rtems_bsdnet_fdToSocket (s)) != NULL) {
 		error = solisten (so, backlog);
 		if (error == 0)
@@ -253,5 +182,5 @@
 
 	rtems_bsdnet_semaphore_obtain ();
-	if ((head = fdToSocket (s)) == NULL) {
+	if ((head = rtems_bsdnet_fdToSocket (s)) == NULL) {
 		rtems_bsdnet_semaphore_release ();
 		return -1;
@@ -285,5 +214,5 @@
 	head->so_qlen--;
 
-	fd = makeFd (so);
+	fd = rtems_bsdnet_makeFdForSocket (so);
 	if (fd < 0) {
 		TAILQ_INSERT_HEAD(&head->so_comp, so, so_list);
@@ -326,5 +255,5 @@
 
 	rtems_bsdnet_semaphore_obtain ();
-	if ((so = fdToSocket (s)) == NULL) {
+	if ((so = rtems_bsdnet_fdToSocket (s)) == NULL) {
 		rtems_bsdnet_semaphore_release ();
 		return -1;
@@ -429,5 +358,5 @@
 
 	rtems_bsdnet_semaphore_obtain ();
-	if ((so = fdToSocket (s)) == NULL) {
+	if ((so = rtems_bsdnet_fdToSocket (s)) == NULL) {
 		rtems_bsdnet_semaphore_release ();
 		return -1;
@@ -548,5 +477,5 @@
 
 	rtems_bsdnet_semaphore_obtain ();
-	if ((so = fdToSocket (s)) == NULL) {
+	if ((so = rtems_bsdnet_fdToSocket (s)) == NULL) {
 		rtems_bsdnet_semaphore_release ();
 		return -1;
@@ -585,5 +514,5 @@
 
 	rtems_bsdnet_semaphore_obtain ();
-	if ((so = fdToSocket (s)) == NULL) {
+	if ((so = rtems_bsdnet_fdToSocket (s)) == NULL) {
 		rtems_bsdnet_semaphore_release ();
 		return -1;
@@ -627,5 +556,5 @@
 
 	rtems_bsdnet_semaphore_obtain ();
-	if ((so = fdToSocket (s)) == NULL) {
+	if ((so = rtems_bsdnet_fdToSocket (s)) == NULL) {
 		rtems_bsdnet_semaphore_release ();
 		return -1;
@@ -677,14 +606,10 @@
 	struct socket *so;
 	int error;
-	int i;
-
-	rtems_bsdnet_semaphore_obtain ();
-	if ((so = fdToSocket (fd)) == NULL) {
-		rtems_bsdnet_semaphore_release ();
-		return -1;
-	}
-	i = rtems_file_descriptor_base(fd);
-	fdsock[i].indexFreeNext = indexFreeHead;;
-	indexFreeHead = i;
+
+	rtems_bsdnet_semaphore_obtain ();
+	if ((so = rtems_bsdnet_fdToSocket (fd)) == NULL) {
+		rtems_bsdnet_semaphore_release ();
+		return -1;
+	}
 	error = soclose (so);
 	rtems_bsdnet_semaphore_release ();
@@ -738,5 +663,5 @@
 
 	rtems_bsdnet_semaphore_obtain ();
-	if ((so = fdToSocket (fd)) == NULL) {
+	if ((so = rtems_bsdnet_fdToSocket (fd)) == NULL) {
 		rtems_bsdnet_semaphore_release ();
 		return -1;
Index: c/src/lib/libnetworking/sys/sockio.h
===================================================================
--- c/src/lib/libnetworking/sys/sockio.h	(revision 6c980128774e4359b32d7325a987283c0635869f)
+++ c/src/lib/libnetworking/sys/sockio.h	(revision cca44008d81209e9fa992157637d9de0384e0536)
@@ -84,10 +84,9 @@
 #define	SIOCGIFMEDIA	_IOWR('i', 56, struct ifmediareq) /* get net media */
 
-
 /*
  * RTEMS additions for setting/getting `tap' function on incoming packets.
  */
-#define SIOCSIFTAP	 _IOW('i', 80, struct ifreq)	/* set tap function */
-#define SIOCGIFTAP	_IOWR('i', 81, struct ifreq)	/* get tap function */
+#define SIOCSIFTAP	_IOW('i', 80, struct ifreq)	/* set tap function */
+#define SIOCGIFTAP	_IOW('i', 81, struct ifreq)	/* get tap function */
 
 #endif /* !_SYS_SOCKIO_H_ */
Index: c/src/libnetworking/rtems/Makefile.in
===================================================================
--- c/src/libnetworking/rtems/Makefile.in	(revision 6c980128774e4359b32d7325a987283c0635869f)
+++ c/src/libnetworking/rtems/Makefile.in	(revision cca44008d81209e9fa992157637d9de0384e0536)
@@ -17,5 +17,5 @@
         rtems_showmbuf rtems_showroute \
         rtems_showifstat rtems_showipstat rtems_showicmpstat \
-        rtems_showtcpstat rtems_showudpstat
+        rtems_showtcpstat rtems_showudpstat rtems_select
 C_FILES=$(C_PIECES:%=%.c)
 C_O_FILES=$(C_PIECES:%=${ARCH}/%.o)
Index: c/src/libnetworking/rtems/rtems_glue.c
===================================================================
--- c/src/libnetworking/rtems/rtems_glue.c	(revision 6c980128774e4359b32d7325a987283c0635869f)
+++ c/src/libnetworking/rtems/rtems_glue.c	(revision cca44008d81209e9fa992157637d9de0384e0536)
@@ -30,17 +30,4 @@
 #include <net/netisr.h>
 #include <net/route.h>
-
-/*
- * Events used by networking routines.
- * Everything will break if the application
- * tries to use these events or if the `sleep'
- * events are equal to any of the NETISR * events.
- */
-#define SBWAIT_EVENT	RTEMS_EVENT_24
-#define SOSLEEP_EVENT	RTEMS_EVENT_25
-#define NETISR_IP_EVENT		(1 << NETISR_IP)
-#define NETISR_ARP_EVENT	(1 << NETISR_ARP)
-#define NETISR_EVENTS	(NETISR_IP_EVENT|NETISR_ARP_EVENT)
-
 
 /*
@@ -247,5 +234,5 @@
 	 * Register as an external I/O handler
 	 */
-	rtems_register_libio_handler (RTEMS_FILE_DESCRIPTOR_TYPE_SOCKET,
+	rtems_register_libio_handler (LIBIO_FLAGS_HANDLER_SOCK,
 						&rtems_bsdnet_io_handler);
 
@@ -306,15 +293,8 @@
 	 * Set this task as the target of the wakeup operation.
 	 */
-	if (sb->sb_sel.si_pid)
-		rtems_panic ("Another task is already sleeping on that socket buffer");
 	rtems_task_ident (RTEMS_SELF, 0, &tid);
 	sb->sb_sel.si_pid = tid;
 
 	/*
-	 * Mark the socket buffer as waiting.
-	 */
-	sb->sb_flags |= SB_WAIT;
-
-	/*
 	 * Release the network semaphore.
 	 */
@@ -330,10 +310,4 @@
 	 */
 	rtems_bsdnet_semaphore_obtain ();
-
-	/*
-	 * Relinquish ownership of the socket buffer
-	 */
-	sb->sb_flags &= ~SB_WAIT;
-	sb->sb_sel.si_pid = 0;
 
 	/*
@@ -356,7 +330,9 @@
 	register struct sockbuf *sb;
 {
-	if (sb->sb_flags & SB_WAIT) {
-		sb->sb_flags &= ~SB_WAIT;
-		rtems_event_send (sb->sb_sel.si_pid, SBWAIT_EVENT);
+	rtems_id tid;
+
+	if ((tid = sb->sb_sel.si_pid) != 0) {
+		sb->sb_sel.si_pid = 0;
+		rtems_event_send (tid, SBWAIT_EVENT);
 	}
 }
Index: c/src/libnetworking/rtems/rtems_syscall.c
===================================================================
--- c/src/libnetworking/rtems/rtems_syscall.c	(revision 6c980128774e4359b32d7325a987283c0635869f)
+++ c/src/libnetworking/rtems/rtems_syscall.c	(revision cca44008d81209e9fa992157637d9de0384e0536)
@@ -25,75 +25,4 @@
 #include <net/if.h>
 #include <net/route.h>
-
-/*
- *********************************************************************
- *              Map RTEMS file descriptor to BSD socket              *
- *********************************************************************
- */
-struct fdsock {
-	int		indexFreeNext;
-	struct socket	*sock;
-};
-static struct fdsock *fdsock;
-static int fdsockCount;
-static int indexFreeHead = -1;
-
-/*
- * Convert an RTEMS file descriptor to a BSD socket pointer.
- */
-static struct socket *
-fdToSocket (int fd)
-{
-	int i;
-	struct socket *s;
-
-	if ((fd < 0)
-	 || (rtems_file_descriptor_type(fd) != RTEMS_FILE_DESCRIPTOR_TYPE_SOCKET)
-	 || ((i = rtems_file_descriptor_base(fd)) >= fdsockCount)
-	 || ((s = fdsock[i].sock) == NULL)) {
-		errno = EBADF;
-		return NULL;
-	}
-	return s;
-}
-
-/*
- * Enlarge the size of the file-descritor/socket pointer map.
- */
-static int
-enlargeFdMap (void)
-{
-	struct fdsock *nfdsock;
-	int i;
-
-	nfdsock = realloc (fdsock, sizeof *fdsock * (fdsockCount + 20));
-	if (nfdsock == NULL) {
-		errno = ENFILE;
-		return 0;
-	}
-	fdsock = nfdsock;
-	for (i = fdsockCount, fdsockCount += 20 ; i < fdsockCount ; i++) {
-		fdsock[i].sock = NULL;
-		fdsock[i].indexFreeNext = indexFreeHead;
-		indexFreeHead = i;
-	}
-	return 1;
-}
-
-/*
- * Create a file descriptor for a new socket
- */
-static int
-makeFd (struct socket *s)
-{
-	int i;
-
-	if ((indexFreeHead < 0) && !enlargeFdMap ())
-		return -1;
-	i = indexFreeHead;
-	indexFreeHead = fdsock[i].indexFreeNext;
-	fdsock[i].sock = s;
-	return rtems_make_file_descriptor(i,RTEMS_FILE_DESCRIPTOR_TYPE_SOCKET);
-}
 
 /*
@@ -129,5 +58,5 @@
 socket (int domain, int type, int protocol)
 {
-	int fd = -1;
+	int fd;
 	int error;
 	struct socket *so;
@@ -136,5 +65,5 @@
 	error = socreate(domain, &so, type, protocol, NULL);
 	if (error == 0) {
-		fd = makeFd (so);
+		fd = rtems_bsdnet_makeFdForSocket (so);
 		if (fd < 0)
 			soclose (so);
@@ -157,5 +86,5 @@
 
 	rtems_bsdnet_semaphore_obtain ();
-	if ((so = fdToSocket (s)) != NULL) {
+	if ((so = rtems_bsdnet_fdToSocket (s)) != NULL) {
 		error = sockargstombuf (&nam, name, namelen, MT_SONAME);
 		if (error == 0) {
@@ -184,5 +113,5 @@
 
 	rtems_bsdnet_semaphore_obtain ();
-	if ((so = fdToSocket (s)) == NULL) {
+	if ((so = rtems_bsdnet_fdToSocket (s)) == NULL) {
 		rtems_bsdnet_semaphore_release ();
 		return -1;
@@ -234,5 +163,5 @@
 
 	rtems_bsdnet_semaphore_obtain ();
-	if ((so = fdToSocket (s)) != NULL) {
+	if ((so = rtems_bsdnet_fdToSocket (s)) != NULL) {
 		error = solisten (so, backlog);
 		if (error == 0)
@@ -253,5 +182,5 @@
 
 	rtems_bsdnet_semaphore_obtain ();
-	if ((head = fdToSocket (s)) == NULL) {
+	if ((head = rtems_bsdnet_fdToSocket (s)) == NULL) {
 		rtems_bsdnet_semaphore_release ();
 		return -1;
@@ -285,5 +214,5 @@
 	head->so_qlen--;
 
-	fd = makeFd (so);
+	fd = rtems_bsdnet_makeFdForSocket (so);
 	if (fd < 0) {
 		TAILQ_INSERT_HEAD(&head->so_comp, so, so_list);
@@ -326,5 +255,5 @@
 
 	rtems_bsdnet_semaphore_obtain ();
-	if ((so = fdToSocket (s)) == NULL) {
+	if ((so = rtems_bsdnet_fdToSocket (s)) == NULL) {
 		rtems_bsdnet_semaphore_release ();
 		return -1;
@@ -429,5 +358,5 @@
 
 	rtems_bsdnet_semaphore_obtain ();
-	if ((so = fdToSocket (s)) == NULL) {
+	if ((so = rtems_bsdnet_fdToSocket (s)) == NULL) {
 		rtems_bsdnet_semaphore_release ();
 		return -1;
@@ -548,5 +477,5 @@
 
 	rtems_bsdnet_semaphore_obtain ();
-	if ((so = fdToSocket (s)) == NULL) {
+	if ((so = rtems_bsdnet_fdToSocket (s)) == NULL) {
 		rtems_bsdnet_semaphore_release ();
 		return -1;
@@ -585,5 +514,5 @@
 
 	rtems_bsdnet_semaphore_obtain ();
-	if ((so = fdToSocket (s)) == NULL) {
+	if ((so = rtems_bsdnet_fdToSocket (s)) == NULL) {
 		rtems_bsdnet_semaphore_release ();
 		return -1;
@@ -627,5 +556,5 @@
 
 	rtems_bsdnet_semaphore_obtain ();
-	if ((so = fdToSocket (s)) == NULL) {
+	if ((so = rtems_bsdnet_fdToSocket (s)) == NULL) {
 		rtems_bsdnet_semaphore_release ();
 		return -1;
@@ -677,14 +606,10 @@
 	struct socket *so;
 	int error;
-	int i;
-
-	rtems_bsdnet_semaphore_obtain ();
-	if ((so = fdToSocket (fd)) == NULL) {
-		rtems_bsdnet_semaphore_release ();
-		return -1;
-	}
-	i = rtems_file_descriptor_base(fd);
-	fdsock[i].indexFreeNext = indexFreeHead;;
-	indexFreeHead = i;
+
+	rtems_bsdnet_semaphore_obtain ();
+	if ((so = rtems_bsdnet_fdToSocket (fd)) == NULL) {
+		rtems_bsdnet_semaphore_release ();
+		return -1;
+	}
 	error = soclose (so);
 	rtems_bsdnet_semaphore_release ();
@@ -738,5 +663,5 @@
 
 	rtems_bsdnet_semaphore_obtain ();
-	if ((so = fdToSocket (fd)) == NULL) {
+	if ((so = rtems_bsdnet_fdToSocket (fd)) == NULL) {
 		rtems_bsdnet_semaphore_release ();
 		return -1;
Index: c/src/libnetworking/sys/sockio.h
===================================================================
--- c/src/libnetworking/sys/sockio.h	(revision 6c980128774e4359b32d7325a987283c0635869f)
+++ c/src/libnetworking/sys/sockio.h	(revision cca44008d81209e9fa992157637d9de0384e0536)
@@ -84,10 +84,9 @@
 #define	SIOCGIFMEDIA	_IOWR('i', 56, struct ifmediareq) /* get net media */
 
-
 /*
  * RTEMS additions for setting/getting `tap' function on incoming packets.
  */
-#define SIOCSIFTAP	 _IOW('i', 80, struct ifreq)	/* set tap function */
-#define SIOCGIFTAP	_IOWR('i', 81, struct ifreq)	/* get tap function */
+#define SIOCSIFTAP	_IOW('i', 80, struct ifreq)	/* set tap function */
+#define SIOCGIFTAP	_IOW('i', 81, struct ifreq)	/* get tap function */
 
 #endif /* !_SYS_SOCKIO_H_ */
Index: configure
===================================================================
--- configure	(revision 6c980128774e4359b32d7325a987283c0635869f)
+++ configure	(revision cca44008d81209e9fa992157637d9de0384e0536)
@@ -807,5 +807,5 @@
 esac
 else
-  RTEMS_HAS_RDBG=yes
+  RTEMS_HAS_RDBG=no
 fi
 
Index: configure.in
===================================================================
--- configure.in	(revision 6c980128774e4359b32d7325a987283c0635869f)
+++ configure.in	(revision cca44008d81209e9fa992157637d9de0384e0536)
@@ -50,5 +50,5 @@
   no) RTEMS_HAS_RDBG=no ;;
   *)  AC_MSG_ERROR(bad value ${enableval} for enable-rdbg option) ;;
-esac],[RTEMS_HAS_RDBG=yes])
+esac],[RTEMS_HAS_RDBG=no])
 
 AC_ARG_ENABLE(rtems-inlines, \
Index: cpukit/include/rtems/libio_.h
===================================================================
--- cpukit/include/rtems/libio_.h	(revision 6c980128774e4359b32d7325a987283c0635869f)
+++ cpukit/include/rtems/libio_.h	(revision cca44008d81209e9fa992157637d9de0384e0536)
@@ -66,4 +66,5 @@
 extern rtems_libio_t  *rtems_libio_iops;
 extern rtems_libio_t  *rtems_libio_last_iop;
+extern rtems_libio_t *rtems_libio_iop_freelist;
 
 /*
Index: cpukit/libcsupport/include/rtems/libio.h
===================================================================
--- cpukit/libcsupport/include/rtems/libio.h	(revision 6c980128774e4359b32d7325a987283c0635869f)
+++ cpukit/libcsupport/include/rtems/libio.h	(revision cca44008d81209e9fa992157637d9de0384e0536)
@@ -394,9 +394,13 @@
 #define LIBIO_FLAGS_READ          0x0002  /* reading */
 #define LIBIO_FLAGS_WRITE         0x0004  /* writing */
-#define LIBIO_FLAGS_LINE_BUFFERED 0x0008  /* line buffered io (^h, ^u, etc) */
 #define LIBIO_FLAGS_OPEN          0x0100  /* device is open */
 #define LIBIO_FLAGS_APPEND        0x0200  /* all writes append */
 #define LIBIO_FLAGS_CREATE        0x0400  /* create file */
 #define LIBIO_FLAGS_CLOSE_ON_EXEC 0x0800  /* close on process exec() */
+
+#define LIBIO_FLAGS_HANDLER_SHIFT 12
+#define LIBIO_FLAGS_HANDLER_MASK  0xF000  /* mask for external handler type */
+#define LIBIO_FLAGS_HANDLER_RTEMS 0x0000  /* `traditional' RTEMS I/O */
+#define LIBIO_FLAGS_HANDLER_SOCK  0x1000  /* BSD socket */
 
 #define LIBIO_FLAGS_READ_WRITE    (LIBIO_FLAGS_READ | LIBIO_FLAGS_WRITE)
Index: cpukit/libcsupport/include/rtems/libio_.h
===================================================================
--- cpukit/libcsupport/include/rtems/libio_.h	(revision 6c980128774e4359b32d7325a987283c0635869f)
+++ cpukit/libcsupport/include/rtems/libio_.h	(revision cca44008d81209e9fa992157637d9de0384e0536)
@@ -66,4 +66,5 @@
 extern rtems_libio_t  *rtems_libio_iops;
 extern rtems_libio_t  *rtems_libio_last_iop;
+extern rtems_libio_t *rtems_libio_iop_freelist;
 
 /*
Index: cpukit/libcsupport/include/sys/sockio.h
===================================================================
--- cpukit/libcsupport/include/sys/sockio.h	(revision 6c980128774e4359b32d7325a987283c0635869f)
+++ cpukit/libcsupport/include/sys/sockio.h	(revision cca44008d81209e9fa992157637d9de0384e0536)
@@ -84,10 +84,9 @@
 #define	SIOCGIFMEDIA	_IOWR('i', 56, struct ifmediareq) /* get net media */
 
-
 /*
  * RTEMS additions for setting/getting `tap' function on incoming packets.
  */
-#define SIOCSIFTAP	 _IOW('i', 80, struct ifreq)	/* set tap function */
-#define SIOCGIFTAP	_IOWR('i', 81, struct ifreq)	/* get tap function */
+#define SIOCSIFTAP	_IOW('i', 80, struct ifreq)	/* set tap function */
+#define SIOCGIFTAP	_IOW('i', 81, struct ifreq)	/* get tap function */
 
 #endif /* !_SYS_SOCKIO_H_ */
Index: cpukit/libcsupport/src/close.c
===================================================================
--- cpukit/libcsupport/src/close.c	(revision 6c980128774e4359b32d7325a987283c0635869f)
+++ cpukit/libcsupport/src/close.c	(revision cca44008d81209e9fa992157637d9de0384e0536)
@@ -23,8 +23,11 @@
   int                 status;
 
-  if ( rtems_file_descriptor_type( fd ) ) {
+  rtems_libio_check_fd(fd);
+  iop = rtems_libio_iop(fd);
+  if ( iop->flags & LIBIO_FLAGS_HANDLER_MASK ) {
     int (*fp)(int  fd);
 
-    fp = rtems_libio_handlers[rtems_file_descriptor_type_index(fd)].close;
+    fp = rtems_libio_handlers[
+            (iop->flags >> LIBIO_FLAGS_HANDLER_SHIFT) - 1].close;
     if ( fp == NULL )
       set_errno_and_return_minus_one( EBADF );
@@ -32,6 +35,4 @@
     return status;
   }
-  iop = rtems_libio_iop(fd);
-  rtems_libio_check_fd(fd);
 
   if ( !iop->handlers )
Index: cpukit/libcsupport/src/fchmod.c
===================================================================
--- cpukit/libcsupport/src/fchmod.c	(revision 6c980128774e4359b32d7325a987283c0635869f)
+++ cpukit/libcsupport/src/fchmod.c	(revision cca44008d81209e9fa992157637d9de0384e0536)
@@ -28,9 +28,12 @@
   rtems_libio_t *iop;
   
+  rtems_libio_check_fd( fd );
+  iop = rtems_libio_iop( fd );
+
   /*
    *  If this is not a file system based entity, it is an error.
    */
 
-  if ( rtems_file_descriptor_type( fd ) )
+  if ( iop->flags & LIBIO_FLAGS_HANDLER_MASK )
     set_errno_and_return_minus_one( EBADF );
 
@@ -39,6 +42,4 @@
    */
 
-  iop = rtems_libio_iop( fd );
-  rtems_libio_check_fd( fd );
   rtems_libio_check_permissions( iop, LIBIO_FLAGS_WRITE );
 
Index: cpukit/libcsupport/src/fcntl.c
===================================================================
--- cpukit/libcsupport/src/fcntl.c	(revision 6c980128774e4359b32d7325a987283c0635869f)
+++ cpukit/libcsupport/src/fcntl.c	(revision cca44008d81209e9fa992157637d9de0384e0536)
@@ -33,9 +33,12 @@
   va_start( ap, cmd );
 
+  rtems_libio_check_fd( fd );
+  iop = rtems_libio_iop( fd );
+
   /*
    *  If this is not a file system based entity, it is an error.
    */
 
-  if ( rtems_file_descriptor_type( fd ) )
+  if ( iop->flags & LIBIO_FLAGS_HANDLER_MASK )
     set_errno_and_return_minus_one( EBADF );
 
@@ -43,7 +46,4 @@
    *  Now process the fcntl().
    */
-
-  iop = rtems_libio_iop( fd );
-  rtems_libio_check_fd( fd );
 
   /*
Index: cpukit/libcsupport/src/fdatasync.c
===================================================================
--- cpukit/libcsupport/src/fdatasync.c	(revision 6c980128774e4359b32d7325a987283c0635869f)
+++ cpukit/libcsupport/src/fdatasync.c	(revision cca44008d81209e9fa992157637d9de0384e0536)
@@ -23,4 +23,8 @@
   rtems_libio_t *iop;
 
+  rtems_libio_check_fd( fd );
+  iop = rtems_libio_iop( fd );
+  rtems_libio_check_permissions( iop, LIBIO_FLAGS_WRITE );
+
   /*
    *  If this file descriptor is mapped to an external set of handlers,
@@ -28,5 +32,5 @@
    */
 
-  if ( rtems_file_descriptor_type( fd ) )
+  if ( iop->flags & LIBIO_FLAGS_HANDLER_MASK )
     set_errno_and_return_minus_one( EBADF );
 
@@ -35,8 +39,4 @@
    */
 
-  iop = rtems_libio_iop( fd );
-  rtems_libio_check_fd( fd );
-  rtems_libio_check_permissions( iop, LIBIO_FLAGS_WRITE );
-
   if ( !iop->handlers->fdatasync )
     set_errno_and_return_minus_one( ENOTSUP );
Index: cpukit/libcsupport/src/fpathconf.c
===================================================================
--- cpukit/libcsupport/src/fpathconf.c	(revision 6c980128774e4359b32d7325a987283c0635869f)
+++ cpukit/libcsupport/src/fpathconf.c	(revision cca44008d81209e9fa992157637d9de0384e0536)
@@ -27,4 +27,8 @@
   rtems_filesystem_limits_and_options_t  *the_limits;
 
+  rtems_libio_check_fd(fd);
+  iop = rtems_libio_iop(fd);
+  rtems_libio_check_permissions(iop, LIBIO_FLAGS_READ);
+
   /*
    *  If this file descriptor is mapped to an external set of handlers,
@@ -33,5 +37,5 @@
    */
 
-  if ( rtems_file_descriptor_type( fd ) )
+  if ( iop->flags & LIBIO_FLAGS_HANDLER_MASK )
     set_errno_and_return_minus_one( EBADF );
 
@@ -39,8 +43,4 @@
    *  Now process the information request.
    */
-
-  iop = rtems_libio_iop(fd);
-  rtems_libio_check_fd(fd);
-  rtems_libio_check_permissions(iop, LIBIO_FLAGS_READ);
 
   the_limits = &iop->pathinfo.mt_entry->pathconf_limits_and_options;
Index: cpukit/libcsupport/src/fsync.c
===================================================================
--- cpukit/libcsupport/src/fsync.c	(revision 6c980128774e4359b32d7325a987283c0635869f)
+++ cpukit/libcsupport/src/fsync.c	(revision cca44008d81209e9fa992157637d9de0384e0536)
@@ -23,4 +23,8 @@
   rtems_libio_t *iop;
 
+  rtems_libio_check_fd( fd );
+  iop = rtems_libio_iop( fd );
+  rtems_libio_check_permissions( iop, LIBIO_FLAGS_WRITE );
+
   /*
    *  If this file descriptor is mapped to an external set of handlers,
@@ -28,5 +32,5 @@
    */
 
-  if ( rtems_file_descriptor_type( fd ) )
+  if ( iop->flags & LIBIO_FLAGS_HANDLER_MASK )
     set_errno_and_return_minus_one( EBADF );
 
@@ -35,8 +39,4 @@
    */
 
-  iop = rtems_libio_iop( fd );
-  rtems_libio_check_fd( fd );
-  rtems_libio_check_permissions( iop, LIBIO_FLAGS_WRITE );
-
   if ( !iop->handlers->fsync )
     set_errno_and_return_minus_one( ENOTSUP );
Index: cpukit/libcsupport/src/ftruncate.c
===================================================================
--- cpukit/libcsupport/src/ftruncate.c	(revision 6c980128774e4359b32d7325a987283c0635869f)
+++ cpukit/libcsupport/src/ftruncate.c	(revision cca44008d81209e9fa992157637d9de0384e0536)
@@ -26,9 +26,12 @@
   rtems_filesystem_location_info_t  loc;
   
+  rtems_libio_check_fd( fd );
+  iop = rtems_libio_iop( fd );
+
   /*
    *  If this is not a file system based entity, it is an error.
    */
 
-  if ( rtems_file_descriptor_type( fd ) )
+  if ( iop->flags & LIBIO_FLAGS_HANDLER_MASK )
     set_errno_and_return_minus_one( EBADF );
 
@@ -37,6 +40,4 @@
    */
   
-  iop = rtems_libio_iop( fd );
-
   /*
    *  Make sure we are not working on a directory
@@ -50,5 +51,4 @@
     set_errno_and_return_minus_one( EISDIR );
 
-  rtems_libio_check_fd( fd );
   rtems_libio_check_permissions( iop, LIBIO_FLAGS_WRITE );
 
Index: cpukit/libcsupport/src/ioctl.c
===================================================================
--- cpukit/libcsupport/src/ioctl.c	(revision 6c980128774e4359b32d7325a987283c0635869f)
+++ cpukit/libcsupport/src/ioctl.c	(revision cca44008d81209e9fa992157637d9de0384e0536)
@@ -27,4 +27,7 @@
   rtems_libio_t     *iop;
 
+  rtems_libio_check_fd( fd );
+  iop = rtems_libio_iop( fd );
+
   /*
    *  If this file descriptor is mapped to an external set of handlers,
@@ -32,8 +35,9 @@
    */ 
 
-  if ( rtems_file_descriptor_type( fd ) ) {
+  if ( iop->flags & LIBIO_FLAGS_HANDLER_MASK ) {
     rtems_libio_ioctl_t fp;
 
-    fp = rtems_libio_handlers[rtems_file_descriptor_type_index(fd)].ioctl;
+    fp = rtems_libio_handlers[
+           (iop->flags >> LIBIO_FLAGS_HANDLER_SHIFT) - 1].ioctl;
     if ( fp == NULL )
       set_errno_and_return_minus_one( EBADF );
@@ -46,7 +50,4 @@
    */
 
-  iop = rtems_libio_iop( fd );
-  rtems_libio_check_fd( fd );
-
   if ( !iop->handlers->ioctl )
     set_errno_and_return_minus_one( ENOTSUP );
Index: cpukit/libcsupport/src/libio.c
===================================================================
--- cpukit/libcsupport/src/libio.c	(revision 6c980128774e4359b32d7325a987283c0635869f)
+++ cpukit/libcsupport/src/libio.c	(revision cca44008d81209e9fa992157637d9de0384e0536)
@@ -4,7 +4,4 @@
  *  POSIX system calls like open(), read, fstat(), etc.
  *
- *  This provides the foundation for POSIX compliant IO system calls
- *  for RTEMS.
- *
  *  COPYRIGHT (c) 1989-1998.
  *  On-Line Applications Research Corporation (OAR).
@@ -19,14 +16,52 @@
 
 #include "libio_.h"                   /* libio_.h pulls in rtems */
-
-/*
- *  Global variables used to manage the File Descriptor Table.
- *  IOP = IO Pointer.
- */
-
-rtems_id                rtems_libio_semaphore;
-rtems_libio_t          *rtems_libio_iops;
-rtems_libio_t          *rtems_libio_last_iop;
+#include <rtems.h>
+#include <rtems/assoc.h>                /* assoc.h not included by rtems.h */
+
+#include <stdio.h>                      /* O_RDONLY, et.al. */
+#include <fcntl.h>                      /* O_RDONLY, et.al. */
+#include <assert.h>
+#include <errno.h>
+
+#if ! defined(O_NDELAY)
+# if defined(solaris2)
+#  define O_NDELAY O_NONBLOCK
+# elif defined(RTEMS_NEWLIB)
+#  define O_NDELAY _FNBIO
+# endif
+#endif
+
+
+#include <errno.h>
+#include <string.h>                     /* strcmp */
+#include <unistd.h>
+#include <stdlib.h>                     /* calloc() */
+
+#include "libio.h"                      /* libio.h not pulled in by rtems */
+
+/*                            
+ *  File descriptor Table Information
+ */         
+
+extern unsigned32  rtems_libio_number_iops;
+rtems_id           rtems_libio_semaphore;
+rtems_libio_t     *rtems_libio_iops;
+rtems_libio_t     *rtems_libio_last_iop;
+rtems_libio_t     *rtems_libio_iop_freelist;
+
+/*
+ *  External I/O Handlers Table
+ *
+ *  Space for all possible handlers is preallocated
+ *  to speed up dispatch to external handlers.
+ */
+
 rtems_libio_handler_t   rtems_libio_handlers[15];
+
+/*
+ *  Default mode for all files.
+ */
+
+mode_t    rtems_filesystem_umask;
 
 /*
@@ -45,6 +80,5 @@
 )
 {
-  int handler_index = rtems_file_descriptor_type_index( handler_flag );
-
+  int handler_index = (handler_flag >> LIBIO_FLAGS_HANDLER_SHIFT) - 1;
 
   if ((handler_index < 0) || (handler_index >= 15))
@@ -62,19 +96,20 @@
 void rtems_libio_init( void )
 {
-  rtems_status_code rc;
-
-  /*
-   *  Allocate memory for the IOP Table
-   */
-
-  if ( rtems_libio_number_iops > 0 ) {
-    rtems_libio_iops =
-      (rtems_libio_t *) calloc(rtems_libio_number_iops, sizeof(rtems_libio_t));
-
-    if (rtems_libio_iops == NULL)
-        rtems_fatal_error_occurred( RTEMS_NO_MEMORY );
-
-    rtems_libio_last_iop = rtems_libio_iops + (rtems_libio_number_iops - 1);
-  }
+    rtems_status_code rc;
+    int i;
+    rtems_libio_t *iop;
+
+    if (rtems_libio_number_iops > 0)
+    {
+        rtems_libio_iops = (rtems_libio_t *) calloc(rtems_libio_number_iops,
+                                                    sizeof(rtems_libio_t));
+        if (rtems_libio_iops == NULL)
+            rtems_fatal_error_occurred(RTEMS_NO_MEMORY);
+
+        iop = rtems_libio_iop_freelist = rtems_libio_iops;
+	for (i = 0 ; i < (rtems_libio_number_iops - 1) ; i++, iop++)
+		iop->data1 = iop + 1;
+	iop->data1 = NULL;
+    }
 
   /*
@@ -158,22 +193,19 @@
   rtems_semaphore_obtain( rtems_libio_semaphore, RTEMS_WAIT, RTEMS_NO_TIMEOUT );
 
-  for (iop = rtems_libio_iops; iop <= rtems_libio_last_iop; iop++)
-    if ((iop->flags & LIBIO_FLAGS_OPEN) == 0) {
-      /*
-       *  Got an IOP -- create a semaphore for it.
-       */
-
-      rc = rtems_semaphore_create(
-        RTEMS_LIBIO_IOP_SEM(iop - rtems_libio_iops),
-        1,
-        RTEMS_BINARY_SEMAPHORE | RTEMS_INHERIT_PRIORITY | RTEMS_PRIORITY,
-        RTEMS_NO_PRIORITY,
-        &iop->sem
-      );
-      if ( rc != RTEMS_SUCCESSFUL )
-        goto failed;
-    
-      iop->flags = LIBIO_FLAGS_OPEN;
-      goto done;
+  if (rtems_libio_iop_freelist) {
+    iop = rtems_libio_iop_freelist;
+    rc = rtems_semaphore_create(
+      RTEMS_LIBIO_IOP_SEM(iop - rtems_libio_iops),
+      1,
+      RTEMS_BINARY_SEMAPHORE | RTEMS_INHERIT_PRIORITY | RTEMS_PRIORITY,
+      RTEMS_NO_PRIORITY,
+      &iop->sem
+    );
+    if (rc != RTEMS_SUCCESSFUL)
+      goto failed;
+    rtems_libio_iop_freelist = iop->data1;
+    iop->data1 = 0;
+    iop->flags = LIBIO_FLAGS_OPEN;
+    goto done;
   }
   
@@ -199,10 +231,11 @@
   rtems_semaphore_obtain( rtems_libio_semaphore, RTEMS_WAIT, RTEMS_NO_TIMEOUT );
 
-  if (iop->sem)
-    rtems_semaphore_delete(iop->sem);
-
-  (void) memset(iop, 0, sizeof(*iop));
-
-  rtems_semaphore_release( rtems_libio_semaphore );
+    if (iop->sem)
+      rtems_semaphore_delete(iop->sem);
+
+    iop->data1 = rtems_libio_iop_freelist;
+    rtems_libio_iop_freelist = iop;
+
+  rtems_semaphore_release(rtems_libio_semaphore);
 }
 
@@ -211,5 +244,5 @@
  *
  *  This routine scans the entire file descriptor table to determine if the
- *  are any active file descriptors that refer to the atleast one node in the
+ *  are any active file descriptors that refer to the at least one node in the
  *  file system that we are trying to dismount.
  *
@@ -226,17 +259,17 @@
 
   rtems_semaphore_obtain( rtems_libio_semaphore, RTEMS_WAIT, RTEMS_NO_TIMEOUT );
-
+              
   /*
    *  Look for any active file descriptor entry.
-   */
-
+   */         
+     
   for ( iop=rtems_libio_iops ; iop <= rtems_libio_last_iop ; iop++ ) {
-
+        
     if ((iop->flags & LIBIO_FLAGS_OPEN) != 0) {
-
+        
        /*
         *  Check if this node is under the file system that we
         *  are trying to dismount.
-        */ 
+        */
 
        if ( iop->pathinfo.mt_entry == fs_mt_entry ) {
@@ -248,5 +281,5 @@
 
   rtems_semaphore_release( rtems_libio_semaphore );
-
+ 
   return result;
 }
@@ -257,5 +290,5 @@
  *  This routine scans the entire file descriptor table to determine if the
  *  given file refers to an active file descriptor.
- *
+ * 
  *  If the given file is open a 1 is returned, otherwise a 0 is returned.
  */
@@ -266,5 +299,5 @@
 {
   rtems_libio_t     *iop;
-  int                result=0; 
+  int                result=0;
 
   rtems_semaphore_obtain( rtems_libio_semaphore, RTEMS_WAIT, RTEMS_NO_TIMEOUT );
@@ -273,13 +306,13 @@
    *  Look for any active file descriptor entry.
    */
-
+  
   for ( iop=rtems_libio_iops ; iop <= rtems_libio_last_iop ; iop++ ) {
-
+  
     if ((iop->flags & LIBIO_FLAGS_OPEN) != 0) {
-
+  
        /*
         *  Check if this node is under the file system that we
         *  are trying to dismount.
-        */ 
+        */
 
        if ( iop->pathinfo.node_access == node_access ) {
@@ -294,2 +327,4 @@
   return result;
 }
+
+
Index: cpukit/libcsupport/src/lseek.c
===================================================================
--- cpukit/libcsupport/src/lseek.c	(revision 6c980128774e4359b32d7325a987283c0635869f)
+++ cpukit/libcsupport/src/lseek.c	(revision cca44008d81209e9fa992157637d9de0384e0536)
@@ -25,4 +25,7 @@
   rtems_libio_t *iop;
 
+  rtems_libio_check_fd( fd );
+  iop = rtems_libio_iop( fd );
+
   /*
    *  If this file descriptor is mapped to an external set of handlers,
@@ -30,8 +33,9 @@
    */
 
-  if ( rtems_file_descriptor_type( fd ) ) {
+  if ( iop->flags & LIBIO_FLAGS_HANDLER_MASK ) {
     rtems_libio_lseek_t fp;
 
-    fp = rtems_libio_handlers[rtems_file_descriptor_type_index(fd)].lseek;
+    fp = rtems_libio_handlers[
+           (iop->flags >> LIBIO_FLAGS_HANDLER_SHIFT) - 1].lseek;
     if ( fp == NULL )
       set_errno_and_return_minus_one( EBADF );
@@ -43,7 +47,4 @@
    *  Now process the lseek().
    */
-
-  iop = rtems_libio_iop( fd );
-  rtems_libio_check_fd( fd );
 
   switch ( whence ) {
Index: cpukit/libcsupport/src/read.c
===================================================================
--- cpukit/libcsupport/src/read.c	(revision 6c980128774e4359b32d7325a987283c0635869f)
+++ cpukit/libcsupport/src/read.c	(revision cca44008d81209e9fa992157637d9de0384e0536)
@@ -27,4 +27,10 @@
   rtems_libio_t *iop;
 
+  rtems_libio_check_fd( fd );
+  iop = rtems_libio_iop( fd );
+  rtems_libio_check_buffer( buffer );
+  rtems_libio_check_count( count );
+  rtems_libio_check_permissions( iop, LIBIO_FLAGS_READ );
+
   /*
    *  If this file descriptor is mapped to an external set of handlers,
@@ -32,8 +38,9 @@
    */ 
 
-  if ( rtems_file_descriptor_type( fd ) ) {
+  if ( iop->flags & LIBIO_FLAGS_HANDLER_MASK ) {
     rtems_libio_read_t fp;
 
-    fp = rtems_libio_handlers[rtems_file_descriptor_type_index(fd)].read;
+    fp = rtems_libio_handlers[
+           (iop->flags >> LIBIO_FLAGS_HANDLER_SHIFT) - 1].read;
     if ( fp == NULL )
       set_errno_and_return_minus_one( EBADF );
@@ -45,10 +52,4 @@
    *  Now process the read().
    */
-
-  iop = rtems_libio_iop( fd );
-  rtems_libio_check_fd( fd );
-  rtems_libio_check_buffer( buffer );
-  rtems_libio_check_count( count );
-  rtems_libio_check_permissions( iop, LIBIO_FLAGS_READ );
 
   if ( !iop->handlers->read )
Index: cpukit/libcsupport/src/write.c
===================================================================
--- cpukit/libcsupport/src/write.c	(revision 6c980128774e4359b32d7325a987283c0635869f)
+++ cpukit/libcsupport/src/write.c	(revision cca44008d81209e9fa992157637d9de0384e0536)
@@ -35,4 +35,10 @@
   rtems_libio_t     *iop;
 
+  rtems_libio_check_fd( fd );
+  iop = rtems_libio_iop( fd );
+  rtems_libio_check_buffer( buffer );
+  rtems_libio_check_count( count );
+  rtems_libio_check_permissions( iop, LIBIO_FLAGS_WRITE );
+
   /*
    *  If this file descriptor is mapped to an external set of handlers,
@@ -40,8 +46,9 @@
    */
 
-  if ( rtems_file_descriptor_type( fd ) ) {
+  if ( iop->flags & LIBIO_FLAGS_HANDLER_MASK ) {
     rtems_libio_write_t fp;
 
-    fp = rtems_libio_handlers[rtems_file_descriptor_type_index(fd)].write;
+    fp = rtems_libio_handlers[
+           (iop->flags >> LIBIO_FLAGS_HANDLER_SHIFT) - 1].write;
     if ( fp == NULL )
       set_errno_and_return_minus_one( EBADF );
@@ -53,10 +60,4 @@
    *  Now process the write() request.
    */
-
-  iop = rtems_libio_iop( fd );
-  rtems_libio_check_fd( fd );
-  rtems_libio_check_buffer( buffer );
-  rtems_libio_check_count( count );
-  rtems_libio_check_permissions( iop, LIBIO_FLAGS_WRITE );
 
   if ( !iop->handlers->write )
Index: cpukit/libnetworking/rtems/rtems_glue.c
===================================================================
--- cpukit/libnetworking/rtems/rtems_glue.c	(revision 6c980128774e4359b32d7325a987283c0635869f)
+++ cpukit/libnetworking/rtems/rtems_glue.c	(revision cca44008d81209e9fa992157637d9de0384e0536)
@@ -30,17 +30,4 @@
 #include <net/netisr.h>
 #include <net/route.h>
-
-/*
- * Events used by networking routines.
- * Everything will break if the application
- * tries to use these events or if the `sleep'
- * events are equal to any of the NETISR * events.
- */
-#define SBWAIT_EVENT	RTEMS_EVENT_24
-#define SOSLEEP_EVENT	RTEMS_EVENT_25
-#define NETISR_IP_EVENT		(1 << NETISR_IP)
-#define NETISR_ARP_EVENT	(1 << NETISR_ARP)
-#define NETISR_EVENTS	(NETISR_IP_EVENT|NETISR_ARP_EVENT)
-
 
 /*
@@ -247,5 +234,5 @@
 	 * Register as an external I/O handler
 	 */
-	rtems_register_libio_handler (RTEMS_FILE_DESCRIPTOR_TYPE_SOCKET,
+	rtems_register_libio_handler (LIBIO_FLAGS_HANDLER_SOCK,
 						&rtems_bsdnet_io_handler);
 
@@ -306,15 +293,8 @@
 	 * Set this task as the target of the wakeup operation.
 	 */
-	if (sb->sb_sel.si_pid)
-		rtems_panic ("Another task is already sleeping on that socket buffer");
 	rtems_task_ident (RTEMS_SELF, 0, &tid);
 	sb->sb_sel.si_pid = tid;
 
 	/*
-	 * Mark the socket buffer as waiting.
-	 */
-	sb->sb_flags |= SB_WAIT;
-
-	/*
 	 * Release the network semaphore.
 	 */
@@ -330,10 +310,4 @@
 	 */
 	rtems_bsdnet_semaphore_obtain ();
-
-	/*
-	 * Relinquish ownership of the socket buffer
-	 */
-	sb->sb_flags &= ~SB_WAIT;
-	sb->sb_sel.si_pid = 0;
 
 	/*
@@ -356,7 +330,9 @@
 	register struct sockbuf *sb;
 {
-	if (sb->sb_flags & SB_WAIT) {
-		sb->sb_flags &= ~SB_WAIT;
-		rtems_event_send (sb->sb_sel.si_pid, SBWAIT_EVENT);
+	rtems_id tid;
+
+	if ((tid = sb->sb_sel.si_pid) != 0) {
+		sb->sb_sel.si_pid = 0;
+		rtems_event_send (tid, SBWAIT_EVENT);
 	}
 }
Index: cpukit/libnetworking/rtems/rtems_syscall.c
===================================================================
--- cpukit/libnetworking/rtems/rtems_syscall.c	(revision 6c980128774e4359b32d7325a987283c0635869f)
+++ cpukit/libnetworking/rtems/rtems_syscall.c	(revision cca44008d81209e9fa992157637d9de0384e0536)
@@ -25,75 +25,4 @@
 #include <net/if.h>
 #include <net/route.h>
-
-/*
- *********************************************************************
- *              Map RTEMS file descriptor to BSD socket              *
- *********************************************************************
- */
-struct fdsock {
-	int		indexFreeNext;
-	struct socket	*sock;
-};
-static struct fdsock *fdsock;
-static int fdsockCount;
-static int indexFreeHead = -1;
-
-/*
- * Convert an RTEMS file descriptor to a BSD socket pointer.
- */
-static struct socket *
-fdToSocket (int fd)
-{
-	int i;
-	struct socket *s;
-
-	if ((fd < 0)
-	 || (rtems_file_descriptor_type(fd) != RTEMS_FILE_DESCRIPTOR_TYPE_SOCKET)
-	 || ((i = rtems_file_descriptor_base(fd)) >= fdsockCount)
-	 || ((s = fdsock[i].sock) == NULL)) {
-		errno = EBADF;
-		return NULL;
-	}
-	return s;
-}
-
-/*
- * Enlarge the size of the file-descritor/socket pointer map.
- */
-static int
-enlargeFdMap (void)
-{
-	struct fdsock *nfdsock;
-	int i;
-
-	nfdsock = realloc (fdsock, sizeof *fdsock * (fdsockCount + 20));
-	if (nfdsock == NULL) {
-		errno = ENFILE;
-		return 0;
-	}
-	fdsock = nfdsock;
-	for (i = fdsockCount, fdsockCount += 20 ; i < fdsockCount ; i++) {
-		fdsock[i].sock = NULL;
-		fdsock[i].indexFreeNext = indexFreeHead;
-		indexFreeHead = i;
-	}
-	return 1;
-}
-
-/*
- * Create a file descriptor for a new socket
- */
-static int
-makeFd (struct socket *s)
-{
-	int i;
-
-	if ((indexFreeHead < 0) && !enlargeFdMap ())
-		return -1;
-	i = indexFreeHead;
-	indexFreeHead = fdsock[i].indexFreeNext;
-	fdsock[i].sock = s;
-	return rtems_make_file_descriptor(i,RTEMS_FILE_DESCRIPTOR_TYPE_SOCKET);
-}
 
 /*
@@ -129,5 +58,5 @@
 socket (int domain, int type, int protocol)
 {
-	int fd = -1;
+	int fd;
 	int error;
 	struct socket *so;
@@ -136,5 +65,5 @@
 	error = socreate(domain, &so, type, protocol, NULL);
 	if (error == 0) {
-		fd = makeFd (so);
+		fd = rtems_bsdnet_makeFdForSocket (so);
 		if (fd < 0)
 			soclose (so);
@@ -157,5 +86,5 @@
 
 	rtems_bsdnet_semaphore_obtain ();
-	if ((so = fdToSocket (s)) != NULL) {
+	if ((so = rtems_bsdnet_fdToSocket (s)) != NULL) {
 		error = sockargstombuf (&nam, name, namelen, MT_SONAME);
 		if (error == 0) {
@@ -184,5 +113,5 @@
 
 	rtems_bsdnet_semaphore_obtain ();
-	if ((so = fdToSocket (s)) == NULL) {
+	if ((so = rtems_bsdnet_fdToSocket (s)) == NULL) {
 		rtems_bsdnet_semaphore_release ();
 		return -1;
@@ -234,5 +163,5 @@
 
 	rtems_bsdnet_semaphore_obtain ();
-	if ((so = fdToSocket (s)) != NULL) {
+	if ((so = rtems_bsdnet_fdToSocket (s)) != NULL) {
 		error = solisten (so, backlog);
 		if (error == 0)
@@ -253,5 +182,5 @@
 
 	rtems_bsdnet_semaphore_obtain ();
-	if ((head = fdToSocket (s)) == NULL) {
+	if ((head = rtems_bsdnet_fdToSocket (s)) == NULL) {
 		rtems_bsdnet_semaphore_release ();
 		return -1;
@@ -285,5 +214,5 @@
 	head->so_qlen--;
 
-	fd = makeFd (so);
+	fd = rtems_bsdnet_makeFdForSocket (so);
 	if (fd < 0) {
 		TAILQ_INSERT_HEAD(&head->so_comp, so, so_list);
@@ -326,5 +255,5 @@
 
 	rtems_bsdnet_semaphore_obtain ();
-	if ((so = fdToSocket (s)) == NULL) {
+	if ((so = rtems_bsdnet_fdToSocket (s)) == NULL) {
 		rtems_bsdnet_semaphore_release ();
 		return -1;
@@ -429,5 +358,5 @@
 
 	rtems_bsdnet_semaphore_obtain ();
-	if ((so = fdToSocket (s)) == NULL) {
+	if ((so = rtems_bsdnet_fdToSocket (s)) == NULL) {
 		rtems_bsdnet_semaphore_release ();
 		return -1;
@@ -548,5 +477,5 @@
 
 	rtems_bsdnet_semaphore_obtain ();
-	if ((so = fdToSocket (s)) == NULL) {
+	if ((so = rtems_bsdnet_fdToSocket (s)) == NULL) {
 		rtems_bsdnet_semaphore_release ();
 		return -1;
@@ -585,5 +514,5 @@
 
 	rtems_bsdnet_semaphore_obtain ();
-	if ((so = fdToSocket (s)) == NULL) {
+	if ((so = rtems_bsdnet_fdToSocket (s)) == NULL) {
 		rtems_bsdnet_semaphore_release ();
 		return -1;
@@ -627,5 +556,5 @@
 
 	rtems_bsdnet_semaphore_obtain ();
-	if ((so = fdToSocket (s)) == NULL) {
+	if ((so = rtems_bsdnet_fdToSocket (s)) == NULL) {
 		rtems_bsdnet_semaphore_release ();
 		return -1;
@@ -677,14 +606,10 @@
 	struct socket *so;
 	int error;
-	int i;
-
-	rtems_bsdnet_semaphore_obtain ();
-	if ((so = fdToSocket (fd)) == NULL) {
-		rtems_bsdnet_semaphore_release ();
-		return -1;
-	}
-	i = rtems_file_descriptor_base(fd);
-	fdsock[i].indexFreeNext = indexFreeHead;;
-	indexFreeHead = i;
+
+	rtems_bsdnet_semaphore_obtain ();
+	if ((so = rtems_bsdnet_fdToSocket (fd)) == NULL) {
+		rtems_bsdnet_semaphore_release ();
+		return -1;
+	}
 	error = soclose (so);
 	rtems_bsdnet_semaphore_release ();
@@ -738,5 +663,5 @@
 
 	rtems_bsdnet_semaphore_obtain ();
-	if ((so = fdToSocket (fd)) == NULL) {
+	if ((so = rtems_bsdnet_fdToSocket (fd)) == NULL) {
 		rtems_bsdnet_semaphore_release ();
 		return -1;
