strerror.c 8.8 KB

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