file: add String::chdir
authorJean Privat <jean@pryen.org>
Tue, 3 Sep 2013 18:08:54 +0000 (14:08 -0400)
committerJean Privat <jean@pryen.org>
Wed, 4 Sep 2013 14:12:57 +0000 (10:12 -0400)
Signed-off-by: Jean Privat <jean@pryen.org>

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

index 0b2c71f..f21edc9 100644 (file)
@@ -321,6 +321,16 @@ redef class String
                end
        end
 
+       # Change the current working directory
+       #
+       #     "/etc".chdir
+       #     assert getcwd == "/etc"
+       #     "..".chdir
+       #     assert getcwd == "/"
+       #
+       # TODO: errno
+       fun chdir do to_cstring.file_chdir
+
        # Return right-most extension (without the dot)
        fun file_extension : nullable String
        do
@@ -349,6 +359,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"
 end
 
 extern FileStat `{ struct stat * `}
index 56ed616..f34769d 100644 (file)
@@ -49,6 +49,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_stdin_poll_in(self) file_stdin_poll_in_()
 int file_stdin_poll_in_(void);