1// { dg-require-fork "" }
2// { dg-require-mkfifo "" }
3
4// 2003-04-30  Petur Runolfsson <peturr02@ru.is>
5
6// Copyright (C) 2003-2015 Free Software Foundation, Inc.
7//
8// This file is part of the GNU ISO C++ Library.  This library is free
9// software; you can redistribute it and/or modify it under the
10// terms of the GNU General Public License as published by the
11// Free Software Foundation; either version 3, or (at your option)
12// any later version.
13
14// This library is distributed in the hope that it will be useful,
15// but WITHOUT ANY WARRANTY; without even the implied warranty of
16// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17// GNU General Public License for more details.
18
19// You should have received a copy of the GNU General Public License along
20// with this library; see the file COPYING3.  If not see
21// <http://www.gnu.org/licenses/>.
22
23// No asserts, avoid leaking the semaphores if a VERIFY fails.
24#undef _GLIBCXX_ASSERT
25
26#include <testsuite_hooks.h>
27#include <cstdio>
28#include <cstdlib>
29#include <iostream>
30#include <unistd.h>
31#include <signal.h>
32#include <fcntl.h>
33#include <sys/types.h>
34#include <sys/stat.h>
35
36// Check that cin.rdbuf()->sputbackc() puts characters back to stdin.
37// If cin.rdbuf() is a filebuf, this succeeds when stdin is a regular
38// file, but fails otherwise, hence the named fifo.
39bool test01()
40{
41  using namespace std;
42  using namespace __gnu_test;
43
44  bool test __attribute__((unused)) = true;
45
46  const char* name = "tmp_fifo5";
47
48  signal(SIGPIPE, SIG_IGN);
49
50  unlink(name);
51  mkfifo(name, S_IRWXU);
52  semaphore s1, s2;
53
54  int child = fork();
55  VERIFY( child != -1 );
56
57  if (child == 0)
58    {
59      FILE* file = fopen(name, "r+");
60      VERIFY( file != 0 );
61      fputs("Whatever\n", file);
62      fflush(file);
63      s1.signal();
64      s2.wait();
65      fclose(file);
66      s1.signal();
67      exit(0);
68    }
69
70  VERIFY( freopen(name, "r", stdin) );
71  s1.wait();
72
73  int c1 = fgetc(stdin);
74  VERIFY( c1 != EOF );
75  int c2 = cin.rdbuf()->sputbackc('a');
76  VERIFY( c2 != EOF );
77  VERIFY( c2 == 'a' );
78
79  int c3 = fgetc(stdin);
80  VERIFY( c3 != EOF );
81  VERIFY( c3 == c2 );
82  int c4 = ungetc('b', stdin);
83  VERIFY( c4 != EOF );
84  VERIFY( c4 == 'b' );
85
86  int c5 = cin.rdbuf()->sgetc();
87  VERIFY( c5 != EOF );
88  VERIFY( c5 == c4 );
89  s2.signal();
90  s1.wait();
91
92  return test;
93}
94
95int main()
96{
97  return !test01();
98}
99