hio/lib/sys-err.c

250 lines
5.8 KiB
C
Raw Permalink Normal View History

/*
Copyright (c) 2016-2020 Chung, Hyung-Hwan. All rights reserved.
Redistribution and use in source and binary forms, with or without
modification, are permitted provided that the following conditions
are met:
1. Redistributions of source code must retain the above copyright
notice, this list of conditions and the following disclaimer.
2. Redistributions in binary form must reproduce the above copyright
notice, this list of conditions and the following disclaimer in the
documentation and/or other materials provided with the distribution.
THIS SOFTWARE IS PROVIDED BY THE AUTHOR "AS IS" AND ANY EXPRESS OR
2022-06-11 05:32:01 +00:00
IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
2021-07-22 07:30:20 +00:00
#include "hio-prv.h"
#include <errno.h>
2020-02-11 09:02:46 +00:00
#include <string.h>
2021-07-22 07:30:20 +00:00
static hio_errnum_t errno_to_errnum (int errcode)
{
switch (errcode)
{
2021-07-22 07:30:20 +00:00
case ENOMEM: return HIO_ESYSMEM;
case EINVAL: return HIO_EINVAL;
#if defined(EBUSY)
2021-07-22 07:30:20 +00:00
case EBUSY: return HIO_EBUSY;
#endif
2021-07-22 07:30:20 +00:00
case EACCES: return HIO_EACCES;
#if defined(EPERM)
2021-07-22 07:30:20 +00:00
case EPERM: return HIO_EPERM;
#endif
2022-11-15 14:31:14 +00:00
#if defined(EISDIR)
case EISDIR: return HIO_EISDIR;
#endif
#if defined(ENOTDIR)
2021-07-22 07:30:20 +00:00
case ENOTDIR: return HIO_ENOTDIR;
#endif
2021-07-22 07:30:20 +00:00
case ENOENT: return HIO_ENOENT;
#if defined(EEXIST)
2021-07-22 07:30:20 +00:00
case EEXIST: return HIO_EEXIST;
#endif
#if defined(EINTR)
2021-07-22 07:30:20 +00:00
case EINTR: return HIO_EINTR;
#endif
#if defined(EPIPE)
2021-07-22 07:30:20 +00:00
case EPIPE: return HIO_EPIPE;
#endif
#if defined(EAGAIN) && defined(EWOULDBLOCK) && (EAGAIN != EWOULDBLOCK)
case EAGAIN:
2021-07-22 07:30:20 +00:00
case EWOULDBLOCK: return HIO_EAGAIN;
#elif defined(EAGAIN)
2021-07-22 07:30:20 +00:00
case EAGAIN: return HIO_EAGAIN;
#elif defined(EWOULDBLOCK)
2021-07-22 07:30:20 +00:00
case EWOULDBLOCK: return HIO_EAGAIN;
#endif
#if defined(EBADF)
2021-07-22 07:30:20 +00:00
case EBADF: return HIO_EBADHND;
#endif
#if defined(EIO)
2021-07-22 07:30:20 +00:00
case EIO: return HIO_EIOERR;
#endif
2019-01-27 02:09:22 +00:00
#if defined(EMFILE)
case EMFILE:
2021-07-22 07:30:20 +00:00
return HIO_EMFILE;
2019-01-27 02:09:22 +00:00
#endif
#if defined(ENFILE)
case ENFILE:
2021-07-22 07:30:20 +00:00
return HIO_ENFILE;
2019-01-27 02:09:22 +00:00
#endif
#if defined(ECONNREFUSED)
case ECONNREFUSED:
2021-07-22 07:30:20 +00:00
return HIO_ECONRF;
2019-01-27 02:09:22 +00:00
#endif
#if defined(ECONNRESETD)
case ECONNRESET:
2021-07-22 07:30:20 +00:00
return HIO_ECONRS;
2019-01-27 02:09:22 +00:00
#endif
2021-07-22 07:30:20 +00:00
default: return HIO_ESYSERR;
}
}
#if defined(_WIN32)
2021-07-22 07:30:20 +00:00
static hio_errnum_t winerr_to_errnum (DWORD errcode)
{
switch (errcode)
{
case ERROR_NOT_ENOUGH_MEMORY:
case ERROR_OUTOFMEMORY:
2021-07-22 07:30:20 +00:00
return HIO_ESYSMEM;
case ERROR_INVALID_PARAMETER:
case ERROR_INVALID_NAME:
2021-07-22 07:30:20 +00:00
return HIO_EINVAL;
case ERROR_INVALID_HANDLE:
2021-07-22 07:30:20 +00:00
return HIO_EBADHND;
case ERROR_ACCESS_DENIED:
case ERROR_SHARING_VIOLATION:
2021-07-22 07:30:20 +00:00
return HIO_EACCES;
#if defined(ERROR_IO_PRIVILEGE_FAILED)
case ERROR_IO_PRIVILEGE_FAILED:
#endif
case ERROR_PRIVILEGE_NOT_HELD:
2021-07-22 07:30:20 +00:00
return HIO_EPERM;
case ERROR_FILE_NOT_FOUND:
case ERROR_PATH_NOT_FOUND:
2021-07-22 07:30:20 +00:00
return HIO_ENOENT;
case ERROR_ALREADY_EXISTS:
case ERROR_FILE_EXISTS:
2021-07-22 07:30:20 +00:00
return HIO_EEXIST;
case ERROR_BROKEN_PIPE:
2021-07-22 07:30:20 +00:00
return HIO_EPIPE;
default:
2021-07-22 07:30:20 +00:00
return HIO_ESYSERR;
}
}
#endif
#if defined(__OS2__)
2021-07-22 07:30:20 +00:00
static hio_errnum_t os2err_to_errnum (APIRET errcode)
{
/* APIRET e */
switch (errcode)
{
case ERROR_NOT_ENOUGH_MEMORY:
2021-07-22 07:30:20 +00:00
return HIO_ESYSMEM;
case ERROR_INVALID_PARAMETER:
case ERROR_INVALID_NAME:
2021-07-22 07:30:20 +00:00
return HIO_EINVAL;
case ERROR_INVALID_HANDLE:
2021-07-22 07:30:20 +00:00
return HIO_EBADHND;
case ERROR_ACCESS_DENIED:
case ERROR_SHARING_VIOLATION:
2021-07-22 07:30:20 +00:00
return HIO_EACCES;
case ERROR_FILE_NOT_FOUND:
case ERROR_PATH_NOT_FOUND:
2021-07-22 07:30:20 +00:00
return HIO_ENOENT;
case ERROR_ALREADY_EXISTS:
2021-07-22 07:30:20 +00:00
return HIO_EEXIST;
/*TODO: add more mappings */
default:
2021-07-22 07:30:20 +00:00
return HIO_ESYSERR;
}
}
#endif
#if defined(macintosh)
2021-07-22 07:30:20 +00:00
static hio_errnum_t macerr_to_errnum (int errcode)
{
switch (e)
{
case notEnoughMemoryErr:
2021-07-22 07:30:20 +00:00
return HIO_ESYSMEM;
case paramErr:
2021-07-22 07:30:20 +00:00
return HIO_EINVAL;
case qErr: /* queue element not found during deletion */
case fnfErr: /* file not found */
case dirNFErr: /* direcotry not found */
case resNotFound: /* resource not found */
case resFNotFound: /* resource file not found */
case nbpNotFound: /* name not found on remove */
2021-07-22 07:30:20 +00:00
return HIO_ENOENT;
/*TODO: add more mappings */
default:
2021-07-22 07:30:20 +00:00
return HIO_ESYSERR;
}
}
#endif
2021-07-22 07:30:20 +00:00
hio_errnum_t hio_sys_syserrstrb (hio_t* hio, int syserr_type, int syserr_code, hio_bch_t* buf, hio_oow_t len)
{
switch (syserr_type)
{
case 1:
#if defined(_WIN32)
if (buf)
{
DWORD rc;
rc = FormatMessageA (
FORMAT_MESSAGE_FROM_SYSTEM | FORMAT_MESSAGE_IGNORE_INSERTS,
NULL, syserr_code, MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT),
2021-07-22 07:30:20 +00:00
buf, len, HIO_NULL
);
while (rc > 0 && buf[rc - 1] == '\r' || buf[rc - 1] == '\n') buf[--rc] = '\0';
}
return winerr_to_errnum(syserr_code);
#elif defined(__OS2__)
/* TODO: convert code to string */
2021-07-22 07:30:20 +00:00
if (buf) hio_copy_bcstr (buf, len, "system error");
return os2err_to_errnum(syserr_code);
#elif defined(macintosh)
/* TODO: convert code to string */
2021-07-22 07:30:20 +00:00
if (buf) hio_copy_bcstr (buf, len, "system error");
return os2err_to_errnum(syserr_code);
#else
/* in other systems, errno is still the native system error code.
* fall thru */
#endif
case 0:
#if defined(HAVE_STRERROR_R)
if (buf) strerror_r (syserr_code, buf, len);
#else
/* this is not thread safe */
2021-07-22 07:30:20 +00:00
if (buf) hio_copy_bcstr (buf, len, strerror(syserr_code));
#endif
return errno_to_errnum(syserr_code);
}
2020-02-11 09:02:46 +00:00
2021-07-22 07:30:20 +00:00
if (buf) hio_copy_bcstr (buf, len, "system error");
return HIO_ESYSERR;
}