Merged trunk at revision 161680 into branch.
[official-gcc.git] / libstdc++-v3 / testsuite / 27_io / objects / char / 9661-1.cc
blob8ca77c76e7593d0de2b73d9f4f9b60526649485a
1 // { dg-require-fork "" }
2 // { dg-require-mkfifo "" }
4 // 2003-04-30 Petur Runolfsson <peturr02@ru.is>
6 // Copyright (C) 2003, 2004, 2005, 2006, 2007, 2009, 2010
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.
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.
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/>.
24 // No asserts, avoid leaking the semaphores if a VERIFY fails.
25 #undef _GLIBCXX_ASSERT
27 #include <testsuite_hooks.h>
28 #include <cstdio>
29 #include <cstdlib>
30 #include <iostream>
31 #include <unistd.h>
32 #include <signal.h>
33 #include <fcntl.h>
34 #include <sys/types.h>
35 #include <sys/stat.h>
37 // Check that cin.rdbuf()->sputbackc() puts characters back to stdin.
38 // If cin.rdbuf() is a filebuf, this succeeds when stdin is a regular
39 // file, but fails otherwise, hence the named fifo.
40 bool test01()
42 using namespace std;
43 using namespace __gnu_test;
45 bool test __attribute__((unused)) = true;
47 const char* name = "tmp_fifo5";
49 signal(SIGPIPE, SIG_IGN);
51 unlink(name);
52 mkfifo(name, S_IRWXU);
53 semaphore s1, s2;
55 int child = fork();
56 VERIFY( child != -1 );
58 if (child == 0)
60 FILE* file = fopen(name, "r+");
61 VERIFY( file != 0 );
62 fputs("Whatever\n", file);
63 fflush(file);
64 s1.signal();
65 s2.wait();
66 fclose(file);
67 s1.signal();
68 exit(0);
71 VERIFY( freopen(name, "r", stdin) );
72 s1.wait();
74 int c1 = fgetc(stdin);
75 VERIFY( c1 != EOF );
76 int c2 = cin.rdbuf()->sputbackc('a');
77 VERIFY( c2 != EOF );
78 VERIFY( c2 == 'a' );
80 int c3 = fgetc(stdin);
81 VERIFY( c3 != EOF );
82 VERIFY( c3 == c2 );
83 int c4 = ungetc('b', stdin);
84 VERIFY( c4 != EOF );
85 VERIFY( c4 == 'b' );
87 int c5 = cin.rdbuf()->sgetc();
88 VERIFY( c5 != EOF );
89 VERIFY( c5 == c4 );
90 s2.signal();
91 s1.wait();
93 return test;
96 int main()
98 return !test01();