/* WARNING: This file was generated by dkct. Changes you make here will be lost if dkct is run again! You should modify the original source and run dkct on it. Original source: dk4fd.ctr */ /* Copyright (C) 2015-2017, Dirk Krause Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met: * Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer. * Redistributions in binary form must reproduce the above opyright notice, this list of conditions and the following disclaimer in the documentation and/or other materials provided with the distribution. * Neither the name of the author nor the names of contributors may be used to endorse or promote products derived from this software without specific prior written permission. THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS ``AS IS'' AND ANY EXPRESS OR 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 COPYRIGHT OWNER OR CONTRIBUTORS 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. */ /** @file dk4fd.c The dk4fd module. */ #line 260 "dk4fd.ctr" #include "dk4fd.h" #if DK4_HAVE_ERRNO_H #ifndef ERRNO_H_INCLUDED #include #define ERRNO_H_INCLUDED 1 #endif #endif #if DK4_HAVE_UNISTD_H #ifndef UNISTD_H_INCLUDED #include #define UNISTD_H_INCLUDED 1 #endif #endif #if DK4_HAVE_SHARE_H #ifndef SHARE_H_INCLUDED #include #define SHARE_H_INCLUDED 1 #endif #endif #line 287 "dk4fd.ctr" int dk4fd_c8_open(const char *path, int flags, dk4_er_t *erp) { int back = -1; if (NULL != path) { #if DK4_ON_WINDOWS #if _MSC_VER >= 1700 int res; if (0 != (res = _sopen_s(&back,path,flags,_SH_DENYNO,(_S_IREAD|_S_IWRITE)))) { if (-1 != back) { _close(back); back = -1; } dk4error_set_idetails(erp, DK4_E_OPEN_FAILED, res); } #else errno = 0; back = _open(path, flags, (_S_IREAD | _S_IWRITE)); if (-1 == back) { dk4error_set_idetails(erp, DK4_E_OPEN_FAILED, errno); } #endif #else errno = 0; back = open(path, flags, 0600); if (-1 == back) { dk4error_set_idetails(erp, DK4_E_OPEN_FAILED, errno); } #endif } else { dk4error_set_simple_error_code(erp, DK4_E_INVALID_ARGUMENTS); } return back; } int dk4fd_wc_open(const wchar_t *path, int flags, dk4_er_t *erp) { int back = -1; if (NULL != path) { #if DK4_ON_WINDOWS #if _MSC_VER >= 1700 int res; if (0 != (res=_wsopen_s(&back,path,flags,_SH_DENYNO,(_S_IREAD|_S_IWRITE)))) { if (-1 != back) { _close(back); back = -1; } dk4error_set_idetails(erp, DK4_E_OPEN_FAILED, res); } #else errno = 0; back = _wopen(path, flags, (_S_IREAD | _S_IWRITE)); if (-1 == back) { dk4error_set_idetails(erp, DK4_E_OPEN_FAILED, errno); } #endif #else dk4error_set_simple_error_code(erp, DK4_E_NOT_SUPPORTED); #endif } else { dk4error_set_simple_error_code(erp, DK4_E_INVALID_ARGUMENTS); } return back; } int dk4fd_open(const dkChar *path, int flags, dk4_er_t *erp) { #if DK4_CHAR_SIZE > 1 return (dk4fd_wc_open(path, flags, erp)); #else return (dk4fd_c8_open(path, flags, erp)); #endif } int dk4fd_close(int fd, dk4_er_t *erp) { int back = 0; #if DK4_ON_WINDOWS errno = 0; if (0 == _close(fd)) { back = 1; } else { dk4error_set_idetails(erp, DK4_E_CLOSE_FAILED, errno); } #else errno = 0; if (0 == close(fd)) { back = 1; } else { dk4error_set_idetails(erp, DK4_E_CLOSE_FAILED, errno); } #endif return back; } #if !DK4_ON_WINDOWS #ifndef SSIZE_MAX /** Maximum value for ssize_t. */ #define SSIZE_MAX ((SIZE_MAX) / 2U) #endif #endif int dk4fd_write(int fd, const void *buffer, size_t sz, dk4_er_t *erp) { #if DK4_ON_WINDOWS int back = 0; int res; if ((NULL != buffer) && (0 < sz)) { if ((dk4_um_t)INT_MAX >= (dk4_um_t)sz) { errno = 0; res = _write(fd, buffer, (unsigned)sz); if ((unsigned)res == (unsigned)sz) { back = 1; } else { if (0 <= res) { dk4error_set_elsize_nelem(erp, DK4_E_WRITE_FAILED, 1, (size_t)res); } else { dk4error_set_elsize_nelem(erp, DK4_E_WRITE_FAILED, 1, 0); } } } else { dk4error_set_simple_error_code(erp, DK4_E_INVALID_ARGUMENTS); } } else { dk4error_set_simple_error_code(erp, DK4_E_INVALID_ARGUMENTS); } return back; #else ssize_t res; int back = 0; if ((NULL != buffer) && (0 < sz)) { if ((dk4_um_t)SSIZE_MAX >= (dk4_um_t)sz) { errno = 0; res = write(fd, buffer, sz); if ((size_t)res == sz) { back = 1; } else { if (0 <= res) { dk4error_set_elsize_nelem(erp, DK4_E_WRITE_FAILED, 1, res); } else { dk4error_set_elsize_nelem(erp, DK4_E_WRITE_FAILED, 1, 0); } } } else { dk4error_set_simple_error_code(erp, DK4_E_INVALID_ARGUMENTS); } } else { dk4error_set_simple_error_code(erp, DK4_E_INVALID_ARGUMENTS); } return back; #endif } int dk4fd_read(int fd, void *buffer, size_t *psz, dk4_er_t *erp) { #if DK4_ON_WINDOWS int res = 0; /* Result from read operation */ int back = 0; if ((NULL != buffer) && (NULL != psz)) { if (0 < *psz) { if ((dk4_um_t)INT_MAX >= (dk4_um_t)(*psz)) { errno = 0; res = _read(fd, buffer, (unsigned)(*psz)); if (0 < res) { back = 1; *psz = (size_t)res; } else { if (0 > res) { back = 0; *psz = 0; dk4error_set_idetails(erp, DK4_E_READ_FAILED, errno); } else { back = 1; *psz = 0; } } } else { dk4error_set_simple_error_code(erp, DK4_E_INVALID_ARGUMENTS); } } else { dk4error_set_simple_error_code(erp, DK4_E_INVALID_ARGUMENTS); } } else { dk4error_set_simple_error_code(erp, DK4_E_INVALID_ARGUMENTS); } return back; #else int back = 0; ssize_t res; if ((NULL != buffer) && (NULL != psz)) { if (0 < *psz) { if ((dk4_um_t)SSIZE_MAX >= (dk4_um_t)(*psz)) { errno = 0; res = read(fd, buffer, *psz); if (0 < res) { back = 1; *psz = (size_t)res; } else { if (0 > res) { back = 0; *psz = 0; dk4error_set_idetails(erp, DK4_E_READ_FAILED, errno); } else { back = 1; *psz = 0; } } } else { dk4error_set_simple_error_code(erp, DK4_E_INVALID_ARGUMENTS); } } else { dk4error_set_simple_error_code(erp, DK4_E_INVALID_ARGUMENTS); } } else { dk4error_set_simple_error_code(erp, DK4_E_INVALID_ARGUMENTS); } return back; #endif }