strerror.c 8.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347
  1. /* strerror.c --- POSIX compatible system error routine
  2. Copyright (C) 2007-2009 Free Software Foundation, Inc.
  3. This program is free software: you can redistribute it and/or modify
  4. it under the terms of the GNU General Public License as published by
  5. the Free Software Foundation; either version 3 of the License, or
  6. (at your option) any later version.
  7. This program is distributed in the hope that it will be useful,
  8. but WITHOUT ANY WARRANTY; without even the implied warranty of
  9. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  10. GNU General Public License for more details.
  11. You should have received a copy of the GNU General Public License
  12. along with this program. If not, see <http://www.gnu.org/licenses/>. */
  13. #include <config.h>
  14. #include <string.h>
  15. #if REPLACE_STRERROR
  16. # include <errno.h>
  17. # include <stdio.h>
  18. # if GNULIB_defined_ESOCK /* native Windows platforms */
  19. # if HAVE_WINSOCK2_H
  20. # include <winsock2.h>
  21. # endif
  22. # endif
  23. # include "intprops.h"
  24. # undef strerror
  25. # if ! HAVE_DECL_STRERROR
  26. # define strerror(n) NULL
  27. # endif
  28. char *
  29. rpl_strerror (int n)
  30. {
  31. char const *msg = NULL;
  32. /* These error messages are taken from glibc/sysdeps/gnu/errlist.c. */
  33. switch (n)
  34. {
  35. # if GNULIB_defined_ETXTBSY
  36. case ETXTBSY:
  37. msg = "Text file busy";
  38. break;
  39. # endif
  40. # if GNULIB_defined_ESOCK /* native Windows platforms */
  41. /* EWOULDBLOCK is the same as EAGAIN. */
  42. case EINPROGRESS:
  43. msg = "Operation now in progress";
  44. break;
  45. case EALREADY:
  46. msg = "Operation already in progress";
  47. break;
  48. case ENOTSOCK:
  49. msg = "Socket operation on non-socket";
  50. break;
  51. case EDESTADDRREQ:
  52. msg = "Destination address required";
  53. break;
  54. case EMSGSIZE:
  55. msg = "Message too long";
  56. break;
  57. case EPROTOTYPE:
  58. msg = "Protocol wrong type for socket";
  59. break;
  60. case ENOPROTOOPT:
  61. msg = "Protocol not available";
  62. break;
  63. case EPROTONOSUPPORT:
  64. msg = "Protocol not supported";
  65. break;
  66. case ESOCKTNOSUPPORT:
  67. msg = "Socket type not supported";
  68. break;
  69. case EOPNOTSUPP:
  70. msg = "Operation not supported";
  71. break;
  72. case EPFNOSUPPORT:
  73. msg = "Protocol family not supported";
  74. break;
  75. case EAFNOSUPPORT:
  76. msg = "Address family not supported by protocol";
  77. break;
  78. case EADDRINUSE:
  79. msg = "Address already in use";
  80. break;
  81. case EADDRNOTAVAIL:
  82. msg = "Cannot assign requested address";
  83. break;
  84. case ENETDOWN:
  85. msg = "Network is down";
  86. break;
  87. case ENETUNREACH:
  88. msg = "Network is unreachable";
  89. break;
  90. case ENETRESET:
  91. msg = "Network dropped connection on reset";
  92. break;
  93. case ECONNABORTED:
  94. msg = "Software caused connection abort";
  95. break;
  96. case ECONNRESET:
  97. msg = "Connection reset by peer";
  98. break;
  99. case ENOBUFS:
  100. msg = "No buffer space available";
  101. break;
  102. case EISCONN:
  103. msg = "Transport endpoint is already connected";
  104. break;
  105. case ENOTCONN:
  106. msg = "Transport endpoint is not connected";
  107. break;
  108. case ESHUTDOWN:
  109. msg = "Cannot send after transport endpoint shutdown";
  110. break;
  111. case ETOOMANYREFS:
  112. msg = "Too many references: cannot splice";
  113. break;
  114. case ETIMEDOUT:
  115. msg = "Connection timed out";
  116. break;
  117. case ECONNREFUSED:
  118. msg = "Connection refused";
  119. break;
  120. case ELOOP:
  121. msg = "Too many levels of symbolic links";
  122. break;
  123. case EHOSTDOWN:
  124. msg = "Host is down";
  125. break;
  126. case EHOSTUNREACH:
  127. msg = "No route to host";
  128. break;
  129. case EPROCLIM:
  130. msg = "Too many processes";
  131. break;
  132. case EUSERS:
  133. msg = "Too many users";
  134. break;
  135. case EDQUOT:
  136. msg = "Disk quota exceeded";
  137. break;
  138. case ESTALE:
  139. msg = "Stale NFS file handle";
  140. break;
  141. case EREMOTE:
  142. msg = "Object is remote";
  143. break;
  144. # if HAVE_WINSOCK2_H
  145. /* WSA_INVALID_HANDLE maps to EBADF */
  146. /* WSA_NOT_ENOUGH_MEMORY maps to ENOMEM */
  147. /* WSA_INVALID_PARAMETER maps to EINVAL */
  148. case WSA_OPERATION_ABORTED:
  149. msg = "Overlapped operation aborted";
  150. break;
  151. case WSA_IO_INCOMPLETE:
  152. msg = "Overlapped I/O event object not in signaled state";
  153. break;
  154. case WSA_IO_PENDING:
  155. msg = "Overlapped operations will complete later";
  156. break;
  157. /* WSAEINTR maps to EINTR */
  158. /* WSAEBADF maps to EBADF */
  159. /* WSAEACCES maps to EACCES */
  160. /* WSAEFAULT maps to EFAULT */
  161. /* WSAEINVAL maps to EINVAL */
  162. /* WSAEMFILE maps to EMFILE */
  163. /* WSAEWOULDBLOCK maps to EWOULDBLOCK */
  164. /* WSAEINPROGRESS is EINPROGRESS */
  165. /* WSAEALREADY is EALREADY */
  166. /* WSAENOTSOCK is ENOTSOCK */
  167. /* WSAEDESTADDRREQ is EDESTADDRREQ */
  168. /* WSAEMSGSIZE is EMSGSIZE */
  169. /* WSAEPROTOTYPE is EPROTOTYPE */
  170. /* WSAENOPROTOOPT is ENOPROTOOPT */
  171. /* WSAEPROTONOSUPPORT is EPROTONOSUPPORT */
  172. /* WSAESOCKTNOSUPPORT is ESOCKTNOSUPPORT */
  173. /* WSAEOPNOTSUPP is EOPNOTSUPP */
  174. /* WSAEPFNOSUPPORT is EPFNOSUPPORT */
  175. /* WSAEAFNOSUPPORT is EAFNOSUPPORT */
  176. /* WSAEADDRINUSE is EADDRINUSE */
  177. /* WSAEADDRNOTAVAIL is EADDRNOTAVAIL */
  178. /* WSAENETDOWN is ENETDOWN */
  179. /* WSAENETUNREACH is ENETUNREACH */
  180. /* WSAENETRESET is ENETRESET */
  181. /* WSAECONNABORTED is ECONNABORTED */
  182. /* WSAECONNRESET is ECONNRESET */
  183. /* WSAENOBUFS is ENOBUFS */
  184. /* WSAEISCONN is EISCONN */
  185. /* WSAENOTCONN is ENOTCONN */
  186. /* WSAESHUTDOWN is ESHUTDOWN */
  187. /* WSAETOOMANYREFS is ETOOMANYREFS */
  188. /* WSAETIMEDOUT is ETIMEDOUT */
  189. /* WSAECONNREFUSED is ECONNREFUSED */
  190. /* WSAELOOP is ELOOP */
  191. /* WSAENAMETOOLONG maps to ENAMETOOLONG */
  192. /* WSAEHOSTDOWN is EHOSTDOWN */
  193. /* WSAEHOSTUNREACH is EHOSTUNREACH */
  194. /* WSAENOTEMPTY maps to ENOTEMPTY */
  195. /* WSAEPROCLIM is EPROCLIM */
  196. /* WSAEUSERS is EUSERS */
  197. /* WSAEDQUOT is EDQUOT */
  198. /* WSAESTALE is ESTALE */
  199. /* WSAEREMOTE is EREMOTE */
  200. case WSASYSNOTREADY:
  201. msg = "Network subsystem is unavailable";
  202. break;
  203. case WSAVERNOTSUPPORTED:
  204. msg = "Winsock.dll version out of range";
  205. break;
  206. case WSANOTINITIALISED:
  207. msg = "Successful WSAStartup not yet performed";
  208. break;
  209. case WSAEDISCON:
  210. msg = "Graceful shutdown in progress";
  211. break;
  212. case WSAENOMORE: case WSA_E_NO_MORE:
  213. msg = "No more results";
  214. break;
  215. case WSAECANCELLED: case WSA_E_CANCELLED:
  216. msg = "Call was canceled";
  217. break;
  218. case WSAEINVALIDPROCTABLE:
  219. msg = "Procedure call table is invalid";
  220. break;
  221. case WSAEINVALIDPROVIDER:
  222. msg = "Service provider is invalid";
  223. break;
  224. case WSAEPROVIDERFAILEDINIT:
  225. msg = "Service provider failed to initialize";
  226. break;
  227. case WSASYSCALLFAILURE:
  228. msg = "System call failure";
  229. break;
  230. case WSASERVICE_NOT_FOUND:
  231. msg = "Service not found";
  232. break;
  233. case WSATYPE_NOT_FOUND:
  234. msg = "Class type not found";
  235. break;
  236. case WSAEREFUSED:
  237. msg = "Database query was refused";
  238. break;
  239. case WSAHOST_NOT_FOUND:
  240. msg = "Host not found";
  241. break;
  242. case WSATRY_AGAIN:
  243. msg = "Nonauthoritative host not found";
  244. break;
  245. case WSANO_RECOVERY:
  246. msg = "Nonrecoverable error";
  247. break;
  248. case WSANO_DATA:
  249. msg = "Valid name, no data record of requested type";
  250. break;
  251. /* WSA_QOS_* omitted */
  252. # endif
  253. # endif
  254. # if GNULIB_defined_ENOMSG
  255. case ENOMSG:
  256. msg = "No message of desired type";
  257. break;
  258. # endif
  259. # if GNULIB_defined_EIDRM
  260. case EIDRM:
  261. msg = "Identifier removed";
  262. break;
  263. # endif
  264. # if GNULIB_defined_ENOLINK
  265. case ENOLINK:
  266. msg = "Link has been severed";
  267. break;
  268. # endif
  269. # if GNULIB_defined_EPROTO
  270. case EPROTO:
  271. msg = "Protocol error";
  272. break;
  273. # endif
  274. # if GNULIB_defined_EMULTIHOP
  275. case EMULTIHOP:
  276. msg = "Multihop attempted";
  277. break;
  278. # endif
  279. # if GNULIB_defined_EBADMSG
  280. case EBADMSG:
  281. msg = "Bad message";
  282. break;
  283. # endif
  284. # if GNULIB_defined_EOVERFLOW
  285. case EOVERFLOW:
  286. msg = "Value too large for defined data type";
  287. break;
  288. # endif
  289. # if GNULIB_defined_ENOTSUP
  290. case ENOTSUP:
  291. msg = "Not supported";
  292. break;
  293. # endif
  294. # if GNULIB_defined_ESTALE
  295. case ESTALE:
  296. msg = "Stale NFS file handle";
  297. break;
  298. # endif
  299. # if GNULIB_defined_ECANCELED
  300. case ECANCELED:
  301. msg = "Operation canceled";
  302. break;
  303. # endif
  304. }
  305. if (msg)
  306. return (char *) msg;
  307. {
  308. char *result = strerror (n);
  309. if (result == NULL || result[0] == '\0')
  310. {
  311. static char const fmt[] = "Unknown error (%d)";
  312. static char msg_buf[sizeof fmt + INT_STRLEN_BOUND (n)];
  313. sprintf (msg_buf, fmt, n);
  314. return msg_buf;
  315. }
  316. return result;
  317. }
  318. }
  319. #endif