Adds fwrite()
This commit is contained in:
38
src/file.asm
38
src/file.asm
@@ -1,5 +1,6 @@
|
||||
%include "src/constants.asm"
|
||||
|
||||
extern strlen
|
||||
extern umask_get
|
||||
|
||||
section .rodata
|
||||
@@ -51,6 +52,7 @@ section .text
|
||||
global fopen
|
||||
global fclose
|
||||
global fexist
|
||||
global fwrite
|
||||
|
||||
;----- fopen(*file[], char mode) -----;
|
||||
; Opens a file
|
||||
@@ -146,7 +148,7 @@ fopen:
|
||||
add rsp, SIZE_QWORD
|
||||
ret
|
||||
|
||||
;----- fclose(file) -----;
|
||||
;----- fclose(*filePointer) -----;
|
||||
; Closes an opened file
|
||||
; Return value: 0 if file closed. Negative error number if an error occured
|
||||
; Used registers:
|
||||
@@ -167,11 +169,12 @@ fclose:
|
||||
|
||||
;----- fexist(*file[]) -----;
|
||||
; Checks if given file exists (attempts to open the file)
|
||||
; Return value: 0- File closed succesfully. -errno (negative error number) if an error occured
|
||||
; Return value: 1- File exists. 0- File does not exist or is a directory
|
||||
; Used registers:
|
||||
; rax* (ret)
|
||||
; rdi (arg) Pointer to string holding (path+)file name
|
||||
fexist:
|
||||
;sub rsp, SIZE_QWORD
|
||||
mov rsi, 'r'
|
||||
call fopen
|
||||
test rax, rax
|
||||
@@ -183,4 +186,35 @@ fexist:
|
||||
.not_exist:
|
||||
xor rax, rax
|
||||
.quit:
|
||||
;add rsp, SIZE_QWORD
|
||||
ret
|
||||
|
||||
;--- fwrite(*filePointer, *data[]) -----;
|
||||
; Writes data to given file
|
||||
; Return value: Length of given string(data[]), 0 if nothing was written (eg invalid file or file not writable, or data[] is empty)
|
||||
; Used registers:
|
||||
; rax* (ret)
|
||||
; rdi (arg) Pointer to file
|
||||
; rsi (arg) Data to write
|
||||
; r8* Backs up rdi
|
||||
; r9* Backs up rsi
|
||||
fwrite:
|
||||
sub rsp, SIZE_QWORD
|
||||
xor rax, rax
|
||||
cmp rdi, 3
|
||||
jl .quit
|
||||
mov r8, rdi
|
||||
mov r9, rsi
|
||||
|
||||
mov rdi, rsi
|
||||
call strlen
|
||||
|
||||
mov rdx, rax
|
||||
mov rax, NR_write
|
||||
mov rdi, r8
|
||||
mov rsi, r9
|
||||
syscall
|
||||
|
||||
.quit:
|
||||
add rsp, SIZE_QWORD
|
||||
ret
|
||||
|
@@ -87,6 +87,7 @@ extern utoa
|
||||
extern fopen
|
||||
extern fclose
|
||||
extern fexist
|
||||
extern fwrite
|
||||
|
||||
section .rodata
|
||||
;;;
|
||||
@@ -125,6 +126,7 @@ section .rodata
|
||||
TEST_utoa equ 1
|
||||
;file.asm
|
||||
TEST_fopen equ 1 ;Includes fclose
|
||||
TEST_fwrite equ 1
|
||||
|
||||
;;;
|
||||
;;; Global test messages
|
||||
@@ -323,6 +325,11 @@ section .rodata
|
||||
addTest(fopen1, "fp1 = fopen(''testfile_1.txt'', 'w')")
|
||||
addTest(fclose1, "fclose(fp1)")
|
||||
file1 db "testfile_1.txt",EOS
|
||||
;fwrite()
|
||||
addTestHeader(_fwrite, "fwrite")
|
||||
addTest(fwrite1, "fwrite(fp1, 'Hello world!\n') //mode 'a'")
|
||||
addTest(fwrite2, "fwrite(fp1, 'How are you doing?\n') //mode 'a'")
|
||||
addTest(fwrite3, "fwrite(fp1, 'Howdy environment!') //mode 'r'")
|
||||
|
||||
section .bss
|
||||
strBuff1 resb 64
|
||||
@@ -1152,6 +1159,50 @@ _start:
|
||||
assert_eq(1)
|
||||
%endif
|
||||
|
||||
;--- fwrite()
|
||||
%if TEST_fwrite
|
||||
printTestHeader(_fwrite)
|
||||
|
||||
; Open file1 in append mode
|
||||
lea rdi, [rel file1]
|
||||
mov rsi, 'a'
|
||||
call fopen
|
||||
mov [fp1], rax
|
||||
|
||||
; TEST 1
|
||||
printTest(fwrite1)
|
||||
mov rdi, [fp1]
|
||||
lea rsi, [rel str1]
|
||||
call fwrite
|
||||
assert_eq(13)
|
||||
|
||||
; TEST 2
|
||||
printTest(fwrite2)
|
||||
mov rdi, [fp1]
|
||||
lea rsi, [rel str1]
|
||||
call fwrite
|
||||
assert_eq(13)
|
||||
|
||||
; Re-open file1 in write mode
|
||||
mov rdi, [fp1]
|
||||
call fclose
|
||||
lea rdi, [rel file1]
|
||||
mov rsi, 'w'
|
||||
call fopen
|
||||
mov [fp1], rax
|
||||
|
||||
; TEST 3
|
||||
printTest(fwrite3)
|
||||
mov rdi, [fp1]
|
||||
lea rsi, [rel str4]
|
||||
call fwrite
|
||||
assert_eq(18)
|
||||
|
||||
; Close file1
|
||||
mov rdi, [fp1]
|
||||
call fclose
|
||||
%endif
|
||||
|
||||
;;;
|
||||
;;; TEST RESULTS
|
||||
;;;
|
||||
|
Reference in New Issue
Block a user