File: ncbi_strerror.c

package info (click to toggle)
ncbi-tools6 6.1.20120620-8
  • links: PTS, VCS
  • area: main
  • in suites: jessie, jessie-kfreebsd
  • size: 241,628 kB
  • ctags: 101,236
  • sloc: ansic: 1,431,713; cpp: 6,248; pascal: 3,949; xml: 3,390; sh: 3,090; perl: 1,077; csh: 488; makefile: 449; ruby: 93; lisp: 81
file content (306 lines) | stat: -rw-r--r-- 10,891 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
/* $Id: ncbi_strerror.c,v 1.4 2011/11/04 14:24:30 kazimird Exp $
 * ===========================================================================
 *
 *                            PUBLIC DOMAIN NOTICE
 *               National Center for Biotechnology Information
 *
 *  This software/database is a "United States Government Work" under the
 *  terms of the United States Copyright Act.  It was written as part of
 *  the author's official duties as a United States Government employee and
 *  thus cannot be copyrighted.  This software/database is freely available
 *  to the public for use. The National Library of Medicine and the U.S.
 *  Government have not placed any restriction on its use or reproduction.
 *
 *  Although all reasonable efforts have been taken to ensure the accuracy
 *  and reliability of the software and data, the NLM and the U.S.
 *  Government do not and cannot warrant the performance or results that
 *  may be obtained by using this software or data. The NLM and the U.S.
 *  Government disclaim all warranties, express or implied, including
 *  warranties of performance, merchantability or fitness for any particular
 *  purpose.
 *
 *  Please cite the author in any work or product based on this material.
 *
 * ===========================================================================
 *
 * Authors:  Pavel Ivanov, Anton Lavrentiev, Denis Vakatov
 *
 * File Description:
 *   errno->text conversion helper
 */


#ifdef NCBI_INCLUDE_STRERROR_C


#  ifdef _FREETDS_LIBRARY_SOURCE

#    define s_WinStrdup               s_WinStrdup_ftds64
#    define s_StrErrorInternal        s_StrErrorInternal_ftds64
#    define s_StrError                s_StrErrorInternal
#    define UTIL_TcharToUtf8          UTIL_TcharToUtf8_ftds64
#    define UTIL_ReleaseBufferOnHeap  UTIL_ReleaseBufferOnHeap_ftds64

#  endif /*_FREETDS_LIBRARY_SOURCE*/


#  if defined(NCBI_OS_MSWIN)  &&  defined(_UNICODE)

static const char* s_WinStrdup(const char* s)
{
    size_t n = strlen(s);
    char*  p = (char*) LocalAlloc(LMEM_FIXED, ++n * sizeof(char));
    return p ? (const char*) memcpy(p, s, n) : 0;
}

#    define   MSWIN_STRDUP(s)         s_WinStrdup(s)

#    ifndef   UTIL_ReleaseBuffer
#      define UTIL_ReleaseBuffer(x)   UTIL_ReleaseBufferOnHeap(x)
#    endif

#  else /*NCBI_OS_MSWIN && _UNICODE*/

#    define   MSWIN_STRDUP(s)         (s)

#    ifndef   UTIL_TcharToUtf8
#      define UTIL_TcharToUtf8(x)     (x)
#    endif

#    ifndef   UTIL_ReleaseBuffer
#      define UTIL_ReleaseBuffer(x)   /*void*/
#    endif

#  endif /*NCBI_OS_MSWIN && _UNICODE*/


#  ifdef NCBI_OS_MSWIN


#    ifdef _UNICODE

extern const char* UTIL_TcharToUtf8(const TCHAR* buffer)
{
    char* p = NULL;
    if (buffer) {
        int n = WideCharToMultiByte(CP_UTF8, 0, buffer, -1, NULL,
            0, NULL, NULL);
        if (n >= 0) {
            p = (char*) LocalAlloc(LMEM_FIXED, (n + 1) * sizeof(char));
            if (p) {
                WideCharToMultiByte(CP_UTF8, 0, buffer, -1, p,
                    n, NULL, NULL);
                p[n] = '\0';
            }
        }
    }
    return p;
}

#    endif /*_UNICODE*/


extern void UTIL_ReleaseBufferOnHeap(const void* buffer)
{
    if (buffer)
        LocalFree((HLOCAL) buffer);
}


#  endif /*NCBI_OS_MSWIN*/


static const char* s_StrErrorInternal(int error)
{
    static const struct {
        int         errnum;
        const char* errtxt;
    } errmap[] = {
#  ifdef NCBI_OS_MSWIN
        {WSAEINTR,  "Interrupted system call"},
        {WSAEBADF,  "Bad file number"},
        {WSAEACCES, "Access denied"},
        {WSAEFAULT, "Segmentation fault"},
        {WSAEINVAL, "Invalid agrument"},
        {WSAEMFILE, "Too many open files"},
        /*
         * Windows Sockets definitions of regular Berkeley error constants
         */
        {WSAEWOULDBLOCK,     "Resource temporarily unavailable"},
        {WSAEINPROGRESS,     "Operation now in progress"},
        {WSAEALREADY,        "Operation already in progress"},
        {WSAENOTSOCK,        "Not a socket"},
        {WSAEDESTADDRREQ,    "Destination address required"},
        {WSAEMSGSIZE,        "Invalid message size"},
        {WSAEPROTOTYPE,      "Wrong protocol type"},
        {WSAENOPROTOOPT,     "Bad protocol option"},
        {WSAEPROTONOSUPPORT, "Protocol not supported"},
        {WSAESOCKTNOSUPPORT, "Socket type not supported"},
        {WSAEOPNOTSUPP,      "Operation not supported"},
        {WSAEPFNOSUPPORT,    "Protocol family not supported"},
        {WSAEAFNOSUPPORT,    "Address family not supported"},
        {WSAEADDRINUSE,      "Address already in use"},
        {WSAEADDRNOTAVAIL,   "Cannot assign requested address"},
        {WSAENETDOWN,        "Network is down"},
        {WSAENETUNREACH,     "Network is unreachable"},
        {WSAENETRESET,       "Connection dropped on network reset"},
        {WSAECONNABORTED,    "Software caused connection abort"},
        {WSAECONNRESET,      "Connection reset by peer"},
        {WSAENOBUFS,         "No buffer space available"},
        {WSAEISCONN,         "Socket is already connected"},
        {WSAENOTCONN,        "Socket is not connected"},
        {WSAESHUTDOWN,       "Cannot send after socket shutdown"},
        {WSAETOOMANYREFS,    "Too many references"},
        {WSAETIMEDOUT,       "Operation timed out"},
        {WSAECONNREFUSED,    "Connection refused"},
        {WSAELOOP,           "Infinite loop"},
        {WSAENAMETOOLONG,    "Name too long"},
        {WSAEHOSTDOWN,       "Host is down"},
        {WSAEHOSTUNREACH,    "Host unreachable"},
        {WSAENOTEMPTY,       "Not empty"},
        {WSAEPROCLIM,        "Too many processes"},
        {WSAEUSERS,          "Too many users"},
        {WSAEDQUOT,          "Quota exceeded"},
        {WSAESTALE,          "Stale descriptor"},
        {WSAEREMOTE,         "Remote error"},
        /*
         * Extended Windows Sockets error constant definitions
         */
        {WSASYSNOTREADY,         "Network subsystem is unavailable"},
        {WSAVERNOTSUPPORTED,     "Winsock.dll version out of range"},
        {WSANOTINITIALISED,      "Not yet initialized"},
        {WSAEDISCON,             "Graceful shutdown in progress"},
#    ifdef WSAENOMORE
        /*NB: replaced with WSA_E_NO_MORE*/
        {WSAENOMORE,             "No more data available"},
#    endif /*WSAENOMORE*/
#    ifdef WSA_E_NO_MORE
        {WSA_E_NO_MORE,          "No more data available"},
#    endif /*WSA_E_NO_MORE*/
#    ifdef WSAECANCELLED
        /*NB: replaced with WSA_E_CANCELLED*/
        {WSAECANCELLED,          "Call has been cancelled"},
#    endif /*WSAECANCELLED*/
#    ifdef WSA_E_CANCELLED
        {WSA_E_CANCELLED,        "Call has been cancelled"},
#    endif /*WSA_E_CANCELLED*/
        {WSAEINVALIDPROCTABLE,   "Invalid procedure table"},
        {WSAEINVALIDPROVIDER,    "Invalid provider version number"},
        {WSAEPROVIDERFAILEDINIT, "Cannot init provider"},
        {WSASYSCALLFAILURE,      "System call failed"},
        {WSASERVICE_NOT_FOUND,   "Service not found"},
        {WSATYPE_NOT_FOUND,      "Class type not found"},
        {WSAEREFUSED,            "Query refused"},
        /*
         * WinSock 2 extension
         */
#    ifdef WSA_IO_PENDING
        {WSA_IO_PENDING,         "Operation has been queued"},
#    endif /*WSA_IO_PENDING*/
#    ifdef WSA_IO_INCOMPLETE
        {WSA_IO_INCOMPLETE,      "Operation still in progress"},
#    endif /*WSA_IO_INCOMPLETE*/
#    ifdef WSA_INVALID_HANDLE
        {WSA_INVALID_HANDLE,     "Invalid handle"},
#    endif /*WSA_INVALID_HANDLE*/
#    ifdef WSA_INVALID_PARAMETER
        {WSA_INVALID_PARAMETER,  "Invalid parameter"},
#    endif /*WSA_INVALID_PARAMETER*/
#    ifdef WSA_NOT_ENOUGH_MEMORY
        {WSA_NOT_ENOUGH_MEMORY,  "Out of memory"},
#    endif /*WSA_NOT_ENOUGH_MEMORY*/
#    ifdef WSA_OPERATION_ABORTED
        {WSA_OPERATION_ABORTED,  "Operation aborted"},
#    endif /*WSA_OPERATION_ABORTED*/
#  endif /*NCBI_OS_MSWIN*/
#  ifdef NCBI_OS_MSWIN
#    define EAI_BASE 0
#  else
#    define EAI_BASE 100000
#  endif /*NCBI_OS_MSWIN*/
#  ifdef EAI_ADDRFAMILY
        {EAI_ADDRFAMILY + EAI_BASE,
                                 "Address family not supported"},
#  endif /*EAI_ADDRFAMILY*/
#  ifdef EAI_AGAIN
        {EAI_AGAIN + EAI_BASE,
                                 "Temporary failure in name resolution"},
#  endif /*EAI_AGAIN*/
#  ifdef EAI_BADFLAGS
        {EAI_BADFLAGS + EAI_BASE,
                                 "Invalid value for lookup flags"},
#  endif /*EAI_BADFLAGS*/
#  ifdef EAI_FAIL
        {EAI_FAIL + EAI_BASE,
                                 "Non-recoverable failure in name resolution"},
#  endif /*EAI_FAIL*/
#  ifdef EAI_FAMILY
        {EAI_FAMILY + EAI_BASE,
                                 "Address family not supported"},
#  endif /*EAI_FAMILY*/
#  ifdef EAI_MEMORY
        {EAI_MEMORY + EAI_BASE,
                                 "Memory allocation failure"},
#  endif /*EAI_MEMORY*/
#  ifdef EAI_NODATA
        {EAI_NODATA + EAI_BASE,
                                 "No address associated with nodename"},
#  endif /*EAI_NODATA*/
#  ifdef EAI_NONAME
        {EAI_NONAME + EAI_BASE,
                                 "Host/service name not known"},
#  endif /*EAI_NONAME*/
#  ifdef EAI_SERVICE
        {EAI_SERVICE + EAI_BASE,
                                 "Service name not supported for socket type"},
#  endif /*EAI_SERVICE*/
#  ifdef EAI_SOCKTYPE
        {EAI_SOCKTYPE + EAI_BASE,
                                 "Socket type not supported"},
#  endif /*EAI_SOCKTYPE*/
#  ifdef NCBI_OS_MSWIN
#    define DNS_BASE 0
#  else
#    define DNS_BASE 200000
#  endif /*NCBI_OS_MSWIN*/
#  ifdef HOST_NOT_FOUND
        {HOST_NOT_FOUND + DNS_BASE,
                                 "Host not found"},
#  endif /*HOST_NOT_FOUND*/
#  ifdef TRY_AGAIN
        {TRY_AGAIN + DNS_BASE,
                                 "DNS server failure"},
#  endif /*TRY_AGAIN*/
#  ifdef NO_RECOVERY
        {NO_RECOVERY + DNS_BASE,
                                 "Unrecoverable DNS error"},
#  endif /*NO_RECOVERY*/
#  ifdef NO_ADDRESS
        {NO_ADDRESS + DNS_BASE,
                                 "No address record found in DNS"},
#  endif /*NO_ADDRESS*/
#  ifdef NO_DATA
        {NO_DATA + DNS_BASE,
                                 "No DNS data of requested type"},
#  endif /*NO_DATA*/

        /* Last dummy entry - must present */
        {0, 0}
    };
    size_t i;

    if (!error)
        return 0;
    for (i = 0;  i < sizeof(errmap) / sizeof(errmap[0]) - 1/*dummy*/;  i++) {
        if (errmap[i].errnum == error)
            return MSWIN_STRDUP(errmap[i].errtxt);
    }
#  if defined(NCBI_OS_MSWIN)  &&  defined(_UNICODE)
    return UTIL_TcharToUtf8(error > 0 ? _wcserror(error) : L"");
#  else
    return error > 0 ? strerror(error) : "";
#  endif /*NCBI_OS_MSWIN && _UNICODE*/
}


#endif /*NCBI_INCLUDE_STRERROR_C*/