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 wcin.rdbuf()->sputbackc() puts characters back to stdin.
37// If wcin.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, "w");
60      fputs("Whatever\n", file);
61      fflush(file);
62      s1.signal();
63      s2.wait();
64      fclose(file);
65      s1.signal();
66      exit(0);
67    }
68
69  VERIFY( freopen(name, "r", stdin) );
70  s1.wait();
71
72  wint_t c1 = fgetwc(stdin);
73  VERIFY( c1 != WEOF );
74  wint_t c2 = wcin.rdbuf()->sputbackc(L'a');
75  VERIFY( c2 != WEOF );
76  VERIFY( c2 == L'a' );
77
78  wint_t c3 = fgetwc(stdin);
79  VERIFY( c3 != WEOF );
80  VERIFY( c3 == c2 );
81  wint_t c4 = ungetwc(L'b', stdin);
82  VERIFY( c4 != WEOF );
83  VERIFY( c4 == L'b' );
84
85  wint_t c5 = wcin.rdbuf()->sgetc();
86  VERIFY( c5 != WEOF );
87  VERIFY( c5 == c4 );
88  s2.signal();
89  s1.wait();
90
91  return test;
92}
93
94int main()
95{
96  return !test01();
97}
98