annotate tests/test-freopen.c @ 17460:d11431703671

autoupdate
author Karl Berry <karl@freefriends.org>
date Fri, 09 Aug 2013 08:03:30 -0700
parents 3a09cc104f4c
children
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
9303
136d24950879 Test for module 'freopen'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
1 /* Test of opening a file stream.
17249
e542fd46ad6f maint: update all copyright year number ranges
Eric Blake <eblake@redhat.com>
parents: 16201
diff changeset
2 Copyright (C) 2007-2013 Free Software Foundation, Inc.
9303
136d24950879 Test for module 'freopen'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
3
9309
bbbbbf4cd1c5 Change copyright notice from GPLv2+ to GPLv3+.
Bruno Haible <bruno@clisp.org>
parents: 9303
diff changeset
4 This program is free software: you can redistribute it and/or modify
9303
136d24950879 Test for module 'freopen'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
5 it under the terms of the GNU General Public License as published by
9309
bbbbbf4cd1c5 Change copyright notice from GPLv2+ to GPLv3+.
Bruno Haible <bruno@clisp.org>
parents: 9303
diff changeset
6 the Free Software Foundation; either version 3 of the License, or
bbbbbf4cd1c5 Change copyright notice from GPLv2+ to GPLv3+.
Bruno Haible <bruno@clisp.org>
parents: 9303
diff changeset
7 (at your option) any later version.
9303
136d24950879 Test for module 'freopen'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
8
136d24950879 Test for module 'freopen'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
9 This program is distributed in the hope that it will be useful,
136d24950879 Test for module 'freopen'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
10 but WITHOUT ANY WARRANTY; without even the implied warranty of
136d24950879 Test for module 'freopen'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
136d24950879 Test for module 'freopen'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
12 GNU General Public License for more details.
136d24950879 Test for module 'freopen'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
13
136d24950879 Test for module 'freopen'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
14 You should have received a copy of the GNU General Public License
9309
bbbbbf4cd1c5 Change copyright notice from GPLv2+ to GPLv3+.
Bruno Haible <bruno@clisp.org>
parents: 9303
diff changeset
15 along with this program. If not, see <http://www.gnu.org/licenses/>. */
9303
136d24950879 Test for module 'freopen'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
16
136d24950879 Test for module 'freopen'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
17 /* Written by Bruno Haible <bruno@clisp.org>, 2007. */
136d24950879 Test for module 'freopen'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
18
136d24950879 Test for module 'freopen'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
19 #include <config.h>
136d24950879 Test for module 'freopen'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
20
136d24950879 Test for module 'freopen'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
21 #include <stdio.h>
12489
33ab12a7cea2 tests: add signature checks
Eric Blake <ebb9@byu.net>
parents: 12421
diff changeset
22
33ab12a7cea2 tests: add signature checks
Eric Blake <ebb9@byu.net>
parents: 12421
diff changeset
23 #include "signature.h"
33ab12a7cea2 tests: add signature checks
Eric Blake <ebb9@byu.net>
parents: 12421
diff changeset
24 SIGNATURE_CHECK (freopen, FILE *, (char const *, char const *, FILE *));
33ab12a7cea2 tests: add signature checks
Eric Blake <ebb9@byu.net>
parents: 12421
diff changeset
25
15720
3a20a51c1323 freopen tests: EBADF tests.
Bruno Haible <bruno@clisp.org>
parents: 14079
diff changeset
26 #include <errno.h>
3a20a51c1323 freopen tests: EBADF tests.
Bruno Haible <bruno@clisp.org>
parents: 14079
diff changeset
27 #include <unistd.h>
3a20a51c1323 freopen tests: EBADF tests.
Bruno Haible <bruno@clisp.org>
parents: 14079
diff changeset
28
12496
a48d3d749ca5 Refactor common macros used in tests.
Bruno Haible <bruno@clisp.org>
parents: 12489
diff changeset
29 #include "macros.h"
9303
136d24950879 Test for module 'freopen'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
30
136d24950879 Test for module 'freopen'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
31 int
136d24950879 Test for module 'freopen'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
32 main ()
136d24950879 Test for module 'freopen'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
33 {
15720
3a20a51c1323 freopen tests: EBADF tests.
Bruno Haible <bruno@clisp.org>
parents: 14079
diff changeset
34 const char *filename = "test-freopen.txt";
3a20a51c1323 freopen tests: EBADF tests.
Bruno Haible <bruno@clisp.org>
parents: 14079
diff changeset
35
9303
136d24950879 Test for module 'freopen'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
36 ASSERT (freopen ("/dev/null", "r", stdin) != NULL);
136d24950879 Test for module 'freopen'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
37
15720
3a20a51c1323 freopen tests: EBADF tests.
Bruno Haible <bruno@clisp.org>
parents: 14079
diff changeset
38 #if 0 /* freopen (NULL, ...) is unsupported on most platforms. */
3a20a51c1323 freopen tests: EBADF tests.
Bruno Haible <bruno@clisp.org>
parents: 14079
diff changeset
39 /* Test that freopen() sets errno if someone else closes the stream
3a20a51c1323 freopen tests: EBADF tests.
Bruno Haible <bruno@clisp.org>
parents: 14079
diff changeset
40 fd behind the back of stdio. */
3a20a51c1323 freopen tests: EBADF tests.
Bruno Haible <bruno@clisp.org>
parents: 14079
diff changeset
41 {
3a20a51c1323 freopen tests: EBADF tests.
Bruno Haible <bruno@clisp.org>
parents: 14079
diff changeset
42 FILE *fp = fopen (filename, "w+");
3a20a51c1323 freopen tests: EBADF tests.
Bruno Haible <bruno@clisp.org>
parents: 14079
diff changeset
43 ASSERT (fp != NULL);
3a20a51c1323 freopen tests: EBADF tests.
Bruno Haible <bruno@clisp.org>
parents: 14079
diff changeset
44 ASSERT (close (fileno (fp)) == 0);
3a20a51c1323 freopen tests: EBADF tests.
Bruno Haible <bruno@clisp.org>
parents: 14079
diff changeset
45 errno = 0;
3a20a51c1323 freopen tests: EBADF tests.
Bruno Haible <bruno@clisp.org>
parents: 14079
diff changeset
46 ASSERT (freopen (NULL, "r", fp) == NULL);
3a20a51c1323 freopen tests: EBADF tests.
Bruno Haible <bruno@clisp.org>
parents: 14079
diff changeset
47 perror("freopen");
3a20a51c1323 freopen tests: EBADF tests.
Bruno Haible <bruno@clisp.org>
parents: 14079
diff changeset
48 ASSERT (errno == EBADF);
3a20a51c1323 freopen tests: EBADF tests.
Bruno Haible <bruno@clisp.org>
parents: 14079
diff changeset
49 fclose (fp);
3a20a51c1323 freopen tests: EBADF tests.
Bruno Haible <bruno@clisp.org>
parents: 14079
diff changeset
50 }
3a20a51c1323 freopen tests: EBADF tests.
Bruno Haible <bruno@clisp.org>
parents: 14079
diff changeset
51
3a20a51c1323 freopen tests: EBADF tests.
Bruno Haible <bruno@clisp.org>
parents: 14079
diff changeset
52 /* Test that freopen() sets errno if the stream was constructed with
3a20a51c1323 freopen tests: EBADF tests.
Bruno Haible <bruno@clisp.org>
parents: 14079
diff changeset
53 an invalid file descriptor. */
3a20a51c1323 freopen tests: EBADF tests.
Bruno Haible <bruno@clisp.org>
parents: 14079
diff changeset
54 {
3a20a51c1323 freopen tests: EBADF tests.
Bruno Haible <bruno@clisp.org>
parents: 14079
diff changeset
55 FILE *fp = fdopen (-1, "w+");
3a20a51c1323 freopen tests: EBADF tests.
Bruno Haible <bruno@clisp.org>
parents: 14079
diff changeset
56 if (fp != NULL)
3a20a51c1323 freopen tests: EBADF tests.
Bruno Haible <bruno@clisp.org>
parents: 14079
diff changeset
57 {
3a20a51c1323 freopen tests: EBADF tests.
Bruno Haible <bruno@clisp.org>
parents: 14079
diff changeset
58 errno = 0;
3a20a51c1323 freopen tests: EBADF tests.
Bruno Haible <bruno@clisp.org>
parents: 14079
diff changeset
59 ASSERT (freopen (NULL, "r", fp) == NULL);
3a20a51c1323 freopen tests: EBADF tests.
Bruno Haible <bruno@clisp.org>
parents: 14079
diff changeset
60 ASSERT (errno == EBADF);
3a20a51c1323 freopen tests: EBADF tests.
Bruno Haible <bruno@clisp.org>
parents: 14079
diff changeset
61 fclose (fp);
3a20a51c1323 freopen tests: EBADF tests.
Bruno Haible <bruno@clisp.org>
parents: 14079
diff changeset
62 }
3a20a51c1323 freopen tests: EBADF tests.
Bruno Haible <bruno@clisp.org>
parents: 14079
diff changeset
63 }
3a20a51c1323 freopen tests: EBADF tests.
Bruno Haible <bruno@clisp.org>
parents: 14079
diff changeset
64 {
17295
3a09cc104f4c tests: don't assume fd 99 is closed
Paul Eggert <eggert@cs.ucla.edu>
parents: 17249
diff changeset
65 FILE *fp;
3a09cc104f4c tests: don't assume fd 99 is closed
Paul Eggert <eggert@cs.ucla.edu>
parents: 17249
diff changeset
66 close (99);
3a09cc104f4c tests: don't assume fd 99 is closed
Paul Eggert <eggert@cs.ucla.edu>
parents: 17249
diff changeset
67 fp = fdopen (99, "w+");
15720
3a20a51c1323 freopen tests: EBADF tests.
Bruno Haible <bruno@clisp.org>
parents: 14079
diff changeset
68 if (fp != NULL)
3a20a51c1323 freopen tests: EBADF tests.
Bruno Haible <bruno@clisp.org>
parents: 14079
diff changeset
69 {
3a20a51c1323 freopen tests: EBADF tests.
Bruno Haible <bruno@clisp.org>
parents: 14079
diff changeset
70 errno = 0;
3a20a51c1323 freopen tests: EBADF tests.
Bruno Haible <bruno@clisp.org>
parents: 14079
diff changeset
71 ASSERT (freopen (NULL, "r", fp) == NULL);
3a20a51c1323 freopen tests: EBADF tests.
Bruno Haible <bruno@clisp.org>
parents: 14079
diff changeset
72 ASSERT (errno == EBADF);
3a20a51c1323 freopen tests: EBADF tests.
Bruno Haible <bruno@clisp.org>
parents: 14079
diff changeset
73 fclose (fp);
3a20a51c1323 freopen tests: EBADF tests.
Bruno Haible <bruno@clisp.org>
parents: 14079
diff changeset
74 }
3a20a51c1323 freopen tests: EBADF tests.
Bruno Haible <bruno@clisp.org>
parents: 14079
diff changeset
75 }
3a20a51c1323 freopen tests: EBADF tests.
Bruno Haible <bruno@clisp.org>
parents: 14079
diff changeset
76 #endif
3a20a51c1323 freopen tests: EBADF tests.
Bruno Haible <bruno@clisp.org>
parents: 14079
diff changeset
77
3a20a51c1323 freopen tests: EBADF tests.
Bruno Haible <bruno@clisp.org>
parents: 14079
diff changeset
78 /* Clean up. */
3a20a51c1323 freopen tests: EBADF tests.
Bruno Haible <bruno@clisp.org>
parents: 14079
diff changeset
79 unlink (filename);
3a20a51c1323 freopen tests: EBADF tests.
Bruno Haible <bruno@clisp.org>
parents: 14079
diff changeset
80
9303
136d24950879 Test for module 'freopen'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
81 return 0;
136d24950879 Test for module 'freopen'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
82 }