Adds ftell(), removed reading from file in fseek/ftell tests (since that modifies pointer location)
This commit is contained in:
23
src/file.asm
23
src/file.asm
@@ -16,6 +16,7 @@ section .text
|
||||
global fwrite
|
||||
global fread
|
||||
global fseek
|
||||
global ftell
|
||||
|
||||
;----- stat(*file[], *statBuffer[]) -----;
|
||||
; Gets information from a file, stores it into statBuffer[]
|
||||
@@ -297,3 +298,25 @@ fseek:
|
||||
syscall
|
||||
add rsp, SIZE_QWORD
|
||||
ret
|
||||
|
||||
;--- ftell(*filePointer) ---;
|
||||
; Gets pointer location in file.
|
||||
; Return value: Pointer location or -errno on error
|
||||
; Used registers:
|
||||
; rax* (ret)
|
||||
; rdi (arg) Pointer to file
|
||||
; rsi syscall arg
|
||||
; rdx syscall arg
|
||||
ftell:
|
||||
cmp rdi, 2
|
||||
jg .cnt
|
||||
mov rax, -EBADF
|
||||
ret
|
||||
.cnt:
|
||||
sub rsp, SIZE_QWORD
|
||||
mov rax, NR_lseek
|
||||
xor rsi, rsi
|
||||
mov rdx, SEEK_CUR
|
||||
syscall
|
||||
add rsp, SIZE_QWORD
|
||||
ret
|
||||
|
@@ -98,6 +98,7 @@ extern fgettype
|
||||
extern fgetmod
|
||||
extern fread
|
||||
extern fseek
|
||||
extern ftell
|
||||
|
||||
section .rodata
|
||||
;;;
|
||||
@@ -139,7 +140,7 @@ section .rodata
|
||||
TEST_fopen equ 1 ;Includes fclose
|
||||
TEST_fwrite equ 1
|
||||
TEST_fread equ 1
|
||||
TEST_fseek equ 1
|
||||
TEST_fseek equ 1 ;Includes ftell
|
||||
TEST_stat equ 1
|
||||
TEST_lstat equ 1
|
||||
TEST_fgettype equ 1
|
||||
@@ -356,9 +357,10 @@ section .rodata
|
||||
;fread()
|
||||
addTestHeader(_fread, "fread")
|
||||
addTest(fread1, "fread(fp1, strBuff1, 64)")
|
||||
;fseek()
|
||||
;fseek() / ftell()
|
||||
addTestHeader(_fseek, "fseek")
|
||||
addTest(fseek1, "fseek(fp1, SEEK_SET, 5)")
|
||||
addTest(ftell1, "ftell(fp1)")
|
||||
addTest(fseek2, "fseek(fp1, SEEK_CUR, -1)")
|
||||
addTest(fseek3, "fseek(fp1, SEEK_SET, 0)")
|
||||
;stat()
|
||||
@@ -1326,16 +1328,6 @@ _start:
|
||||
|
||||
;--- fseek()
|
||||
%if TEST_fseek
|
||||
%macro fseek_relptr 1
|
||||
%endmacro
|
||||
%macro fseek_readput 0
|
||||
mov rdi, [fp1]
|
||||
lea rsi, [rel strBuff1]
|
||||
mov rdx, 64
|
||||
call fread
|
||||
lea rdi, [rel strBuff1]
|
||||
call puts
|
||||
%endmacro
|
||||
printTestHeader(_fseek)
|
||||
|
||||
lea rdi, [rel file1]
|
||||
@@ -1350,27 +1342,22 @@ _start:
|
||||
mov rdx, 5
|
||||
call fseek
|
||||
assert_eq(5)
|
||||
fseek_readput
|
||||
printTest(ftell1)
|
||||
mov rdi, [fp1]
|
||||
call ftell
|
||||
assert_eq(5)
|
||||
|
||||
; TEST 2: fseek(fp1, SEEK_CUR, -1)
|
||||
printTest(fseek2)
|
||||
mov rdi, [fp1]
|
||||
mov rsi, SEEK_SET
|
||||
mov rdx, 5
|
||||
call fseek
|
||||
;^ fseek_readput (fread) modified pointer, so restore it to result of test 1
|
||||
mov rdi, [fp1]
|
||||
mov rsi, SEEK_CUR
|
||||
mov rdx, -1
|
||||
call fseek
|
||||
assert_eq(4)
|
||||
fseek_readput
|
||||
; FAIL. Returned is 26 (which is length of the file)
|
||||
; My best guess is -1 is treated as unsigned. However, according to man 2 lseek:
|
||||
; The off_t data type is a signed integer data type
|
||||
; ( offset in rdx is supposed to be (off_t) offset )
|
||||
; If it turns out unsigned after all, I'll have to manually get current location of pointer (call NR_lseek with SEEK_CUR and offset 0; returned value is current location)
|
||||
; ... and then substract given argument from that
|
||||
printTest(ftell1)
|
||||
mov rdi, [fp1]
|
||||
call ftell
|
||||
assert_eq(4)
|
||||
|
||||
; TEST 3: fseek(fp1, SEEK_SET, 0)
|
||||
printTest(fseek3)
|
||||
@@ -1379,7 +1366,10 @@ _start:
|
||||
xor rdx, rdx
|
||||
call fseek
|
||||
assert_eq(0)
|
||||
fseek_readput
|
||||
printTest(ftell1)
|
||||
mov rdi, [fp1]
|
||||
call ftell
|
||||
assert_eq(0)
|
||||
|
||||
mov rdi, [fp1]
|
||||
call fclose
|
||||
|
Reference in New Issue
Block a user