]> gitweb.factorcode.org Git - factor.git/blob - basis/io/files/unix/unix.factor
47aa5bb34d021897eecbe3ba050961603c8d814f
[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: accessors byte-arrays continuations destructors environment
4 io.backend.unix io.files io.files.private io.pathnames io.ports kernel
5 libc literals math system unix unix.ffi ;
6 IN: io.files.unix
7
8 : (cwd) ( bufsiz -- path )
9     [
10         dup <byte-array> over [ getcwd ] unix-system-call nip
11     ] [
12         dup errno>> ERANGE = [
13             drop 2 * (cwd)
14         ] [ rethrow ] if
15     ] recover ;
16
17 M: unix cwd ( -- path )
18     4096 (cwd) ;
19
20 M: unix cd ( path -- ) [ chdir ] unix-system-call drop ;
21
22 CONSTANT: read-flags flags{ O_RDONLY }
23
24 : open-read ( path -- fd ) read-flags file-mode open-file ;
25
26 M: unix (file-reader) ( path -- stream )
27     open-read <fd> init-fd <input-port> ;
28
29 CONSTANT: write-flags flags{ O_WRONLY O_CREAT O_TRUNC }
30
31 : open-write ( path -- fd )
32     write-flags file-mode open-file ;
33
34 M: unix (file-writer) ( path -- stream )
35     open-write <fd> init-fd <output-port> ;
36
37 CONSTANT: append-flags flags{ O_WRONLY O_APPEND O_CREAT }
38
39 : open-append ( path -- fd )
40     [
41         append-flags file-mode open-file |dispose
42         dup 0 SEEK_END [ lseek ] unix-system-call drop
43     ] with-destructors ;
44
45 M: unix (file-appender) ( path -- stream )
46     open-append <fd> init-fd <output-port> ;
47
48 M: unix home "HOME" os-env ;