NCBI C++ ToolKit
ncbi_strerror.c
Go to the documentation of this file.

Go to the SVN repository for this file.

1 /* $Id: ncbi_strerror.c 102521 2024-05-21 14:51:12Z ucko $
2  * ===========================================================================
3  *
4  * PUBLIC DOMAIN NOTICE
5  * National Center for Biotechnology Information
6  *
7  * This software/database is a "United States Government Work" under the
8  * terms of the United States Copyright Act. It was written as part of
9  * the author's official duties as a United States Government employee and
10  * thus cannot be copyrighted. This software/database is freely available
11  * to the public for use. The National Library of Medicine and the U.S.
12  * Government have not placed any restriction on its use or reproduction.
13  *
14  * Although all reasonable efforts have been taken to ensure the accuracy
15  * and reliability of the software and data, the NLM and the U.S.
16  * Government do not and cannot warrant the performance or results that
17  * may be obtained by using this software or data. The NLM and the U.S.
18  * Government disclaim all warranties, express or implied, including
19  * warranties of performance, merchantability or fitness for any particular
20  * purpose.
21  *
22  * Please cite the author in any work or product based on this material.
23  *
24  * ===========================================================================
25  *
26  * Authors: Pavel Ivanov, Anton Lavrentiev, Denis Vakatov
27  *
28  * File Description:
29  * errno->text conversion helper
30  */
31 
32 
33 #ifdef NCBI_INCLUDE_STRERROR_C
34 
35 
36 # ifdef _FREETDS_LIBRARY_SOURCE
37 # define s_StrError s_StrErrorInternal
38 # endif /*_FREETDS_LIBRARY_SOURCE*/
39 
40 
41 # if defined(NCBI_OS_MSWIN) && defined(_UNICODE)
42 
43 static const char* s_WinStrdup(const char* str)
44 {
45  size_t n = strlen(str);
46  char* s = (char*) LocalAlloc(LMEM_FIXED, ++n * sizeof(*s));
47  return s ? (const char*) memcpy(s, str, n) : 0;
48 }
49 # define ERR_STRDUP(s) s_WinStrdup(s)
50 
51 extern const char* UTIL_TcharToUtf8(const TCHAR* str)
52 {
53  char* s = NULL;
54  if (str) {
55  /* Note "-1" means to consume all input including the trailing NUL */
56  int n = WideCharToMultiByte(CP_UTF8, 0, str, -1, NULL, 0, NULL, NULL);
57  if (n > 0) {
58  s = (char*) LocalAlloc(LMEM_FIXED, n * sizeof(*s));
59  if (s)
60  WideCharToMultiByte(CP_UTF8, 0, str, -1, s, n, NULL, NULL);
61  }
62  }
63  return s;
64 }
65 
66 # ifndef UTIL_ReleaseBuffer
67 # define UTIL_ReleaseBuffer(x) UTIL_ReleaseBufferOnHeap(x)
68 # endif
69 
70 # else /*NCBI_OS_MSWIN && _UNICODE*/
71 
72 # define ERR_STRDUP(s) strdup(s)
73 
74 # ifdef UTIL_TcharToUtf8
75 # undef UTIL_TcharToUtf8
76 # endif
77 # define UTIL_TcharToUtf8(x) strdup(x)
78 
79 # ifdef UTIL_ReleaseBuffer
80 # undef UTIL_ReleaseBuffer
81 # endif
82 # define UTIL_ReleaseBuffer(x) free((void*)(x))
83 
84 # endif /*NCBI_OS_MSWIN && _UNICODE*/
85 
86 
87 # ifdef NCBI_OS_MSWIN
88 
89 extern void UTIL_ReleaseBufferOnHeap(const void* ptr)
90 {
91  if (ptr)
92  LocalFree((HLOCAL) ptr);
93 }
94 
95 # endif /*NCBI_OS_MSWIN*/
96 
97 
98 static const char* s_StrErrorInternal(int error)
99 {
100  static const struct {
101  int errnum;
102  const char* errstr;
103  } errmap[] = {
104 # ifdef NCBI_OS_MSWIN
105  {WSAEINTR, "Interrupted system call"},
106  {WSAEBADF, "Bad file number"},
107  {WSAEACCES, "Access denied"},
108  {WSAEFAULT, "Segmentation fault"},
109  {WSAEINVAL, "Invalid agrument"},
110  {WSAEMFILE, "Too many open files"},
111  /*
112  * Windows Sockets definitions of regular Berkeley error constants
113  */
114  {WSAEWOULDBLOCK, "Resource temporarily unavailable"},
115  {WSAEINPROGRESS, "Operation now in progress"},
116  {WSAEALREADY, "Operation already in progress"},
117  {WSAENOTSOCK, "Not a socket"},
118  {WSAEDESTADDRREQ, "Destination address required"},
119  {WSAEMSGSIZE, "Invalid message size"},
120  {WSAEPROTOTYPE, "Wrong protocol type"},
121  {WSAENOPROTOOPT, "Bad protocol option"},
122  {WSAEPROTONOSUPPORT, "Protocol not supported"},
123  {WSAESOCKTNOSUPPORT, "Socket type not supported"},
124  {WSAEOPNOTSUPP, "Operation not supported"},
125  {WSAEPFNOSUPPORT, "Protocol family not supported"},
126  {WSAEAFNOSUPPORT, "Address family not supported"},
127  {WSAEADDRINUSE, "Address already in use"},
128  {WSAEADDRNOTAVAIL, "Cannot assign requested address"},
129  {WSAENETDOWN, "Network is down"},
130  {WSAENETUNREACH, "Network is unreachable"},
131  {WSAENETRESET, "Connection dropped on network reset"},
132  {WSAECONNABORTED, "Software caused connection abort"},
133  {WSAECONNRESET, "Connection reset by peer"},
134  {WSAENOBUFS, "No buffer space available"},
135  {WSAEISCONN, "Socket is already connected"},
136  {WSAENOTCONN, "Socket is not connected"},
137  {WSAESHUTDOWN, "Cannot send after socket shutdown"},
138  {WSAETOOMANYREFS, "Too many references"},
139  {WSAETIMEDOUT, "Operation timed out"},
140  {WSAECONNREFUSED, "Connection refused"},
141  {WSAELOOP, "Infinite loop"},
142  {WSAENAMETOOLONG, "Name too long"},
143  {WSAEHOSTDOWN, "Host is down"},
144  {WSAEHOSTUNREACH, "Host unreachable"},
145  {WSAENOTEMPTY, "Not empty"},
146  {WSAEPROCLIM, "Too many processes"},
147  {WSAEUSERS, "Too many users"},
148  {WSAEDQUOT, "Quota exceeded"},
149  {WSAESTALE, "Stale descriptor"},
150  {WSAEREMOTE, "Remote error"},
151  /*
152  * Extended Windows Sockets error constant definitions
153  */
154  {WSASYSNOTREADY, "Network subsystem is unavailable"},
155  {WSAVERNOTSUPPORTED, "Winsock.dll version out of range"},
156  {WSANOTINITIALISED, "Not yet initialized"},
157  {WSAEDISCON, "Graceful shutdown in progress"},
158 # ifdef WSAENOMORE
159  /*NB: replaced with WSA_E_NO_MORE*/
160  {WSAENOMORE, "No more data available"},
161 # endif /*WSAENOMORE*/
162 # ifdef WSA_E_NO_MORE
163  {WSA_E_NO_MORE, "No more data available"},
164 # endif /*WSA_E_NO_MORE*/
165 # ifdef WSAECANCELLED
166  /*NB: replaced with WSA_E_CANCELLED*/
167  {WSAECANCELLED, "Call has been cancelled"},
168 # endif /*WSAECANCELLED*/
169 # ifdef WSA_E_CANCELLED
170  {WSA_E_CANCELLED, "Call has been cancelled"},
171 # endif /*WSA_E_CANCELLED*/
172  {WSAEINVALIDPROCTABLE, "Invalid procedure table"},
173  {WSAEINVALIDPROVIDER, "Invalid provider version number"},
174  {WSAEPROVIDERFAILEDINIT,"Cannot init provider"},
175  {WSASYSCALLFAILURE, "System call failed"},
176  {WSASERVICE_NOT_FOUND, "Service not found"},
177  {WSATYPE_NOT_FOUND, "Class type not found"},
178  {WSAEREFUSED, "Query refused"},
179  /*
180  * WinSock 2 extension
181  */
182 # ifdef WSA_IO_PENDING
183  {WSA_IO_PENDING, "Operation has been queued"},
184 # endif /*WSA_IO_PENDING*/
185 # ifdef WSA_IO_INCOMPLETE
186  {WSA_IO_INCOMPLETE, "Operation still in progress"},
187 # endif /*WSA_IO_INCOMPLETE*/
188 # ifdef WSA_INVALID_HANDLE
189  {WSA_INVALID_HANDLE, "Invalid handle"},
190 # endif /*WSA_INVALID_HANDLE*/
191 # ifdef WSA_INVALID_PARAMETER
192  {WSA_INVALID_PARAMETER, "Invalid parameter"},
193 # endif /*WSA_INVALID_PARAMETER*/
194 # ifdef WSA_NOT_ENOUGH_MEMORY
195  {WSA_NOT_ENOUGH_MEMORY, "Out of memory"},
196 # endif /*WSA_NOT_ENOUGH_MEMORY*/
197 # ifdef WSA_OPERATION_ABORTED
198  {WSA_OPERATION_ABORTED, "Operation aborted"},
199 # endif /*WSA_OPERATION_ABORTED*/
200 # endif /*NCBI_OS_MSWIN*/
201 # ifdef NCBI_OS_MSWIN
202 # define EAI_BASE 0
203 # else
204 # define EAI_BASE 100000
205 # endif /*NCBI_OS_MSWIN*/
206 # ifdef EAI_ADDRFAMILY
207  {EAI_ADDRFAMILY + EAI_BASE,
208  "Address family not supported"},
209 # endif /*EAI_ADDRFAMILY*/
210 # ifdef EAI_AGAIN
211  {EAI_AGAIN + EAI_BASE,
212  "Temporary failure in name resolution"},
213 # endif /*EAI_AGAIN*/
214 # ifdef EAI_BADFLAGS
215  {EAI_BADFLAGS + EAI_BASE,
216  "Invalid value for lookup flags"},
217 # endif /*EAI_BADFLAGS*/
218 # ifdef EAI_FAIL
219  {EAI_FAIL + EAI_BASE,
220  "Non-recoverable failure in name resolution"},
221 # endif /*EAI_FAIL*/
222 # ifdef EAI_FAMILY
223  {EAI_FAMILY + EAI_BASE,
224  "Address family not supported"},
225 # endif /*EAI_FAMILY*/
226 # ifdef EAI_MEMORY
227  {EAI_MEMORY + EAI_BASE,
228  "Memory allocation failure"},
229 # endif /*EAI_MEMORY*/
230 # ifdef EAI_NODATA
231 # if EAI_NODATA != EAI_NONAME
232  {EAI_NODATA + EAI_BASE,
233  "No address associated with nodename"},
234 # endif /*EAI_NODATA!=EAI_NONAME*/
235 # endif /*EAI_NODATA*/
236 # ifdef EAI_NONAME
237  {EAI_NONAME + EAI_BASE,
238  "Host and/or service name unknown"},
239 # endif /*EAI_NONAME*/
240 # ifdef EAI_OVERFLOW
241  {EAI_OVERFLOW + EAI_BASE,
242  "Argument buffer overflow"},
243 # endif /*EAI_OVERFLOW*/
244 # ifdef EAI_SERVICE
245  {EAI_SERVICE + EAI_BASE,
246  "Service name not supported for socket type"},
247 # endif /*EAI_SERVICE*/
248 # ifdef EAI_SOCKTYPE
249  {EAI_SOCKTYPE + EAI_BASE,
250  "Socket type not supported"},
251 # endif /*EAI_SOCKTYPE*/
252  /* GNU extensions */
253 # ifdef EAI_ALLDONE
254  {EAI_ALLDONE + EAI_BASE,
255  "All requests done"},
256 # endif /*EAI_ALLDONE*/
257 # ifdef EAI_CANCELED
258  {EAI_CANCELED + EAI_BASE,
259  "Request canceled"},
260 # endif /*EAI_BADFLAGS*/
261 # ifdef EAI_INPROGRESS
262  {EAI_INPROGRESS + EAI_BASE,
263  "Processing request in progress"},
264 # endif /*EAI_INPROGRESS*/
265 # ifdef EAI_INTR
266  {EAI_INTR + EAI_BASE,
267  "Interrupted by a signal"},
268 # endif /*EAI_INTR*/
269 # ifdef EAI_NOTCANCELED
270  {EAI_NOTCANCELED + EAI_BASE,
271  "Request not canceled"},
272 # endif /*EAI_NOTCANCELED*/
273 # ifdef EAI_IDN_ENCODE
274  {EAI_IDN_ENCODE + EAI_BASE,
275  "IDN encoding failed"},
276 # endif /*EAI_IDN_ENCODE*/
277 # ifdef NCBI_OS_MSWIN
278 # define DNS_BASE 0
279 # else
280 # define DNS_BASE 200000
281 # endif /*NCBI_OS_MSWIN*/
282 # ifdef HOST_NOT_FOUND
283  {HOST_NOT_FOUND + DNS_BASE,
284  "Host not found"},
285 # endif /*HOST_NOT_FOUND*/
286 # ifdef TRY_AGAIN
287  {TRY_AGAIN + DNS_BASE,
288  "DNS server failure"},
289 # endif /*TRY_AGAIN*/
290 # ifdef NO_RECOVERY
291  {NO_RECOVERY + DNS_BASE,
292  "Unrecoverable DNS error"},
293 # endif /*NO_RECOVERY*/
294 # ifdef NO_ADDRESS
295  {NO_ADDRESS + DNS_BASE,
296  "No address record found in DNS"},
297 # endif /*NO_ADDRESS*/
298 # ifdef NO_DATA
299  {NO_DATA + DNS_BASE,
300  "No DNS data of requested type"},
301 # endif /*NO_DATA*/
302 
303  /* Last dummy entry - must be present */
304  {0, 0}
305  };
306 #if defined(NCBI_OS_LINUX) || defined(NCBI_OS_CYGWIN)
307  /* To work correctly, descending order of offsets is required here */
308  static const struct {
309  int erroff;
310  const char* (*errfun)(int errnum);
311  } errsup[] = {
312 # ifdef __GLIBC__
313  { DNS_BASE, hstrerror },
314 # endif /*__GLIBC__*/
315 # if defined(HAVE_GETADDRINFO) || defined(HAVE_GETNAMEINFO)
316  { EAI_BASE, gai_strerror },
317 # endif /*HAVE_GETADDRINFO || HAVE_GETNAMEINFO*/
318  /* Last dummy entry - must present */
319  { 0, 0 }
320  };
321 #endif /*NCBI_OS_LINUX || NCBI_OS_CYGWIN*/
322  size_t i;
323 
324  if (!error)
325  return 0;
326 
327 #if defined(NCBI_OS_LINUX) || defined(NCBI_OS_CYGWIN)
328  for (i = 0; i < sizeof(errsup) / sizeof(errsup[0]) - 1/*dummy*/; ++i) {
329  if (errsup[i].erroff - 10000 < error
330  && error < errsup[i].erroff + 10000) {
331  const char* errstr = errsup[i].errfun(error - errsup[i].erroff);
332  if (errstr && *errstr && strncasecmp(errstr, "Unknown ", 8)!=0)
333  return ERR_STRDUP(errstr);
334  }
335  }
336 #endif /*NCBI_OS_LINUX || NCBI_OS_CYGWIN*/
337 
338  for (i = 0; i < sizeof(errmap) / sizeof(errmap[0]) - 1/*dummy*/; ++i) {
339  if (errmap[i].errnum == error)
340  return ERR_STRDUP(errmap[i].errstr);
341  }
342 
343 # if defined(NCBI_OS_MSWIN) && defined(_UNICODE)
344  return UTIL_TcharToUtf8(_wcserror(error));
345 # else
346  return ERR_STRDUP(strerror(error));
347 # endif /*NCBI_OS_MSWIN && _UNICODE*/
348 }
349 
350 
351 #endif /*NCBI_INCLUDE_STRERROR_C*/
static const char * str(char *buf, int n)
Definition: stats.c:84
#define NULL
Definition: ncbistd.hpp:225
void UTIL_ReleaseBufferOnHeap(const void *ptr)
#define UTIL_TcharToUtf8(x)
Definition: ncbi_util.h:807
int i
yy_size_t n
#define strncasecmp
char * strerror(int n)
Definition: pcregrep.c:835
char TCHAR
Definition: sqltypes.h:91
Modified on Sun Jun 16 04:34:27 2024 by modify_doxy.py rev. 669887