1 ! Copyright (C) 2005, 2008 Slava Pestov, Doug Coleman.
2 ! See http://factorcode.org/license.txt for BSD license.
3 USING: unix byte-arrays kernel io.backend.unix math.bitwise
4 io.ports io.files io.files.private io.pathnames environment
5 destructors system unix.ffi literals ;
8 M: unix cwd ( -- path )
9 MAXPATHLEN [ <byte-array> ] keep
10 [ getcwd ] unix-system-call
11 [ (io-error) ] unless* ;
13 M: unix cd ( path -- ) [ chdir ] unix-system-call drop ;
15 CONSTANT: read-flags flags{ O_RDONLY }
17 : open-read ( path -- fd ) read-flags file-mode open-file ;
19 M: unix (file-reader) ( path -- stream )
20 open-read <fd> init-fd <input-port> ;
22 CONSTANT: write-flags flags{ O_WRONLY O_CREAT O_TRUNC }
24 : open-write ( path -- fd )
25 write-flags file-mode open-file ;
27 M: unix (file-writer) ( path -- stream )
28 open-write <fd> init-fd <output-port> ;
30 CONSTANT: append-flags flags{ O_WRONLY O_APPEND O_CREAT }
32 : open-append ( path -- fd )
34 append-flags file-mode open-file |dispose
35 dup 0 SEEK_END [ lseek ] unix-system-call drop
38 M: unix (file-appender) ( path -- stream )
39 open-append <fd> init-fd <output-port> ;
41 M: unix home "HOME" os-env ;