]> gitweb.factorcode.org Git - factor.git/blob - basis/unix/unix.factor
unix: adding truncate-file.
[factor.git] / basis / unix / unix.factor
1 ! Copyright (C) 2005, 2010 Slava Pestov.
2 ! Copyright (C) 2008 Eduardo Cavazos.
3 ! See http://factorcode.org/license.txt for BSD license.
4 USING: accessors alien alien.c-types alien.libraries
5 alien.syntax byte-arrays classes.struct combinators
6 combinators.short-circuit combinators.smart continuations
7 generalizations io kernel libc locals macros math namespaces
8 sequences sequences.generalizations stack-checker strings system
9 unix.time unix.types vocabs vocabs.loader unix.ffi ;
10 IN: unix
11
12 ERROR: unix-error errno message ;
13
14 : (io-error) ( -- * ) errno dup strerror unix-error ;
15
16 : io-error ( n -- ) 0 < [ (io-error) ] when ;
17
18 ERROR: unix-system-call-error args errno message word ;
19
20 : unix-call-failed? ( ret -- ? )
21     {
22         [ { [ integer? ] [ 0 < ] } 1&& ]
23         [ not ]
24     } 1|| ;
25
26 MACRO:: unix-system-call ( quot -- )
27     quot inputs :> n
28     quot first :> word
29     0 :> ret!
30     f :> failed!
31     [
32         [
33             n ndup quot call ret!
34             ret {
35                 [ unix-call-failed? dup failed! ]
36                 [ drop errno EINTR = ]
37             } 1&&
38         ] loop
39         failed [
40             n narray
41             errno dup strerror
42             word unix-system-call-error
43         ] [
44             n ndrop
45             ret
46         ] if
47     ] ;
48
49 HOOK: open-file os ( path flags mode -- fd )
50
51 : close-file ( fd -- ) [ close ] unix-system-call drop ;
52
53 FUNCTION: int _exit ( int status ) ;
54
55 M: unix open-file [ open ] unix-system-call ;
56
57 : make-fifo ( path mode -- ) [ mkfifo ] unix-system-call drop ;
58
59 : truncate-file ( path n -- ) [ truncate ] unix-system-call drop ;
60
61 : touch ( filename -- ) f [ utime ] unix-system-call drop ;
62
63 : change-file-times ( filename access modification -- )
64     utimbuf <struct>
65         swap >>modtime
66         swap >>actime
67         [ utime ] unix-system-call drop ;
68
69 : read-symbolic-link ( path -- path )
70     PATH_MAX <byte-array> dup [
71         PATH_MAX
72         [ readlink ] unix-system-call
73     ] dip swap head-slice >string ;
74
75 : unlink-file ( path -- ) [ unlink ] unix-system-call drop ;
76
77 <<
78
79 { "unix" "debugger" } "unix.debugger" require-when
80
81 >>