1 /* code-from-errno.c - Mapping errnos to error codes.
2 Copyright (C) 2003 g10 Code GmbH
4 This file is part of libgpg-error.
6 libgpg-error is free software; you can redistribute it and/or
7 modify it under the terms of the GNU Lesser General Public License
8 as published by the Free Software Foundation; either version 2.1 of
9 the License, or (at your option) any later version.
11 libgpg-error is distributed in the hope that it will be useful, but
12 WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 Lesser General Public License for more details.
16 You should have received a copy of the GNU Lesser General Public
17 License along with libgpg-error; if not, write to the Free
18 Software Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA
26 #ifdef HAVE_W32_SYSTEM
30 #include <gpg-error.h>
32 #include "code-from-errno.h"
34 #ifdef HAVE_W32_SYSTEM
35 /* Under Windows socket related error codes are defined in a different
36 file and prefixed with "WSA". As their ranges don't overlap, we map
37 some of them to our usual error codes. */
39 w32_special_errnos (int err)
43 case WSAEADDRINUSE: return GPG_ERR_EADDRINUSE;
44 case WSAEADDRNOTAVAIL: return GPG_ERR_EADDRNOTAVAIL;
45 case WSAECONNABORTED: return GPG_ERR_ECONNABORTED;
46 case WSAECONNREFUSED: return GPG_ERR_ECONNREFUSED;
47 case WSAECONNRESET: return GPG_ERR_ECONNRESET;
48 case WSAENAMETOOLONG: return GPG_ERR_ENAMETOOLONG;
49 case WSAEHOSTDOWN: return GPG_ERR_EHOSTDOWN;
50 case WSAEHOSTUNREACH: return GPG_ERR_EHOSTUNREACH;
52 return GPG_ERR_UNKNOWN_ERRNO;
55 #endif /*HAVE_W32_SYSTEM*/
57 /* Retrieve the error code for the system error ERR. This returns
58 GPG_ERR_UNKNOWN_ERRNO if the system error is not mapped (report
61 gpg_err_code_from_errno (int err)
66 return GPG_ERR_NO_ERROR;
68 idx = errno_to_idx (err);
72 #ifdef HAVE_W32_SYSTEM
73 return w32_special_errnos (err);
75 return GPG_ERR_UNKNOWN_ERRNO;
79 return GPG_ERR_SYSTEM_ERROR | err_code_from_index[idx];
83 /* Retrieve the error code directly from the ERRNO variable. This
84 returns GPG_ERR_UNKNOWN_ERRNO if the system error is not mapped
85 (report this) and GPG_ERR_MISSING_ERRNO if ERRNO has the value 0. */
87 gpg_err_code_from_syserror (void)
93 return GPG_ERR_MISSING_ERRNO;
95 idx = errno_to_idx (err);
99 #ifdef HAVE_W32_SYSTEM
100 return w32_special_errnos (err);
102 return GPG_ERR_UNKNOWN_ERRNO;
106 return GPG_ERR_SYSTEM_ERROR | err_code_from_index[idx];