riched20: Use the implementation of EM_PASTESPECIAL for EM_CANPASTE.
[wine.git] / dlls / msvcrt / errno.c
blob45bc19415e0783fb3f4762768fcdbe12013c4585
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 <stdio.h>
22 #include <string.h>
23 #include <stdarg.h>
25 #include "ntstatus.h"
26 #define WIN32_NO_STATUS
27 #include "windef.h"
28 #include "winternl.h"
29 #include "msvcrt.h"
30 #include "winnls.h"
31 #include "excpt.h"
32 #include "wine/debug.h"
34 WINE_DEFAULT_DEBUG_CHANNEL(msvcrt);
36 /* error strings generated with glibc strerror */
37 static char str_success[] = "Success";
38 static char str_EPERM[] = "Operation not permitted";
39 static char str_ENOENT[] = "No such file or directory";
40 static char str_ESRCH[] = "No such process";
41 static char str_EINTR[] = "Interrupted system call";
42 static char str_EIO[] = "Input/output error";
43 static char str_ENXIO[] = "No such device or address";
44 static char str_E2BIG[] = "Argument list too long";
45 static char str_ENOEXEC[] = "Exec format error";
46 static char str_EBADF[] = "Bad file descriptor";
47 static char str_ECHILD[] = "No child processes";
48 static char str_EAGAIN[] = "Resource temporarily unavailable";
49 static char str_ENOMEM[] = "Cannot allocate memory";
50 static char str_EACCES[] = "Permission denied";
51 static char str_EFAULT[] = "Bad address";
52 static char str_EBUSY[] = "Device or resource busy";
53 static char str_EEXIST[] = "File exists";
54 static char str_EXDEV[] = "Invalid cross-device link";
55 static char str_ENODEV[] = "No such device";
56 static char str_ENOTDIR[] = "Not a directory";
57 static char str_EISDIR[] = "Is a directory";
58 static char str_EINVAL[] = "Invalid argument";
59 static char str_ENFILE[] = "Too many open files in system";
60 static char str_EMFILE[] = "Too many open files";
61 static char str_ENOTTY[] = "Inappropriate ioctl for device";
62 static char str_EFBIG[] = "File too large";
63 static char str_ENOSPC[] = "No space left on device";
64 static char str_ESPIPE[] = "Illegal seek";
65 static char str_EROFS[] = "Read-only file system";
66 static char str_EMLINK[] = "Too many links";
67 static char str_EPIPE[] = "Broken pipe";
68 static char str_EDOM[] = "Numerical argument out of domain";
69 static char str_ERANGE[] = "Numerical result out of range";
70 static char str_EDEADLK[] = "Resource deadlock avoided";
71 static char str_ENAMETOOLONG[] = "File name too long";
72 static char str_ENOLCK[] = "No locks available";
73 static char str_ENOSYS[] = "Function not implemented";
74 static char str_ENOTEMPTY[] = "Directory not empty";
75 static char str_EILSEQ[] = "Invalid or incomplete multibyte or wide character";
76 static char str_generic_error[] = "Unknown error";
78 char *MSVCRT__sys_errlist[] =
80 str_success,
81 str_EPERM,
82 str_ENOENT,
83 str_ESRCH,
84 str_EINTR,
85 str_EIO,
86 str_ENXIO,
87 str_E2BIG,
88 str_ENOEXEC,
89 str_EBADF,
90 str_ECHILD,
91 str_EAGAIN,
92 str_ENOMEM,
93 str_EACCES,
94 str_EFAULT,
95 str_generic_error,
96 str_EBUSY,
97 str_EEXIST,
98 str_EXDEV,
99 str_ENODEV,
100 str_ENOTDIR,
101 str_EISDIR,
102 str_EINVAL,
103 str_ENFILE,
104 str_EMFILE,
105 str_ENOTTY,
106 str_generic_error,
107 str_EFBIG,
108 str_ENOSPC,
109 str_ESPIPE,
110 str_EROFS,
111 str_EMLINK,
112 str_EPIPE,
113 str_EDOM,
114 str_ERANGE,
115 str_generic_error,
116 str_EDEADLK,
117 str_generic_error,
118 str_ENAMETOOLONG,
119 str_ENOLCK,
120 str_ENOSYS,
121 str_ENOTEMPTY,
122 str_EILSEQ,
123 str_generic_error
126 unsigned int MSVCRT__sys_nerr = sizeof(MSVCRT__sys_errlist)/sizeof(MSVCRT__sys_errlist[0]) - 1;
128 static MSVCRT_invalid_parameter_handler invalid_parameter_handler = NULL;
130 /* INTERNAL: Set the crt and dos errno's from the OS error given. */
131 void msvcrt_set_errno(int err)
133 int *errno_ptr = MSVCRT__errno();
134 MSVCRT_ulong *doserrno = MSVCRT___doserrno();
136 *doserrno = err;
138 switch(err)
140 #define ERR_CASE(oserr) case oserr:
141 #define ERR_MAPS(oserr, crterr) case oserr: *errno_ptr = crterr; break
142 ERR_CASE(ERROR_ACCESS_DENIED)
143 ERR_CASE(ERROR_NETWORK_ACCESS_DENIED)
144 ERR_CASE(ERROR_CANNOT_MAKE)
145 ERR_CASE(ERROR_SEEK_ON_DEVICE)
146 ERR_CASE(ERROR_LOCK_FAILED)
147 ERR_CASE(ERROR_FAIL_I24)
148 ERR_CASE(ERROR_CURRENT_DIRECTORY)
149 ERR_CASE(ERROR_DRIVE_LOCKED)
150 ERR_CASE(ERROR_NOT_LOCKED)
151 ERR_CASE(ERROR_INVALID_ACCESS)
152 ERR_CASE(ERROR_SHARING_VIOLATION)
153 ERR_MAPS(ERROR_LOCK_VIOLATION, MSVCRT_EACCES);
154 ERR_CASE(ERROR_FILE_NOT_FOUND)
155 ERR_CASE(ERROR_NO_MORE_FILES)
156 ERR_CASE(ERROR_BAD_PATHNAME)
157 ERR_CASE(ERROR_BAD_NETPATH)
158 ERR_CASE(ERROR_INVALID_DRIVE)
159 ERR_CASE(ERROR_BAD_NET_NAME)
160 ERR_CASE(ERROR_FILENAME_EXCED_RANGE)
161 ERR_MAPS(ERROR_PATH_NOT_FOUND, MSVCRT_ENOENT);
162 ERR_MAPS(ERROR_IO_DEVICE, MSVCRT_EIO);
163 ERR_MAPS(ERROR_BAD_FORMAT, MSVCRT_ENOEXEC);
164 ERR_MAPS(ERROR_INVALID_HANDLE, MSVCRT_EBADF);
165 ERR_CASE(ERROR_OUTOFMEMORY)
166 ERR_CASE(ERROR_INVALID_BLOCK)
167 ERR_CASE(ERROR_NOT_ENOUGH_QUOTA)
168 ERR_MAPS(ERROR_ARENA_TRASHED, MSVCRT_ENOMEM);
169 ERR_MAPS(ERROR_BUSY, MSVCRT_EBUSY);
170 ERR_CASE(ERROR_ALREADY_EXISTS)
171 ERR_MAPS(ERROR_FILE_EXISTS, MSVCRT_EEXIST);
172 ERR_MAPS(ERROR_BAD_DEVICE, MSVCRT_ENODEV);
173 ERR_MAPS(ERROR_TOO_MANY_OPEN_FILES, MSVCRT_EMFILE);
174 ERR_MAPS(ERROR_DISK_FULL, MSVCRT_ENOSPC);
175 ERR_MAPS(ERROR_BROKEN_PIPE, MSVCRT_EPIPE);
176 ERR_MAPS(ERROR_POSSIBLE_DEADLOCK, MSVCRT_EDEADLK);
177 ERR_MAPS(ERROR_DIR_NOT_EMPTY, MSVCRT_ENOTEMPTY);
178 ERR_MAPS(ERROR_BAD_ENVIRONMENT, MSVCRT_E2BIG);
179 ERR_CASE(ERROR_WAIT_NO_CHILDREN)
180 ERR_MAPS(ERROR_CHILD_NOT_COMPLETE, MSVCRT_ECHILD);
181 ERR_CASE(ERROR_NO_PROC_SLOTS)
182 ERR_CASE(ERROR_MAX_THRDS_REACHED)
183 ERR_MAPS(ERROR_NESTING_NOT_ALLOWED, MSVCRT_EAGAIN);
184 default:
185 /* Remaining cases map to EINVAL */
186 /* FIXME: may be missing some errors above */
187 *errno_ptr = MSVCRT_EINVAL;
191 /*********************************************************************
192 * __sys_nerr (MSVCR100.@)
194 int* CDECL __sys_nerr(void)
196 return (int*)&MSVCRT__sys_nerr;
199 /*********************************************************************
200 * __sys_errlist (MSVCR100.@)
202 char** CDECL __sys_errlist(void)
204 return MSVCRT__sys_errlist;
207 /*********************************************************************
208 * _errno (MSVCRT.@)
210 int* CDECL MSVCRT__errno(void)
212 return &msvcrt_get_thread_data()->thread_errno;
215 /*********************************************************************
216 * __doserrno (MSVCRT.@)
218 MSVCRT_ulong* CDECL MSVCRT___doserrno(void)
220 return &msvcrt_get_thread_data()->thread_doserrno;
223 /*********************************************************************
224 * _get_errno (MSVCRT.@)
226 int CDECL _get_errno(int *pValue)
228 if (!pValue)
229 return MSVCRT_EINVAL;
231 *pValue = *MSVCRT__errno();
232 return 0;
235 /*********************************************************************
236 * _get_doserrno (MSVCRT.@)
238 int CDECL _get_doserrno(int *pValue)
240 if (!pValue)
241 return MSVCRT_EINVAL;
243 *pValue = *MSVCRT___doserrno();
244 return 0;
247 /*********************************************************************
248 * _set_errno (MSVCRT.@)
250 int CDECL _set_errno(int value)
252 *MSVCRT__errno() = value;
253 return 0;
256 /*********************************************************************
257 * _set_doserrno (MSVCRT.@)
259 int CDECL _set_doserrno(int value)
261 *MSVCRT___doserrno() = value;
262 return 0;
265 /*********************************************************************
266 * strerror (MSVCRT.@)
268 char* CDECL MSVCRT_strerror(int err)
270 thread_data_t *data = msvcrt_get_thread_data();
272 if (!data->strerror_buffer)
273 if (!(data->strerror_buffer = MSVCRT_malloc(256))) return NULL;
275 if (err < 0 || err > MSVCRT__sys_nerr) err = MSVCRT__sys_nerr;
276 strcpy( data->strerror_buffer, MSVCRT__sys_errlist[err] );
277 return data->strerror_buffer;
280 /**********************************************************************
281 * strerror_s (MSVCRT.@)
283 int CDECL MSVCRT_strerror_s(char *buffer, MSVCRT_size_t numberOfElements, int errnum)
285 char *ptr;
287 if (!buffer || !numberOfElements)
289 *MSVCRT__errno() = MSVCRT_EINVAL;
290 return MSVCRT_EINVAL;
293 if (errnum < 0 || errnum > MSVCRT__sys_nerr)
294 errnum = MSVCRT__sys_nerr;
296 ptr = MSVCRT__sys_errlist[errnum];
297 while (*ptr && numberOfElements > 1)
299 *buffer++ = *ptr++;
300 numberOfElements--;
303 *buffer = '\0';
304 return 0;
307 /**********************************************************************
308 * _strerror (MSVCRT.@)
310 char* CDECL MSVCRT__strerror(const char* str)
312 thread_data_t *data = msvcrt_get_thread_data();
313 int err;
315 if (!data->strerror_buffer)
316 if (!(data->strerror_buffer = MSVCRT_malloc(256))) return NULL;
318 err = data->thread_errno;
319 if (err < 0 || err > MSVCRT__sys_nerr) err = MSVCRT__sys_nerr;
321 if (str && *str)
322 sprintf( data->strerror_buffer, "%s: %s\n", str, MSVCRT__sys_errlist[err] );
323 else
324 sprintf( data->strerror_buffer, "%s\n", MSVCRT__sys_errlist[err] );
326 return data->strerror_buffer;
329 /*********************************************************************
330 * perror (MSVCRT.@)
332 void CDECL MSVCRT_perror(const char* str)
334 int err = *MSVCRT__errno();
335 if (err < 0 || err > MSVCRT__sys_nerr) err = MSVCRT__sys_nerr;
337 if (str && *str)
339 MSVCRT__write( 2, str, strlen(str) );
340 MSVCRT__write( 2, ": ", 2 );
342 MSVCRT__write( 2, MSVCRT__sys_errlist[err], strlen(MSVCRT__sys_errlist[err]) );
343 MSVCRT__write( 2, "\n", 1 );
346 /*********************************************************************
347 * _wperror (MSVCRT.@)
349 void CDECL MSVCRT__wperror(const MSVCRT_wchar_t* str)
351 MSVCRT_size_t size;
352 char *buffer = NULL;
354 if (str && *str)
356 size = MSVCRT_wcstombs(NULL, str, 0);
357 if (size == -1) return;
358 size++;
359 buffer = MSVCRT_malloc(size);
360 if (!buffer) return;
361 if (MSVCRT_wcstombs(buffer, str, size) == -1)
363 MSVCRT_free(buffer);
364 return;
367 MSVCRT_perror(buffer);
368 MSVCRT_free(buffer);
371 /*********************************************************************
372 * _wcserror_s (MSVCRT.@)
374 int CDECL MSVCRT__wcserror_s(MSVCRT_wchar_t* buffer, MSVCRT_size_t nc, int err)
376 if (!MSVCRT_CHECK_PMT(buffer != NULL)) return MSVCRT_EINVAL;
377 if (!MSVCRT_CHECK_PMT(nc > 0)) return MSVCRT_EINVAL;
379 if (err < 0 || err > MSVCRT__sys_nerr) err = MSVCRT__sys_nerr;
380 MultiByteToWideChar(CP_ACP, 0, MSVCRT__sys_errlist[err], -1, buffer, nc);
381 return 0;
384 /*********************************************************************
385 * _wcserror (MSVCRT.@)
387 MSVCRT_wchar_t* CDECL MSVCRT__wcserror(int err)
389 thread_data_t *data = msvcrt_get_thread_data();
391 if (!data->wcserror_buffer)
392 if (!(data->wcserror_buffer = MSVCRT_malloc(256 * sizeof(MSVCRT_wchar_t)))) return NULL;
393 MSVCRT__wcserror_s(data->wcserror_buffer, 256, err);
394 return data->wcserror_buffer;
397 /**********************************************************************
398 * __wcserror_s (MSVCRT.@)
400 int CDECL MSVCRT___wcserror_s(MSVCRT_wchar_t* buffer, MSVCRT_size_t nc, const MSVCRT_wchar_t* str)
402 int err;
403 static const WCHAR colonW[] = {':', ' ', '\0'};
404 static const WCHAR nlW[] = {'\n', '\0'};
405 size_t len;
407 err = *MSVCRT__errno();
408 if (err < 0 || err > MSVCRT__sys_nerr) err = MSVCRT__sys_nerr;
410 len = MultiByteToWideChar(CP_ACP, 0, MSVCRT__sys_errlist[err], -1, NULL, 0) + 1 /* \n */;
411 if (str && *str) len += lstrlenW(str) + 2 /* ': ' */;
412 if (len > nc)
414 MSVCRT_INVALID_PMT("buffer[nc] is too small", MSVCRT_ERANGE);
415 return MSVCRT_ERANGE;
417 if (str && *str)
419 lstrcpyW(buffer, str);
420 lstrcatW(buffer, colonW);
422 else buffer[0] = '\0';
423 len = lstrlenW(buffer);
424 MultiByteToWideChar(CP_ACP, 0, MSVCRT__sys_errlist[err], -1, buffer + len, 256 - len);
425 lstrcatW(buffer, nlW);
427 return 0;
430 /**********************************************************************
431 * __wcserror (MSVCRT.@)
433 MSVCRT_wchar_t* CDECL MSVCRT___wcserror(const MSVCRT_wchar_t* str)
435 thread_data_t *data = msvcrt_get_thread_data();
436 int err;
438 if (!data->wcserror_buffer)
439 if (!(data->wcserror_buffer = MSVCRT_malloc(256 * sizeof(MSVCRT_wchar_t)))) return NULL;
441 err = MSVCRT___wcserror_s(data->wcserror_buffer, 256, str);
442 if (err) FIXME("bad wcserror call (%d)\n", err);
444 return data->wcserror_buffer;
447 /******************************************************************************
448 * _seterrormode (MSVCRT.@)
450 void CDECL _seterrormode(int mode)
452 SetErrorMode( mode );
455 /******************************************************************************
456 * _invalid_parameter (MSVCRT.@)
458 void __cdecl MSVCRT__invalid_parameter(const MSVCRT_wchar_t *expr, const MSVCRT_wchar_t *func,
459 const MSVCRT_wchar_t *file, unsigned int line, MSVCRT_uintptr_t arg)
461 #if _MSVCR_VER >= 140
462 thread_data_t *data = msvcrt_get_thread_data();
464 if (data->invalid_parameter_handler)
466 data->invalid_parameter_handler( expr, func, file, line, arg );
467 return;
469 #endif
471 if (invalid_parameter_handler) invalid_parameter_handler( expr, func, file, line, arg );
472 else
474 ERR( "%s:%u %s: %s %lx\n", debugstr_w(file), line, debugstr_w(func), debugstr_w(expr), arg );
475 #if _MSVCR_VER > 0
476 RaiseException( STATUS_INVALID_CRUNTIME_PARAMETER, EXCEPTION_NONCONTINUABLE, 0, NULL );
477 #endif
481 /*********************************************************************
482 * _invalid_parameter_noinfo (MSVCR100.@)
484 void CDECL _invalid_parameter_noinfo(void)
486 MSVCRT__invalid_parameter( NULL, NULL, NULL, 0, 0 );
489 /*********************************************************************
490 * _invalid_parameter_noinfo_noreturn (MSVCR100.@)
492 void CDECL _invalid_parameter_noinfo_noreturn(void)
494 MSVCRT__invalid_parameter( NULL, NULL, NULL, 0, 0 );
495 MSVCRT__exit( STATUS_INVALID_CRUNTIME_PARAMETER );
498 /*********************************************************************
499 * _get_invalid_parameter_handler (MSVCR80.@)
501 MSVCRT_invalid_parameter_handler CDECL _get_invalid_parameter_handler(void)
503 TRACE("\n");
504 return invalid_parameter_handler;
507 /*********************************************************************
508 * _set_invalid_parameter_handler (MSVCR80.@)
510 MSVCRT_invalid_parameter_handler CDECL _set_invalid_parameter_handler(
511 MSVCRT_invalid_parameter_handler handler)
513 MSVCRT_invalid_parameter_handler old = invalid_parameter_handler;
515 TRACE("(%p)\n", handler);
517 invalid_parameter_handler = handler;
518 return old;
521 #if _MSVCR_VER >= 140
522 /*********************************************************************
523 * _get_thread_local_invalid_parameter_handler (UCRTBASE.@)
525 MSVCRT_invalid_parameter_handler CDECL _get_thread_local_invalid_parameter_handler(void)
527 TRACE("\n");
528 return msvcrt_get_thread_data()->invalid_parameter_handler;
531 /*********************************************************************
532 * _set_thread_local_invalid_parameter_handler (UCRTBASE.@)
534 MSVCRT_invalid_parameter_handler CDECL _set_thread_local_invalid_parameter_handler(
535 MSVCRT_invalid_parameter_handler handler)
537 thread_data_t *data = msvcrt_get_thread_data();
538 MSVCRT_invalid_parameter_handler old = data->invalid_parameter_handler;
540 TRACE("(%p)\n", handler);
542 data->invalid_parameter_handler = handler;
543 return old;
545 #endif