merge tab->whitespace change
[gnulib.git] / tests / test-freopen-safer.c
1 /* Test of reopening a stream.
2    Copyright (C) 2009 Free Software Foundation, Inc.
3
4    This program is free software: you can redistribute it and/or modify
5    it under the terms of the GNU General Public License as published by
6    the Free Software Foundation; either version 3 of the License, or
7    (at your option) any later version.
8
9    This program is distributed in the hope that it will be useful,
10    but WITHOUT ANY WARRANTY; without even the implied warranty of
11    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12    GNU General Public License for more details.
13
14    You should have received a copy of the GNU General Public License
15    along with this program.  If not, see <http://www.gnu.org/licenses/>.  */
16
17 /* Written by Eric Blake <ebb9@byu.net>, 2009.  */
18
19 #include <config.h>
20
21 /* Specification.  */
22 #include "stdio--.h"
23
24 /* Helpers.  */
25 #include <stdlib.h>
26 #include <unistd.h>
27
28 /* This test intentionally closes stderr.  So, we arrange to have fd 10
29    (outside the range of interesting fd's during the test) set up to
30    duplicate the original stderr.  */
31
32 #define BACKUP_STDERR_FILENO 10
33 static FILE *myerr;
34
35 #define ASSERT(expr) \
36   do                                                                         \
37     {                                                                        \
38       if (!(expr))                                                           \
39         {                                                                    \
40           fprintf (myerr, "%s:%d: assertion failed\n", __FILE__, __LINE__);  \
41           fflush (myerr);                                                    \
42           abort ();                                                          \
43         }                                                                    \
44     }                                                                        \
45   while (0)
46
47 int
48 main (int argc, char **argv)
49 {
50   FILE *fp;
51
52   /* We close fd 2 later, so save it in fd 10.  */
53   if (dup2 (STDERR_FILENO, BACKUP_STDERR_FILENO) != BACKUP_STDERR_FILENO
54       || (myerr = fdopen (BACKUP_STDERR_FILENO, "w")) == NULL)
55     return 2;
56
57   {
58     FILE *tmp;
59     ASSERT (tmp = fopen ("/dev/null", "r"));
60     ASSERT (STDERR_FILENO < fileno (tmp));
61     ASSERT (fp = fopen ("/dev/null", "w"));
62     ASSERT (fileno (tmp) < fileno (fp));
63     ASSERT (fclose (tmp) == 0);
64   }
65
66   /* Gap in fds.  */
67   ASSERT (freopen ("/dev/null", "r+", fp) == fp);
68   ASSERT (STDERR_FILENO < fileno (fp));
69
70   ASSERT (freopen ("/dev/null", "r", stdin) == stdin);
71   ASSERT (STDIN_FILENO == fileno (stdin));
72
73   ASSERT (freopen ("/dev/null", "w", stdout) == stdout);
74   ASSERT (STDOUT_FILENO == fileno (stdout));
75
76   ASSERT (freopen ("/dev/null", "w", stderr) == stderr);
77   ASSERT (STDERR_FILENO == fileno (stderr));
78
79   /* fd 0 closed.  */
80   ASSERT (close (STDIN_FILENO) == 0);
81
82   ASSERT (freopen ("/dev/null", "w", stdout) == stdout);
83   ASSERT (STDOUT_FILENO == fileno (stdout));
84
85   ASSERT (freopen ("/dev/null", "w", stderr) == stderr);
86   ASSERT (STDERR_FILENO == fileno (stderr));
87
88   ASSERT (freopen ("/dev/null", "a", fp) == fp);
89   ASSERT (STDERR_FILENO < fileno (fp));
90
91   /* fd 1 closed.  */
92   ASSERT (close (STDOUT_FILENO) == 0);
93
94   ASSERT (freopen ("/dev/null", "w", stderr) == stderr);
95   ASSERT (STDERR_FILENO == fileno (stderr));
96
97   ASSERT (freopen ("/dev/null", "a+", fp) == fp);
98   ASSERT (STDERR_FILENO < fileno (fp));
99
100   /* fd 2 closed.  */
101   ASSERT (close (STDERR_FILENO) == 0);
102
103   ASSERT (freopen ("/dev/null", "w+", fp) == fp);
104   ASSERT (STDERR_FILENO < fileno (fp));
105
106   return 0;
107 }