]> git.ipfire.org Git - thirdparty/gcc.git/blob - libstdc++-v3/config/io/basic_file_stdio.cc
Update copyright years.
[thirdparty/gcc.git] / libstdc++-v3 / config / io / basic_file_stdio.cc
1 // Wrapper of C-language FILE struct -*- C++ -*-
2
3 // Copyright (C) 2000-2024 Free Software Foundation, Inc.
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
8 // Free Software Foundation; either version 3, or (at your option)
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
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.
19
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/>.
24
25 //
26 // ISO C++ 14882: 27.8 File-based streams
27 //
28
29 #include <bits/largefile-config.h>
30 #include <bits/basic_file.h>
31 #include <fcntl.h>
32 #include <errno.h>
33
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
44 #ifdef _GLIBCXX_HAVE_SYS_IOCTL_H
45 #define BSD_COMP
46 #include <sys/ioctl.h>
47 #endif
48
49 // Pick up FIONREAD on Solaris 2.5.
50 #ifdef _GLIBCXX_HAVE_SYS_FILIO_H
51 #include <sys/filio.h>
52 #endif
53
54 #ifdef _GLIBCXX_HAVE_SYS_UIO_H
55 #include <sys/uio.h>
56 #endif
57
58 #if defined(_GLIBCXX_HAVE_S_ISREG) || defined(_GLIBCXX_HAVE_S_IFREG)
59 # include <sys/stat.h>
60 # ifdef _GLIBCXX_HAVE_S_ISREG
61 # define _GLIBCXX_ISREG(x) S_ISREG(x)
62 # else
63 # define _GLIBCXX_ISREG(x) (((x) & S_IFMT) == S_IFREG)
64 # endif
65 #endif
66
67 #include <limits> // For <off_t>::max() and min() and <streamsize>::max()
68
69 #if _GLIBCXX_USE__GET_OSFHANDLE
70 # include <stdint.h> // For intptr_t
71 # include <io.h> // For _get_osfhandle
72 #endif
73
74 namespace
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 {
81 enum
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,
87 binary = std::ios_base::binary,
88 noreplace = std::_S_noreplace
89 };
90
91 // _GLIBCXX_RESOLVE_LIB_DEFECTS
92 // 596. 27.8.1.3 Table 112 omits "a+" and "a+b" modes.
93 switch (mode & (in|out|trunc|app|binary|noreplace))
94 {
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";
119
120 default: return 0; // invalid
121 }
122 }
123
124 // Wrapper handling partial write.
125 static std::streamsize
126 #ifdef _GLIBCXX_USE_STDIO_PURE
127 xwrite(FILE *__file, const char* __s, std::streamsize __n)
128 #else
129 xwrite(int __fd, const char* __s, std::streamsize __n)
130 #endif
131 {
132 std::streamsize __nleft = __n;
133
134 for (;;)
135 {
136 #ifdef _GLIBCXX_USE_STDIO_PURE
137 const std::streamsize __ret = fwrite(__s, 1, __nleft, __file);
138 if (__ret == 0 && ferror(__file))
139 break;
140 #else
141 const std::streamsize __ret = write(__fd, __s, __nleft);
142 if (__ret == -1L && errno == EINTR)
143 continue;
144 if (__ret == -1L)
145 break;
146 #endif
147 __nleft -= __ret;
148 if (__nleft == 0)
149 break;
150
151 __s += __ret;
152 }
153
154 return __n - __nleft;
155 }
156
157 #if defined(_GLIBCXX_HAVE_WRITEV) && !defined(_GLIBCXX_USE_STDIO_PURE)
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 {
163 std::streamsize __nleft = __n1 + __n2;
164 std::streamsize __n1_left = __n1;
165
166 struct iovec __iov[2];
167 __iov[1].iov_base = const_cast<char*>(__s2);
168 __iov[1].iov_len = __n2;
169
170 for (;;)
171 {
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)
187 {
188 __nleft -= xwrite(__fd, __s2 + __off, __n2 - __off);
189 break;
190 }
191
192 __s1 += __ret;
193 __n1_left -= __ret;
194 }
195
196 return __n1 + __n2 - __nleft;
197 }
198 #endif
199 } // anonymous namespace
200
201
202 namespace std _GLIBCXX_VISIBILITY(default)
203 {
204 _GLIBCXX_BEGIN_NAMESPACE_VERSION
205
206 // Definitions for __basic_file<char>.
207 __basic_file<char>::__basic_file(__c_lock* /*__lock*/) throw()
208 : _M_cfile(NULL), _M_cfile_created(false) { }
209
210 __basic_file<char>::~__basic_file()
211 { this->close(); }
212
213 __basic_file<char>*
214 __basic_file<char>::sys_open(__c_file* __file, ios_base::openmode)
215 {
216 __basic_file* __ret = NULL;
217 if (!this->is_open() && __file)
218 {
219 int __err, __save_errno = errno;
220 // POSIX guarantees that fflush sets errno on error, but C doesn't.
221 errno = 0;
222 do
223 __err = fflush(__file);
224 while (__err && errno == EINTR);
225 errno = __save_errno;
226 if (!__err)
227 {
228 _M_cfile = __file;
229 _M_cfile_created = false;
230 __ret = this;
231 }
232 }
233 return __ret;
234 }
235
236 __basic_file<char>*
237 __basic_file<char>::sys_open(int __fd, ios_base::openmode __mode) throw ()
238 {
239 __basic_file* __ret = NULL;
240 const char* __c_mode = fopen_mode(__mode);
241 if (__c_mode && !this->is_open() && (_M_cfile = fdopen(__fd, __c_mode)))
242 {
243 char* __buf = NULL;
244 _M_cfile_created = true;
245 if (__fd == 0)
246 setvbuf(_M_cfile, __buf, _IONBF, 0);
247 __ret = this;
248 }
249 return __ret;
250 }
251
252 __basic_file<char>*
253 __basic_file<char>::open(const char* __name, ios_base::openmode __mode,
254 int /*__prot*/)
255 {
256 __basic_file* __ret = NULL;
257 const char* __c_mode = fopen_mode(__mode);
258 if (__c_mode && !this->is_open())
259 {
260 if ((_M_cfile = fopen(__name, __c_mode)))
261 {
262 _M_cfile_created = true;
263 __ret = this;
264 }
265 }
266 return __ret;
267 }
268
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
302 bool
303 __basic_file<char>::is_open() const throw ()
304 { return _M_cfile != 0; }
305
306 #ifndef _GLIBCCXX_USE_STDIO_PURE
307 int
308 __basic_file<char>::fd() throw ()
309 { return fileno(_M_cfile); }
310 #endif
311
312 __c_file*
313 __basic_file<char>::file() throw ()
314 { return _M_cfile; }
315
316 __basic_file<char>*
317 __basic_file<char>::close()
318 {
319 __basic_file* __ret = static_cast<__basic_file*>(NULL);
320 if (this->is_open())
321 {
322 int __err = 0;
323 if (_M_cfile_created)
324 __err = fclose(_M_cfile);
325 _M_cfile = 0;
326 if (!__err)
327 __ret = this;
328 }
329 return __ret;
330 }
331
332 streamsize
333 __basic_file<char>::xsgetn(char* __s, streamsize __n)
334 {
335 streamsize __ret;
336 #ifdef _GLIBCXX_USE_STDIO_PURE
337 __ret = fread(__s, 1, __n, this->file());
338 if (__ret == 0 && ferror(this->file()))
339 __ret = -1;
340 #else
341 do
342 __ret = read(this->fd(), __s, __n);
343 while (__ret == -1L && errno == EINTR);
344 #endif
345 return __ret;
346 }
347
348 streamsize
349 __basic_file<char>::xsputn(const char* __s, streamsize __n)
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 }
357
358 streamsize
359 __basic_file<char>::xsputn_2(const char* __s1, streamsize __n1,
360 const char* __s2, streamsize __n2)
361 {
362 streamsize __ret = 0;
363 #if defined(_GLIBCXX_HAVE_WRITEV) && !defined(_GLIBCXX_USE_STDIO_PURE)
364 __ret = xwritev(this->fd(), __s1, __n1, __s2, __n2);
365 #else
366 if (__n1)
367 #ifdef _GLIBCXX_USE_STDIO_PURE
368 __ret = xwrite(this->file(), __s1, __n1);
369 #else
370 __ret = xwrite(this->fd(), __s1, __n1);
371 #endif
372
373 if (__ret == __n1)
374 #ifdef _GLIBCXX_USE_STDIO_PURE
375 __ret += xwrite(this->file(), __s2, __n2);
376 #else
377 __ret += xwrite(this->fd(), __s2, __n2);
378 #endif
379 #endif
380 return __ret;
381 }
382
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
394 #else
395 return lseek(__f->fd(), 0, (int)ios_base::cur);
396 #endif
397 }
398 }
399
400 streamoff
401 __basic_file<char>::seekoff(streamoff __off, ios_base::seekdir __way) throw ()
402 {
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);
420 #else
421 if _GLIBCXX17_CONSTEXPR (sizeof(streamoff) > sizeof(off_t))
422 if (__off > numeric_limits<off_t>::max()
423 || __off < numeric_limits<off_t>::min())
424 return -1L;
425 return lseek(this->fd(), __off, __way);
426 #endif
427 }
428
429 int
430 __basic_file<char>::sync()
431 { return fflush(_M_cfile); }
432
433 streamsize
434 __basic_file<char>::showmanyc()
435 {
436 #if !defined(_GLIBCXX_NO_IOCTL) && !defined(_GLIBCXX_USE_STDIO_PURE)
437 #ifdef FIONREAD
438 // Pipes and sockets.
439 int __num = 0;
440 int __r = ioctl(this->fd(), FIONREAD, &__num);
441 if (!__r && __num >= 0)
442 return __num;
443 #endif
444 #endif
445
446 #if defined(_GLIBCXX_HAVE_POLL) && !defined(_GLIBCXX_USE_STDIO_PURE)
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;
453 #endif
454
455 #if defined(_GLIBCXX_HAVE_S_ISREG) || defined(_GLIBCXX_HAVE_S_IFREG)
456 // Regular files.
457 struct stat __buffer;
458 const int __err = fstat(this->fd(), &__buffer);
459 if (!__err && _GLIBCXX_ISREG(__buffer.st_mode))
460 {
461 const streamoff __off = __buffer.st_size - std::get_file_offset(this);
462 return std::min(__off, streamoff(numeric_limits<streamsize>::max()));
463 }
464 #endif
465 return 0;
466 }
467
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;
475 return reinterpret_cast<native_handle_type>(handle);
476 #else
477 return fileno(_M_cfile);
478 #endif
479 }
480
481 _GLIBCXX_END_NAMESPACE_VERSION
482 } // namespace
483