]> git.ipfire.org Git - thirdparty/gcc.git/blame - libstdc++-v3/testsuite/27_io/objects/char/9661-1.cc
Update copyright years.
[thirdparty/gcc.git] / libstdc++-v3 / testsuite / 27_io / objects / char / 9661-1.cc
CommitLineData
16cb9d1f 1// { dg-require-fork "" }
2// { dg-require-mkfifo "" }
3
fd63f08b 4// 2003-04-30 Petur Runolfsson <peturr02@ru.is>
5
f1717362 6// Copyright (C) 2003-2016 Free Software Foundation, Inc.
fd63f08b 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
6bc9506f 11// Free Software Foundation; either version 3, or (at your option)
fd63f08b 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
6bc9506f 20// with this library; see the file COPYING3. If not see
21// <http://www.gnu.org/licenses/>.
fd63f08b 22
dfd22e84 23// No asserts, avoid leaking the semaphores if a VERIFY fails.
24#undef _GLIBCXX_ASSERT
25
fd63f08b 26#include <testsuite_hooks.h>
27#include <cstdio>
ceb6f07a 28#include <cstdlib>
fd63f08b 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.
dfd22e84 39bool test01()
fd63f08b 40{
41 using namespace std;
7f26a832 42 using namespace __gnu_test;
fd63f08b 43
f8ef786c 44 bool test __attribute__((unused)) = true;
fd63f08b 45
46 const char* name = "tmp_fifo5";
47
48 signal(SIGPIPE, SIG_IGN);
49
50 unlink(name);
16cb9d1f 51 mkfifo(name, S_IRWXU);
007b777a 52 semaphore s1, s2;
fd63f08b 53
54 int child = fork();
55 VERIFY( child != -1 );
56
57 if (child == 0)
58 {
aaeb5666 59 FILE* file = fopen(name, "r+");
e4bb1925 60 VERIFY( file != 0 );
fd63f08b 61 fputs("Whatever\n", file);
62 fflush(file);
354363fb 63 s1.signal();
64 s2.wait();
fd63f08b 65 fclose(file);
354363fb 66 s1.signal();
fd63f08b 67 exit(0);
68 }
354363fb 69
34bd3b47 70 VERIFY( freopen(name, "r", stdin) );
354363fb 71 s1.wait();
fd63f08b 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 );
354363fb 89 s2.signal();
90 s1.wait();
dfd22e84 91
92 return test;
fd63f08b 93}
94
95int main()
96{
dfd22e84 97 return !test01();
fd63f08b 98}