2008-12-14 21:03:00 -05:00
|
|
|
! Copyright (C) 2005, 2008 Slava Pestov, Doug Coleman.
|
|
|
|
! See http://factorcode.org/license.txt for BSD license.
|
|
|
|
USING: unix byte-arrays kernel io.backend.unix math.bitwise
|
|
|
|
io.ports io.files io.files.private io.pathnames environment
|
|
|
|
destructors system ;
|
|
|
|
IN: io.files.unix
|
|
|
|
|
|
|
|
M: unix cwd ( -- path )
|
|
|
|
MAXPATHLEN [ <byte-array> ] keep getcwd
|
|
|
|
[ (io-error) ] unless* ;
|
|
|
|
|
|
|
|
M: unix cd ( path -- ) [ chdir ] unix-system-call drop ;
|
|
|
|
|
2008-12-15 20:44:56 -05:00
|
|
|
: read-flags ( -- n ) O_RDONLY ; inline
|
2008-12-14 21:03:00 -05:00
|
|
|
|
|
|
|
: open-read ( path -- fd ) O_RDONLY file-mode open-file ;
|
|
|
|
|
|
|
|
M: unix (file-reader) ( path -- stream )
|
|
|
|
open-read <fd> init-fd <input-port> ;
|
|
|
|
|
2008-12-15 20:44:56 -05:00
|
|
|
: write-flags ( -- n )
|
|
|
|
{ O_WRONLY O_CREAT O_TRUNC } flags ; inline
|
2008-12-14 21:03:00 -05:00
|
|
|
|
|
|
|
: open-write ( path -- fd )
|
|
|
|
write-flags file-mode open-file ;
|
|
|
|
|
|
|
|
M: unix (file-writer) ( path -- stream )
|
|
|
|
open-write <fd> init-fd <output-port> ;
|
|
|
|
|
2008-12-15 20:44:56 -05:00
|
|
|
: append-flags ( -- n )
|
|
|
|
{ O_WRONLY O_APPEND O_CREAT } flags ; inline
|
2008-12-14 21:03:00 -05:00
|
|
|
|
|
|
|
: open-append ( path -- fd )
|
|
|
|
[
|
|
|
|
append-flags file-mode open-file |dispose
|
|
|
|
dup 0 SEEK_END lseek io-error
|
|
|
|
] with-destructors ;
|
|
|
|
|
|
|
|
M: unix (file-appender) ( path -- stream )
|
|
|
|
open-append <fd> init-fd <output-port> ;
|
|
|
|
|
|
|
|
M: unix home "HOME" os-env ;
|