This can be both seen with GCC from MSYS2 (likely from MinGW, too) and with MSVC.
I'd guess the "problematic" ftell
is using the same underlying system function.
We do need the correct "current position" and use ftell
to get it, but sometimes (after READ
?) its position is wrong by 2 (possibly by 3 if UNIX_LF is not active).
This was seen during debugging in lineseq_read()
in the two currently failing test cases:
900: LINE SEQUENTIAL REWRITE FAILED (run_file.at:12043)
902: Concatenated Files FAILED (run_file.at:12401)
MSDN says:
The value returned by ftell [...] may not reflect the physical byte offset for streams opened in text mode [...] use ftell with fseek [...] to return to file locations correctly"
I'm not really sure what that means.... ad if there are possible similar problems on other environments.
As an alternative we could increment the f->record_off
field on each read/write on our own.
@rjnorman74 @ska00 @articuno Any ideas?
More debugging and reading (and testing) later:
For
ftell
to work properly we need binary mode for bothfopen
viab
mode and foropen
viaO_BINARY
.I've adjusted fileio.c to always use
O_BINARY
(that was done in nearly all places already) and useb
in the few places where it was missing, depending oncobsetpr->unix_lf
.With these changes the lsq rewrite tested work fine now with MSYS2 (GCC mingw) and MSVC.
To really fix this the following code change will likely be needed (too many code paths that weren't taken before to enable this before the 3.2 release):
cob_ls_uses_cr
make the default "true" on Win32, otherwise falseCOB_UNIX_LF
as "real" variable, instead: On Win32, ifCOB_LS_USES_CR
is not explicit set additional check forCOB_UNIX_LF
before enabling itcob_ls_uses_cr
code paths, possibly add this to the testsuiteFor MSVC to not break with debug builds I also had prevent sync (actually
_commit
in the MSVC case) after fclose/close and directly after open:There were also issues with "double-close", as
flcose
afterclose
.I'd tend to check for
f->file
first, and if it is setflcose
, otherwiseclose
(I think the former will always call the second after flushing the buffers, but I could be wrong). @rjnorman74 Do you have any different experiences here?