Skip to content

Commit c72d77d

Browse files
authored
os: fix os.File's tell/0 method for windows (fix #24217) (#24218)
1 parent 9e10b23 commit c72d77d

File tree

3 files changed

+13
-5
lines changed

3 files changed

+13
-5
lines changed

vlib/builtin/cfns.c.v

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -148,7 +148,7 @@ fn C.rename(old_filename &char, new_filename &char) int
148148

149149
fn C.fgets(str &char, n int, stream &C.FILE) int
150150

151-
fn C._telli64(handle int) isize
151+
fn C.fgetpos(&C.FILE, voidptr) int
152152

153153
@[trusted]
154154
fn C.sigemptyset() int

vlib/os/file.c.v

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -861,13 +861,15 @@ pub fn (f &File) tell() !i64 {
861861
if !f.is_opened {
862862
return error_file_not_opened()
863863
}
864-
mut pos := isize(0)
864+
865+
mut pos := i64(0)
866+
mut ret := 0
865867
$if windows {
866-
pos = C._telli64(f.fd)
868+
ret = C.fgetpos(f.cfile, &pos)
867869
} $else {
868-
pos = C.ftell(f.cfile)
870+
pos = i64(C.ftell(f.cfile))
869871
}
870-
if pos == -1 {
872+
if ret == -1 || pos == -1 {
871873
return error(posix_get_error_msg(C.errno))
872874
}
873875
return pos

vlib/os/file_test.v

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -357,6 +357,12 @@ fn test_tell() {
357357
mut f := os.open_file(tfile, 'r')!
358358
f.seek(-5, .end)!
359359
pos := f.tell()!
360+
f.seek(0, .start)!
361+
c1 := f.tell()!
362+
_ := f.read_bytes(8)
363+
c2 := f.tell()!
364+
assert c1 == 0
365+
assert c2 == 8
360366
f.close()
361367
// dump(pos)
362368
assert pos == size - 5

0 commit comments

Comments
 (0)