X-Git-Url: http://erislabs.net/gitweb/?a=blobdiff_plain;f=tests%2Ftest-freopen.c;h=a98530c950edcd2bc4fa7275cc1890f5d91b7d37;hb=1276a2c5f24c0c932426aca9c899fa524d2443f2;hp=08214fa4499ac4b3d9c5e71a1a34b6753ba03ca3;hpb=b2e2010c7c902235b5efb5bd3c6529f61b093aa4;p=gnulib.git diff --git a/tests/test-freopen.c b/tests/test-freopen.c index 08214fa44..a98530c95 100644 --- a/tests/test-freopen.c +++ b/tests/test-freopen.c @@ -1,5 +1,5 @@ /* Test of opening a file stream. - Copyright (C) 2007-2010 Free Software Foundation, Inc. + Copyright (C) 2007-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 @@ -23,12 +23,60 @@ #include "signature.h" SIGNATURE_CHECK (freopen, FILE *, (char const *, char const *, FILE *)); +#include +#include + #include "macros.h" int main () { + const char *filename = "test-freopen.txt"; + ASSERT (freopen ("/dev/null", "r", stdin) != NULL); +#if 0 /* freopen (NULL, ...) is unsupported on most platforms. */ + /* Test that freopen() sets errno if someone else closes the stream + fd behind the back of stdio. */ + { + FILE *fp = fopen (filename, "w+"); + ASSERT (fp != NULL); + ASSERT (close (fileno (fp)) == 0); + errno = 0; + ASSERT (freopen (NULL, "r", fp) == NULL); + perror("freopen"); + ASSERT (errno == EBADF); + fclose (fp); + } + + /* Test that freopen() sets errno if the stream was constructed with + an invalid file descriptor. */ + { + FILE *fp = fdopen (-1, "w+"); + if (fp != NULL) + { + errno = 0; + ASSERT (freopen (NULL, "r", fp) == NULL); + ASSERT (errno == EBADF); + fclose (fp); + } + } + { + FILE *fp; + close (99); + fp = fdopen (99, "w+"); + if (fp != NULL) + { + errno = 0; + ASSERT (freopen (NULL, "r", fp) == NULL); + ASSERT (errno == EBADF); + fclose (fp); + } + } +#endif + + /* Clean up. */ + unlink (filename); + return 0; }