X-Git-Url: http://erislabs.net/gitweb/?a=blobdiff_plain;f=tests%2Ftest-fflush.c;h=1c61140b39ddcb8d39d971042f6b61661f1acf92;hb=fa1db0dd22768f09a507674a30beb5b8a87bb35f;hp=9962d1d18254629277447eb99737838cf2d172a1;hpb=89ae4589dd58d12e7cbcf0e92c1d828c1e430f97;p=gnulib.git diff --git a/tests/test-fflush.c b/tests/test-fflush.c index 9962d1d18..1c61140b3 100644 --- a/tests/test-fflush.c +++ b/tests/test-fflush.c @@ -1,5 +1,5 @@ /* Test of POSIX compatible fflush() function. - Copyright (C) 2007, 2009 Free Software Foundation, Inc. + Copyright (C) 2007, 2009-2013 Free Software Foundation, Inc. This program is free software: you can redistribute it and/or modify it under the terms of the GNU General Public License as published by @@ -18,13 +18,19 @@ #include +/* None of the files accessed by this test are large, so disable the + ftell link warning if we are not using the gnulib ftell module. */ +#define _GL_NO_LARGE_FILES #include #include "signature.h" SIGNATURE_CHECK (fflush, int, (FILE *)); +#include #include +#include "macros.h" + int main (void) { @@ -43,11 +49,13 @@ main (void) /* Test fflush. */ f = fopen ("test-fflush.txt", "r"); + ASSERT (f != NULL); fd = fileno (f); if (!f || 0 > fd || fread (buffer, 1, 5, f) != 5) { fputs ("Failed initial read of sample file.\n", stderr); - fclose (f); + if (f) + fclose (f); unlink ("test-fflush.txt"); return 1; } @@ -137,6 +145,47 @@ main (void) return 1; } fclose (f); + + /* Test that fflush() sets errno if someone else closes the stream + fd behind the back of stdio. */ + { + FILE *fp = fopen ("test-fflush.txt", "w"); + ASSERT (fp != NULL); + fputc ('x', fp); + ASSERT (close (fileno (fp)) == 0); + errno = 0; + ASSERT (fflush (fp) == EOF); + ASSERT (errno == EBADF); + fclose (fp); + } + + /* Test that fflush() sets errno if the stream was constructed with + an invalid file descriptor. */ + { + FILE *fp = fdopen (-1, "w"); + if (fp != NULL) + { + fputc ('x', fp); + errno = 0; + ASSERT (fflush (fp) == EOF); + ASSERT (errno == EBADF); + } + } + { + FILE *fp; + close (99); + fp = fdopen (99, "w"); + if (fp != NULL) + { + fputc ('x', fp); + errno = 0; + ASSERT (fflush (fp) == EOF); + ASSERT (errno == EBADF); + } + } + + /* Clean up. */ unlink ("test-fflush.txt"); + return 0; }