X-Git-Url: http://erislabs.net/gitweb/?a=blobdiff_plain;f=tests%2Ftest-fflush.c;h=7c7c474a0ab05a2e29687ee855872ffc47e86527;hb=5bf73fbe89c75118e5e9a53e4b88a57729d26964;hp=38bedd975f953c95ff04d701bde439308b2d20f4;hpb=87c5f6fccee477f7e2c67ea4c95e51a3afd83a41;p=gnulib.git diff --git a/tests/test-fflush.c b/tests/test-fflush.c index 38bedd975..7c7c474a0 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, 2010 Free Software Foundation, Inc. + Copyright (C) 2007, 2009-2011 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,45 @@ 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 = 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; }