server: Do not accept sizeof(struct WS_sockaddr_in6_old).
[wine.git] / dlls / msvcrt / errno.c
blob85752e52df7cd65071743a3530e2c86d69e87c01
1 /*
2 * msvcrt.dll errno functions
4 * Copyright 2000 Jon Griffiths
6 * This library is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Lesser General Public
8 * License as published by the Free Software Foundation; either
9 * version 2.1 of the License, or (at your option) any later version.
11 * This library is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * Lesser General Public License for more details.
16 * You should have received a copy of the GNU Lesser General Public
17 * License along with this library; if not, write to the Free Software
18 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
21 #include <io.h>
22 #include <stdio.h>
23 #include <string.h>
24 #include <stdarg.h>
26 #include "ntstatus.h"
27 #define WIN32_NO_STATUS
28 #include "windef.h"
29 #include "winternl.h"
30 #include "msvcrt.h"
31 #include "winnls.h"
32 #include "excpt.h"
33 #include "wine/debug.h"
35 WINE_DEFAULT_DEBUG_CHANNEL(msvcrt);
37 /* error strings generated with glibc strerror */
38 static char str_success[] = "Success";
39 static char str_EPERM[] = "Operation not permitted";
40 static char str_ENOENT[] = "No such file or directory";
41 static char str_ESRCH[] = "No such process";
42 static char str_EINTR[] = "Interrupted system call";
43 static char str_EIO[] = "Input/output error";
44 static char str_ENXIO[] = "No such device or address";
45 static char str_E2BIG[] = "Argument list too long";
46 static char str_ENOEXEC[] = "Exec format error";
47 static char str_EBADF[] = "Bad file descriptor";
48 static char str_ECHILD[] = "No child processes";
49 static char str_EAGAIN[] = "Resource temporarily unavailable";
50 static char str_ENOMEM[] = "Cannot allocate memory";
51 static char str_EACCES[] = "Permission denied";
52 static char str_EFAULT[] = "Bad address";
53 static char str_EBUSY[] = "Device or resource busy";
54 static char str_EEXIST[] = "File exists";
55 static char str_EXDEV[] = "Invalid cross-device link";
56 static char str_ENODEV[] = "No such device";
57 static char str_ENOTDIR[] = "Not a directory";
58 static char str_EISDIR[] = "Is a directory";
59 static char str_EINVAL[] = "Invalid argument";
60 static char str_ENFILE[] = "Too many open files in system";
61 static char str_EMFILE[] = "Too many open files";
62 static char str_ENOTTY[] = "Inappropriate ioctl for device";
63 static char str_EFBIG[] = "File too large";
64 static char str_ENOSPC[] = "No space left on device";
65 static char str_ESPIPE[] = "Illegal seek";
66 static char str_EROFS[] = "Read-only file system";
67 static char str_EMLINK[] = "Too many links";
68 static char str_EPIPE[] = "Broken pipe";
69 static char str_EDOM[] = "Numerical argument out of domain";
70 static char str_ERANGE[] = "Numerical result out of range";
71 static char str_EDEADLK[] = "Resource deadlock avoided";
72 static char str_ENAMETOOLONG[] = "File name too long";
73 static char str_ENOLCK[] = "No locks available";
74 static char str_ENOSYS[] = "Function not implemented";
75 static char str_ENOTEMPTY[] = "Directory not empty";
76 static char str_EILSEQ[] = "Invalid or incomplete multibyte or wide character";
77 static char str_generic_error[] = "Unknown error";
79 char *MSVCRT__sys_errlist[] =
81 str_success,
82 str_EPERM,
83 str_ENOENT,
84 str_ESRCH,
85 str_EINTR,
86 str_EIO,
87 str_ENXIO,
88 str_E2BIG,
89 str_ENOEXEC,
90 str_EBADF,
91 str_ECHILD,
92 str_EAGAIN,
93 str_ENOMEM,
94 str_EACCES,
95 str_EFAULT,
96 str_generic_error,
97 str_EBUSY,
98 str_EEXIST,
99 str_EXDEV,
100 str_ENODEV,
101 str_ENOTDIR,
102 str_EISDIR,
103 str_EINVAL,
104 str_ENFILE,
105 str_EMFILE,
106 str_ENOTTY,
107 str_generic_error,
108 str_EFBIG,
109 str_ENOSPC,
110 str_ESPIPE,
111 str_EROFS,
112 str_EMLINK,
113 str_EPIPE,
114 str_EDOM,
115 str_ERANGE,
116 str_generic_error,
117 str_EDEADLK,
118 str_generic_error,
119 str_ENAMETOOLONG,
120 str_ENOLCK,
121 str_ENOSYS,
122 str_ENOTEMPTY,
123 str_EILSEQ,
124 str_generic_error
127 unsigned int MSVCRT__sys_nerr = ARRAY_SIZE(MSVCRT__sys_errlist) - 1;
129 static _invalid_parameter_handler invalid_parameter_handler = NULL;
131 /* INTERNAL: Set the crt and dos errno's from the OS error given. */
132 void msvcrt_set_errno(int err)
134 int *errno_ptr = _errno();
135 __msvcrt_ulong *doserrno = __doserrno();
137 *doserrno = err;
139 switch(err)
141 #define ERR_CASE(oserr) case oserr:
142 #define ERR_MAPS(oserr, crterr) case oserr: *errno_ptr = crterr; break
143 ERR_CASE(ERROR_ACCESS_DENIED)
144 ERR_CASE(ERROR_NETWORK_ACCESS_DENIED)
145 ERR_CASE(ERROR_CANNOT_MAKE)
146 ERR_CASE(ERROR_SEEK_ON_DEVICE)
147 ERR_CASE(ERROR_LOCK_FAILED)
148 ERR_CASE(ERROR_FAIL_I24)
149 ERR_CASE(ERROR_CURRENT_DIRECTORY)
150 ERR_CASE(ERROR_DRIVE_LOCKED)
151 ERR_CASE(ERROR_NOT_LOCKED)
152 ERR_CASE(ERROR_INVALID_ACCESS)
153 ERR_CASE(ERROR_SHARING_VIOLATION)
154 ERR_MAPS(ERROR_LOCK_VIOLATION, EACCES);
155 ERR_CASE(ERROR_FILE_NOT_FOUND)
156 ERR_CASE(ERROR_NO_MORE_FILES)
157 ERR_CASE(ERROR_BAD_PATHNAME)
158 ERR_CASE(ERROR_BAD_NETPATH)
159 ERR_CASE(ERROR_INVALID_DRIVE)
160 ERR_CASE(ERROR_BAD_NET_NAME)
161 ERR_CASE(ERROR_FILENAME_EXCED_RANGE)
162 ERR_MAPS(ERROR_PATH_NOT_FOUND, ENOENT);
163 ERR_MAPS(ERROR_IO_DEVICE, EIO);
164 ERR_MAPS(ERROR_BAD_FORMAT, ENOEXEC);
165 ERR_MAPS(ERROR_INVALID_HANDLE, EBADF);
166 ERR_CASE(ERROR_OUTOFMEMORY)
167 ERR_CASE(ERROR_INVALID_BLOCK)
168 ERR_CASE(ERROR_NOT_ENOUGH_QUOTA)
169 ERR_MAPS(ERROR_ARENA_TRASHED, ENOMEM);
170 ERR_MAPS(ERROR_BUSY, EBUSY);
171 ERR_CASE(ERROR_ALREADY_EXISTS)
172 ERR_MAPS(ERROR_FILE_EXISTS, EEXIST);
173 ERR_MAPS(ERROR_BAD_DEVICE, ENODEV);
174 ERR_MAPS(ERROR_TOO_MANY_OPEN_FILES, EMFILE);
175 ERR_MAPS(ERROR_DISK_FULL, ENOSPC);
176 ERR_MAPS(ERROR_BROKEN_PIPE, EPIPE);
177 ERR_MAPS(ERROR_POSSIBLE_DEADLOCK, EDEADLK);
178 ERR_MAPS(ERROR_DIR_NOT_EMPTY, ENOTEMPTY);
179 ERR_MAPS(ERROR_BAD_ENVIRONMENT, E2BIG);
180 ERR_CASE(ERROR_WAIT_NO_CHILDREN)
181 ERR_MAPS(ERROR_CHILD_NOT_COMPLETE, ECHILD);
182 ERR_CASE(ERROR_NO_PROC_SLOTS)
183 ERR_CASE(ERROR_MAX_THRDS_REACHED)
184 ERR_MAPS(ERROR_NESTING_NOT_ALLOWED, EAGAIN);
185 default:
186 /* Remaining cases map to EINVAL */
187 /* FIXME: may be missing some errors above */
188 *errno_ptr = EINVAL;
192 #if _MSVCR_VER >= 80
194 /*********************************************************************
195 * __sys_nerr (MSVCR80.@)
197 int* CDECL __sys_nerr(void)
199 return (int*)&MSVCRT__sys_nerr;
202 /*********************************************************************
203 * __sys_errlist (MSVCR80.@)
205 char** CDECL __sys_errlist(void)
207 return MSVCRT__sys_errlist;
210 #endif /* _MSVCR_VER >= 80 */
212 /*********************************************************************
213 * _errno (MSVCRT.@)
215 int* CDECL _errno(void)
217 return &msvcrt_get_thread_data()->thread_errno;
220 /*********************************************************************
221 * __doserrno (MSVCRT.@)
223 __msvcrt_ulong* CDECL __doserrno(void)
225 return &msvcrt_get_thread_data()->thread_doserrno;
228 /*********************************************************************
229 * _get_errno (MSVCRT.@)
231 int CDECL _get_errno(int *pValue)
233 if (!pValue)
234 return EINVAL;
236 *pValue = *_errno();
237 return 0;
240 /*********************************************************************
241 * _get_doserrno (MSVCRT.@)
243 int CDECL _get_doserrno(int *pValue)
245 if (!pValue)
246 return EINVAL;
248 *pValue = *__doserrno();
249 return 0;
252 /*********************************************************************
253 * _set_errno (MSVCRT.@)
255 int CDECL _set_errno(int value)
257 *_errno() = value;
258 return 0;
261 /*********************************************************************
262 * _set_doserrno (MSVCRT.@)
264 int CDECL _set_doserrno(int value)
266 *__doserrno() = value;
267 return 0;
270 /*********************************************************************
271 * strerror (MSVCRT.@)
273 char* CDECL strerror(int err)
275 thread_data_t *data = msvcrt_get_thread_data();
277 if (!data->strerror_buffer)
278 if (!(data->strerror_buffer = malloc(256))) return NULL;
280 if (err < 0 || err > MSVCRT__sys_nerr) err = MSVCRT__sys_nerr;
281 strcpy( data->strerror_buffer, MSVCRT__sys_errlist[err] );
282 return data->strerror_buffer;
285 /**********************************************************************
286 * strerror_s (MSVCRT.@)
288 int CDECL strerror_s(char *buffer, size_t numberOfElements, int errnum)
290 char *ptr;
292 if (!buffer || !numberOfElements)
294 *_errno() = EINVAL;
295 return EINVAL;
298 if (errnum < 0 || errnum > MSVCRT__sys_nerr)
299 errnum = MSVCRT__sys_nerr;
301 ptr = MSVCRT__sys_errlist[errnum];
302 while (*ptr && numberOfElements > 1)
304 *buffer++ = *ptr++;
305 numberOfElements--;
308 *buffer = '\0';
309 return 0;
312 /**********************************************************************
313 * _strerror (MSVCRT.@)
315 char* CDECL _strerror(const char* str)
317 thread_data_t *data = msvcrt_get_thread_data();
318 int err;
320 if (!data->strerror_buffer)
321 if (!(data->strerror_buffer = malloc(256))) return NULL;
323 err = data->thread_errno;
324 if (err < 0 || err > MSVCRT__sys_nerr) err = MSVCRT__sys_nerr;
326 if (str && *str)
327 sprintf( data->strerror_buffer, "%s: %s\n", str, MSVCRT__sys_errlist[err] );
328 else
329 sprintf( data->strerror_buffer, "%s\n", MSVCRT__sys_errlist[err] );
331 return data->strerror_buffer;
334 /*********************************************************************
335 * perror (MSVCRT.@)
337 void CDECL perror(const char* str)
339 int err = *_errno();
340 if (err < 0 || err > MSVCRT__sys_nerr) err = MSVCRT__sys_nerr;
342 if (str && *str)
344 _write( 2, str, strlen(str) );
345 _write( 2, ": ", 2 );
347 _write( 2, MSVCRT__sys_errlist[err], strlen(MSVCRT__sys_errlist[err]) );
348 _write( 2, "\n", 1 );
351 /*********************************************************************
352 * _wperror (MSVCRT.@)
354 void CDECL _wperror(const wchar_t* str)
356 size_t size;
357 char *buffer = NULL;
359 if (str && *str)
361 size = wcstombs(NULL, str, 0);
362 if (size == -1) return;
363 size++;
364 buffer = malloc(size);
365 if (!buffer) return;
366 if (wcstombs(buffer, str, size) == -1)
368 free(buffer);
369 return;
372 perror(buffer);
373 free(buffer);
376 /*********************************************************************
377 * _wcserror_s (MSVCRT.@)
379 int CDECL _wcserror_s(wchar_t* buffer, size_t nc, int err)
381 if (!MSVCRT_CHECK_PMT(buffer != NULL)) return EINVAL;
382 if (!MSVCRT_CHECK_PMT(nc > 0)) return EINVAL;
384 if (err < 0 || err > MSVCRT__sys_nerr) err = MSVCRT__sys_nerr;
385 MultiByteToWideChar(CP_ACP, 0, MSVCRT__sys_errlist[err], -1, buffer, nc);
386 return 0;
389 /*********************************************************************
390 * _wcserror (MSVCRT.@)
392 wchar_t* CDECL _wcserror(int err)
394 thread_data_t *data = msvcrt_get_thread_data();
396 if (!data->wcserror_buffer)
397 if (!(data->wcserror_buffer = malloc(256 * sizeof(wchar_t)))) return NULL;
398 _wcserror_s(data->wcserror_buffer, 256, err);
399 return data->wcserror_buffer;
402 /**********************************************************************
403 * __wcserror_s (MSVCRT.@)
405 int CDECL __wcserror_s(wchar_t* buffer, size_t nc, const wchar_t* str)
407 int err;
408 size_t len;
410 err = *_errno();
411 if (err < 0 || err > MSVCRT__sys_nerr) err = MSVCRT__sys_nerr;
413 len = MultiByteToWideChar(CP_ACP, 0, MSVCRT__sys_errlist[err], -1, NULL, 0) + 1 /* \n */;
414 if (str && *str) len += wcslen(str) + 2 /* ': ' */;
415 if (len > nc)
417 MSVCRT_INVALID_PMT("buffer[nc] is too small", ERANGE);
418 return ERANGE;
420 if (str && *str)
422 lstrcpyW(buffer, str);
423 lstrcatW(buffer, L": ");
425 else buffer[0] = '\0';
426 len = wcslen(buffer);
427 MultiByteToWideChar(CP_ACP, 0, MSVCRT__sys_errlist[err], -1, buffer + len, 256 - len);
428 lstrcatW(buffer, L"\n");
430 return 0;
433 /**********************************************************************
434 * __wcserror (MSVCRT.@)
436 wchar_t* CDECL __wcserror(const wchar_t* str)
438 thread_data_t *data = msvcrt_get_thread_data();
439 int err;
441 if (!data->wcserror_buffer)
442 if (!(data->wcserror_buffer = malloc(256 * sizeof(wchar_t)))) return NULL;
444 err = __wcserror_s(data->wcserror_buffer, 256, str);
445 if (err) FIXME("bad wcserror call (%d)\n", err);
447 return data->wcserror_buffer;
450 /******************************************************************************
451 * _seterrormode (MSVCRT.@)
453 void CDECL _seterrormode(int mode)
455 SetErrorMode( mode );
458 /******************************************************************************
459 * _invalid_parameter (MSVCRT.@)
461 void __cdecl _invalid_parameter(const wchar_t *expr, const wchar_t *func,
462 const wchar_t *file, unsigned int line, uintptr_t arg)
464 #if _MSVCR_VER >= 140
465 thread_data_t *data = msvcrt_get_thread_data();
467 if (data->invalid_parameter_handler)
469 data->invalid_parameter_handler( expr, func, file, line, arg );
470 return;
472 #endif
474 if (invalid_parameter_handler) invalid_parameter_handler( expr, func, file, line, arg );
475 else
477 ERR( "%s:%u %s: %s %Ix\n", debugstr_w(file), line, debugstr_w(func), debugstr_w(expr), arg );
478 #if _MSVCR_VER >= 80
479 RaiseException( STATUS_INVALID_CRUNTIME_PARAMETER, EXCEPTION_NONCONTINUABLE, 0, NULL );
480 #endif
484 #if _MSVCR_VER >= 80
486 /*********************************************************************
487 * _invalid_parameter_noinfo (MSVCR80.@)
489 void CDECL _invalid_parameter_noinfo(void)
491 _invalid_parameter( NULL, NULL, NULL, 0, 0 );
494 /*********************************************************************
495 * _invalid_parameter_noinfo_noreturn (MSVCR80.@)
497 void CDECL _invalid_parameter_noinfo_noreturn(void)
499 _invalid_parameter( NULL, NULL, NULL, 0, 0 );
500 _exit( STATUS_INVALID_CRUNTIME_PARAMETER );
503 /*********************************************************************
504 * _get_invalid_parameter_handler (MSVCR80.@)
506 _invalid_parameter_handler CDECL _get_invalid_parameter_handler(void)
508 TRACE("\n");
509 return invalid_parameter_handler;
512 /*********************************************************************
513 * _set_invalid_parameter_handler (MSVCR80.@)
515 _invalid_parameter_handler CDECL _set_invalid_parameter_handler(
516 _invalid_parameter_handler handler)
518 _invalid_parameter_handler old = invalid_parameter_handler;
520 TRACE("(%p)\n", handler);
522 invalid_parameter_handler = handler;
523 return old;
526 #endif /* _MSVCR_VER >= 80 */
528 #if _MSVCR_VER >= 140
530 /*********************************************************************
531 * _get_thread_local_invalid_parameter_handler (UCRTBASE.@)
533 _invalid_parameter_handler CDECL _get_thread_local_invalid_parameter_handler(void)
535 TRACE("\n");
536 return msvcrt_get_thread_data()->invalid_parameter_handler;
539 /*********************************************************************
540 * _set_thread_local_invalid_parameter_handler (UCRTBASE.@)
542 _invalid_parameter_handler CDECL _set_thread_local_invalid_parameter_handler(
543 _invalid_parameter_handler handler)
545 thread_data_t *data = msvcrt_get_thread_data();
546 _invalid_parameter_handler old = data->invalid_parameter_handler;
548 TRACE("(%p)\n", handler);
550 data->invalid_parameter_handler = handler;
551 return old;
554 #endif /* _MSVCR_VER >= 140 */