strerror.c

来自「开放源码的编译器open watcom 1.6.0版的源代码」· C语言 代码 · 共 345 行 · 第 1/2 页

C
345
字号
};
#else
char *_sys_errlist[] = {
    /* 0    EZERO           */  "No error",
    /* 1    ENOENT          */  "No such file or directory",
    /* 2    E2BIG           */  "Arg list too big",
    /* 3    ENOEXEC         */  "Exec format error",
    /* 4    EBADF           */  "Bad file number",
    /* 5    ENOMEM          */  "Not enough memory",
    /* 6    EACCES          */  "Permission denied",
    /* 7    EEXIST          */  "File exists",
    /* 8    EXDEV           */  "Cross-device link",
    /* 9    EINVAL          */  "Invalid argument",
    /* 10   ENFILE          */  "File table overflow",
    /* 11   EMFILE          */  "Too many open files",
    /* 12   ENOSPC          */  "No space left on device",
    /* 13   EDOM            */  "Argument too large",
    /* 14   ERANGE          */  "Result too large",
    /* 15   EDEADLK         */  "Resource deadlock would occur",
    /* 16   EINTR           */  "System call interrupted",
    /* 17   ECHILD          */  "Child does not exist",
    /* 18   EAGAIN          */  "Resource unavailable, try again",
    /* 19   EBUSY           */  "Device or resource busy",
    /* 20   EFBIG           */  "File too large",
    /* 21   EIO             */  "I/O error",
    /* 22   EISDIR          */  "Is a directory",
    /* 23   ENOTDIR         */  "Not a directory",
    /* 24   EMLINK          */  "Too many links",
    /* 25   ENOTBLK         */  "Block device required",
    /* 26   ENOTTY          */  "Not a character device",
    /* 27   ENXIO           */  "No such device or address",
    /* 28   EPERM           */  "Not owner",
    /* 29   EPIPE           */  "Broken pipe",
    /* 30   EROFS           */  "Read-only file system",
    /* 31   ESPIPE          */  "Illegal seek",
    /* 32   ESRCH           */  "No such process",
    /* 33   ETXTBSY         */  "Text file busy",
    /* 34   EFAULT          */  "Bad address",
    /* 35   ENAMETOOLONG    */  "Filename too long",
    /* 36   ENODEV          */  "No such device",
    /* 37   ENOLCK          */  "No locks available in system",
    /* 38   ENOSYS          */  "Unknown system call",
    /* 39   ENOTEMPTY       */  "Directory not empty",
    /* 40   EILSEQ          */  "Illegal multibyte sequence"
    /* if more are added, be sure to update _sys_nerr accordingly */
};
#endif

int _WCNEAR _sys_nerr = ( sizeof( _sys_errlist ) / sizeof( *_sys_errlist ) );

#endif

_WCRTLINK CHAR_TYPE *__F_NAME(strerror,wcserror)( int errnum )
{
#ifdef __WIDECHAR__
    static wchar_t  Wide_Error_String[40];
#endif
    char            *msg;

    if( errnum < 0 || errnum >= _sys_nerr ) {
        msg = UNKNOWN_ERROR;
    } else {
        msg = _sys_errlist[ errnum ];
    }
    return( _AToUni( Wide_Error_String, msg ) );
}

// Note: Windows FORMAT_MESSAGE_MAX_WIDTH_MASK is 255

#if !defined(FORMAT_MESSAGE_MAX_WIDTH_MASK)
    #define FORMAT_MESSAGE_MAX_WIDTH_MASK 255
#endif

#ifdef __WIDECHAR__
static  wchar_t Wide_Error_String[FORMAT_MESSAGE_MAX_WIDTH_MASK+1];
#else
static  char    Error_String[FORMAT_MESSAGE_MAX_WIDTH_MASK+1];
#endif


/*
    char *_strerror( const char *strErrMsg );

    Description from the MSDN:

    If strErrMsg is passed as NULL, _strerror returns a pointer to a
    string containing the system error message for the last library call
    that produced an error. The error-message string is terminated by
    the newline character ('\n'). If strErrMsg is not equal to NULL,
    then _strerror returns a pointer to a string containing (in order)
    your string message, a colon, a space, the system error message for
    the last library call producing an error, and a newline character.
    Your string message can be, at most, 94 bytes long.

    The actual error number for _strerror is stored in the variable
    errno. The system error messages are accessed through the variable
    _sys_errlist, which is an array of messages ordered by error number.
    _strerror accesses the appropriate error message by using the errno
    value as an index to the variable _sys_errlist. The value of the
    variable _sys_nerr is defined as the maximum number of elements in
    the _sys_errlist array. To produce accurate results, call _strerror
    immediately after a library routine returns with an error.
    Otherwise, subsequent calls to strerror or _strerror can overwrite
    the errno value.

    _strerror is not part of the ANSI definition but is instead a
    Microsoft extension to it. Do not use it where portability is
    desired; for ANSI compatibility, use strerror instead.

 */

_WCRTLINK CHAR_TYPE *__F_NAME(_strerror,_wcserror)( const CHAR_TYPE *strErrMsg )
{
    int errnum;

    errnum = _RWD_errno;
#ifdef __WIDECHAR__
    Wide_Error_String[0] = L'\0';
    if( strErrMsg != NULL ) {
        wcsncpy( Wide_Error_String, strErrMsg, 94 );
        Wide_Error_String[94] = L'\0';    // just in case more than 94
        wcscat( Wide_Error_String, L": " );
    }
    wcscat( Wide_Error_String, wcserror( errnum ) );
    wcscat( Wide_Error_String, L"\n" );
    return( Wide_Error_String );
#else
    Error_String[0] = '\0';
    if( strErrMsg != NULL ) {
        strncpy( Error_String, strErrMsg, 94 );
        Error_String[94] = '\0';    // just in case more than 94
        strcat( Error_String, ": " );
    }
    strcat( Error_String, strerror( errnum ) );
    strcat( Error_String, "\n" );
    return( Error_String );
#endif
}

#if defined(__NT__)

_WCRTLINK CHAR_TYPE *__F_NAME(_doserror,_wdoserror)( int errnum )
{
#ifdef __WIDECHAR__
    Wide_Error_String[0] = L'\0';
    FormatMessageW( FORMAT_MESSAGE_IGNORE_INSERTS |
                    FORMAT_MESSAGE_FROM_SYSTEM |
                    FORMAT_MESSAGE_MAX_WIDTH_MASK,
                    NULL,
                    errnum,
                    0,
                    Wide_Error_String,
                    FORMAT_MESSAGE_MAX_WIDTH_MASK,
                    NULL );
    return( Wide_Error_String );
#else
    Error_String[0] = '\0';
    FormatMessageA( FORMAT_MESSAGE_IGNORE_INSERTS |
                    FORMAT_MESSAGE_FROM_SYSTEM |
                    FORMAT_MESSAGE_MAX_WIDTH_MASK,
                    NULL,
                    errnum,
                    0,
                    Error_String,
                    FORMAT_MESSAGE_MAX_WIDTH_MASK,
                    NULL );
    return( Error_String );
#endif
}

#endif

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?