]> gitweb.factorcode.org Git - factor.git/blob - basis/io/files/unix/unix.factor
bf0a21f997921bd32b6256e3ea847571968b5669
[factor.git] / basis / io / files / unix / unix.factor
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 ;
6 IN: io.files.unix
7
8 M: unix cwd ( -- path )
9     MAXPATHLEN [ <byte-array> ] keep
10     [ getcwd ] unix-system-call
11     [ (io-error) ] unless* ;
12
13 M: unix cd ( path -- ) [ chdir ] unix-system-call drop ;
14
15 : read-flags ( -- n ) O_RDONLY ; inline
16
17 : open-read ( path -- fd ) O_RDONLY file-mode open-file ;
18
19 M: unix (file-reader) ( path -- stream )
20     open-read <fd> init-fd <input-port> ;
21
22 : write-flags ( -- n )
23     { O_WRONLY O_CREAT O_TRUNC } flags ; inline
24
25 : open-write ( path -- fd )
26     write-flags file-mode open-file ;
27
28 M: unix (file-writer) ( path -- stream )
29     open-write <fd> init-fd <output-port> ;
30
31 : append-flags ( -- n )
32     { O_WRONLY O_APPEND O_CREAT } flags ; inline
33
34 : open-append ( path -- fd )
35     [
36         append-flags file-mode open-file |dispose
37         dup 0 SEEK_END [ lseek ] unix-system-call drop
38     ] with-destructors ;
39
40 M: unix (file-appender) ( path -- stream )
41     open-append <fd> init-fd <output-port> ;
42
43 M: unix home "HOME" os-env ;