Larry Jones <lawrence.jones <at> siemens.com> writes: > Looks good. You might want to consider adding tests to make sure that > any pending ungetc() data is cleared like it's supposed to be. I don't > see any code that obviously does that, so I suspect it's not.
Does this look correct? I'm checking it in. > > On a more general note, it looks like fseeko and ftello both need more > work to be generally useful on platforms that have a wide off_t but > don't have native fseeko/ftello. Name such a platform, then we'll worry about it. In general, since fseeko and off_t were invented at the same time as two parts of the solution to the same problem, pretty much every system out there either has both or neither. But maybe it's at least worth adding a compile-time assertion that sizeof(off_t) ==sizeof(long) when !HAVE_FSEEKO. From: Eric Blake <[EMAIL PROTECTED]> Date: Thu, 13 Dec 2007 14:58:44 -0700 Subject: [PATCH] Another fseek test. * tests/test-fseek.c (main): Also test ungetc handling. * tests/test-fseeko.c (main): Likewise. * modules/fseeko (Depends-on): Add verify. * lib/fseeko.c [!HAVE_FSEEKO]: Verify that off_t is not too large. Reported by Larry Jones. Signed-off-by: Eric Blake <[EMAIL PROTECTED]> --- ChangeLog | 8 ++++++++ lib/fseeko.c | 3 +++ modules/fseeko | 1 + tests/test-fseek.c | 10 ++++++++-- tests/test-fseeko.c | 10 ++++++++-- 5 files changed, 28 insertions(+), 4 deletions(-) diff --git a/ChangeLog b/ChangeLog index dd3d5f2..a7828da 100644 --- a/ChangeLog +++ b/ChangeLog @@ -1,5 +1,13 @@ 2007-12-13 Eric Blake <[EMAIL PROTECTED]> + Another fseek test. + * tests/test-fseek.c (main): Also test ungetc handling. + * tests/test-fseeko.c (main): Likewise. + * modules/fseeko (Depends-on): Add verify. + * lib/fseeko.c [!HAVE_FSEEKO]: Verify that off_t is not too + large. + Reported by Larry Jones. + Fix fseeko on mingw. * lib/fseeko.c (rpl_fseeko) [_IOERR]: Reset EOF flag on successful seek. diff --git a/lib/fseeko.c b/lib/fseeko.c index 97dcf6d..6d55ec8 100644 --- a/lib/fseeko.c +++ b/lib/fseeko.c @@ -27,6 +27,9 @@ #if !HAVE_FSEEKO # undef fseek # define fseeko fseek + +# include <verify.h> +verify (sizeof (off_t) == sizeof (long)); #endif int diff --git a/modules/fseeko b/modules/fseeko index eb10901..cb26003 100644 --- a/modules/fseeko +++ b/modules/fseeko @@ -8,6 +8,7 @@ m4/fseeko.m4 Depends-on: lseek stdio +verify configure.ac-early: AC_REQUIRE([AC_FUNC_FSEEKO]) diff --git a/tests/test-fseek.c b/tests/test-fseek.c index dde7ea0..0c7db95 100644 --- a/tests/test-fseek.c +++ b/tests/test-fseek.c @@ -23,17 +23,23 @@ int main (int argc, char **argv) { - /* Assume stdin is seekable iff argc > 1. */ + /* Assume stdin is non-empty and seekable iff argc > 1. */ int expected = argc > 1 ? 0 : -1; if (fseek (stdin, 0, SEEK_CUR) != expected) return 1; if (argc > 1) { - /* Test that fseek resets end-of-file marker. */ + /* Test that fseek discards ungetc data. */ + int ch = fgetc (stdin); + if (ch == EOF) + return 1; + if (ungetc (ch ^ 0xff, stdin) != (ch ^ 0xff)) + return 1; if (fseek (stdin, 0, SEEK_END)) return 1; if (fgetc (stdin) != EOF) return 1; + /* Test that fseek resets end-of-file marker. */ if (!feof (stdin)) return 1; if (fseek (stdin, 0, SEEK_END)) diff --git a/tests/test-fseeko.c b/tests/test-fseeko.c index 3be4f6e..25289ce 100644 --- a/tests/test-fseeko.c +++ b/tests/test-fseeko.c @@ -26,7 +26,7 @@ int main (int argc, char **argv) { - /* Assume stdin is seekable iff argc > 1. */ + /* Assume stdin is non-empty and seekable iff argc > 1. */ int expected = argc > 1 ? 0 : -1; /* Exit with success only if fseek/fseeko agree. */ int r1 = fseeko (stdin, (off_t)0, SEEK_CUR); @@ -35,11 +35,17 @@ main (int argc, char **argv) return 1; if (argc > 1) { - /* Test that fseek resets end-of-file marker. */ + /* Test that fseek discards ungetc data. */ + int ch = fgetc (stdin); + if (ch == EOF) + return 1; + if (ungetc (ch ^ 0xff, stdin) != (ch ^ 0xff)) + return 1; if (fseeko (stdin, (off_t) 0, SEEK_END)) return 1; if (fgetc (stdin) != EOF) return 1; + /* Test that fseek resets end-of-file marker. */ if (!feof (stdin)) return 1; if (fseeko (stdin, (off_t) 0, SEEK_END)) -- 1.5.3.5