xref: /netbsd-src/external/gpl3/gcc.old/dist/libstdc++-v3/src/c++11/system_error.cc (revision 8feb0f0b7eaff0608f8350bbfa3098827b4bb91b)
1 // <system_error> implementation file
2 
3 // Copyright (C) 2007-2020 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 #define _GLIBCXX_USE_CXX11_ABI 1
27 #define __sso_string __sso_stringxxx
28 #include <cstring>
29 #include <system_error>
30 #include <bits/functexcept.h>
31 #include <limits>
32 #include <errno.h>
33 #undef __sso_string
34 
35 namespace
36 {
37   using std::string;
38 
39   struct generic_error_category : public std::error_category
40   {
41     virtual const char*
name__anon139e6fbe0111::generic_error_category42     name() const noexcept
43     { return "generic"; }
44 
45     _GLIBCXX_DEFAULT_ABI_TAG
46     virtual string
message__anon139e6fbe0111::generic_error_category47     message(int i) const
48     {
49       // XXX locale issues: how does one get or set loc.
50       // _GLIBCXX_HAVE_STRERROR_L, strerror_l(i, cloc)
51       return string(strerror(i));
52     }
53   };
54 
55   struct system_error_category : public std::error_category
56   {
57     virtual const char*
name__anon139e6fbe0111::system_error_category58     name() const noexcept
59     { return "system"; }
60 
61     _GLIBCXX_DEFAULT_ABI_TAG
62     virtual string
message__anon139e6fbe0111::system_error_category63     message(int i) const
64     {
65       // XXX locale issues: how does one get or set loc.
66       // _GLIBCXX_HAVE_STRERROR_L, strerror_l(i, cloc)
67       return string(strerror(i));
68     }
69 
70     virtual std::error_condition
default_error_condition__anon139e6fbe0111::system_error_category71     default_error_condition(int ev) const noexcept
72     {
73       // Use generic category for all known POSIX errno values (including zero)
74       // and system category otherwise.
75       switch (ev)
76       {
77       // List of errno macros from [cerrno.syn].
78       // C11 only defines EDOM, EILSEQ and ERANGE, the rest are from POSIX.
79       // They expand to integer constant expressions with type int,
80       // and distinct positive values, suitable for use in #if directives.
81       // POSIX adds more macros (but they're not defined on all targets,
82       // see config/os/*/error_constants.h), and POSIX allows
83       // EAGAIN == EWOULDBLOCK and ENOTSUP == EOPNOTSUPP.
84 
85 #ifdef E2BIG
86       case E2BIG:
87 #endif
88 #ifdef EACCES
89       case EACCES:
90 #endif
91 #ifdef EADDRINUSE
92       case EADDRINUSE:
93 #endif
94 #ifdef EADDRNOTAVAIL
95       case EADDRNOTAVAIL:
96 #endif
97 #ifdef EAFNOSUPPORT
98       case EAFNOSUPPORT:
99 #endif
100 #ifdef EAGAIN
101       case EAGAIN:
102 #endif
103 #ifdef EALREADY
104       case EALREADY:
105 #endif
106 #ifdef EBADF
107       case EBADF:
108 #endif
109 #ifdef EBADMSG
110       case EBADMSG:
111 #endif
112 #ifdef EBUSY
113       case EBUSY:
114 #endif
115 #ifdef ECANCELED
116       case ECANCELED:
117 #endif
118 #ifdef ECHILD
119       case ECHILD:
120 #endif
121 #ifdef ECONNABORTED
122       case ECONNABORTED:
123 #endif
124 #ifdef ECONNREFUSED
125       case ECONNREFUSED:
126 #endif
127 #ifdef ECONNRESET
128       case ECONNRESET:
129 #endif
130 #ifdef EDEADLK
131       case EDEADLK:
132 #endif
133 #ifdef EDESTADDRREQ
134       case EDESTADDRREQ:
135 #endif
136       case EDOM:
137 #ifdef EEXIST
138       case EEXIST:
139 #endif
140 #ifdef EFAULT
141       case EFAULT:
142 #endif
143 #ifdef EFBIG
144       case EFBIG:
145 #endif
146 #ifdef EHOSTUNREACH
147       case EHOSTUNREACH:
148 #endif
149 #ifdef EIDRM
150       case EIDRM:
151 #endif
152       case EILSEQ:
153 #ifdef EINPROGRESS
154       case EINPROGRESS:
155 #endif
156 #ifdef EINTR
157       case EINTR:
158 #endif
159 #ifdef EINVAL
160       case EINVAL:
161 #endif
162 #ifdef EIO
163       case EIO:
164 #endif
165 #ifdef EISCONN
166       case EISCONN:
167 #endif
168 #ifdef EISDIR
169       case EISDIR:
170 #endif
171 #ifdef ELOOP
172       case ELOOP:
173 #endif
174 #ifdef EMFILE
175       case EMFILE:
176 #endif
177 #ifdef EMLINK
178       case EMLINK:
179 #endif
180 #ifdef EMSGSIZE
181       case EMSGSIZE:
182 #endif
183 #ifdef ENAMETOOLONG
184       case ENAMETOOLONG:
185 #endif
186 #ifdef ENETDOWN
187       case ENETDOWN:
188 #endif
189 #ifdef ENETRESET
190       case ENETRESET:
191 #endif
192 #ifdef ENETUNREACH
193       case ENETUNREACH:
194 #endif
195 #ifdef ENFILE
196       case ENFILE:
197 #endif
198 #ifdef ENOBUFS
199       case ENOBUFS:
200 #endif
201 #ifdef ENODATA
202       case ENODATA:
203 #endif
204 #ifdef ENODEV
205       case ENODEV:
206 #endif
207 #ifdef ENOENT
208       case ENOENT:
209 #endif
210 #ifdef ENOEXEC
211       case ENOEXEC:
212 #endif
213 #ifdef ENOLCK
214       case ENOLCK:
215 #endif
216 #ifdef ENOLINK
217       case ENOLINK:
218 #endif
219 #ifdef ENOMEM
220       case ENOMEM:
221 #endif
222 #ifdef ENOMSG
223       case ENOMSG:
224 #endif
225 #ifdef ENOPROTOOPT
226       case ENOPROTOOPT:
227 #endif
228 #ifdef ENOSPC
229       case ENOSPC:
230 #endif
231 #ifdef ENOSR
232       case ENOSR:
233 #endif
234 #ifdef ENOSTR
235       case ENOSTR:
236 #endif
237 #ifdef ENOSYS
238       case ENOSYS:
239 #endif
240 #ifdef ENOTCONN
241       case ENOTCONN:
242 #endif
243 #ifdef ENOTDIR
244       case ENOTDIR:
245 #endif
246 #if defined ENOTEMPTY && (!defined EEXIST || ENOTEMPTY != EEXIST)
247       // AIX sometimes uses the same value for EEXIST and ENOTEMPTY
248       case ENOTEMPTY:
249 #endif
250 #ifdef ENOTRECOVERABLE
251       case ENOTRECOVERABLE:
252 #endif
253 #ifdef ENOTSOCK
254       case ENOTSOCK:
255 #endif
256 #if defined ENOTSUP && (!defined ENOSYS || ENOTSUP != ENOSYS)
257       // zTPF uses the same value for ENOSYS and ENOTSUP
258       case ENOTSUP:
259 #endif
260 #ifdef ENOTTY
261       case ENOTTY:
262 #endif
263 #ifdef ENXIO
264       case ENXIO:
265 #endif
266 #if defined EOPNOTSUPP && (!defined ENOTSUP || EOPNOTSUPP != ENOTSUP)
267       case EOPNOTSUPP:
268 #endif
269 #ifdef EOVERFLOW
270       case EOVERFLOW:
271 #endif
272 #ifdef EOWNERDEAD
273       case EOWNERDEAD:
274 #endif
275 #ifdef EPERM
276       case EPERM:
277 #endif
278 #ifdef EPIPE
279       case EPIPE:
280 #endif
281 #ifdef EPROTO
282       case EPROTO:
283 #endif
284 #ifdef EPROTONOSUPPORT
285       case EPROTONOSUPPORT:
286 #endif
287 #ifdef EPROTOTYPE
288       case EPROTOTYPE:
289 #endif
290       case ERANGE:
291 #ifdef EROFS
292       case EROFS:
293 #endif
294 #ifdef ESPIPE
295       case ESPIPE:
296 #endif
297 #ifdef ESRCH
298       case ESRCH:
299 #endif
300 #ifdef ETIME
301       case ETIME:
302 #endif
303 #ifdef ETIMEDOUT
304       case ETIMEDOUT:
305 #endif
306 #ifdef ETXTBSY
307       case ETXTBSY:
308 #endif
309 #if defined EWOULDBLOCK && (!defined EAGAIN || EWOULDBLOCK != EAGAIN)
310       case EWOULDBLOCK:
311 #endif
312 #ifdef EXDEV
313       case EXDEV:
314 #endif
315       case 0:
316         return std::error_condition(ev, std::generic_category());
317 
318       /* Additional system-dependent mappings from non-standard error codes
319        * to one of the POSIX values above would go here, e.g.
320       case EBLAH:
321 	return std::error_condition(EINVAL, std::generic_category());
322        */
323 
324       default:
325 	return std::error_condition(ev, std::system_category());
326       }
327     }
328   };
329 
330   const generic_error_category generic_category_instance{};
331   const system_error_category system_category_instance{};
332 }
333 
334 namespace std _GLIBCXX_VISIBILITY(default)
335 {
336 _GLIBCXX_BEGIN_NAMESPACE_VERSION
337 
338   void
__throw_system_error(int __i)339   __throw_system_error(int __i __attribute__((unused)))
340   {
341     _GLIBCXX_THROW_OR_ABORT(system_error(error_code(__i, generic_category())));
342   }
343 
344   error_category::~error_category() noexcept = default;
345 
346   const error_category&
system_category()347   _V2::system_category() noexcept { return system_category_instance; }
348 
349   const error_category&
generic_category()350   _V2::generic_category() noexcept { return generic_category_instance; }
351 
352   system_error::~system_error() noexcept = default;
353 
354   error_condition
default_error_condition(int __i) const355   error_category::default_error_condition(int __i) const noexcept
356   { return error_condition(__i, *this); }
357 
358   bool
equivalent(int __i,const error_condition & __cond) const359   error_category::equivalent(int __i,
360 			     const error_condition& __cond) const noexcept
361   { return default_error_condition(__i) == __cond; }
362 
363   bool
equivalent(const error_code & __code,int __i) const364   error_category::equivalent(const error_code& __code, int __i) const noexcept
365   { return *this == __code.category() && __code.value() == __i; }
366 
367   error_condition
default_error_condition() const368   error_code::default_error_condition() const noexcept
369   { return category().default_error_condition(value()); }
370 
371 #if _GLIBCXX_USE_CXX11_ABI
372   // Return error_category::message() as a COW string
373   __cow_string
_M_message(int i) const374   error_category::_M_message(int i) const
375   {
376     string msg = this->message(i);
377     return {msg.c_str(), msg.length()};
378   }
379 #endif
380 
381 _GLIBCXX_END_NAMESPACE_VERSION
382 } // namespace
383