Adds fwrite()

This commit is contained in:
2025-07-25 14:16:20 +02:00
parent a7c1c6bdb5
commit 5434e48ee4
2 changed files with 87 additions and 2 deletions

View File

@@ -1,5 +1,6 @@
%include "src/constants.asm" %include "src/constants.asm"
extern strlen
extern umask_get extern umask_get
section .rodata section .rodata
@@ -51,6 +52,7 @@ section .text
global fopen global fopen
global fclose global fclose
global fexist global fexist
global fwrite
;----- fopen(*file[], char mode) -----; ;----- fopen(*file[], char mode) -----;
; Opens a file ; Opens a file
@@ -146,7 +148,7 @@ fopen:
add rsp, SIZE_QWORD add rsp, SIZE_QWORD
ret ret
;----- fclose(file) -----; ;----- fclose(*filePointer) -----;
; Closes an opened file ; Closes an opened file
; Return value: 0 if file closed. Negative error number if an error occured ; Return value: 0 if file closed. Negative error number if an error occured
; Used registers: ; Used registers:
@@ -167,11 +169,12 @@ fclose:
;----- fexist(*file[]) -----; ;----- fexist(*file[]) -----;
; Checks if given file exists (attempts to open the 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: ; Used registers:
; rax* (ret) ; rax* (ret)
; rdi (arg) Pointer to string holding (path+)file name ; rdi (arg) Pointer to string holding (path+)file name
fexist: fexist:
;sub rsp, SIZE_QWORD
mov rsi, 'r' mov rsi, 'r'
call fopen call fopen
test rax, rax test rax, rax
@@ -183,4 +186,35 @@ fexist:
.not_exist: .not_exist:
xor rax, rax xor rax, rax
.quit: .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 ret

View File

@@ -87,6 +87,7 @@ extern utoa
extern fopen extern fopen
extern fclose extern fclose
extern fexist extern fexist
extern fwrite
section .rodata section .rodata
;;; ;;;
@@ -125,6 +126,7 @@ section .rodata
TEST_utoa equ 1 TEST_utoa equ 1
;file.asm ;file.asm
TEST_fopen equ 1 ;Includes fclose TEST_fopen equ 1 ;Includes fclose
TEST_fwrite equ 1
;;; ;;;
;;; Global test messages ;;; Global test messages
@@ -323,6 +325,11 @@ section .rodata
addTest(fopen1, "fp1 = fopen(''testfile_1.txt'', 'w')") addTest(fopen1, "fp1 = fopen(''testfile_1.txt'', 'w')")
addTest(fclose1, "fclose(fp1)") addTest(fclose1, "fclose(fp1)")
file1 db "testfile_1.txt",EOS 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 section .bss
strBuff1 resb 64 strBuff1 resb 64
@@ -1152,6 +1159,50 @@ _start:
assert_eq(1) assert_eq(1)
%endif %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 ;;; TEST RESULTS
;;; ;;;