X-Git-Url: http://erislabs.net/gitweb/?a=blobdiff_plain;f=tests%2Ftest-freopen.c;h=4b54d8d52888d6f795686da6e5c27d7f132b0435;hb=23eecb48e39afd0d267d64d40ba6bf97aa865e13;hp=bdb5de0fe9927ca4d2cfc50b4d7dc0ec4de7a63a;hpb=89ae4589dd58d12e7cbcf0e92c1d828c1e430f97;p=gnulib.git diff --git a/tests/test-freopen.c b/tests/test-freopen.c index bdb5de0fe..4b54d8d52 100644 --- a/tests/test-freopen.c +++ b/tests/test-freopen.c @@ -1,5 +1,5 @@ /* Test of opening a file stream. - Copyright (C) 2007-2009 Free Software Foundation, Inc. + Copyright (C) 2007-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 @@ -23,24 +23,60 @@ #include "signature.h" SIGNATURE_CHECK (freopen, FILE *, (char const *, char const *, FILE *)); -#include +#include +#include -#define ASSERT(expr) \ - do \ - { \ - if (!(expr)) \ - { \ - fprintf (stderr, "%s:%d: assertion failed\n", __FILE__, __LINE__); \ - fflush (stderr); \ - abort (); \ - } \ - } \ - while (0) +#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; }