Index: trunk/kernel/syscalls/sys_getcwd.c
===================================================================
--- trunk/kernel/syscalls/sys_getcwd.c	(revision 1)
+++ trunk/kernel/syscalls/sys_getcwd.c	(revision 23)
@@ -1,15 +1,16 @@
 /*
- * kern/sys_getcwd.c - get process current work directory
+ * sys_getcwd.c - get process current work directory
  * 
- * Copyright (c) 2008,2009,2010,2011,2012 Ghassan Almaless
- * Copyright (c) 2011,2012 UPMC Sorbonne Universites
+ * Author    Alain Greiner (2016,2017)
  *
- * This file is part of ALMOS-kernel.
+ * Copyright (c)  UPMC Sorbonne Universites
  *
- * ALMOS-kernel is free software; you can redistribute it and/or modify it
+ * This file is part of ALMOS-MKH.
+ *
+ * ALMOS-MKH is free software; you can redistribute it and/or modify it
  * under the terms of the GNU General Public License as published by
  * the Free Software Foundation; version 2.0 of the License.
  *
- * ALMOS-kernel is distributed in the hope that it will be useful, but
+ * ALMOS-MKH is distributed in the hope that it will be useful, but
  * WITHOUT ANY WARRANTY; without even the implied warranty of
  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
@@ -17,51 +18,71 @@
  *
  * You should have received a copy of the GNU General Public License
- * along with ALMOS-kernel; if not, write to the Free Software Foundation,
+ * along with ALMOS-MKH; if not, write to the Free Software Foundation,
  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
  */
 
-#include <libk.h>
+#include <kernel_config.h>
+#include <hal_types.h>
+#include <hal_uspace.h>
+#include <hal_special.h>
+#include <errno.h>
 #include <vfs.h>
-#include <sys-vfs.h>
-#include <task.h>
-#include <kmem.h>
-#include <ppm.h>
+#include <vmm.h>
+#include <process.h>
 #include <thread.h>
+#include <printk.h>
 
-/* TODO: user page need to be locked as long as its region */
+/* TODO: user page(s) need to be locked  [AG] */
 
-int sys_getcwd (char *buff, size_t size)
+////////////////////////////////
+int sys_getcwd ( char     * buf, 
+                 uint32_t   nbytes )
 {
-	register struct thread_s *this;
-	register struct task_s *task;
-	register error_t err;
-	struct ku_obj ku_buff;
+	error_t    error;
+    paddr_t    paddr;
+    char       kbuf[CONFIG_VFS_MAX_PATH_LENGTH];
   
-	this      = current_thread;
-	task      = current_task;
+	thread_t  * this    = CURRENT_THREAD;
+    process_t * process = this->process;
 
-	if((size < VFS_MAX_NAME_LENGTH) || (!buff)) 
+    // check buffer size
+	if( nbytes < CONFIG_VFS_MAX_PATH_LENGTH )
 	{
-		err = ERANGE;
-		goto SYS_GETCWD_ERROR;
+        printk("\n[ERROR] in %s : buffer too small\n", __FUNCTION__ );
+		this->errno = ERANGE;
+        return -1;
 	}
 
-	if(vmm_check_address("usr cwd buffer", task, buff, size))
+    // check buffer in user space
+    error = vmm_v2p_translate( false , buf , &paddr );
+
+	if( error )
 	{
-		err = EFAULT;
-		goto SYS_GETCWD_ERROR;
+        printk("\n[ERROR] in %s : user buffer unmapped\n", __FUNCTION__ );
+		this->errno = EFAULT;
+        return -1;
 	}
 
-	KU_SZ_BUFF(ku_buff, buff, size);
+    // get reference process cluster and local pointer
+    xptr_t      ref_xp  = process->ref_xp;
+    cxy_t       ref_cxy = GET_CXY( ref_xp );
+    process_t * ref_ptr = (process_t *)GET_PTR( ref_xp );
 
-	rwlock_rdlock(&task->cwd_lock);
+    // get CWD lock in read mode
+	remote_rwlock_rd_lock( XPTR( ref_cxy , &ref_ptr->cwd_lock ) );
 
-	err = vfs_get_path(&task->vfs_cwd, &ku_buff);
+    // call relevant VFS function
+	error = vfs_get_path( XPTR( ref_cxy , &ref_ptr->vfs_cwd_xp ) ,
+                          kbuf , CONFIG_VFS_MAX_PATH_LENGTH );
 
-	rwlock_unlock(&task->cwd_lock);
+    // release CWD lock
+	remote_rwlock_rd_unlock( XPTR( ref_cxy , &ref_ptr->cwd_lock ) );
 
+    // copy kernel buffer to user space
+    hal_copy_to_uspace( buf , kbuf , CONFIG_VFS_MAX_PATH_LENGTH );
 
-SYS_GETCWD_ERROR:
-	this->info.errno = err;
-	return (int)buff;
-}
+    hal_wbflush();
+
+	return 0;
+
+}  // end sys_getcwd()
