unix
Factor documentation > Factor handbook > The implementation > System interface > Operating system detection
Prev:windows


Vocabulary
system

Definition
IN: system

UNION: unix macosx linux ;


Methods
USING: accessors io.sockets.private io.sockets.unix kernel
system unix.ffi ;

M: unix (broadcast)
dup handle>> SOL_SOCKET SO_BROADCAST set-socket-option ;


USING: destructors io.sockets.private io.sockets.unix system
unix.ffi ;

M: unix (datagram)
[ SOCK_DGRAM server-socket-fd ] with-destructors ;


USING: io.directories io.directories.unix kernel sequences
system ;

M: unix (directory-entries)
[
[ find-next-file dup ] curry
[ >directory-entry ] produce nip
] with-unix-directory ;


USING: accessors math.order system tools.files unix.groups
unix.users ;

M: unix (directory.)
<listing-tool> {
+permissions+
+nlinks+
+user+
+group+
+file-size+
+file-date+
+file-name+
} >>specs { { directory-entry>> name>> <=> } } >>sort
[ [ list-files ] with-group-cache ] with-user-cache ;


USING: io.backend.unix io.files io.files.unix io.ports system ;

M: unix (file-appender)
open-append <fd> init-fd <output-port> ;


USING: io.backend.unix io.files io.files.unix io.ports system ;

M: unix (file-reader) open-read <fd> init-fd <input-port> ;


USING: io.backend.unix io.files io.files.unix io.ports system ;

M: unix (file-writer) open-write <fd> init-fd <output-port> ;


USING: io.mmap.private io.mmap.unix system unix.ffi ;

M: unix (mapped-file-r/w) 3 1 O_RDWR mmap-open ;


USING: io.mmap.private io.mmap.unix system unix.ffi ;

M: unix (mapped-file-reader) 1 1 O_RDONLY mmap-open ;


USING: alien.c-types alien.data environment environment.unix
io.encodings.utf8 system unix.utilities ;

M: unix (os-envs) environ void* deref utf8 alien>strings ;


USING: alien.c-types alien.data io.backend.unix io.pipes kernel
sequences system unix unix.ffi ;

M: unix (pipe)
2 int <c-array>
[ pipe io-error ] [ first2 [ <fd> init-fd ] bi@ pipe boa ]
bi ;


USING: destructors io.sockets.private io.sockets.unix system
unix.ffi ;

M: unix (raw) [ SOCK_RAW server-socket-fd ] with-destructors ;


USING: io.sockets.private io.sockets.unix system ;

M: unix (receive-unsafe) (receive-loop) ;


USING: accessors io.sockets.private io.sockets.unix kernel
system ;

M: unix (send)
[ make-sockaddr/size ] [ [ handle>> ] keep ] bi* do-send ;


USING: alien.data environment io.encodings.utf8 system
unix.utilities ;

M: unix (set-os-envs)
utf8 strings>alien malloc-byte-array set-os-envs-pointer ;


USING: accessors classes.struct io.streams.c kernel math system
terminal terminal.linux.private unix.ffi ;

M: unix (terminal-size)
stdout-handle fileno TIOCGWINSZ winsize <struct>
[ ioctl ] keep swap 0 <
[ drop 0 0 ] [ [ ws_col>> ] [ ws_row>> ] bi ] if ;


USING: io.backend.unix io.files.unique io.files.unique.unix
system unix ;

M: unix (touch-unique-file)
open-unique-flags file-mode open-file close-file ;


USING: accessors destructors io.backend.unix io.ports kernel
system ;

M: unix (wait-to-read)
dup dup handle>> dup check-disposed refill dup
[ dupd wait-for-port (wait-to-read) ] [ 2drop ] if ;


USING: accessors destructors io.backend.unix io.ports kernel
system ;

M: unix (wait-to-write)
dup dup handle>> dup check-disposed drain dup
[ wait-for-port ] [ 2drop ] if ;


USING: alien.libraries io.pathnames system ;

M: unix >deployed-library-path
file-name "$ORIGIN" prepend-path ;


USING: accessors alien.strings combinators io.directories
io.directories.unix io.encodings.utf8 kernel system ;

M: unix >directory-entry
{
[ d_name>> underlying>> utf8 alien>string ]
[ d_type>> dirent-type>file-type ]
} cleave directory-entry boa ;


USING: io.sockets.private kernel math system unix.ffi ;

M: unix addrinfo-error [ gai_strerror throw ] unless-zero ;


USING: combinators io.sockets io.sockets.private kernel system
unix.ffi ;

M: unix addrspec-of-family
{
{ AF_INET [ T{ ipv4 } ] }
{ AF_INET6 [ T{ ipv6 } ] }
{ AF_UNIX [ T{ local } ] }
[ drop f ]
} case ;


USING: accessors io.backend.unix.multiplexers kernel namespaces
sequences system threads x11.io x11.io.unix ;

M: unix awaken-event-loop
dpy-fd get [
fd>> mx get-global remove-input-callbacks
[ resume ] each
] when* ;


USING: io.files.temp io.files.temp.unix system ;

M: unix cache-directory (cache-directory) ;


USING: accessors io.ports kernel system unix.ffi ;

M: unix can-seek-handle? fd>> SEEK_CUR 0 lseek -1 = not ;


USING: io.files.private kernel system unix unix.ffi ;

M: unix cd [ chdir ] unix-system-call drop ;


USING: accessors io.mmap kernel system unix unix.ffi ;

M: unix close-mapped-file
[ [ address>> ] [ length>> ] bi munmap io-error ]
[ handle>> close-file ] bi ;


USING: generic io.backend io.directories io.files.info.unix
kernel system ;

M: unix copy-file
[ normalize-path ] bi@
[ M\ unix copy-file (call-next-method) ]
[ [ file-permissions ] dip swap set-file-permissions ] 2bi
;


USING: io.launcher system unix.ffi ;

M: unix current-process-handle getpid ;


USING: byte-arrays io.files.private kernel system unix unix.ffi
;

M: unix cwd
MAXPATHLEN [ <byte-array> ] keep
[ getcwd ] unix-system-call [ (io-error) ] unless* ;


USING: accessors io.serial system ;

M: unix default-serial-flags
260 >>iflag 0 >>oflag 2224 >>cflag 2 >>lflag ;


USING: io.files.unique system ;

M: unix default-temporary-directory "/tmp" ;


USING: io.backend io.directories kernel system unix unix.ffi ;

M: unix delete-directory
normalize-path [ rmdir ] unix-system-call drop ;


USING: io.backend io.directories system unix ;

M: unix delete-file normalize-path unlink-file ;


USING: io io.backend io.directories kernel namespaces sequences
system tools.deploy.backend tools.deploy.config.editor
tools.deploy.unix webbrowser ;

M: unix deploy*
"resource:" [
dup deploy-config [
[ bundle-name create-app-dir ] keep
[ deployed-image-name ] keep
namespace make-deploy-image-executable
bundle-name ""
[ copy-resources ] [ copy-libraries ] 3bi
bundle-name normalize-path "Binary deployed to "
"." surround print bundle-name open-file
] with-variables
] with-directory ;


USING: alien.libraries alien.libraries.unix system ;

M: unix dlerror (dlerror) ;


USING: alien.libraries environment.unix system ;

M: unix environ "environ" f address-of ;


USING: io.files.info io.files.info.unix.private system unix.ffi
;

M: unix file-executable? X_OK access? ;


USING: io.backend io.files.info io.files.info.unix system
unix.stat ;

M: unix file-info normalize-path file-status stat>file-info ;


USING: io.files.info io.files.info.unix.private system unix.ffi
;

M: unix file-readable? R_OK access? ;


USING: accessors combinators generic io.files.info.unix kernel
math.parser sequences system tools.files
tools.files.unix.private unix.groups unix.users ;

M: unix file-spec>string
{
{
+file-name/type+
[
directory-entry>>
[ name>> ] [ file-type>trailing ] bi append
]
}
{ +permissions+ [ file-info>> permissions-string ] }
{ +nlinks+ [ file-info>> nlink>> number>string ] }
{ +user+ [ file-info>> uid>> user-name ] }
{ +group+ [ file-info>> gid>> group-name ] }
{ +uid+ [ file-info>> uid>> number>string ] }
{ +gid+ [ file-info>> gid>> number>string ] }
[ M\ unix file-spec>string (call-next-method) ]
} case ;


USING: io.backend io.files.info io.files.info.unix kernel
system ;

M: unix file-system-info
normalize-path [ new-file-system-info ] dip
[ file-system-statfs statfs>file-system-info ]
[ file-system-statvfs statvfs>file-system-info ] bi
file-system-calculations ;


USING: io.files.info.unix kernel system ;

M: unix file-system-statfs drop f ;


USING: io.files.info.unix kernel system ;

M: unix file-system-statvfs drop f ;


USING: io.files.info io.files.info.unix.private system unix.ffi
;

M: unix file-writable? W_OK access? ;


USING: environment io.files io.pathnames io.standard-paths
kernel sequences splitting system ;

M: unix find-in-path*
[ "PATH" os-env ":" split ] dip
[ append-path exists? ] curry find nip ;


USING: io.pathnames kernel sequences system
tools.deploy.libraries tools.deploy.libraries.unix ;

M: unix find-library-file
dup absolute-path?
[ ?exists ] [
{
"/lib"
"/usr/lib"
"/usr/local/lib"
"/opt/local/lib"
"resource:"
} [ prepend-path ?exists ] with map-find drop
] if ;


USING: alien.c-types alien.data classes.struct
io.directories.unix kernel libc system unix unix.ffi ;

M: unix find-next-file
dirent <struct> f void* <ref> 0 set-errno [
readdir_r 0 =
[ errno 0 = [ (io-error) ] unless ] unless
] 2keep void* deref [ drop f ] unless ;


USING: editors.geany kernel namespaces system ;

M: unix geany-path
\ geany-path get-global [ "geany" ] unless* ;


USING: calendar calendar.unix system ;

M: unix gmt current-timeval timeval>unix-time ;


USING: accessors calendar calendar.unix math system ;

M: unix gmt-offset get-time gmtoff>> 3600 /mod 60 /mod ;


USING: accessors classes.struct io.ports kernel system
unix.stat ;

M: unix handle-length
fd>> \ stat <struct> [ fstat -1 = not ] keep swap
[ st_size>> ] [ drop f ] if ;


USING: environment io.pathnames system ;

M: unix home "HOME" os-env ;


USING: io.backend io.backend.unix io.ports system ;

M: unix init-stdio
<stdin> <input-port> 1 <fd> <output-port> 2 <fd>
<output-port> set-stdio ;


USING: io.backend.unix namespaces system x11 x11.io x11.io.unix
x11.xlib ;

M: unix init-x-io
dpy get XConnectionNumber <fd> dpy-fd set-global ;


USING: accessors dns resolv-conf system ;

M: unix initial-dns-servers default-resolv.conf nameserver>> ;


USING: io.backend io.backend.unix.multiplexers namespaces
system ;

M: unix io-multiplex mx get-global wait-for-events ;


USING: accessors combinators io.launcher kernel system unix
unix.ffi unix.process ;

M: unix kill-process*
[ handle>> SIGTERM ] [ group>> ] bi {
{ +same-group+ [ kill ] }
{ +new-group+ [ killpg ] }
{ +new-session+ [ killpg ] }
} case io-error ;


USING: io.backend io.files.info io.files.info.unix system
unix.stat ;

M: unix link-info normalize-path link-status stat>file-info ;


USING: io.backend io.directories kernel system unix unix.ffi ;

M: unix make-directory
normalize-path 511 [ mkdir ] unix-system-call drop ;


USING: io.backend io.files.links kernel system unix unix.ffi ;

M: unix make-hard-link
normalize-path [ link ] unix-system-call drop ;


USING: io.backend io.files.links kernel system unix unix.ffi ;

M: unix make-link
normalize-path [ symlink ] unix-system-call drop ;


USING: io.backend io.directories kernel system unix unix.ffi ;

M: unix move-file
[ normalize-path ] bi@ [ rename ] unix-system-call drop ;


USING: alien.strings io.encodings.utf8 system ;

M: unix native-string-encoding utf8 ;


USING: io.files.info.unix kernel system ;

M: unix new-file-info unix-file-info new ;


USING: io.files.info.unix kernel system ;

M: unix new-file-system-info unix-file-system-info new ;


USING: kernel system unix.users ;

M: unix new-passwd passwd new ;


USING: system unix unix.ffi ;

M: unix open-file [ open ] unix-system-call ;


USING: combinators io.serial io.serial.linux system ;

M: unix open-serial
{
[ open-unix-serial-port ]
[ set-termios ]
[ configure-termios ]
[ tciflush ]
[ apply-termios ]
[ ]
} cleave ;


USING: environment system unix.ffi ;

M: unix os-env getenv ;


USING: accessors combinators kernel system unix.users ;

M: unix passwd>new-passwd
[ new-passwd ] dip {
[ pw_name>> >>user-name ]
[ pw_passwd>> >>password ]
[ pw_uid>> >>uid ]
[ pw_gid>> >>gid ]
[ pw_gecos>> >>gecos ]
[ pw_dir>> >>dir ]
[ pw_shell>> >>shell ]
} cleave ;


USING: io.backend io.files.links system unix ;

M: unix read-link normalize-path read-symbolic-link ;


USING: io.files io.files.links io.pathnames kernel sequences
system ;

M: unix resolve-symlinks
path-components "/"
[ append-path dup exists? [ follow-links ] when ] reduce ;


USING: io.launcher io.launcher.unix kernel system unix.process
;

M: unix run-process* [ spawn-process ] curry [ ] with-fork ;


USING: system tools.scaffold ;

M: unix scaffold-emacs ".emacs" scaffold-rc ;


USING: accessors combinators io io.ports kernel system unix
unix.ffi ;

M: unix seek-handle
swap {
{ seek-absolute [ SEEK_SET ] }
{ seek-relative [ SEEK_CUR ] }
{ seek-end [ SEEK_END ] }
[ bad-seek-type ]
} case [ fd>> swap ] dip [ lseek ] unix-system-call drop ;


USING: calendar formatting io io.directories io.encodings.utf8
io.files io.files.trash io.files.trash.unix.private
io.pathnames kernel sequences system ;

M: unix send-to-trash
dup trash-path [
"files" append-path [ make-user-directory ] keep
to-directory safe-file-name
] [
"info" append-path [ make-user-directory ] keep
to-directory ".trashinfo" append [ over ] dip utf8 [
"[Trash Info]" write nl "Path=" write write
nl "DeletionDate=" write
now "%Y-%m-%dT%H:%M:%S" strftime write nl
] with-file-writer
] bi move-file ;


USING: environment kernel system unix unix.ffi ;

M: unix set-os-env
over [ swap 1 setenv io-error ] [ nip unset-os-env ] if ;


USING: environment environment.unix system ;

M: unix set-os-envs-pointer environ set-void* ;


USING: calendar kernel math system time time.unix unix
unix.time ;

M: unix set-time
[
unix-1970 time-
duration>microseconds >integer make-timeval
] [ timestamp>timezone ] bi settimeofday io-error ;


USING: debugger debugger.unix io kernel prettyprint sequences
system ;

M: unix signal-error.
"Unix signal #" write third [ pprint ] [ signal-name. ] bi
nl ;


USING: classes.struct combinators io.sockets.private kernel
system unix.ffi ;

M: unix sockaddr-of-family
{
{ AF_INET [ sockaddr-in memory>struct ] }
{ AF_INET6 [ sockaddr-in6 memory>struct ] }
{ AF_UNIX [ sockaddr-un memory>struct ] }
[ 2drop f ]
} case ;


USING: accessors calendar.unix combinators io.files.info.unix
kernel math system ;

M: unix stat>file-info
[ new-file-info ] dip {
[ stat>type >>type ]
[ st_size>> >>size ]
[ st_mode>> >>permissions ]
[ st_ctimespec>> timespec>unix-time >>created ]
[ st_mtimespec>> timespec>unix-time >>modified ]
[ st_atimespec>> timespec>unix-time >>accessed ]
[ st_uid>> >>uid ]
[ st_gid>> >>gid ]
[ st_dev>> >>dev ]
[ st_ino>> >>ino ]
[ st_nlink>> >>nlink ]
[ st_rdev>> >>rdev ]
[ st_blocks>> >>blocks ]
[ st_blksize>> >>blocksize ]
[
drop dup blocks>> standard-unix-block-size *
>>size-on-disk
]
} cleave ;


USING: accessors io.files.info.unix system ;

M: unix stat>type st_mode>> n>file-type ;


USING: io.files.info.unix kernel system ;

M: unix statfs>file-system-info drop ;


USING: io.files.info.unix kernel system ;

M: unix statvfs>file-system-info drop ;


USING: accessors io.ports kernel system unix unix.ffi ;

M: unix tell-handle
fd>> 0 SEEK_CUR [ lseek ] unix-system-call
[ io-error ] [ ] bi ;


USING: io.files.temp io.files.temp.unix system ;

M: unix temp-directory (temp-directory) ;


USING: io.backend io.directories io.directories.unix io.files
kernel system unix ;

M: unix touch-file
normalize-path dup exists?
[ touch ] [ touch-mode file-mode open-file close-file ] if
;


USING: environment system unix unix.ffi ;

M: unix unset-os-env unsetenv io-error ;


USING: io.backend.unix namespaces system x11.io x11.io.unix ;

M: unix wait-for-display dpy-fd get +input+ wait-for-fd ;


USING: alien.c-types alien.data io.launcher io.launcher.unix
kernel math system unix.process ;

M: unix wait-for-processes
{ int } [ -1 swap WNOHANG waitpid ] with-out-parameters
swap dup 0 <=
[ 2drop t ] [
find-process dup
[ swap code>status notify-exit f ] [ 2drop f ] if
] if ;


USING: alien.c-types combinators continuations glib.ffi
io.thread kernel libc locals system ui.backend.gtk.io
ui.backend.gtk.io.unix ;

M:: unix with-event-loop ( quot -- )
stop-io-thread <funcs> &free GSource heap-size g_source_new
&g_source_unref :> source source <poll-fd>
g_source_add_poll source f g_source_attach drop
[ quot ( -- ) call-effect ]
[ source g_source_destroy start-io-thread ] [ ] cleanup ;