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 fwrite
|
||||||
global fread
|
global fread
|
||||||
global fseek
|
global fseek
|
||||||
|
global ftell
|
||||||
|
|
||||||
;----- stat(*file[], *statBuffer[]) -----;
|
;----- stat(*file[], *statBuffer[]) -----;
|
||||||
; Gets information from a file, stores it into statBuffer[]
|
; Gets information from a file, stores it into statBuffer[]
|
||||||
@@ -297,3 +298,25 @@ fseek:
|
|||||||
syscall
|
syscall
|
||||||
add rsp, SIZE_QWORD
|
add rsp, SIZE_QWORD
|
||||||
ret
|
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 fgetmod
|
||||||
extern fread
|
extern fread
|
||||||
extern fseek
|
extern fseek
|
||||||
|
extern ftell
|
||||||
|
|
||||||
section .rodata
|
section .rodata
|
||||||
;;;
|
;;;
|
||||||
@@ -139,7 +140,7 @@ section .rodata
|
|||||||
TEST_fopen equ 1 ;Includes fclose
|
TEST_fopen equ 1 ;Includes fclose
|
||||||
TEST_fwrite equ 1
|
TEST_fwrite equ 1
|
||||||
TEST_fread equ 1
|
TEST_fread equ 1
|
||||||
TEST_fseek equ 1
|
TEST_fseek equ 1 ;Includes ftell
|
||||||
TEST_stat equ 1
|
TEST_stat equ 1
|
||||||
TEST_lstat equ 1
|
TEST_lstat equ 1
|
||||||
TEST_fgettype equ 1
|
TEST_fgettype equ 1
|
||||||
@@ -356,9 +357,10 @@ section .rodata
|
|||||||
;fread()
|
;fread()
|
||||||
addTestHeader(_fread, "fread")
|
addTestHeader(_fread, "fread")
|
||||||
addTest(fread1, "fread(fp1, strBuff1, 64)")
|
addTest(fread1, "fread(fp1, strBuff1, 64)")
|
||||||
;fseek()
|
;fseek() / ftell()
|
||||||
addTestHeader(_fseek, "fseek")
|
addTestHeader(_fseek, "fseek")
|
||||||
addTest(fseek1, "fseek(fp1, SEEK_SET, 5)")
|
addTest(fseek1, "fseek(fp1, SEEK_SET, 5)")
|
||||||
|
addTest(ftell1, "ftell(fp1)")
|
||||||
addTest(fseek2, "fseek(fp1, SEEK_CUR, -1)")
|
addTest(fseek2, "fseek(fp1, SEEK_CUR, -1)")
|
||||||
addTest(fseek3, "fseek(fp1, SEEK_SET, 0)")
|
addTest(fseek3, "fseek(fp1, SEEK_SET, 0)")
|
||||||
;stat()
|
;stat()
|
||||||
@@ -1326,16 +1328,6 @@ _start:
|
|||||||
|
|
||||||
;--- fseek()
|
;--- fseek()
|
||||||
%if TEST_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)
|
printTestHeader(_fseek)
|
||||||
|
|
||||||
lea rdi, [rel file1]
|
lea rdi, [rel file1]
|
||||||
@@ -1350,27 +1342,22 @@ _start:
|
|||||||
mov rdx, 5
|
mov rdx, 5
|
||||||
call fseek
|
call fseek
|
||||||
assert_eq(5)
|
assert_eq(5)
|
||||||
fseek_readput
|
printTest(ftell1)
|
||||||
|
mov rdi, [fp1]
|
||||||
|
call ftell
|
||||||
|
assert_eq(5)
|
||||||
|
|
||||||
; TEST 2: fseek(fp1, SEEK_CUR, -1)
|
; TEST 2: fseek(fp1, SEEK_CUR, -1)
|
||||||
printTest(fseek2)
|
printTest(fseek2)
|
||||||
mov rdi, [fp1]
|
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 rsi, SEEK_CUR
|
||||||
mov rdx, -1
|
mov rdx, -1
|
||||||
call fseek
|
call fseek
|
||||||
assert_eq(4)
|
assert_eq(4)
|
||||||
fseek_readput
|
printTest(ftell1)
|
||||||
; FAIL. Returned is 26 (which is length of the file)
|
mov rdi, [fp1]
|
||||||
; My best guess is -1 is treated as unsigned. However, according to man 2 lseek:
|
call ftell
|
||||||
; The off_t data type is a signed integer data type
|
assert_eq(4)
|
||||||
; ( 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
|
|
||||||
|
|
||||||
; TEST 3: fseek(fp1, SEEK_SET, 0)
|
; TEST 3: fseek(fp1, SEEK_SET, 0)
|
||||||
printTest(fseek3)
|
printTest(fseek3)
|
||||||
@@ -1379,7 +1366,10 @@ _start:
|
|||||||
xor rdx, rdx
|
xor rdx, rdx
|
||||||
call fseek
|
call fseek
|
||||||
assert_eq(0)
|
assert_eq(0)
|
||||||
fseek_readput
|
printTest(ftell1)
|
||||||
|
mov rdi, [fp1]
|
||||||
|
call ftell
|
||||||
|
assert_eq(0)
|
||||||
|
|
||||||
mov rdi, [fp1]
|
mov rdi, [fp1]
|
||||||
call fclose
|
call fclose
|
||||||
|
Reference in New Issue
Block a user