Adds fopen() and fexist() and tests for all existing functions
This commit is contained in:
89
src/file.asm
89
src/file.asm
@@ -54,7 +54,7 @@ section .text
|
||||
|
||||
;----- fopen(*file[], char mode) -----;
|
||||
; Opens a file
|
||||
; Return value: Pointer to opened file pointer, 0 (EOS) otherwise.
|
||||
; Return value: Pointer to opened file pointer, -errno otherwise
|
||||
; Possible modes:
|
||||
; 'r' Read-only, file must exist
|
||||
; 'w' Write-only. Creates empty file or truncates an existing file
|
||||
@@ -68,6 +68,82 @@ section .text
|
||||
; rdi (arg) Pointer to string holding (path+)file name
|
||||
; rsi Mode to open the file with (see description above)
|
||||
fopen:
|
||||
%macro createFile 1
|
||||
push rdi
|
||||
push rsi
|
||||
call fexist
|
||||
pop rsi
|
||||
pop rdi
|
||||
mov rax, 1
|
||||
jz %%createFile
|
||||
jmp %1
|
||||
%%createFile:
|
||||
push rsi
|
||||
push rdi
|
||||
call umask_get
|
||||
pop rdi
|
||||
push rdi
|
||||
not rax
|
||||
mov rsi, 00666q
|
||||
and rsi, rax
|
||||
mov rax, NR_creat
|
||||
syscall
|
||||
pop rdi
|
||||
pop rsi
|
||||
%endmacro
|
||||
;;;
|
||||
;;; ENTRY
|
||||
;;;
|
||||
sub rsp, SIZE_QWORD
|
||||
|
||||
cmp sil, 'r'
|
||||
je .setMode_r
|
||||
cmp sil, 'w'
|
||||
je .setMode_w
|
||||
cmp sil, 'a'
|
||||
je .setMode_a
|
||||
cmp sil, 'R'
|
||||
je .setMode_R
|
||||
cmp sil, 'W'
|
||||
je .setMode_W
|
||||
cmp sil, 'A'
|
||||
je .setMode_A
|
||||
mov rax, -EINVAL
|
||||
jmp .quit
|
||||
|
||||
.setMode_r:
|
||||
mov rsi, O_RDONLY
|
||||
jmp .open
|
||||
.setMode_w:
|
||||
createFile .open_w
|
||||
.open_w:
|
||||
mov rsi, O_WRONLY | O_TRUNC
|
||||
jmp .open
|
||||
.setMode_a:
|
||||
createFile .open_a
|
||||
.open_a:
|
||||
mov rsi, O_WRONLY | O_APPEND
|
||||
jmp .open
|
||||
.setMode_R:
|
||||
mov rsi, O_RDWR
|
||||
jmp .open
|
||||
.setMode_W:
|
||||
createFile .open_W
|
||||
.open_W:
|
||||
mov rsi, O_RDWR | O_TRUNC
|
||||
jmp .open
|
||||
.setMode_A:
|
||||
createFile .open_A
|
||||
.open_A:
|
||||
mov rsi, O_RDWR | O_APPEND
|
||||
jmp .open
|
||||
|
||||
.open:
|
||||
mov rax, NR_open
|
||||
syscall
|
||||
|
||||
.quit:
|
||||
add rsp, SIZE_QWORD
|
||||
ret
|
||||
|
||||
;----- fclose(file) -----;
|
||||
@@ -96,4 +172,15 @@ fclose:
|
||||
; rax* (ret)
|
||||
; rdi (arg) Pointer to string holding (path+)file name
|
||||
fexist:
|
||||
mov rsi, 'r'
|
||||
call fopen
|
||||
test rax, rax
|
||||
js .not_exist
|
||||
mov rdi, rax
|
||||
call fclose
|
||||
mov rax, 1
|
||||
jmp .quit
|
||||
.not_exist:
|
||||
xor rax, rax
|
||||
.quit:
|
||||
ret
|
||||
|
Reference in New Issue
Block a user