More posix.

This commit is contained in:
Brad Nelson
2021-01-03 19:34:03 -08:00
parent fe10e1e6af
commit f3bcf32a12
3 changed files with 28 additions and 2 deletions

View File

@ -144,7 +144,7 @@ variable hld
: sign ( n -- ) 0< if 45 hold then ;
: #> ( w -- b u ) drop hld @ pad over - ;
: str ( n -- b u ) dup >r abs <# #s r> sign #> ;
: hex ( -- ) 16 base ! ;
: hex ( -- ) 16 base ! ; : octal ( -- ) 8 base ! ;
: decimal ( -- ) 10 base ! ;
: u. ( u -- ) <# #s #> type space ;
: . ( w -- ) base @ 10 xor if u. exit then str type space ;

View File

@ -90,7 +90,7 @@ static cell_t *eval1(cell_t *sp, cell_t *call) {
*++sp = n;
}
} else {
//fwrite((void *) name, 1, len, stderr);
//write(2, (void *) name, len);
*++sp = -1;
*call = g_sys.tthrow;
}

View File

@ -25,6 +25,8 @@ z" wait" 1 sysfunc wait
z" waitpid" 3 sysfunc waitpid
z" mmap" 6 sysfunc mmap
z" munmap" 2 sysfunc munmap
z" unlink" 1 sysfunc unlink
z" rename" 2 sysfunc rename
( Errno )
z" __errno_location" 0 sysfunc __errno_location
@ -48,6 +50,14 @@ z" __errno_location" 0 sysfunc __errno_location
$10 constant MAP_FIXED
$20 constant MAP_ANONYMOUS
( open )
0 constant O_RDONLY
1 constant O_WRONLY
2 constant O_RDWR
$100 constant O_CREAT
$200 constant O_TRUNC
$2000 constant O_APPEND
( Terminal handling )
: n. ( n -- ) base @ swap decimal <# #s #> type base ! ;
: esc 27 emit ;
@ -74,6 +84,7 @@ $20 constant MAP_ANONYMOUS
( I/O Error Helpers )
: 0ior ( n -- n ior ) dup 0= if errno else 0 then ;
: 0<ior ( n -- n ior ) dup 0< if errno else 0 then ;
( Words with OS assist )
z" malloc" 1 sysfunc malloc
@ -82,3 +93,18 @@ z" realloc" 2 sysfunc realloc
: allocate ( n -- a ior ) malloc 0ior ;
: free ( a -- ior ) sysfree 0 ;
: resize ( a n -- a ior ) realloc 0ior ;
( Generic Files )
O_RDONLY constant r/o
O_WRONLY constant w/o
O_RDWR constant r/w
octal 777 constant 0777 decimal
: s>z ( a n -- z ) here >r $place r> ;
: open-file ( a n fam -- fh ior ) >r s>z r> 0777 open 0<ior ;
: create-file ( a n fam -- fh ior ) >r s>z r> O_CREAT or 0777 open 0<ior ;
: close-file ( fh -- ior ) close 0<ior ;
: delete-file ( a n -- ior ) s>z unlink 0<ior ;
: rename-file ( a n a n -- ior ) s>z -rot s>z swap rename 0<ior ;
: read-file ( a n fh -- n ior ) -rot read 0<ior ;
: write-file ( a n fh -- ior ) -rot dup >r write r> = 0ior ;