1// { dg-require-fork "" }
2// { dg-require-mkfifo "" }
3
4// 2003-04-26 Petur Runolfsson  <peturr02@ru.is>
5
6// Copyright (C) 2003, 2004, 2005, 2006, 2007, 2009
7// Free Software Foundation, Inc.
8//
9// This file is part of the GNU ISO C++ Library.  This library is free
10// software; you can redistribute it and/or modify it under the
11// terms of the GNU General Public License as published by the
12// Free Software Foundation; either version 3, or (at your option)
13// any later version.
14
15// This library is distributed in the hope that it will be useful,
16// but WITHOUT ANY WARRANTY; without even the implied warranty of
17// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18// GNU General Public License for more details.
19
20// You should have received a copy of the GNU General Public License along
21// with this library; see the file COPYING3.  If not see
22// <http://www.gnu.org/licenses/>.
23
24// 27.3 Standard iostream objects
25
26#include <fstream>
27#include <iostream>
28#include <cstdlib>
29#include <unistd.h>
30#include <signal.h>
31#include <fcntl.h>
32#include <sys/types.h>
33#include <sys/stat.h>
34
35// No asserts, avoid leaking the semaphore if a VERIFY fails.
36#undef _GLIBCXX_ASSERT
37
38#include <testsuite_hooks.h>
39
40// Check that cout.flush() is called when last ios_base::Init is destroyed.
41bool test07()
42{
43  using namespace std;
44  using namespace __gnu_test;
45  bool test __attribute__((unused)) = true;
46
47  const char* name = "tmp_fifo4";
48
49  signal(SIGPIPE, SIG_IGN);
50
51  unlink(name);
52  mkfifo(name, S_IRWXU);
53  semaphore s1;
54
55  int child = fork();
56  VERIFY( child != -1 );
57
58  if (child == 0)
59    {
60      filebuf fbout;
61      fbout.open(name, ios_base::in|ios_base::out);
62      VERIFY( fbout.is_open() );
63      s1.wait();
64      cout.rdbuf(&fbout);
65      fbout.sputc('a');
66      // NB: fbout is *not* destroyed here!
67      exit(0);
68    }
69
70  filebuf fbin;
71  fbin.open(name, ios_base::in);
72  s1.signal();
73  filebuf::int_type c = fbin.sbumpc();
74  VERIFY( c != filebuf::traits_type::eof() );
75  VERIFY( c == filebuf::traits_type::to_int_type('a') );
76
77  fbin.close();
78
79  return test;
80}
81
82int
83main()
84{
85  return !test07();
86}
87