errno.c 15.9 KB
Newer Older
1 2 3 4
/*
 * msvcrt.dll errno functions
 *
 * Copyright 2000 Jon Griffiths
5 6 7 8 9 10 11 12 13 14 15 16 17
 *
 * This library 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 library 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 library; if not, write to the Free Software
18
 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
19 20
 */

21 22
#include <stdio.h>
#include <string.h>
23
#include <stdarg.h>
24

25 26 27 28
#include "ntstatus.h"
#define WIN32_NO_STATUS
#include "windef.h"
#include "winternl.h"
29
#include "msvcrt.h"
30
#include "winnls.h"
31
#include "excpt.h"
32 33 34
#include "wine/debug.h"

WINE_DEFAULT_DEBUG_CHANNEL(msvcrt);
35

36
/* error strings generated with glibc strerror */
37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76
static char str_success[]       = "Success";
static char str_EPERM[]         = "Operation not permitted";
static char str_ENOENT[]        = "No such file or directory";
static char str_ESRCH[]         = "No such process";
static char str_EINTR[]         = "Interrupted system call";
static char str_EIO[]           = "Input/output error";
static char str_ENXIO[]         = "No such device or address";
static char str_E2BIG[]         = "Argument list too long";
static char str_ENOEXEC[]       = "Exec format error";
static char str_EBADF[]         = "Bad file descriptor";
static char str_ECHILD[]        = "No child processes";
static char str_EAGAIN[]        = "Resource temporarily unavailable";
static char str_ENOMEM[]        = "Cannot allocate memory";
static char str_EACCES[]        = "Permission denied";
static char str_EFAULT[]        = "Bad address";
static char str_EBUSY[]         = "Device or resource busy";
static char str_EEXIST[]        = "File exists";
static char str_EXDEV[]         = "Invalid cross-device link";
static char str_ENODEV[]        = "No such device";
static char str_ENOTDIR[]       = "Not a directory";
static char str_EISDIR[]        = "Is a directory";
static char str_EINVAL[]        = "Invalid argument";
static char str_ENFILE[]        = "Too many open files in system";
static char str_EMFILE[]        = "Too many open files";
static char str_ENOTTY[]        = "Inappropriate ioctl for device";
static char str_EFBIG[]         = "File too large";
static char str_ENOSPC[]        = "No space left on device";
static char str_ESPIPE[]        = "Illegal seek";
static char str_EROFS[]         = "Read-only file system";
static char str_EMLINK[]        = "Too many links";
static char str_EPIPE[]         = "Broken pipe";
static char str_EDOM[]          = "Numerical argument out of domain";
static char str_ERANGE[]        = "Numerical result out of range";
static char str_EDEADLK[]       = "Resource deadlock avoided";
static char str_ENAMETOOLONG[]  = "File name too long";
static char str_ENOLCK[]        = "No locks available";
static char str_ENOSYS[]        = "Function not implemented";
static char str_ENOTEMPTY[]     = "Directory not empty";
static char str_EILSEQ[]        = "Invalid or incomplete multibyte or wide character";
static char str_generic_error[] = "Unknown error";
77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126

char *MSVCRT__sys_errlist[] =
{
    str_success,
    str_EPERM,
    str_ENOENT,
    str_ESRCH,
    str_EINTR,
    str_EIO,
    str_ENXIO,
    str_E2BIG,
    str_ENOEXEC,
    str_EBADF,
    str_ECHILD,
    str_EAGAIN,
    str_ENOMEM,
    str_EACCES,
    str_EFAULT,
    str_generic_error,
    str_EBUSY,
    str_EEXIST,
    str_EXDEV,
    str_ENODEV,
    str_ENOTDIR,
    str_EISDIR,
    str_EINVAL,
    str_ENFILE,
    str_EMFILE,
    str_ENOTTY,
    str_generic_error,
    str_EFBIG,
    str_ENOSPC,
    str_ESPIPE,
    str_EROFS,
    str_EMLINK,
    str_EPIPE,
    str_EDOM,
    str_ERANGE,
    str_generic_error,
    str_EDEADLK,
    str_generic_error,
    str_ENAMETOOLONG,
    str_ENOLCK,
    str_ENOSYS,
    str_ENOTEMPTY,
    str_EILSEQ,
    str_generic_error
};

unsigned int MSVCRT__sys_nerr = sizeof(MSVCRT__sys_errlist)/sizeof(MSVCRT__sys_errlist[0]) - 1;
127 128

static MSVCRT_invalid_parameter_handler invalid_parameter_handler = NULL;
129 130

/* INTERNAL: Set the crt and dos errno's from the OS error given. */
131
void msvcrt_set_errno(int err)
132
{
133
  int *errno_ptr = MSVCRT__errno();
134
  MSVCRT_ulong *doserrno = MSVCRT___doserrno();
135 136 137 138 139 140

  *doserrno = err;

  switch(err)
  {
#define ERR_CASE(oserr) case oserr:
141
#define ERR_MAPS(oserr, crterr) case oserr: *errno_ptr = crterr; break
142 143 144 145 146 147 148 149 150 151
    ERR_CASE(ERROR_ACCESS_DENIED)
    ERR_CASE(ERROR_NETWORK_ACCESS_DENIED)
    ERR_CASE(ERROR_CANNOT_MAKE)
    ERR_CASE(ERROR_SEEK_ON_DEVICE)
    ERR_CASE(ERROR_LOCK_FAILED)
    ERR_CASE(ERROR_FAIL_I24)
    ERR_CASE(ERROR_CURRENT_DIRECTORY)
    ERR_CASE(ERROR_DRIVE_LOCKED)
    ERR_CASE(ERROR_NOT_LOCKED)
    ERR_CASE(ERROR_INVALID_ACCESS)
152
    ERR_CASE(ERROR_SHARING_VIOLATION)
153 154 155 156 157 158 159 160 161 162 163 164 165 166
    ERR_MAPS(ERROR_LOCK_VIOLATION,       MSVCRT_EACCES);
    ERR_CASE(ERROR_FILE_NOT_FOUND)
    ERR_CASE(ERROR_NO_MORE_FILES)
    ERR_CASE(ERROR_BAD_PATHNAME)
    ERR_CASE(ERROR_BAD_NETPATH)
    ERR_CASE(ERROR_INVALID_DRIVE)
    ERR_CASE(ERROR_BAD_NET_NAME)
    ERR_CASE(ERROR_FILENAME_EXCED_RANGE)
    ERR_MAPS(ERROR_PATH_NOT_FOUND,       MSVCRT_ENOENT);
    ERR_MAPS(ERROR_IO_DEVICE,            MSVCRT_EIO);
    ERR_MAPS(ERROR_BAD_FORMAT,           MSVCRT_ENOEXEC);
    ERR_MAPS(ERROR_INVALID_HANDLE,       MSVCRT_EBADF);
    ERR_CASE(ERROR_OUTOFMEMORY)
    ERR_CASE(ERROR_INVALID_BLOCK)
167
    ERR_CASE(ERROR_NOT_ENOUGH_QUOTA)
168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186
    ERR_MAPS(ERROR_ARENA_TRASHED,        MSVCRT_ENOMEM);
    ERR_MAPS(ERROR_BUSY,                 MSVCRT_EBUSY);
    ERR_CASE(ERROR_ALREADY_EXISTS)
    ERR_MAPS(ERROR_FILE_EXISTS,          MSVCRT_EEXIST);
    ERR_MAPS(ERROR_BAD_DEVICE,           MSVCRT_ENODEV);
    ERR_MAPS(ERROR_TOO_MANY_OPEN_FILES,  MSVCRT_EMFILE);
    ERR_MAPS(ERROR_DISK_FULL,            MSVCRT_ENOSPC);
    ERR_MAPS(ERROR_BROKEN_PIPE,          MSVCRT_EPIPE);
    ERR_MAPS(ERROR_POSSIBLE_DEADLOCK,    MSVCRT_EDEADLK);
    ERR_MAPS(ERROR_DIR_NOT_EMPTY,        MSVCRT_ENOTEMPTY);
    ERR_MAPS(ERROR_BAD_ENVIRONMENT,      MSVCRT_E2BIG);
    ERR_CASE(ERROR_WAIT_NO_CHILDREN)
    ERR_MAPS(ERROR_CHILD_NOT_COMPLETE,   MSVCRT_ECHILD);
    ERR_CASE(ERROR_NO_PROC_SLOTS)
    ERR_CASE(ERROR_MAX_THRDS_REACHED)
    ERR_MAPS(ERROR_NESTING_NOT_ALLOWED,  MSVCRT_EAGAIN);
  default:
    /*  Remaining cases map to EINVAL */
    /* FIXME: may be missing some errors above */
187
    *errno_ptr = MSVCRT_EINVAL;
188 189 190
  }
}

191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206
/*********************************************************************
 * __sys_nerr (MSVCR100.@)
 */
int* CDECL __sys_nerr(void)
{
    return (int*)&MSVCRT__sys_nerr;
}

/*********************************************************************
 *  __sys_errlist (MSVCR100.@)
 */
char** CDECL __sys_errlist(void)
{
    return MSVCRT__sys_errlist;
}

207 208 209
/*********************************************************************
 *		_errno (MSVCRT.@)
 */
210
int* CDECL MSVCRT__errno(void)
211
{
212
    return &msvcrt_get_thread_data()->thread_errno;
213 214 215 216 217
}

/*********************************************************************
 *		__doserrno (MSVCRT.@)
 */
218
MSVCRT_ulong* CDECL MSVCRT___doserrno(void)
219
{
220
    return &msvcrt_get_thread_data()->thread_doserrno;
221 222
}

223 224 225 226 227 228 229 230 231 232 233 234
/*********************************************************************
 *		_get_errno (MSVCRT.@)
 */
int CDECL _get_errno(int *pValue)
{
    if (!pValue)
        return MSVCRT_EINVAL;

    *pValue = *MSVCRT__errno();
    return 0;
}

235 236 237 238 239 240 241 242 243 244 245 246
/*********************************************************************
 *		_get_doserrno (MSVCRT.@)
 */
int CDECL _get_doserrno(int *pValue)
{
    if (!pValue)
        return MSVCRT_EINVAL;

    *pValue = *MSVCRT___doserrno();
    return 0;
}

247 248 249 250 251 252 253 254 255
/*********************************************************************
 *		_set_errno (MSVCRT.@)
 */
int CDECL _set_errno(int value)
{
    *MSVCRT__errno() = value;
    return 0;
}

256 257 258 259 260 261 262 263 264
/*********************************************************************
 *		_set_doserrno (MSVCRT.@)
 */
int CDECL _set_doserrno(int value)
{
    *MSVCRT___doserrno() = value;
    return 0;
}

265 266 267
/*********************************************************************
 *		strerror (MSVCRT.@)
 */
268
char* CDECL MSVCRT_strerror(int err)
269
{
270 271 272 273 274 275 276 277
    thread_data_t *data = msvcrt_get_thread_data();

    if (!data->strerror_buffer)
        if (!(data->strerror_buffer = MSVCRT_malloc(256))) return NULL;

    if (err < 0 || err > MSVCRT__sys_nerr) err = MSVCRT__sys_nerr;
    strcpy( data->strerror_buffer, MSVCRT__sys_errlist[err] );
    return data->strerror_buffer;
278 279
}

280 281 282
/**********************************************************************
 *		strerror_s	(MSVCRT.@)
 */
283
int CDECL MSVCRT_strerror_s(char *buffer, MSVCRT_size_t numberOfElements, int errnum)
284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306
{
    char *ptr;

    if (!buffer || !numberOfElements)
    {
        *MSVCRT__errno() = MSVCRT_EINVAL;
        return MSVCRT_EINVAL;
    }

    if (errnum < 0 || errnum > MSVCRT__sys_nerr)
        errnum = MSVCRT__sys_nerr;

    ptr = MSVCRT__sys_errlist[errnum];
    while (*ptr && numberOfElements > 1)
    {
        *buffer++ = *ptr++;
        numberOfElements--;
    }

    *buffer = '\0';
    return 0;
}

307 308 309
/**********************************************************************
 *		_strerror	(MSVCRT.@)
 */
310
char* CDECL MSVCRT__strerror(const char* str)
311
{
312 313 314 315 316 317 318 319 320 321 322 323 324 325 326
    thread_data_t *data = msvcrt_get_thread_data();
    int err;

    if (!data->strerror_buffer)
        if (!(data->strerror_buffer = MSVCRT_malloc(256))) return NULL;

    err = data->thread_errno;
    if (err < 0 || err > MSVCRT__sys_nerr) err = MSVCRT__sys_nerr;

    if (str && *str)
        sprintf( data->strerror_buffer, "%s: %s\n", str, MSVCRT__sys_errlist[err] );
    else
        sprintf( data->strerror_buffer, "%s\n", MSVCRT__sys_errlist[err] );

    return data->strerror_buffer;
327 328 329 330 331
}

/*********************************************************************
 *		perror (MSVCRT.@)
 */
332
void CDECL MSVCRT_perror(const char* str)
333
{
334 335 336 337 338
    int err = *MSVCRT__errno();
    if (err < 0 || err > MSVCRT__sys_nerr) err = MSVCRT__sys_nerr;

    if (str && *str)
    {
339 340
        MSVCRT__write( 2, str, strlen(str) );
        MSVCRT__write( 2, ": ", 2 );
341
    }
342 343
    MSVCRT__write( 2, MSVCRT__sys_errlist[err], strlen(MSVCRT__sys_errlist[err]) );
    MSVCRT__write( 2, "\n", 1 );
344
}
345

346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367
/*********************************************************************
 *		_wperror (MSVCRT.@)
 */
void CDECL MSVCRT__wperror(const MSVCRT_wchar_t* str)
{
    MSVCRT_size_t size;
    char *buffer = NULL;

    if (str && *str)
    {
        size = MSVCRT_wcstombs(NULL, str, 0);
        if (size == -1) return;
        size++;
        buffer = MSVCRT_malloc(size);
        if (!buffer) return;
        if (MSVCRT_wcstombs(buffer, str, size) == -1)
        {
            MSVCRT_free(buffer);
            return;
        }
    }
    MSVCRT_perror(buffer);
368
    MSVCRT_free(buffer);
369 370
}

371 372 373
/*********************************************************************
 *		_wcserror_s (MSVCRT.@)
 */
374
int CDECL MSVCRT__wcserror_s(MSVCRT_wchar_t* buffer, MSVCRT_size_t nc, int err)
375
{
376 377 378
    if (!MSVCRT_CHECK_PMT(buffer != NULL)) return MSVCRT_EINVAL;
    if (!MSVCRT_CHECK_PMT(nc > 0)) return MSVCRT_EINVAL;

379 380 381 382 383
    if (err < 0 || err > MSVCRT__sys_nerr) err = MSVCRT__sys_nerr;
    MultiByteToWideChar(CP_ACP, 0, MSVCRT__sys_errlist[err], -1, buffer, nc);
    return 0;
}

384 385 386
/*********************************************************************
 *		_wcserror (MSVCRT.@)
 */
387
MSVCRT_wchar_t* CDECL MSVCRT__wcserror(int err)
388 389 390 391 392
{
    thread_data_t *data = msvcrt_get_thread_data();

    if (!data->wcserror_buffer)
        if (!(data->wcserror_buffer = MSVCRT_malloc(256 * sizeof(MSVCRT_wchar_t)))) return NULL;
393
    MSVCRT__wcserror_s(data->wcserror_buffer, 256, err);
394 395 396 397
    return data->wcserror_buffer;
}

/**********************************************************************
398
 *		__wcserror_s	(MSVCRT.@)
399
 */
400
int CDECL MSVCRT___wcserror_s(MSVCRT_wchar_t* buffer, MSVCRT_size_t nc, const MSVCRT_wchar_t* str)
401 402 403 404 405 406
{
    int err;
    static const WCHAR colonW[] = {':', ' ', '\0'};
    static const WCHAR nlW[] = {'\n', '\0'};
    size_t len;

407
    err = *MSVCRT__errno();
408 409
    if (err < 0 || err > MSVCRT__sys_nerr) err = MSVCRT__sys_nerr;

410 411 412 413
    len = MultiByteToWideChar(CP_ACP, 0, MSVCRT__sys_errlist[err], -1, NULL, 0) + 1 /* \n */;
    if (str && *str) len += lstrlenW(str) + 2 /* ': ' */;
    if (len > nc)
    {
414
        MSVCRT_INVALID_PMT("buffer[nc] is too small", MSVCRT_ERANGE);
415 416
        return MSVCRT_ERANGE;
    }
417 418
    if (str && *str)
    {
419 420
        lstrcpyW(buffer, str);
        lstrcatW(buffer, colonW);
421
    }
422 423 424 425 426 427 428 429 430 431 432
    else buffer[0] = '\0';
    len = lstrlenW(buffer);
    MultiByteToWideChar(CP_ACP, 0, MSVCRT__sys_errlist[err], -1, buffer + len, 256 - len);
    lstrcatW(buffer, nlW);

    return 0;
}

/**********************************************************************
 *		__wcserror	(MSVCRT.@)
 */
433
MSVCRT_wchar_t* CDECL MSVCRT___wcserror(const MSVCRT_wchar_t* str)
434 435 436 437 438 439 440
{
    thread_data_t *data = msvcrt_get_thread_data();
    int err;

    if (!data->wcserror_buffer)
        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);
443 444 445 446

    return data->wcserror_buffer;
}

447 448 449
/******************************************************************************
 *		_seterrormode (MSVCRT.@)
 */
450
void CDECL _seterrormode(int mode)
451 452 453
{
    SetErrorMode( mode );
}
454

455 456 457 458 459 460
/******************************************************************************
 *		_invalid_parameter (MSVCRT.@)
 */
void __cdecl MSVCRT__invalid_parameter(const MSVCRT_wchar_t *expr, const MSVCRT_wchar_t *func,
                                       const MSVCRT_wchar_t *file, unsigned int line, MSVCRT_uintptr_t arg)
{
461 462 463 464 465 466 467 468 469 470
#if _MSVCR_VER >= 140
    thread_data_t *data = msvcrt_get_thread_data();

    if (data->invalid_parameter_handler)
    {
        data->invalid_parameter_handler( expr, func, file, line, arg );
        return;
    }
#endif

471 472 473 474
    if (invalid_parameter_handler) invalid_parameter_handler( expr, func, file, line, arg );
    else
    {
        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
478 479 480
    }
}

481 482 483 484 485 486 487 488
/*********************************************************************
 * _invalid_parameter_noinfo (MSVCR100.@)
 */
void CDECL _invalid_parameter_noinfo(void)
{
    MSVCRT__invalid_parameter( NULL, NULL, NULL, 0, 0 );
}

489 490 491
/*********************************************************************
 * _get_invalid_parameter_handler (MSVCR80.@)
 */
492 493 494
MSVCRT_invalid_parameter_handler CDECL _get_invalid_parameter_handler(void)
{
    TRACE("\n");
495
    return invalid_parameter_handler;
496 497
}

498 499 500
/*********************************************************************
 * _set_invalid_parameter_handler (MSVCR80.@)
 */
501 502 503
MSVCRT_invalid_parameter_handler CDECL _set_invalid_parameter_handler(
        MSVCRT_invalid_parameter_handler handler)
{
504
    MSVCRT_invalid_parameter_handler old = invalid_parameter_handler;
505 506 507

    TRACE("(%p)\n", handler);

508
    invalid_parameter_handler = handler;
509 510
    return old;
}
511

512
#if _MSVCR_VER >= 140
513 514 515 516 517
/*********************************************************************
 * _get_thread_local_invalid_parameter_handler (UCRTBASE.@)
 */
MSVCRT_invalid_parameter_handler CDECL _get_thread_local_invalid_parameter_handler(void)
{
518 519
    TRACE("\n");
    return msvcrt_get_thread_data()->invalid_parameter_handler;
520 521 522 523 524 525 526 527
}

/*********************************************************************
 * _set_thread_local_invalid_parameter_handler (UCRTBASE.@)
 */
MSVCRT_invalid_parameter_handler CDECL _set_thread_local_invalid_parameter_handler(
        MSVCRT_invalid_parameter_handler handler)
{
528 529 530 531 532 533 534
    thread_data_t *data = msvcrt_get_thread_data();
    MSVCRT_invalid_parameter_handler old = data->invalid_parameter_handler;

    TRACE("(%p)\n", handler);

    data->invalid_parameter_handler = handler;
    return old;
535
}
536
#endif