strerror.c 8.6 KB
Newer Older
J
Jim Meyering 已提交
1 2
/* strerror.c --- POSIX compatible system error routine

3
   Copyright (C) 2007-2009 Free Software Foundation, Inc.
J
Jim Meyering 已提交
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

   This program is free software: you can redistribute it and/or modify
   it under the terms of the GNU Lesser General Public License as published by
   the Free Software Foundation; either version 2.1 of the License, or
   (at your option) any later version.

   This program is distributed in the hope that it will be useful,
   but WITHOUT ANY WARRANTY; without even the implied warranty of
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
   GNU Lesser General Public License for more details.

   You should have received a copy of the GNU Lesser General Public License
   along with this program.  If not, see <http://www.gnu.org/licenses/>.  */

#include <config.h>

#include <string.h>

#if REPLACE_STRERROR

# include <errno.h>
# include <stdio.h>

# if GNULIB_defined_ESOCK /* native Windows platforms */
#  if HAVE_WINSOCK2_H
#   include <winsock2.h>
#  endif
# endif

# include "intprops.h"

# undef strerror
# if ! HAVE_DECL_STRERROR
#  define strerror(n) NULL
# endif

char *
rpl_strerror (int n)
{
43
  char const *msg = NULL;
J
Jim Meyering 已提交
44 45 46 47 48
  /* These error messages are taken from glibc/sysdeps/gnu/errlist.c.  */
  switch (n)
    {
# if GNULIB_defined_ETXTBSY
    case ETXTBSY:
49 50
      msg = "Text file busy";
      break;
J
Jim Meyering 已提交
51 52 53 54 55
# endif

# if GNULIB_defined_ESOCK /* native Windows platforms */
    /* EWOULDBLOCK is the same as EAGAIN.  */
    case EINPROGRESS:
56 57
      msg = "Operation now in progress";
      break;
J
Jim Meyering 已提交
58
    case EALREADY:
59 60
      msg = "Operation already in progress";
      break;
J
Jim Meyering 已提交
61
    case ENOTSOCK:
62 63
      msg = "Socket operation on non-socket";
      break;
J
Jim Meyering 已提交
64
    case EDESTADDRREQ:
65 66
      msg = "Destination address required";
      break;
J
Jim Meyering 已提交
67
    case EMSGSIZE:
68 69
      msg = "Message too long";
      break;
J
Jim Meyering 已提交
70
    case EPROTOTYPE:
71 72
      msg = "Protocol wrong type for socket";
      break;
J
Jim Meyering 已提交
73
    case ENOPROTOOPT:
74 75
      msg = "Protocol not available";
      break;
J
Jim Meyering 已提交
76
    case EPROTONOSUPPORT:
77 78
      msg = "Protocol not supported";
      break;
J
Jim Meyering 已提交
79
    case ESOCKTNOSUPPORT:
80 81
      msg = "Socket type not supported";
      break;
J
Jim Meyering 已提交
82
    case EOPNOTSUPP:
83 84
      msg = "Operation not supported";
      break;
J
Jim Meyering 已提交
85
    case EPFNOSUPPORT:
86 87
      msg = "Protocol family not supported";
      break;
J
Jim Meyering 已提交
88
    case EAFNOSUPPORT:
89 90
      msg = "Address family not supported by protocol";
      break;
J
Jim Meyering 已提交
91
    case EADDRINUSE:
92 93
      msg = "Address already in use";
      break;
J
Jim Meyering 已提交
94
    case EADDRNOTAVAIL:
95 96
      msg = "Cannot assign requested address";
      break;
J
Jim Meyering 已提交
97
    case ENETDOWN:
98 99
      msg = "Network is down";
      break;
J
Jim Meyering 已提交
100
    case ENETUNREACH:
101 102
      msg = "Network is unreachable";
      break;
J
Jim Meyering 已提交
103
    case ENETRESET:
104 105
      msg = "Network dropped connection on reset";
      break;
J
Jim Meyering 已提交
106
    case ECONNABORTED:
107 108
      msg = "Software caused connection abort";
      break;
J
Jim Meyering 已提交
109
    case ECONNRESET:
110 111
      msg = "Connection reset by peer";
      break;
J
Jim Meyering 已提交
112
    case ENOBUFS:
113 114
      msg = "No buffer space available";
      break;
J
Jim Meyering 已提交
115
    case EISCONN:
116 117
      msg = "Transport endpoint is already connected";
      break;
J
Jim Meyering 已提交
118
    case ENOTCONN:
119 120
      msg = "Transport endpoint is not connected";
      break;
J
Jim Meyering 已提交
121
    case ESHUTDOWN:
122 123
      msg = "Cannot send after transport endpoint shutdown";
      break;
J
Jim Meyering 已提交
124
    case ETOOMANYREFS:
125 126
      msg = "Too many references: cannot splice";
      break;
J
Jim Meyering 已提交
127
    case ETIMEDOUT:
128 129
      msg = "Connection timed out";
      break;
J
Jim Meyering 已提交
130
    case ECONNREFUSED:
131 132
      msg = "Connection refused";
      break;
J
Jim Meyering 已提交
133
    case ELOOP:
134 135
      msg = "Too many levels of symbolic links";
      break;
J
Jim Meyering 已提交
136
    case EHOSTDOWN:
137 138
      msg = "Host is down";
      break;
J
Jim Meyering 已提交
139
    case EHOSTUNREACH:
140 141
      msg = "No route to host";
      break;
J
Jim Meyering 已提交
142
    case EPROCLIM:
143 144
      msg = "Too many processes";
      break;
J
Jim Meyering 已提交
145
    case EUSERS:
146 147
      msg = "Too many users";
      break;
J
Jim Meyering 已提交
148
    case EDQUOT:
149 150
      msg = "Disk quota exceeded";
      break;
J
Jim Meyering 已提交
151
    case ESTALE:
152 153
      msg = "Stale NFS file handle";
      break;
J
Jim Meyering 已提交
154
    case EREMOTE:
155 156
      msg = "Object is remote";
      break;
J
Jim Meyering 已提交
157 158 159 160 161
#  if HAVE_WINSOCK2_H
    /* WSA_INVALID_HANDLE maps to EBADF */
    /* WSA_NOT_ENOUGH_MEMORY maps to ENOMEM */
    /* WSA_INVALID_PARAMETER maps to EINVAL */
    case WSA_OPERATION_ABORTED:
162 163
      msg = "Overlapped operation aborted";
      break;
J
Jim Meyering 已提交
164
    case WSA_IO_INCOMPLETE:
165 166
      msg = "Overlapped I/O event object not in signaled state";
      break;
J
Jim Meyering 已提交
167
    case WSA_IO_PENDING:
168 169
      msg = "Overlapped operations will complete later";
      break;
J
Jim Meyering 已提交
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
    /* WSAEINTR maps to EINTR */
    /* WSAEBADF maps to EBADF */
    /* WSAEACCES maps to EACCES */
    /* WSAEFAULT maps to EFAULT */
    /* WSAEINVAL maps to EINVAL */
    /* WSAEMFILE maps to EMFILE */
    /* WSAEWOULDBLOCK maps to EWOULDBLOCK */
    /* WSAEINPROGRESS is EINPROGRESS */
    /* WSAEALREADY is EALREADY */
    /* WSAENOTSOCK is ENOTSOCK */
    /* WSAEDESTADDRREQ is EDESTADDRREQ */
    /* WSAEMSGSIZE is EMSGSIZE */
    /* WSAEPROTOTYPE is EPROTOTYPE */
    /* WSAENOPROTOOPT is ENOPROTOOPT */
    /* WSAEPROTONOSUPPORT is EPROTONOSUPPORT */
    /* WSAESOCKTNOSUPPORT is ESOCKTNOSUPPORT */
    /* WSAEOPNOTSUPP is EOPNOTSUPP */
    /* WSAEPFNOSUPPORT is EPFNOSUPPORT */
    /* WSAEAFNOSUPPORT is EAFNOSUPPORT */
    /* WSAEADDRINUSE is EADDRINUSE */
    /* WSAEADDRNOTAVAIL is EADDRNOTAVAIL */
    /* WSAENETDOWN is ENETDOWN */
    /* WSAENETUNREACH is ENETUNREACH */
    /* WSAENETRESET is ENETRESET */
    /* WSAECONNABORTED is ECONNABORTED */
    /* WSAECONNRESET is ECONNRESET */
    /* WSAENOBUFS is ENOBUFS */
    /* WSAEISCONN is EISCONN */
    /* WSAENOTCONN is ENOTCONN */
    /* WSAESHUTDOWN is ESHUTDOWN */
    /* WSAETOOMANYREFS is ETOOMANYREFS */
    /* WSAETIMEDOUT is ETIMEDOUT */
    /* WSAECONNREFUSED is ECONNREFUSED */
    /* WSAELOOP is ELOOP */
    /* WSAENAMETOOLONG maps to ENAMETOOLONG */
    /* WSAEHOSTDOWN is EHOSTDOWN */
    /* WSAEHOSTUNREACH is EHOSTUNREACH */
    /* WSAENOTEMPTY maps to ENOTEMPTY */
    /* WSAEPROCLIM is EPROCLIM */
    /* WSAEUSERS is EUSERS */
    /* WSAEDQUOT is EDQUOT */
    /* WSAESTALE is ESTALE */
    /* WSAEREMOTE is EREMOTE */
    case WSASYSNOTREADY:
214 215
      msg = "Network subsystem is unavailable";
      break;
J
Jim Meyering 已提交
216
    case WSAVERNOTSUPPORTED:
217 218
      msg = "Winsock.dll version out of range";
      break;
J
Jim Meyering 已提交
219
    case WSANOTINITIALISED:
220 221
      msg = "Successful WSAStartup not yet performed";
      break;
J
Jim Meyering 已提交
222
    case WSAEDISCON:
223 224
      msg = "Graceful shutdown in progress";
      break;
J
Jim Meyering 已提交
225
    case WSAENOMORE: case WSA_E_NO_MORE:
226 227
      msg = "No more results";
      break;
J
Jim Meyering 已提交
228
    case WSAECANCELLED: case WSA_E_CANCELLED:
229 230
      msg = "Call was canceled";
      break;
J
Jim Meyering 已提交
231
    case WSAEINVALIDPROCTABLE:
232 233
      msg = "Procedure call table is invalid";
      break;
J
Jim Meyering 已提交
234
    case WSAEINVALIDPROVIDER:
235 236
      msg = "Service provider is invalid";
      break;
J
Jim Meyering 已提交
237
    case WSAEPROVIDERFAILEDINIT:
238 239
      msg = "Service provider failed to initialize";
      break;
J
Jim Meyering 已提交
240
    case WSASYSCALLFAILURE:
241 242
      msg = "System call failure";
      break;
J
Jim Meyering 已提交
243
    case WSASERVICE_NOT_FOUND:
244 245
      msg = "Service not found";
      break;
J
Jim Meyering 已提交
246
    case WSATYPE_NOT_FOUND:
247 248
      msg = "Class type not found";
      break;
J
Jim Meyering 已提交
249
    case WSAEREFUSED:
250 251
      msg = "Database query was refused";
      break;
J
Jim Meyering 已提交
252
    case WSAHOST_NOT_FOUND:
253 254
      msg = "Host not found";
      break;
J
Jim Meyering 已提交
255
    case WSATRY_AGAIN:
256 257
      msg = "Nonauthoritative host not found";
      break;
J
Jim Meyering 已提交
258
    case WSANO_RECOVERY:
259 260
      msg = "Nonrecoverable error";
      break;
J
Jim Meyering 已提交
261
    case WSANO_DATA:
262 263
      msg = "Valid name, no data record of requested type";
      break;
J
Jim Meyering 已提交
264 265 266 267 268 269
    /* WSA_QOS_* omitted */
#  endif
# endif

# if GNULIB_defined_ENOMSG
    case ENOMSG:
270 271
      msg = "No message of desired type";
      break;
J
Jim Meyering 已提交
272 273 274 275
# endif

# if GNULIB_defined_EIDRM
    case EIDRM:
276 277
      msg = "Identifier removed";
      break;
J
Jim Meyering 已提交
278 279 280 281
# endif

# if GNULIB_defined_ENOLINK
    case ENOLINK:
282 283
      msg = "Link has been severed";
      break;
J
Jim Meyering 已提交
284 285 286 287
# endif

# if GNULIB_defined_EPROTO
    case EPROTO:
288 289
      msg = "Protocol error";
      break;
J
Jim Meyering 已提交
290 291 292 293
# endif

# if GNULIB_defined_EMULTIHOP
    case EMULTIHOP:
294 295
      msg = "Multihop attempted";
      break;
J
Jim Meyering 已提交
296 297 298 299
# endif

# if GNULIB_defined_EBADMSG
    case EBADMSG:
300 301
      msg = "Bad message";
      break;
J
Jim Meyering 已提交
302 303 304 305
# endif

# if GNULIB_defined_EOVERFLOW
    case EOVERFLOW:
306 307
      msg = "Value too large for defined data type";
      break;
J
Jim Meyering 已提交
308 309 310 311
# endif

# if GNULIB_defined_ENOTSUP
    case ENOTSUP:
312 313
      msg = "Not supported";
      break;
J
Jim Meyering 已提交
314 315 316 317
# endif

# if GNULIB_defined_
    case ECANCELED:
318 319
      msg = "Operation canceled";
      break;
J
Jim Meyering 已提交
320 321 322
# endif
    }

323 324 325
  if (msg)
    return (char *) msg;

J
Jim Meyering 已提交
326 327 328 329 330 331
  {
    char *result = strerror (n);

    if (result == NULL || result[0] == '\0')
      {
	static char const fmt[] = "Unknown error (%d)";
332 333 334
	static char msg_buf[sizeof fmt + INT_STRLEN_BOUND (n)];
	sprintf (msg_buf, fmt, n);
	return msg_buf;
J
Jim Meyering 已提交
335 336 337 338 339 340 341
      }

    return result;
  }
}

#endif