OSDN Git Service

2006-05-19 Paolo Carlini <pcarlini@suse.de>
[pf3gnuchains/gcc-fork.git] / libstdc++-v3 / testsuite / 27_io / basic_filebuf / close / char / 4879.cc
1 // { dg-require-fork "" }
2 // { dg-require-mkfifo "" }
3  
4 // Copyright (C) 2001, 2002, 2003, 2004, 2005, 2006
5 // Free Software Foundation, Inc.
6 //
7 // This file is part of the GNU ISO C++ Library.  This library is free
8 // software; you can redistribute it and/or modify it under the
9 // terms of the GNU General Public License as published by the
10 // Free Software Foundation; either version 2, or (at your option)
11 // any later version.
12
13 // This library is distributed in the hope that it will be useful,
14 // but WITHOUT ANY WARRANTY; without even the implied warranty of
15 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16 // GNU General Public License for more details.
17
18 // You should have received a copy of the GNU General Public License along
19 // with this library; see the file COPYING.  If not, write to the Free
20 // Software Foundation, 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301,
21 // USA.
22
23 // 27.8.1.3 filebuf member functions
24 // @require@ %-*.tst %-*.txt
25 // @diff@ %-*.tst %-*.txt
26
27 // various tests for filebuf::open() and filebuf::close() including
28 // the non-portable functionality in the libstdc++-v3 IO library
29
30 #include <fstream>
31 #include <iostream>
32 #include <unistd.h>
33 #include <signal.h>
34 #include <fcntl.h>
35 #include <sys/types.h>
36 #include <sys/stat.h>
37
38 // No asserts, avoid leaking the semaphores if a VERIFY fails.
39 #undef _GLIBCXX_ASSERT
40
41 #include <testsuite_hooks.h>
42
43 // libstdc++/2913, libstdc++/4879
44 // John Fardo  <jfardo@laurelnetworks.com>, Brad Garcia <garsh@attbi.com>
45 bool
46 test_04()
47 {
48   using namespace __gnu_test;
49
50   bool test __attribute__((unused)) = true;
51   const char* name = "tmp_fifo1";
52   semaphore s1, s2;
53
54   signal(SIGPIPE, SIG_IGN);
55   
56   unlink(name);
57   if (0 != mkfifo(name, S_IRWXU))
58     {
59       std::cerr << "failed to create fifo" << std::endl;
60       exit(-1);
61     }
62   
63   int fval = fork();
64   if (fval == -1)
65     {
66       std::cerr << "failed to fork" << std::endl;
67       unlink(name);
68       return false;
69     }
70   else if (fval == 0)
71     {
72       std::ifstream ifs(name);
73       s1.wait();
74       ifs.close();
75       s2.signal();
76       exit(0);
77     }
78
79   std::ofstream ofs(name);
80   s1.signal();
81   s2.wait();
82   ofs.put('t');
83
84   /*
85    * ISO/IED 14882:1998(E) 27.8.1.10.4
86    *
87    * void close();
88    *
89    * Effects:  Calls rdbuf()->close() and, if that function fails
90    * (returns a null pointer), calls setstate(failbit)...
91    */
92   ofs.close();
93   if (!(ofs.rdstate() & std::ios::failbit))
94     {
95       test = false;
96       VERIFY( test );
97     }
98
99   unlink(name);
100
101   return test;
102 }
103
104 int
105 main()
106 {
107   return !test_04();
108 }