]> git.ipfire.org Git - thirdparty/gcc.git/blame - libstdc++-v3/config/io/basic_file_stdio.cc
Update copyright years.
[thirdparty/gcc.git] / libstdc++-v3 / config / io / basic_file_stdio.cc
CommitLineData
c0a26060
BK
1// Wrapper of C-language FILE struct -*- C++ -*-
2
a945c346 3// Copyright (C) 2000-2024 Free Software Foundation, Inc.
c0a26060
BK
4//
5// This file is part of the GNU ISO C++ Library. This library is free
6// software; you can redistribute it and/or modify it under the
7// terms of the GNU General Public License as published by the
748086b7 8// Free Software Foundation; either version 3, or (at your option)
c0a26060
BK
9// any later version.
10
11// This library is distributed in the hope that it will be useful,
12// but WITHOUT ANY WARRANTY; without even the implied warranty of
13// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14// GNU General Public License for more details.
15
748086b7
JJ
16// Under Section 7 of GPL version 3, you are granted additional
17// permissions described in the GCC Runtime Library Exception, version
18// 3.1, as published by the Free Software Foundation.
c0a26060 19
748086b7
JJ
20// You should have received a copy of the GNU General Public License and
21// a copy of the GCC Runtime Library Exception along with this program;
22// see the files COPYING3 and COPYING.RUNTIME respectively. If not, see
23// <http://www.gnu.org/licenses/>.
c0a26060
BK
24
25//
26// ISO C++ 14882: 27.8 File-based streams
27//
28
61fe96d4 29#include <bits/largefile-config.h>
c0a26060 30#include <bits/basic_file.h>
6eeb7d7a 31#include <fcntl.h>
95dca20c 32#include <errno.h>
c0a26060 33
2b1be54b
BS
34#ifdef _GLIBCXX_HAVE_POLL
35#include <poll.h>
36#endif
37
38// Pick up ioctl on Solaris 2.8
39#ifdef _GLIBCXX_HAVE_UNISTD_H
40#include <unistd.h>
41#endif
42
43// Pick up FIONREAD on Solaris 2
3d7c150e 44#ifdef _GLIBCXX_HAVE_SYS_IOCTL_H
f92ab29f 45#define BSD_COMP
bbacb998
PC
46#include <sys/ioctl.h>
47#endif
48
49// Pick up FIONREAD on Solaris 2.5.
3d7c150e 50#ifdef _GLIBCXX_HAVE_SYS_FILIO_H
bbacb998
PC
51#include <sys/filio.h>
52#endif
53
bda243ec
PC
54#ifdef _GLIBCXX_HAVE_SYS_UIO_H
55#include <sys/uio.h>
56#endif
57
3d7c150e 58#if defined(_GLIBCXX_HAVE_S_ISREG) || defined(_GLIBCXX_HAVE_S_IFREG)
bbacb998 59# include <sys/stat.h>
3d7c150e
BK
60# ifdef _GLIBCXX_HAVE_S_ISREG
61# define _GLIBCXX_ISREG(x) S_ISREG(x)
bbacb998 62# else
3d7c150e 63# define _GLIBCXX_ISREG(x) (((x) & S_IFMT) == S_IFREG)
bbacb998
PC
64# endif
65#endif
66
dd5d134b 67#include <limits> // For <off_t>::max() and min() and <streamsize>::max()
3d05b345 68
c4baeaec
JW
69#if _GLIBCXX_USE__GET_OSFHANDLE
70# include <stdint.h> // For intptr_t
71# include <io.h> // For _get_osfhandle
72#endif
73
f92ab29f 74namespace
6a734d61
BK
75{
76 // Map ios_base::openmode flags to a string for use in fopen().
77 // Table of valid combinations as given in [lib.filebuf.members]/2.
78 static const char*
79 fopen_mode(std::ios_base::openmode mode)
80 {
f92ab29f 81 enum
6a734d61
BK
82 {
83 in = std::ios_base::in,
84 out = std::ios_base::out,
85 trunc = std::ios_base::trunc,
86 app = std::ios_base::app,
a219139e
JW
87 binary = std::ios_base::binary,
88 noreplace = std::_S_noreplace
6a734d61 89 };
ec01f292
PC
90
91 // _GLIBCXX_RESOLVE_LIB_DEFECTS
92 // 596. 27.8.1.3 Table 112 omits "a+" and "a+b" modes.
a219139e 93 switch (mode & (in|out|trunc|app|binary|noreplace))
6a734d61 94 {
a219139e
JW
95 case ( out ): return "w";
96 case ( out |noreplace): return "wx";
97 case ( out|trunc ): return "w";
98 case ( out|trunc |noreplace): return "wx";
99 case ( out |app ): return "a";
100 case ( app ): return "a";
101 case (in ): return "r";
102 case (in|out ): return "r+";
103 case (in|out|trunc ): return "w+";
104 case (in|out|trunc |noreplace): return "w+x";
105 case (in|out |app ): return "a+";
106 case (in |app ): return "a+";
107
108 case ( out |binary ): return "wb";
109 case ( out |binary|noreplace): return "wbx";
110 case ( out |app|binary ): return "ab";
111 case ( app|binary ): return "ab";
112 case ( out|trunc |binary ): return "wb";
113 case (in |binary ): return "rb";
114 case (in|out |binary ): return "r+b";
115 case (in|out|trunc |binary ): return "w+b";
116 case (in|out|trunc |binary|noreplace): return "w+bx";
117 case (in|out |app|binary ): return "a+b";
118 case (in |app|binary ): return "a+b";
ec01f292 119
6a734d61
BK
120 default: return 0; // invalid
121 }
122 }
2e9a1f6b
PC
123
124 // Wrapper handling partial write.
125 static std::streamsize
75aee072
KP
126#ifdef _GLIBCXX_USE_STDIO_PURE
127 xwrite(FILE *__file, const char* __s, std::streamsize __n)
128#else
2e9a1f6b 129 xwrite(int __fd, const char* __s, std::streamsize __n)
75aee072 130#endif
2e9a1f6b
PC
131 {
132 std::streamsize __nleft = __n;
94b8de97
PC
133
134 for (;;)
2e9a1f6b 135 {
75aee072 136#ifdef _GLIBCXX_USE_STDIO_PURE
bb4f8f14 137 const std::streamsize __ret = fwrite(__s, 1, __nleft, __file);
2f6bbc9a
JW
138 if (__ret == 0 && ferror(__file))
139 break;
75aee072 140#else
2e9a1f6b
PC
141 const std::streamsize __ret = write(__fd, __s, __nleft);
142 if (__ret == -1L && errno == EINTR)
143 continue;
94b8de97 144 if (__ret == -1L)
2e9a1f6b 145 break;
2f6bbc9a 146#endif
2e9a1f6b 147 __nleft -= __ret;
94b8de97
PC
148 if (__nleft == 0)
149 break;
150
2e9a1f6b
PC
151 __s += __ret;
152 }
94b8de97 153
2e9a1f6b
PC
154 return __n - __nleft;
155 }
156
75aee072 157#if defined(_GLIBCXX_HAVE_WRITEV) && !defined(_GLIBCXX_USE_STDIO_PURE)
2e9a1f6b
PC
158 // Wrapper handling partial writev.
159 static std::streamsize
160 xwritev(int __fd, const char* __s1, std::streamsize __n1,
161 const char* __s2, std::streamsize __n2)
162 {
94b8de97
PC
163 std::streamsize __nleft = __n1 + __n2;
164 std::streamsize __n1_left = __n1;
2e9a1f6b
PC
165
166 struct iovec __iov[2];
2e9a1f6b
PC
167 __iov[1].iov_base = const_cast<char*>(__s2);
168 __iov[1].iov_len = __n2;
169
94b8de97 170 for (;;)
2e9a1f6b 171 {
94b8de97
PC
172 __iov[0].iov_base = const_cast<char*>(__s1);
173 __iov[0].iov_len = __n1_left;
174
175 const std::streamsize __ret = writev(__fd, __iov, 2);
176 if (__ret == -1L && errno == EINTR)
177 continue;
178 if (__ret == -1L)
179 break;
180
181 __nleft -= __ret;
182 if (__nleft == 0)
183 break;
184
185 const std::streamsize __off = __ret - __n1_left;
186 if (__off >= 0)
2e9a1f6b 187 {
94b8de97
PC
188 __nleft -= xwrite(__fd, __s2 + __off, __n2 - __off);
189 break;
2e9a1f6b 190 }
f92ab29f 191
94b8de97
PC
192 __s1 += __ret;
193 __n1_left -= __ret;
2e9a1f6b
PC
194 }
195
94b8de97 196 return __n1 + __n2 - __nleft;
2e9a1f6b
PC
197 }
198#endif
c1a03f03 199} // anonymous namespace
6a734d61 200
3cbc7af0 201
12ffa228
BK
202namespace std _GLIBCXX_VISIBILITY(default)
203{
204_GLIBCXX_BEGIN_NAMESPACE_VERSION
3cbc7af0 205
00532602 206 // Definitions for __basic_file<char>.
32ade559 207 __basic_file<char>::__basic_file(__c_lock* /*__lock*/) throw()
00532602
BK
208 : _M_cfile(NULL), _M_cfile_created(false) { }
209
210 __basic_file<char>::~__basic_file()
d3a193e3 211 { this->close(); }
f92ab29f 212
00532602 213 __basic_file<char>*
f92ab29f 214 __basic_file<char>::sys_open(__c_file* __file, ios_base::openmode)
00532602
BK
215 {
216 __basic_file* __ret = NULL;
217 if (!this->is_open() && __file)
218 {
04d07b61
JW
219 int __err, __save_errno = errno;
220 // POSIX guarantees that fflush sets errno on error, but C doesn't.
f92ab29f 221 errno = 0;
ce894603 222 do
04d07b61 223 __err = fflush(__file);
ce894603 224 while (__err && errno == EINTR);
04d07b61 225 errno = __save_errno;
ce894603
PC
226 if (!__err)
227 {
228 _M_cfile = __file;
229 _M_cfile_created = false;
230 __ret = this;
231 }
5cdd50a5
BK
232 }
233 return __ret;
234 }
f92ab29f 235
5cdd50a5 236 __basic_file<char>*
b91cc3b9 237 __basic_file<char>::sys_open(int __fd, ios_base::openmode __mode) throw ()
5cdd50a5
BK
238 {
239 __basic_file* __ret = NULL;
c1a03f03 240 const char* __c_mode = fopen_mode(__mode);
563ae04f 241 if (__c_mode && !this->is_open() && (_M_cfile = fdopen(__fd, __c_mode)))
5cdd50a5 242 {
563ae04f 243 char* __buf = NULL;
e80213d2 244 _M_cfile_created = true;
5cdd50a5 245 if (__fd == 0)
563ae04f 246 setvbuf(_M_cfile, __buf, _IONBF, 0);
00532602
BK
247 __ret = this;
248 }
249 return __ret;
250 }
f92ab29f
CG
251
252 __basic_file<char>*
253 __basic_file<char>::open(const char* __name, ios_base::openmode __mode,
00532602
BK
254 int /*__prot*/)
255 {
256 __basic_file* __ret = NULL;
c1a03f03 257 const char* __c_mode = fopen_mode(__mode);
6a734d61 258 if (__c_mode && !this->is_open())
00532602
BK
259 {
260 if ((_M_cfile = fopen(__name, __c_mode)))
261 {
262 _M_cfile_created = true;
263 __ret = this;
264 }
265 }
266 return __ret;
267 }
f92ab29f 268
b0292359
JW
269#if _GLIBCXX_HAVE__WFOPEN && _GLIBCXX_USE_WCHAR_T
270 __basic_file<char>*
271 __basic_file<char>::open(const wchar_t* __name, ios_base::openmode __mode)
272 {
273 __basic_file* __ret = NULL;
274 const char* __c_mode = fopen_mode(__mode);
275 if (__c_mode && !this->is_open())
276 {
277 wchar_t __wc_mode[4] = { };
278 int __i = 0;
279 do
280 {
281 switch(__c_mode[__i]) {
282 case 'a': __wc_mode[__i] = L'a'; break;
283 case 'b': __wc_mode[__i] = L'b'; break;
284 case 'r': __wc_mode[__i] = L'r'; break;
285 case 'w': __wc_mode[__i] = L'w'; break;
286 case '+': __wc_mode[__i] = L'+'; break;
287 default: return __ret;
288 }
289 }
290 while (__c_mode[++__i]);
291
292 if ((_M_cfile = _wfopen(__name, __wc_mode)))
293 {
294 _M_cfile_created = true;
295 __ret = this;
296 }
297 }
298 return __ret;
299 }
300#endif
301
f92ab29f 302 bool
b91cc3b9 303 __basic_file<char>::is_open() const throw ()
5cdd50a5 304 { return _M_cfile != 0; }
f92ab29f 305
75aee072 306#ifndef _GLIBCCXX_USE_STDIO_PURE
f92ab29f 307 int
b91cc3b9 308 __basic_file<char>::fd() throw ()
803cb0b5 309 { return fileno(_M_cfile); }
75aee072 310#endif
f92ab29f 311
803cb0b5 312 __c_file*
b91cc3b9 313 __basic_file<char>::file() throw ()
803cb0b5 314 { return _M_cfile; }
f92ab29f
CG
315
316 __basic_file<char>*
00532602 317 __basic_file<char>::close()
f92ab29f 318 {
e80213d2 319 __basic_file* __ret = static_cast<__basic_file*>(NULL);
d3a193e3
BK
320 if (this->is_open())
321 {
ee19761d 322 int __err = 0;
e6670c79 323 if (_M_cfile_created)
0fdba3a8 324 __err = fclose(_M_cfile);
ee19761d 325 _M_cfile = 0;
ce894603
PC
326 if (!__err)
327 __ret = this;
d3a193e3 328 }
e80213d2 329 return __ret;
00532602 330 }
f92ab29f
CG
331
332 streamsize
51ff8149 333 __basic_file<char>::xsgetn(char* __s, streamsize __n)
69ef29fd
BK
334 {
335 streamsize __ret;
75aee072 336#ifdef _GLIBCXX_USE_STDIO_PURE
2f6bbc9a
JW
337 __ret = fread(__s, 1, __n, this->file());
338 if (__ret == 0 && ferror(this->file()))
339 __ret = -1;
75aee072 340#else
2f6bbc9a 341 do
69ef29fd
BK
342 __ret = read(this->fd(), __s, __n);
343 while (__ret == -1L && errno == EINTR);
2f6bbc9a 344#endif
69ef29fd
BK
345 return __ret;
346 }
98e96784 347
f92ab29f 348 streamsize
98e96784 349 __basic_file<char>::xsputn(const char* __s, streamsize __n)
75aee072
KP
350 {
351#ifdef _GLIBCXX_USE_STDIO_PURE
352 return xwrite(this->file(), __s, __n);
353#else
354 return xwrite(this->fd(), __s, __n);
355#endif
356 }
cc5112c9 357
f92ab29f 358 streamsize
bda243ec
PC
359 __basic_file<char>::xsputn_2(const char* __s1, streamsize __n1,
360 const char* __s2, streamsize __n2)
361 {
362 streamsize __ret = 0;
75aee072 363#if defined(_GLIBCXX_HAVE_WRITEV) && !defined(_GLIBCXX_USE_STDIO_PURE)
c1a03f03 364 __ret = xwritev(this->fd(), __s1, __n1, __s2, __n2);
bda243ec
PC
365#else
366 if (__n1)
75aee072
KP
367#ifdef _GLIBCXX_USE_STDIO_PURE
368 __ret = xwrite(this->file(), __s1, __n1);
369#else
c1a03f03 370 __ret = xwrite(this->fd(), __s1, __n1);
75aee072 371#endif
bda243ec
PC
372
373 if (__ret == __n1)
75aee072
KP
374#ifdef _GLIBCXX_USE_STDIO_PURE
375 __ret += xwrite(this->file(), __s2, __n2);
376#else
c1a03f03 377 __ret += xwrite(this->fd(), __s2, __n2);
75aee072 378#endif
bda243ec
PC
379#endif
380 return __ret;
381 }
382
2f6bbc9a
JW
383 namespace
384 {
385 inline streamoff
386 get_file_offset(__basic_file<char>* __f)
387 {
388#ifdef _GLIBCXX_USE_STDIO_PURE
389# ifdef _GLIBCXX_USE_FSEEKO_FTELLO
390 return ftello(__f->file());
391# else
392 return ftell(__f->file());
393# endif
2f6bbc9a
JW
394#else
395 return lseek(__f->fd(), 0, (int)ios_base::cur);
396#endif
397 }
398 }
399
4c4809c1 400 streamoff
b91cc3b9 401 __basic_file<char>::seekoff(streamoff __off, ios_base::seekdir __way) throw ()
3d05b345 402 {
2f6bbc9a
JW
403#ifdef _GLIBCXX_USE_STDIO_PURE
404#ifdef _GLIBCXX_USE_FSEEKO_FTELLO
405 if _GLIBCXX17_CONSTEXPR (sizeof(off_t) > sizeof(long))
406 {
407 if (fseeko(this->file(), __off, __way))
408 return -1;
409 }
410 else
411#endif
412 {
413 if (__off > numeric_limits<long>::max()
414 || __off < numeric_limits<long>::min())
415 return -1;
416 if (fseek(this->file(), __off, __way))
417 return -1;
418 }
419 return __way == ios_base::beg ? __off : std::get_file_offset(this);
3d05b345 420#else
61fe96d4
JW
421 if _GLIBCXX17_CONSTEXPR (sizeof(streamoff) > sizeof(off_t))
422 if (__off > numeric_limits<off_t>::max()
423 || __off < numeric_limits<off_t>::min())
3d05b345
PC
424 return -1L;
425 return lseek(this->fd(), __off, __way);
426#endif
427 }
cc5112c9 428
f92ab29f
CG
429 int
430 __basic_file<char>::sync()
5cdd50a5 431 { return fflush(_M_cfile); }
bbacb998
PC
432
433 streamsize
51ff8149 434 __basic_file<char>::showmanyc()
bbacb998 435 {
75aee072 436#if !defined(_GLIBCXX_NO_IOCTL) && !defined(_GLIBCXX_USE_STDIO_PURE)
bbacb998 437#ifdef FIONREAD
f92ab29f 438 // Pipes and sockets.
bbacb998
PC
439 int __num = 0;
440 int __r = ioctl(this->fd(), FIONREAD, &__num);
441 if (!__r && __num >= 0)
f92ab29f 442 return __num;
266afdd6
KT
443#endif
444#endif
bbacb998 445
75aee072 446#if defined(_GLIBCXX_HAVE_POLL) && !defined(_GLIBCXX_USE_STDIO_PURE)
bbacb998
PC
447 // Cheap test.
448 struct pollfd __pfd[1];
449 __pfd[0].fd = this->fd();
450 __pfd[0].events = POLLIN;
451 if (poll(__pfd, 1, 0) <= 0)
452 return 0;
f92ab29f 453#endif
bbacb998 454
3d7c150e 455#if defined(_GLIBCXX_HAVE_S_ISREG) || defined(_GLIBCXX_HAVE_S_IFREG)
bbacb998 456 // Regular files.
61fe96d4
JW
457 struct stat __buffer;
458 const int __err = fstat(this->fd(), &__buffer);
dd5d134b
PC
459 if (!__err && _GLIBCXX_ISREG(__buffer.st_mode))
460 {
2f6bbc9a 461 const streamoff __off = __buffer.st_size - std::get_file_offset(this);
dd5d134b
PC
462 return std::min(__off, streamoff(numeric_limits<streamsize>::max()));
463 }
bbacb998
PC
464#endif
465 return 0;
466 }
3cbc7af0 467
c4baeaec
JW
468 __basic_file<char>::native_handle_type
469 __basic_file<char>::native_handle() const noexcept
470 {
471#ifdef _GLIBCXX_USE_STDIO_PURE
472 return _M_cfile;
473#elif _GLIBCXX_USE__GET_OSFHANDLE
474 const intptr_t handle = _M_cfile ? _get_osfhandle(fileno(_M_cfile)) : -1;
29ad35a1 475 return reinterpret_cast<native_handle_type>(handle);
c4baeaec
JW
476#else
477 return fileno(_M_cfile);
478#endif
479 }
480
12ffa228
BK
481_GLIBCXX_END_NAMESPACE_VERSION
482} // namespace
3cbc7af0 483