Merge from mainline.
[official-gcc.git] / libstdc++-v3 / testsuite / 27_io / objects / char / 9661-1.cc
blob4723282650361a73b07fa1a4e438b0791ecdd5e8
1 // { dg-require-fork "" }
2 // { dg-require-mkfifo "" }
4 // 2003-04-30 Petur Runolfsson <peturr02@ru.is>
6 // Copyright (C) 2003, 2004, 2005, 2006 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 2, or (at your option)
12 // any later version.
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.
19 // You should have received a copy of the GNU General Public License along
20 // with this library; see the file COPYING. If not, write to the Free
21 // Software Foundation, 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301,
22 // USA.
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 <iostream>
30 #include <unistd.h>
31 #include <signal.h>
32 #include <fcntl.h>
33 #include <sys/types.h>
34 #include <sys/stat.h>
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.
39 bool test01()
41 using namespace std;
42 using namespace __gnu_test;
44 bool test __attribute__((unused)) = true;
46 const char* name = "tmp_fifo5";
48 signal(SIGPIPE, SIG_IGN);
50 unlink(name);
51 mkfifo(name, S_IRWXU);
52 semaphore s1, s2;
54 int child = fork();
55 VERIFY( child != -1 );
57 if (child == 0)
59 FILE* file = fopen(name, "r+");
60 VERIFY( file != NULL );
61 fputs("Whatever\n", file);
62 fflush(file);
63 s1.signal();
64 s2.wait();
65 fclose(file);
66 s1.signal();
67 exit(0);
70 freopen(name, "r", stdin);
71 s1.wait();
73 int c1 = fgetc(stdin);
74 VERIFY( c1 != EOF );
75 int c2 = cin.rdbuf()->sputbackc('a');
76 VERIFY( c2 != EOF );
77 VERIFY( c2 == 'a' );
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' );
86 int c5 = cin.rdbuf()->sgetc();
87 VERIFY( c5 != EOF );
88 VERIFY( c5 == c4 );
89 s2.signal();
90 s1.wait();
92 return test;
95 int main()
97 return !test01();