Index: trunk/kernel/syscalls/sys_write.c
===================================================================
--- trunk/kernel/syscalls/sys_write.c	(revision 594)
+++ trunk/kernel/syscalls/sys_write.c	(revision 604)
@@ -55,12 +55,18 @@
                uint32_t   count )
 {
-    error_t      error;
-    vseg_t     * vseg;            // required for user space checking
-	xptr_t       file_xp;         // remote file extended pointer
-    uint32_t     nbytes;          // number of bytes actually written
-    reg_t        save_sr;         // required to enable IRQs during syscall
-
-	thread_t   * this = CURRENT_THREAD;
-	process_t  * process = this->process;
+    error_t       error;
+    vseg_t      * vseg;            // required for user space checking
+	xptr_t        file_xp;         // remote file extended pointer
+    vfs_file_t  * file_ptr;        // remote file local pointer
+    cxy_t         file_cxy;        // remote file cluster identifier
+    uint32_t      file_type;       // file type
+    uint32_t      file_offset;     // current file offset
+    uint32_t      file_attr;       // file_attribute
+    vfs_inode_t * inode_ptr;       // local pointer on associated inode
+    uint32_t      nbytes;          // number of bytes actually written
+    reg_t         save_sr;         // required to enable IRQs during syscall
+
+	thread_t    * this = CURRENT_THREAD;
+	process_t   * process = this->process;
 
 #if (DEBUG_SYS_WRITE || CONFIG_INSTRUMENTATION_SYSCALLS)
@@ -113,5 +119,5 @@
 
 #if DEBUG_SYSCALLS_ERROR
-printk("\n[ERROR] in %s : thread[%x,%x] undefined file descriptor index = %d\n",
+printk("\n[ERROR] in %s : thread[%x,%x] undefined file descriptor = %d\n",
 __FUNCTION__, process->pid, this->trdid, file_id );
 #endif
@@ -121,10 +127,12 @@
 
     // get file descriptor cluster and local pointer
-    vfs_file_t * file_ptr = (vfs_file_t *)GET_PTR( file_xp );
-    cxy_t        file_cxy = GET_CXY( file_xp );
-
- 
-    // get file type
-    vfs_inode_type_t type = hal_remote_l32( XPTR( file_cxy , &file_ptr->type ) );
+    file_ptr = GET_PTR( file_xp );
+    file_cxy = GET_CXY( file_xp );
+
+    // get file type, offset, aatributes, and associated inode
+    file_type   = hal_remote_l32( XPTR( file_cxy , &file_ptr->type ) );
+    file_offset = hal_remote_l32( XPTR( file_cxy , &file_ptr->offset ) );
+    inode_ptr   = hal_remote_lpt( XPTR( file_cxy , &file_ptr->inode ) );
+    file_attr   = hal_remote_l32( XPTR( file_cxy , &file_ptr->attr ) );
 
     // enable IRQs
@@ -132,9 +140,8 @@
 
    // action depend on file type
-    if( type == INODE_TYPE_FILE )  // check file writable & write to mapper 
+    if( file_type == INODE_TYPE_FILE )  // write to file mapper 
     {
         // check file writable
-        uint32_t attr = hal_remote_l32( XPTR( file_cxy , &file_ptr->attr ) );
-        if( (attr & FD_ATTR_WRITE_ENABLE) == 0 )
+        if( (file_attr & FD_ATTR_WRITE_ENABLE) == 0 )
 	    {
 
@@ -143,4 +150,5 @@
 __FUNCTION__ , process->pid, this->trdid, file_id );
 #endif
+            hal_restore_irq( save_sr );
 		    this->errno = EBADFD;
 		    return -1;
@@ -152,26 +160,34 @@
                                 vaddr, 
                                 count );
-    }
-    else if( type == INODE_TYPE_DEV )  // write to TXT device
+        if ( nbytes != count )
+        {
+
+#if DEBUG_SYSCALLS_ERROR
+printk("\n[ERROR] in %s : thread[%x,%x] cannot write %d bytes into file %d\n",
+__FUNCTION__ , process->pid, this->trdid, count, file_id );
+#endif
+            hal_restore_irq( save_sr );
+            this->errno = EIO;
+            return -1;
+
+        }
+
+        // update size field in inode if required
+        xptr_t   size_xp    = XPTR( file_cxy , &inode_ptr->size );
+        uint32_t inode_size = hal_remote_l32( size_xp );
+        if ( (file_offset + count) > inode_size )
+        {
+            hal_remote_s32( size_xp , file_offset + count );
+        }
+    }
+    else if( file_type == INODE_TYPE_DEV )  // write to TXT device
     {
         // move count bytes to device
         nbytes = devfs_user_move( false,             // from buffer to device
-                                 file_xp,
-                                 vaddr,
-                                 count );
-    }
-    else  // not FILE and not DEV
-    {
-
-#if DEBUG_SYSCALLS_ERROR
-printk("\n[ERROR] in %s : thread[%x,%x] / illegal inode type %\n",
-__FUNCTION__, vfs_inode_type_str( type ) );
-#endif
-		this->errno = EBADFD;
-		return -1;
-    }
-
-    if( nbytes != count )
-    {
+                                  file_xp,
+                                  vaddr,
+                                  count );
+        if( nbytes != count )
+        {
 
 #if DEBUG_SYSCALLS_ERROR
@@ -179,6 +195,19 @@
 __FUNCTION__ , process->pid, this->trdid, file_id );
 #endif
-        this->errno = EIO;
-        return -1;
+            hal_restore_irq( save_sr );
+            this->errno = EIO;
+            return -1;
+        }
+    }
+    else  // not FILE and not DEV
+    {
+
+#if DEBUG_SYSCALLS_ERROR
+printk("\n[ERROR] in %s : thread[%x,%x] / illegal inode type %\n",
+__FUNCTION__, vfs_inode_type_str( file_type ) );
+#endif
+        hal_restore_irq( save_sr );
+		this->errno = EBADFD;
+		return -1;
     }
 
