factor/basis/unix/unix.factor

76 lines
1.9 KiB
Factor
Raw Normal View History

2010-01-14 10:10:13 -05:00
! Copyright (C) 2005, 2010 Slava Pestov.
2009-01-18 18:28:36 -05:00
! Copyright (C) 2008 Eduardo Cavazos.
2007-09-20 18:09:08 -04:00
! See http://factorcode.org/license.txt for BSD license.
USING: accessors alien alien.c-types alien.libraries
2013-03-29 14:36:13 -04:00
alien.syntax byte-vectors classes.struct combinators
combinators.short-circuit combinators.smart continuations
generalizations io kernel libc locals macros math namespaces
sequences sequences.generalizations stack-checker strings system
unix.time unix.types vocabs vocabs.loader unix.ffi ;
IN: unix
2008-02-26 21:59:46 -05:00
2008-05-21 16:54:02 -04:00
ERROR: unix-system-call-error args errno message word ;
: unix-call-failed? ( ret -- ? )
{
[ { [ integer? ] [ 0 < ] } 1&& ]
[ not ]
} 1|| ;
2008-05-14 01:44:27 -04:00
MACRO:: unix-system-call ( quot -- )
2010-01-14 10:10:13 -05:00
quot inputs :> n
2009-10-27 22:50:31 -04:00
quot first :> word
0 :> ret!
f :> failed!
2009-10-27 22:50:31 -04:00
[
[
n ndup quot call ret!
ret {
[ unix-call-failed? dup failed! ]
[ drop errno EINTR = ]
} 1&&
] loop
failed [
2009-10-27 22:50:31 -04:00
n narray
errno dup strerror
word unix-system-call-error
] [
n ndrop
ret
2009-10-27 22:50:31 -04:00
] if
2008-05-14 01:44:27 -04:00
] ;
2007-09-20 18:09:08 -04:00
2009-09-27 23:16:07 -04:00
HOOK: open-file os ( path flags mode -- fd )
2008-05-13 23:59:42 -04:00
: close-file ( fd -- ) [ close ] unix-system-call drop ;
FUNCTION: int _exit ( int status ) ;
M: unix open-file [ open ] unix-system-call ;
2008-10-19 14:09:48 -04:00
2012-10-24 23:08:32 -04:00
: make-fifo ( path mode -- ) [ mkfifo ] unix-system-call drop ;
2012-10-26 00:51:08 -04:00
: truncate-file ( path n -- ) [ truncate ] unix-system-call drop ;
: touch ( filename -- ) f [ utime ] unix-system-call drop ;
: change-file-times ( filename access modification -- )
2009-08-31 00:07:46 -04:00
utimbuf <struct>
swap >>modtime
swap >>actime
[ utime ] unix-system-call drop ;
2008-05-09 17:24:17 -04:00
2008-05-13 19:28:43 -04:00
: read-symbolic-link ( path -- path )
2013-03-29 14:36:13 -04:00
PATH_MAX <byte-vector> [
underlying>> PATH_MAX
[ readlink ] unix-system-call
2013-03-29 14:36:13 -04:00
] keep swap >>length >string ;
2008-05-13 19:28:43 -04:00
2008-05-13 20:05:12 -04:00
: unlink-file ( path -- ) [ unlink ] unix-system-call drop ;
2008-05-13 19:40:09 -04:00
<<
{ "unix" "debugger" } "unix.debugger" require-when
>>