X-Git-Url: http://erislabs.net/gitweb/?a=blobdiff_plain;f=tests%2Ftest-fflush.c;h=6af23635555af7703a058b79703f521c466dc153;hb=cd56634a4a8179fd5a4419fbb3e27211b042ab1c;hp=12403e2abab24105b19fba26c010abedc83bf9d7;hpb=692679fcbebba0095bae0f1f1a99bd1f0f5a0476;p=gnulib.git diff --git a/tests/test-fflush.c b/tests/test-fflush.c index 12403e2ab..6af236355 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-2014 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 @@ -26,8 +26,11 @@ #include "signature.h" SIGNATURE_CHECK (fflush, int, (FILE *)); +#include #include +#include "macros.h" + int main (void) { @@ -46,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; } @@ -140,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; }