1 ! Copyright (C) 2005, 2008 Slava Pestov, Doug Coleman.
2 ! See https://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 ;
8 : (cwd) ( bufsiz -- path )
10 dup <byte-array> over [ getcwd ] unix-system-call nip
12 dup errno>> ERANGE = [
20 M: unix cd [ chdir ] unix-system-call drop ;
22 CONSTANT: read-flags flags{ O_RDONLY }
24 : open-read ( path -- fd ) read-flags file-mode open-file ;
27 open-read <fd> init-fd <input-port> ;
29 CONSTANT: write-flags flags{ O_WRONLY O_CREAT O_TRUNC }
31 : open-write ( path -- fd )
32 write-flags file-mode open-file ;
35 open-write <fd> init-fd <output-port> ;
37 CONSTANT: secure-write-flags flags{ O_WRONLY O_CREAT O_TRUNC O_EXCL }
39 : open-secure-write ( path -- fd )
40 secure-write-flags file-mode open-file ;
42 M: unix (file-writer-secure)
43 open-secure-write <fd> init-fd <output-port> ;
45 CONSTANT: append-flags flags{ O_WRONLY O_APPEND O_CREAT }
47 : open-append ( path -- fd )
49 append-flags file-mode open-file |dispose
50 dup 0 SEEK_END [ lseek ] unix-system-call drop
53 M: unix (file-appender)
54 open-append <fd> init-fd <output-port> ;
56 M: unix home "HOME" os-env ;