<commit_before>! Copyright (C) 2005, 2008 <NAME>.
! Copyright (C) 2008 <NAME>.
! See http://factorcode.org/license.txt for BSD license.
USING: alien alien.c-types alien.syntax kernel libc
sequences continuations byte-arrays strings math namespaces
system combinators vocabs.loader accessors
stack-checker macros locals generalizations unix.types
io vocabs classes.struct unix.time ;
IN: unix

CONSTANT: PROT_NONE   0
CONSTANT: PROT_READ   1
CONSTANT: PROT_WRITE  2
CONSTANT: PROT_EXEC   4
                       
CONSTANT: MAP_FILE    0
CONSTANT: MAP_SHARED  1
CONSTANT: MAP_PRIVATE 2

CONSTANT: SEEK_SET 0
CONSTANT: SEEK_CUR 1
CONSTANT: SEEK_END 2

: MAP_FAILED ( -- alien ) -1 <alien> ; inline

CONSTANT: NGROUPS_MAX 16

CONSTANT: DT_UNKNOWN   0
CONSTANT: DT_FIFO      1
CONSTANT: DT_CHR       2
CONSTANT: DT_DIR       4
CONSTANT: DT_BLK       6
CONSTANT: DT_REG       8
CONSTANT: DT_LNK      10
CONSTANT: DT_SOCK     12
CONSTANT: DT_WHT      14

LIBRARY: libc

FUNCTION: char* strerror ( int errno ) ;

ERROR: unix-error errno message ;

: (io-error) ( -- * ) errno dup strerror unix-error ;

: io-error ( n -- ) 0 < [ (io-error) ] when ;

ERROR: unix-system-call-error args errno message word ;

MACRO:: unix-system-call ( quot -- )
    [let | n [ quot infer in>> ]
           word [ quot first ] |
        [
            n ndup quot call dup 0 < [
                drop
                n narray
                errno dup strerror
                word unix-system-call-error
            ] [
                n nnip
            ] if
        ]
    ] ;

<<

{
    { [ os linux? ] [ "unix.linux" require ] }
    { [ os bsd? ] [ "unix.bsd" require ] }
    { [ os solaris? ] [ "unix.solaris" require ] }
} cond

"debugger" vocab [
    "unix.debugger" require
] when

>>

STRUCT: group
    { gr_name char* }
    { gr_passwd char* }
    { gr_gid int }
    { gr_mem char** } ;

FUNCTION: int accept ( int s, void* sockaddr, socklen_t* socklen ) ;
FUNCTION: int bind ( int s, void* name, socklen_t namelen ) ;
FUNCTION: int chdir ( char* path ) ;
FUNCTION: int chmod ( char* path, mode_t mode ) ;
FUNCTION: int fchmod ( int fd, mode_t mode ) ;
FUNCTION: int chown ( char* path, uid_t owner, gid_t group ) ;
FUNCTION: int chroot ( char* path ) ;

FUNCTION: int close ( int fd ) ;
FUNCTION: int closedir ( DIR* dirp ) ;

: close-file ( fd -- ) [ close ] unix-system-call drop ;

FUNCTION: int connect ( int s, void* name, socklen_t namelen ) ;
FUNCTION: int dup2 ( int oldd, int newd ) ;
! FUNCTION: int dup ( int oldd ) ;
: _exit ( status -- * )
    #! We throw to give this a terminating stack effect.
    int f "_exit" { int } alien-invoke "Exit failed" throw ;
FUNCTION: void endpwent ( ) ;
FUNCTION: int fchdir ( int fd ) ;
FUNCTION: int fchown ( int fd, uid_t owner, gid_t group ) ;
FUNCTION: int fcntl ( int fd, int cmd, int arg ) ;
FUNCTION: int flock ( int fd, int operation ) ;
FUNCTION: void freeaddrinfo ( addrinfo* ai ) ;
FUNCTION: int futimes ( int id, timeval[2] times ) ;
FUNCTION: char* gai_strerror ( int ecode ) ;
FUNCTION: int getaddrinfo ( char* hostname, char* servname, addrinfo* hints, addrinfo** res ) ;
FUNCTION: char* getcwd ( char* buf, size_t size ) ;
FUNCTION: pid_t getpid ;
FUNCTION: int getdtablesize ;
FUNCTION: gid_t getegid ;
FUNCTION: uid_t geteuid ;
FUNCTION: gid_t getgid ;
FUNCTION: char* getenv ( char* name ) ;

FUNCTION: int getgrgid_r ( gid_t gid, group* grp, char* buffer, size_t bufsize, group** result ) ;
FUNCTION: int getgrnam_r ( char* name, group* grp, char* buffer, size_t bufsize, group** result ) ;
FUNCTION: passwd* getpwent ( ) ;
FUNCTION: passwd* getpwuid ( uid_t uid ) ;
FUNCTION: passwd* getpwnam ( char* login ) ;
FUNCTION: int getpwnam_r ( char* login, passwd* pwd, char* buffer, size_t bufsize, passwd** result ) ;
FUNCTION: int getgroups ( int gidsetlen, gid_t* gidset ) ;
FUNCTION: int getgrouplist ( char* name, int basegid, int* groups, int* ngroups ) ;
FUNCTION: int getrlimit ( int resource, rlimit* rlp ) ;
FUNCTION: int setrlimit ( int resource, rlimit* rlp ) ;

FUNCTION: int getpriority ( int which, id_t who ) ;
FUNCTION: int setpriority ( int which, id_t who, int prio ) ;

FUNCTION: int getrusage ( int who, rusage* r_usage ) ;

FUNCTION: group* getgrent ;
FUNCTION: int gethostname ( char* name, int len ) ;
FUNCTION: int getsockname ( int socket, sockaddr* address, socklen_t* address_len ) ;
FUNCTION: int getpeername ( int socket, sockaddr* address, socklen_t* address_len ) ;
FUNCTION: uid_t getuid ;
FUNCTION: uint htonl ( uint n ) ;
FUNCTION: ushort htons ( ushort n ) ;
! FUNCTION: int issetugid ;
FUNCTION: int ioctl ( int fd, ulong request, char* argp ) ;
FUNCTION: int lchown ( char* path, uid_t owner, gid_t group ) ;
FUNCTION: int listen ( int s, int backlog ) ;
FUNCTION: off_t lseek ( int fildes, off_t offset, int whence ) ;
FUNCTION: int mkdir ( char* path, mode_t mode ) ;
FUNCTION: void* mmap ( void* addr, size_t len, int prot, int flags, int fd, off_t offset ) ;
FUNCTION: int munmap ( void* addr, size_t len ) ;
FUNCTION: uint ntohl ( uint n ) ;
FUNCTION: ushort ntohs ( ushort n ) ;
FUNCTION: int shutdown ( int fd, int how ) ;

FUNCTION: int open ( char* path, int flags, int prot ) ;

HOOK: open-file os ( path flags mode -- fd )

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

FUNCTION: DIR* opendir ( char* path ) ;

STRUCT: utimbuf
    { actime time_t }
    { modtime time_t } ;<commit_msg>fix linux bootstrap<commit_after>! Copyright (C) 2005, 2008 <NAME>.
! Copyright (C) 2008 <NAME>.
! See http://factorcode.org/license.txt for BSD license.
USING: alien alien.c-types alien.syntax kernel libc
sequences continuations byte-arrays strings math namespaces
system combinators vocabs.loader accessors
stack-checker macros locals generalizations unix.types
io vocabs classes.struct unix.time ;
IN: unix

CONSTANT: PROT_NONE   0
CONSTANT: PROT_READ   1
CONSTANT: PROT_WRITE  2
CONSTANT: PROT_EXEC   4
                       
CONSTANT: MAP_FILE    0
CONSTANT: MAP_SHARED  1
CONSTANT: MAP_PRIVATE 2

CONSTANT: SEEK_SET 0
CONSTANT: SEEK_CUR 1
CONSTANT: SEEK_END 2

: MAP_FAILED ( -- alien ) -1 <alien> ; inline

CONSTANT: NGROUPS_MAX 16

CONSTANT: DT_UNKNOWN   0
CONSTANT: DT_FIFO      1
CONSTANT: DT_CHR       2
CONSTANT: DT_DIR       4
CONSTANT: DT_BLK       6
CONSTANT: DT_REG       8
CONSTANT: DT_LNK      10
CONSTANT: DT_SOCK     12
CONSTANT: DT_WHT      14

LIBRARY: libc

FUNCTION: char* strerror ( int errno ) ;

ERROR: unix-error errno message ;

: (io-error) ( -- * ) errno dup strerror unix-error ;

: io-error ( n -- ) 0 < [ (io-error) ] when ;

ERROR: unix-system-call-error args errno message word ;

MACRO:: unix-system-call ( quot -- )
    [let | n [ quot infer in>> ]
           word [ quot first ] |
        [
            n ndup quot call dup 0 < [
                drop
                n narray
                errno dup strerror
                word unix-system-call-error
            ] [
                n nnip
            ] if
        ]
    ] ;

HOOK: open-file os ( path flags mode -- fd )

<<

{
    { [ os linux? ] [ "unix.linux" require ] }
    { [ os bsd? ] [ "unix.bsd" require ] }
    { [ os solaris? ] [ "unix.solaris" require ] }
} cond

"debugger" vocab [
    "unix.debugger" require
] when

>>

STRUCT: group
    { gr_name char* }
    { gr_passwd char* }
    { gr_gid int }
    { gr_mem char** } ;

FUNCTION: int accept ( int s, void* sockaddr, socklen_t* socklen ) ;
FUNCTION: int bind ( int s, void* name, socklen_t namelen ) ;
FUNCTION: int chdir ( char* path ) ;
FUNCTION: int chmod ( char* path, mode_t mode ) ;
FUNCTION: int fchmod ( int fd, mode_t mode ) ;
FUNCTION: int chown ( char* path, uid_t owner, gid_t group ) ;
FUNCTION: int chroot ( char* path ) ;

FUNCTION: int close ( int fd ) ;
FUNCTION: int closedir ( DIR* dirp ) ;

: close-file ( fd -- ) [ close ] unix-system-call drop ;

FUNCTION: int connect ( int s, void* name, socklen_t namelen ) ;
FUNCTION: int dup2 ( int oldd, int newd ) ;
! FUNCTION: int dup ( int oldd ) ;
: _exit ( status -- * )
    #! We throw to give this a terminating stack effect.
    int f "_exit" { int } alien-invoke "Exit failed" throw ;
FUNCTION: void endpwent ( ) ;
FUNCTION: int fchdir ( int fd ) ;
FUNCTION: int fchown ( int fd, uid_t owner, gid_t group ) ;
FUNCTION: int fcntl ( int fd, int cmd, int arg ) ;
FUNCTION: int flock ( int fd, int operation ) ;
FUNCTION: void freeaddrinfo ( addrinfo* ai ) ;
FUNCTION: int futimes ( int id, timeval[2] times ) ;
FUNCTION: char* gai_strerror ( int ecode ) ;
FUNCTION: int getaddrinfo ( char* hostname, char* servname, addrinfo* hints, addrinfo** res ) ;
FUNCTION: char* getcwd ( char* buf, size_t size ) ;
FUNCTION: pid_t getpid ;
FUNCTION: int getdtablesize ;
FUNCTION: gid_t getegid ;
FUNCTION: uid_t geteuid ;
FUNCTION: gid_t getgid ;
FUNCTION: char* getenv ( char* name ) ;

FUNCTION: int getgrgid_r ( gid_t gid, group* grp, char* buffer, size_t bufsize, group** result ) ;
FUNCTION: int getgrnam_r ( char* name, group* grp, char* buffer, size_t bufsize, group** result ) ;
FUNCTION: passwd* getpwent ( ) ;
FUNCTION: passwd* getpwuid ( uid_t uid ) ;
FUNCTION: passwd* getpwnam ( char* login ) ;
FUNCTION: int getpwnam_r ( char* login, passwd* pwd, char* buffer, size_t bufsize, passwd** result ) ;
FUNCTION: int getgroups ( int gidsetlen, gid_t* gidset ) ;
FUNCTION: int getgrouplist ( char* name, int basegid, int* groups, int* ngroups ) ;
FUNCTION: int getrlimit ( int resource, rlimit* rlp ) ;
FUNCTION: int setrlimit ( int resource, rlimit* rlp ) ;

FUNCTION: int getpriority ( int which, id_t who ) ;
FUNCTION: int setpriority ( int which, id_t who, int prio ) ;

FUNCTION: int getrusage ( int who, rusage* r_usage ) ;

FUNCTION: group* getgrent ;
FUNCTION: int gethostname ( char* name, int len ) ;
FUNCTION: int getsockname ( int socket, sockaddr* address, socklen_t* address_len ) ;
FUNCTION: int getpeername ( int socket, sockaddr* address, socklen_t* address_len ) ;
FUNCTION: uid_t getuid ;
FUNCTION: uint htonl ( uint n ) ;
FUNCTION: ushort htons ( ushort n ) ;
! FUNCTION: int issetugid ;
FUNCTION: int ioctl ( int fd, ulong request, char* argp ) ;
FUNCTION: int lchown ( char* path, uid_t owner, gid_t group ) ;
FUNCTION: int listen ( int s, int backlog ) ;
FUNCTION: off_t lseek ( int fildes, off_t offset, int whence ) ;
FUNCTION: int mkdir ( char* path, mode_t mode ) ;
FUNCTION: void* mmap ( void* addr, size_t len, int prot, int flags, int fd, off_t offset ) ;
FUNCTION: int munmap ( void* addr, size_t len ) ;
FUNCTION: uint ntohl ( uint n ) ;
FUNCTION: ushort ntohs ( ushort n ) ;
FUNCTION: int shutdown ( int fd, int how ) ;

FUNCTION: int open ( char* path, int flags, int prot ) ;

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

FUNCTION: DIR* opendir ( char* path ) ;

STRUCT: utimbuf
    { actime time_t }
    { modtime time_t } ;
{'fraction_non_alphanumeric': 0.11012070566388116, 'fraction_numerical': 0.007613741875580316, 'mean_word_length': 3.179666278618549, 'pattern_counts': {'":': 0, '<': 17, '<?xml version=': 0, '>': 17, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '1034340', 'n_tokens_mistral': 3823, 'n_tokens_neox': 3712, 'n_words': 1296}