]> gitweb.factorcode.org Git - factor.git/commitdiff
io.files.unix: loop getcwd with an expanding buffer, fixes part of #1074
authorBjörn Lindqvist <bjourne@gmail.com>
Wed, 23 Mar 2016 12:22:31 +0000 (13:22 +0100)
committerBjörn Lindqvist <bjourne@gmail.com>
Wed, 23 Mar 2016 15:15:29 +0000 (16:15 +0100)
It appears that the right way to do it is using an arbitrary sized buffer
and just expanding it in case getcwd reports ERANGE.

basis/io/files/unix/unix-tests.factor
basis/io/files/unix/unix.factor

index b380c3313a0c4361d62a2b714016e02f5aee088e..5fb143c5e0991841a6aa94b80110b03c7cf59240 100644 (file)
@@ -1,8 +1,7 @@
-USING: tools.test io.files io.files.temp io.pathnames
-io.directories io.files.info io.files.info.unix continuations
-kernel io.files.unix math.bitwise calendar accessors
-math.functions math unix.users unix.groups arrays sequences
-grouping io.pathnames.private literals ;
+USING: accessors arrays calendar continuations grouping io.directories
+io.files.info io.files.info.unix io.files.temp io.files.unix
+io.pathnames kernel literals math math.bitwise math.functions
+sequences strings tools.test unix.groups unix.users ;
 IN: io.files.unix.tests
 
 { "/usr/libexec/" } [ "/usr/libexec/awk/" parent-directory ] unit-test
@@ -163,3 +162,8 @@ prepare-test-file
 { f } [ 0 other-read? ] unit-test
 { f } [ 0 other-write? ] unit-test
 { f } [ 0 other-execute? ] unit-test
+
+! (cwd)
+{ t } [
+    1 (cwd) string?
+] unit-test
index 39517bc68f6d02e75e05b0d3b6edc542ac06dad3..47aa5bb34d021897eecbe3ba050961603c8d814f 100644 (file)
@@ -1,14 +1,21 @@
 ! Copyright (C) 2005, 2008 Slava Pestov, Doug Coleman.
 ! See http://factorcode.org/license.txt for BSD license.
-USING: byte-arrays destructors environment io.backend.unix
-io.files io.files.private io.pathnames io.ports kernel libc
-literals system unix unix.ffi ;
+USING: accessors byte-arrays continuations destructors environment
+io.backend.unix io.files io.files.private io.pathnames io.ports kernel
+libc literals math system unix unix.ffi ;
 IN: io.files.unix
 
+: (cwd) ( bufsiz -- path )
+    [
+        dup <byte-array> over [ getcwd ] unix-system-call nip
+    ] [
+        dup errno>> ERANGE = [
+            drop 2 * (cwd)
+        ] [ rethrow ] if
+    ] recover ;
+
 M: unix cwd ( -- path )
-    MAXPATHLEN [ <byte-array> ] keep
-    [ getcwd ] unix-system-call
-    [ throw-errno ] unless* ;
+    4096 (cwd) ;
 
 M: unix cd ( path -- ) [ chdir ] unix-system-call drop ;