X-Git-Url: http://erislabs.net/gitweb/?a=blobdiff_plain;f=tests%2Ftest-fflush.c;h=1c61140b39ddcb8d39d971042f6b61661f1acf92;hb=4779b635ef35c7b0bc4044fcb5bc746d06f158c4;hp=3afdf7bd69f93a729fbc3b2000d8973fcfabc2a1;hpb=7becd9d708425924f3a402fdd3c6e18ba2a7e4e3;p=gnulib.git diff --git a/tests/test-fflush.c b/tests/test-fflush.c index 3afdf7bd6..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,9 +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) { @@ -39,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; } @@ -70,7 +82,7 @@ main (void) if (lseek (fd, 0, SEEK_CUR) != 5) { fprintf (stderr, "File offset is wrong after fseek: %ld.\n", - (long) lseek (fd, 0, SEEK_CUR)); + (long) lseek (fd, 0, SEEK_CUR)); fclose (f); unlink ("test-fflush.txt"); return 1; @@ -78,7 +90,7 @@ main (void) if (ftell (f) != 5) { fprintf (stderr, "ftell result is wrong after fseek: %ld.\n", - (long) ftell (f)); + (long) ftell (f)); fclose (f); unlink ("test-fflush.txt"); return 1; @@ -111,7 +123,7 @@ main (void) if (lseek (fd, 0, SEEK_CUR) != 6) { fprintf (stderr, "File offset is wrong after fseeko: %ld.\n", - (long) lseek (fd, 0, SEEK_CUR)); + (long) lseek (fd, 0, SEEK_CUR)); fclose (f); unlink ("test-fflush.txt"); return 1; @@ -119,7 +131,7 @@ main (void) if (ftell (f) != 6) { fprintf (stderr, "ftell result is wrong after fseeko: %ld.\n", - (long) ftell (f)); + (long) ftell (f)); fclose (f); unlink ("test-fflush.txt"); return 1; @@ -133,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; }