/* 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: dk4fopwc.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 dk4fopwc.c The dk4fopwc module. */ #line 113 "dk4fopwc.ctr" #include "dk4conf.h" #if DK4_ON_WINDOWS #ifndef WINDOWS_H_INCLUDED #include #define WINDOWS_H_INCLUDED 1 #endif #endif #include "dk4fopc8.h" #include "dk4fopwc.h" #ifndef DK4STAT8_H_INCLUDED #include "dk4statw.h" #endif #ifndef DK4STAT_H_INCLUDED #include "dk4stat.h" #endif #if DK4_HAVE_SYS_TYPES_H #ifndef SYS_TYPES_H_INCLUDED #include #define SYS_TYPES_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_STRING_H #ifndef STRING_H_INCLUDED #include #define STRING_H_INCLUDED 1 #endif #endif #if DK4_HAVE_ERRNO_H #ifndef ERRNO_H_INCLUDED #include #define ERRNO_H_INCLUDED 1 #endif #endif #line 165 "dk4fopwc.ctr" FILE * dk4fopen_wc(const wchar_t *name, const wchar_t *mode, int tests, dk4_er_t *erp) { FILE *back = NULL; const wchar_t *cptr; int ww = 0; #if DK4_HAVE__WFOPEN_S errno_t errn; #endif if ((NULL != name) && (NULL != mode)) { cptr = mode; while(*cptr) { if (L'w' == *cptr) { ww = 1; } if (L'a' == *cptr) { ww = 1; } if (L'+' == *cptr) { ww = 1; } cptr++; } if (0 != dk4fopen_check_wc(name, ww, tests, erp)) { #if DK4_HAVE__WFOPEN_S errn = _wfopen_s(&back, name, mode); if (0 != errn) { back = NULL; dk4error_set_idetails( erp, ((ww) ? DK4_E_OPEN_WRITE_FAILED : DK4_E_OPEN_READ_FAILED), errn ); } #else #if DK4_HAVE__WFOPEN64 errno = 0; back = _wfopen64(name, mode); if (NULL == back) { dk4error_set_idetails( erp, ((ww) ? DK4_E_OPEN_WRITE_FAILED : DK4_E_OPEN_READ_FAILED), errno ); } #else #if DK4_HAVE__WFOPEN errno = 0; back = _wfopen(name, mode); if (NULL == back) { dk4error_set_idetails( erp, ((ww) ? DK4_E_OPEN_WRITE_FAILED : DK4_E_OPEN_READ_FAILED), errn ); } #else dk4error_set_simple_error_code(erp, DK4_E_NOT_SUPPORTED); #endif #endif #endif } } else { dk4error_set_simple_error_code(erp, DK4_E_INVALID_ARGUMENTS); } return back; } int dk4fopen_check_wc(const wchar_t *name, int ww, int tests, dk4_er_t *erp) { #if DK4_ON_WINDOWS WIN32_FIND_DATAW ffdata; HANDLE ha; DWORD dwattr; int back = 1; #line 236 "dk4fopwc.ctr" /* Check name */ if (NULL == name) { #line 239 "dk4fopwc.ctr" dk4error_set_simple_error_code(erp, DK4_E_INVALID_ARGUMENTS); back = 0; goto finished; } /* Retrieve attributes. */ dwattr = GetFileAttributesW(name); if (INVALID_FILE_ATTRIBUTES == dwattr) { ha = FindFirstFileW(name, &ffdata); if (INVALID_HANDLE_VALUE != ha) { dwattr = ffdata.dwFileAttributes; FindClose(ha); } } if (INVALID_FILE_ATTRIBUTES == dwattr) { #line 254 "dk4fopwc.ctr" goto finished; } /* Check directory */ if ((DWORD)0UL != (FILE_ATTRIBUTE_DIRECTORY & dwattr)) { #line 259 "dk4fopwc.ctr" back = 0; dk4error_set_simple_error_code(erp, DK4_E_SYNTAX); goto finished; } /* Check device */ if ((DWORD)0UL != (FILE_ATTRIBUTE_DEVICE & dwattr)) { #line 266 "dk4fopwc.ctr" back = 0; dk4error_set_idetails(erp, DK4_E_SEC_CHECK, DK4_FOPEN_SC_IS_REGULAR); } finished: #line 271 "dk4fopwc.ctr" return back; #else dk4_stat_t stb; dk4_stat_t lstb; int back = 1; /* Check arguments. */ if (NULL == name) { dk4error_set_simple_error_code(erp, DK4_E_INVALID_ARGUMENTS); back = 0; goto finished; } /* Check existing path. */ if (0 == dk4stat_wc(&stb, name, NULL)) { #line 288 "dk4fopwc.ctr" goto finished; } /* Check directory. */ if (0 != dk4stat_is_directory(&stb, NULL)) { #line 294 "dk4fopwc.ctr" dk4error_set_simple_error_code(erp, DK4_E_SYNTAX); back = 0; goto finished; } /* Write regular files only. */ if ((0 != ww) && (0 != (tests & DK4_FOPEN_SC_IS_REGULAR))) { if (0 == dk4stat_is_regular(&stb, NULL)) { #line 303 "dk4fopwc.ctr" dk4error_set_idetails(erp, DK4_E_SEC_CHECK, DK4_FOPEN_SC_IS_REGULAR); back = 0; goto finished; } } #if 0 /* Do not write if any path element is a symbolic link. NO SUCH FUNCTIONS KNOWN FOR wchar_t DATA TYPE! */ if ((0 != ww) && (0 != (tests & DK4_FOPEN_SC_WR_SYMLINK_IN_PATH))) { if (0 == dk4fopen_check_no_symlink_in_path(name)) { #line 315 "dk4fopwc.ctr" dk4error_set_idetails(erp, DK4_E_SEC_CHECK, DK4_FOPEN_SC_WR_SYMLINK_IN_PATH); back = 0; goto finished; } } #endif /* Obtain symbolic link information. */ if (0 == dk4stat_l_wc(&lstb, name, NULL)) { #line 325 "dk4fopwc.ctr" goto finished; } /* Do not write to file via link. */ if ((0 != ww) && (0 != (tests & DK4_FOPEN_SC_WR_PATH_IS_SYMLINK))) { if (0 != dk4stat_is_symlink(&lstb, NULL)) { #line 332 "dk4fopwc.ctr" dk4error_set_idetails(erp, DK4_E_SEC_CHECK, DK4_FOPEN_SC_WR_PATH_IS_SYMLINK); back = 0; goto finished; } } /* Do not write file via link if file owner differs from link owner. */ #if DK4_HAVE_FILE_UIDGID if ((0 != ww) && (0 != (tests & DK4_FOPEN_SC_WR_SYMLINK_OWNER))) { if (lstb.st_uid != stb.st_uid) { #line 343 "dk4fopwc.ctr" dk4error_set_idetails(erp, DK4_E_SEC_CHECK, DK4_FOPEN_SC_WR_SYMLINK_OWNER); back = 0; goto finished; } } #endif finished: return back; #endif }