Tizen 2.1 base
[external/libgpg-error.git] / src / code-from-errno.c
1 /* code-from-errno.c - Mapping errnos to error codes.
2    Copyright (C) 2003 g10 Code GmbH
3
4    This file is part of libgpg-error.
5
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.
10  
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.
15  
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
19    02111-1307, USA.  */
20
21 #if HAVE_CONFIG_H
22 #include <config.h>
23 #endif
24
25 #include <errno.h> 
26 #ifdef HAVE_W32_SYSTEM
27 #include <winsock2.h>
28 #endif
29
30 #include <gpg-error.h>
31
32 #include "code-from-errno.h"
33
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. */
38 gpg_err_code_t
39 w32_special_errnos (int err)
40 {
41   switch (err)
42     {
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;
51     default: 
52       return GPG_ERR_UNKNOWN_ERRNO;
53     }
54 }
55 #endif /*HAVE_W32_SYSTEM*/
56
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
59    this).  */
60 gpg_err_code_t
61 gpg_err_code_from_errno (int err)
62 {
63   int idx;
64
65   if (!err)
66     return GPG_ERR_NO_ERROR;
67
68   idx = errno_to_idx (err);
69
70   if (idx < 0)
71     {
72 #ifdef HAVE_W32_SYSTEM
73       return w32_special_errnos (err);
74 #else
75       return GPG_ERR_UNKNOWN_ERRNO;
76 #endif
77     }
78
79   return GPG_ERR_SYSTEM_ERROR | err_code_from_index[idx];
80 }
81
82
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. */
86 gpg_err_code_t
87 gpg_err_code_from_syserror (void)
88 {
89   int err = errno;
90   int idx;
91
92   if (!err)
93     return GPG_ERR_MISSING_ERRNO;
94
95   idx = errno_to_idx (err);
96
97   if (idx < 0)
98     {
99 #ifdef HAVE_W32_SYSTEM
100       return w32_special_errnos (err);
101 #else
102       return GPG_ERR_UNKNOWN_ERRNO;
103 #endif
104     }
105
106   return GPG_ERR_SYSTEM_ERROR | err_code_from_index[idx];
107 }