lib: add file::realpath
authorJean Privat <jean@pryen.org>
Wed, 29 Jan 2014 21:02:32 +0000 (16:02 -0500)
committerJean Privat <jean@pryen.org>
Wed, 29 Jan 2014 21:05:15 +0000 (16:05 -0500)
Signed-off-by: Jean Privat <jean@pryen.org>

lib/standard/file.nit
lib/standard/file_nit.h

index 964331a..239f785 100644 (file)
@@ -251,6 +251,14 @@ redef class String
                end
        end
 
+       # Return the canonicalized absolute pathname (see POSIX function `realpath`)
+       fun realpath: String do
+               var cs = to_cstring.file_realpath
+               var res = cs.to_s_with_copy
+               # cs.free_malloc # FIXME memory leak
+               return res
+       end
+
        # Simplify a file path by remove useless ".", removing "//", and resolving ".."
        # ".." are not resolved if they start the path
        # starting "/" is not removed
@@ -362,6 +370,7 @@ redef class NativeString
        private fun file_mkdir: Bool is extern "string_NativeString_NativeString_file_mkdir_0"
        private fun file_delete: Bool is extern "string_NativeString_NativeString_file_delete_0"
        private fun file_chdir is extern "string_NativeString_NativeString_file_chdir_0"
+       private fun file_realpath: NativeString is extern "file_NativeString_realpath"
 end
 
 extern FileStat `{ struct stat * `}
index f34769d..6d7967f 100644 (file)
@@ -50,6 +50,7 @@ extern int string_NativeString_NativeString_file_delete_0(char *f);
 #define string_NativeString_NativeString_file_mkdir_0(p) (mkdir(p, 0777))
 #define string_NativeString_NativeString_file_getcwd_0(p) (getcwd(NULL, 0))
 #define string_NativeString_NativeString_file_chdir_0(p) (chdir(p)?-1:0) /* hack to avoid warn_unused_result */
+#define file_NativeString_realpath(p) (realpath(p, NULL))
 
 #define file_stdin_poll_in(self) file_stdin_poll_in_()
 int file_stdin_poll_in_(void);