| 1 |
// Copyright (C) 2001-2025 Free Software Foundation, Inc. |
| 2 |
// |
| 3 |
// This file is part of the GNU ISO C++ Library. This library is free |
| 4 |
// software; you can redistribute it and/or modify it under the |
| 5 |
// terms of the GNU General Public License as published by the |
| 6 |
// Free Software Foundation; either version 3, or (at your option) |
| 7 |
// any later version. |
| 8 |
|
| 9 |
// This library is distributed in the hope that it will be useful, |
| 10 |
// but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 11 |
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 12 |
// GNU General Public License for more details. |
| 13 |
|
| 14 |
// You should have received a copy of the GNU General Public License along |
| 15 |
// with this library; see the file COPYING3. If not see |
| 16 |
// <http://www.gnu.org/licenses/>. |
| 17 |
|
| 18 |
// { dg-require-fileio "" } |
| 19 |
// { dg-additional-files "filebuf_virtuals-1.txt" } |
| 20 |
|
| 21 |
// C++98 27.8.1.3 filebuf member functions |
| 22 |
|
| 23 |
// various tests for filebuf::open() and filebuf::close() including |
| 24 |
// the non-portable functionality in the libstdc++-v3 IO library |
| 25 |
|
| 26 |
#include <fstream> |
| 27 |
#include <ext/stdio_filebuf.h> |
| 28 |
#include <testsuite_hooks.h> |
| 29 |
|
| 30 |
// Verify that std::filebuf doesn't close files that it didn't open |
| 31 |
// when using the following std::filebuf ctor: |
| 32 |
// |
| 33 |
// std::filebuf(__c_file_type* __f, |
| 34 |
// ios_base::openmode __mode, |
| 35 |
// int_type __s); |
| 36 |
// |
| 37 |
// Thanks to "George T. Talbot" <[email protected]> for uncovering |
| 38 |
// this bug/situation. |
| 39 |
|
| 40 |
const char name_01[] = "filebuf_virtuals-1.txt"; // file with data in it |
| 41 |
|
| 42 |
void test_02() |
| 43 |
{ |
| 44 |
int close_num = 0; |
| 45 |
|
| 46 |
// read (ext) |
| 47 |
FILE* f2 = fopen(name_01, "r"); |
| 48 |
VERIFY( f2 ); |
| 49 |
if (f2) |
| 50 |
{ |
| 51 |
__gnu_cxx::stdio_filebuf<char> fb(f2, std::ios_base::in, 512); |
| 52 |
close_num = fclose(f2); |
| 53 |
} |
| 54 |
VERIFY( close_num == 0 ); |
| 55 |
|
| 56 |
// read (standard) |
| 57 |
FILE* f = fopen(name_01, "r"); |
| 58 |
VERIFY( f ); |
| 59 |
if (f) |
| 60 |
{ |
| 61 |
std::ifstream ifstream1(name_01); |
| 62 |
VERIFY( ifstream1.is_open() ); |
| 63 |
std::ios_base::iostate st01 = ifstream1.rdstate(); |
| 64 |
VERIFY( st01 == std::ios_base::goodbit ); |
| 65 |
close_num = fclose(f); |
| 66 |
} |
| 67 |
VERIFY( close_num == 0 ); |
| 68 |
} |
| 69 |
|
| 70 |
int |
| 71 |
main() |
| 72 |
{ |
| 73 |
test_02(); |
| 74 |
return 0; |
| 75 |
} |
| 76 |
|
| 77 |
|