/**************************************** * API - write user data to a mailbox * IN: * mb_id - handle for the mailbox * offset - offset within the mailbox from which to start writing * size - size of the data * data - data ****************************************/ mb_error_t mb_write(mb_id_t mb_id, void* data, off_t offset, size_t size) { mb_handle_t* mbhdl; mb_hdr_t* mbhdr; unsigned char* taddr; mbhdl = (mb_handle_t*) mb_id; if (MB_BAD(mbhdl)) { _set_errno(EINVAL); return (MB_ERROR); } mbhdr = (mb_hdr_t*) mbhdl->maddr; if (offset + size + sizeof(mb_hdr_t) > mbhdr->ttSize) { _set_errno(ENOSPC); return (MB_ERROR); } if (lockf(mbhdl->fd, F_LOCK, 0) == -1) { return (MB_ERROR); } taddr = (unsigned char*) mbhdl->maddr; taddr += sizeof(mb_hdr_t) + offset; memcpy(taddr, data, size); (void) lockf(mbhdl->fd, F_ULOCK, 0); return MB_OK; }
int WRT_access(const char * const _FileName, int _AccessMode) { if (!_FileName || (_AccessMode & ~0x6) != 0) { _set_errno(EINVAL); return -1; } try { auto file = AWait(Windows::Storage::StorageFile::GetFileFromPathAsync(ConvertString(_FileName))); if ((file->Attributes & Windows::Storage::FileAttributes::Directory) != Windows::Storage::FileAttributes::Directory && (file->Attributes & Windows::Storage::FileAttributes::ReadOnly) == Windows::Storage::FileAttributes::ReadOnly && (_AccessMode & 0x2) != 0) { throw ref new Platform::AccessDeniedException(); } return 0; } catch (AccessDeniedException^ e) { _set_errno(EACCES); return -1; } catch (Exception^ e)
/* * @implemented */ wchar_t* _ui64tow(unsigned __int64 value, wchar_t* string, int radix) { wchar_t tmp[65]; wchar_t* tp = tmp; long i; unsigned long v = value; wchar_t* sp; if (radix > 36 || radix <= 1) { _set_errno(EDOM); return 0; } while (v || tp == tmp) { i = v % radix; v = v / radix; if (i < 10) *tp++ = i+L'0'; else *tp++ = i + L'a' - 10; } if (string == 0) string = (wchar_t*)malloc(((tp-tmp)+1)*sizeof(wchar_t)); sp = string; while (tp > tmp) *sp++ = *--tp; *sp = 0; return string; }
size_t _wcrtomb_r( char *s, wchar_t wc, mbstate_t *ps) { int retval = 0; char buf[10]; #ifdef _MB_CAPABLE if (ps == NULL) { ps = &(tls_current()->mbst); } #endif if (s == NULL) retval = __WCTOMB (buf, L'\0', ps); else retval = __WCTOMB (s, wc, ps); if (retval == -1) { ps->__count = 0; _set_errno(EILSEQ); return (size_t)(-1); } else return (size_t)retval; }
/********************************************************************** * __wcserror_s (MSVCRT.@) */ int CDECL __wcserror_s(MSVCRT_wchar_t* buffer, MSVCRT_size_t nc, const MSVCRT_wchar_t* str) { int err; static const WCHAR colonW[] = {':', ' ', '\0'}; static const WCHAR nlW[] = {'\n', '\0'}; size_t len; err = *MSVCRT__errno(); if (err < 0 || err > MSVCRT__sys_nerr) err = MSVCRT__sys_nerr; len = MultiByteToWideChar(CP_ACP, 0, MSVCRT__sys_errlist[err], -1, NULL, 0) + 1 /* \n */; if (str && *str) len += lstrlenW(str) + 2 /* ': ' */; if (len > nc) { MSVCRT_INVALID_PMT("buffer[nc] is too small"); _set_errno(MSVCRT_ERANGE); return MSVCRT_ERANGE; } if (str && *str) { lstrcpyW(buffer, str); lstrcatW(buffer, colonW); } 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; }
/********************************************** * API - set current state of a mailbox * IN: * mb_id - handle for the mailbox * state - current mailbox state ***********************************************/ mb_error_t mb_setstate(mb_id_t mb_id, mb_state_t state) { mb_handle_t* mbhdl; mb_hdr_t* mbhdr; mbhdl = (mb_handle_t*) mb_id; if (MB_BAD(mbhdl)) { _set_errno(EINVAL); return (MB_ERROR); } mbhdr = (mb_hdr_t*) mbhdl->maddr; if (lockf(mbhdl->fd, F_LOCK, 0) == -1) { return (MB_ERROR); } mbhdr->curState = state; if ((mbhdl->flags & (MB_OWNER|MB_CALLBACK)) == (MB_OWNER|MB_CALLBACK)) { mbhdl->flags &= ~MB_CALLBACK; } /* State changed - multicast this mailbox */ _mb_net_send(mbhdr); (void) lockf(mbhdl->fd, F_ULOCK, 0); return (MB_OK); }
bool tScriptState::GetParam(CString& s, tParamType paramType, CString& dest, tParameter& param) { bool bOK = FALSE; dest.Empty(); while (isspace(s[0])) s.Delete(0, 1); switch (paramType) { case ptString: if ((s[0] == '\"') || s[0] == '\'') { char endchar = s[0]; s.Delete(0, 1); int pos = s.Find(endchar); if (pos > 0) { while (IsSuitableCharForString(s[0]) || isspace(s[0])) { dest += s[0]; s.Delete(0, 1); } bOK = s[0] == endchar; if (bOK) { s.Delete(0, 1); while (isspace(dest[dest.GetLength() - 1])) dest.Delete(dest.GetLength() - 1, 1); } } } else { while (IsSuitableCharForString(s[0])) { dest += s[0]; s.Delete(0, 1); } } bOK = !dest.IsEmpty(); param = dest; break; case ptInteger: if (s[0] == '-') { dest += s[0]; s.Delete(0, 1); } while (isdigit(s[0])) { dest += s[0]; s.Delete(0, 1); } bOK = !dest.IsEmpty(); param = dest; if (bOK) { char *end = NULL; _set_errno(0); int val = strtoul(dest.GetBuffer(), &end, 10); bOK = !errno; if (bOK) param = (ULONG)val; } else if (s[0] == '$') { s.Delete(0,1); while (IsSuitableCharForString(s[0])) { dest += s[0]; s.Delete(0, 1); } param.Refer(dest, QueryVariable, this); bOK = TRUE; } break; case ptNone: bOK = TRUE; break; default: break; } return bOK; }
WRT_FILE* WRT_fopen(const char * _Filename, const char * _Mode) { WRT_FILE* ret; int err = WRT_fopen_s(&ret, _Filename, _Mode); _set_errno(err); return err ? nullptr : ret; }
int munmap(void *start, size_t length) { int i, j; for( i = 0; i < g_curMMapInfos; i++ ) { if( g_mmapInfos[i].start == start ) break; } if( i == g_curMMapInfos ) { _set_errno( EINVAL ); return -1; } UnmapViewOfFile( g_mmapInfos[g_curMMapInfos].start ); CloseHandle( g_mmapInfos[g_curMMapInfos].hMap ); CloseHandle( g_mmapInfos[g_curMMapInfos].hFile ); EnterCriticalSection( &cs ); for( j = i + 1; j < g_curMMapInfos; j++ ) { memcpy( &g_mmapInfos[ j - 1 ], &g_mmapInfos[ j ], sizeof( struct mmapInfos ) ); } g_curMMapInfos--; if( g_curMMapInfos == 0 ) { free( g_mmapInfos ); g_mmapInfos = NULL; g_maxMMapInfos = 0; } LeaveCriticalSection( &cs ); return 0; }
/* unlink * The is the ANSI C file * deletion method and is shared by the 'modern' */ int unlink( _In_ const char *path) { if (path == NULL) { _set_errno(EINVAL); return EOF; } return _fval(DeletePath(path, 0)); }
/********************************************************************* * _wcserror_s (MSVCRT.@) */ int CDECL _wcserror_s(MSVCRT_wchar_t* buffer, MSVCRT_size_t nc, int err) { if (!MSVCRT_CHECK_PMT(buffer != NULL) || !MSVCRT_CHECK_PMT(nc > 0)) { _set_errno(MSVCRT_EINVAL); return MSVCRT_EINVAL; } if (err < 0 || err > MSVCRT__sys_nerr) err = MSVCRT__sys_nerr; MultiByteToWideChar(CP_ACP, 0, MSVCRT__sys_errlist[err], -1, buffer, nc); return 0; }
/* Set errno variable */ void dirent_set_errno( int error) { #if defined(_MSC_VER) && _MSC_VER >= 1400 /* Microsoft Visual Studio 2005 and later */ _set_errno (error); #else /* Non-Microsoft compiler or older Microsoft compiler */ errno = error; #endif }
wint_t fgetwc(FILE *stream) { StdioObject_t* Object; wint_t Result; int ch; _lock_file(stream); Object = get_ioinfo(stream->_fd); if (Object == NULL) { _unlock_file(stream); _set_errno(EBADFD); return (wint_t)-1; } if ((Object->exflag & (EF_UTF8 | EF_UTF16)) || !(Object->wxflag & WX_TEXT)) { char *p; for (p = (char *)&Result; (wint_t *)p < &Result + 1; p++) { ch = fgetc(stream); if (ch == EOF) { Result = WEOF; break; } *p = (char)ch; } } else { char mbs[MB_LEN_MAX]; int len = 0; ch = fgetc(stream); if (ch != EOF) { mbs[0] = (char)ch; if (_issjis1((unsigned char)mbs[0])) { ch = fgetc(stream); if (ch != EOF) { mbs[1] = (char)ch; len = 2; } } else { len = 1; } } if (!len || mbtowc((wchar_t*)&Result, mbs, len) == -1) Result = WEOF; } _unlock_file(stream); return Result; }
/********************************************************************* * wcsncat_s (MSVCRT.@) * */ INT CDECL wcsncat_s(wchar_t *dst, size_t elem, const wchar_t *src, size_t count) { size_t srclen; wchar_t dststart; INT ret = 0; if (!MSVCRT_CHECK_PMT(dst != NULL) || !MSVCRT_CHECK_PMT(elem > 0)) { #ifndef _LIBCNT_ _set_errno(EINVAL); #endif return EINVAL; } if (!MSVCRT_CHECK_PMT(src != NULL || count == 0)) return EINVAL; if (count == 0) return 0; for (dststart = 0; dststart < elem; dststart++) { if (dst[dststart] == '\0') break; } if (dststart == elem) { MSVCRT_INVALID_PMT("dst[elem] is not NULL terminated\n", EINVAL); return EINVAL; } if (count == _TRUNCATE) { srclen = strlenW(src); if (srclen >= (elem - dststart)) { srclen = elem - dststart - 1; ret = STRUNCATE; } } else srclen = min(strlenW(src), count); if (srclen < (elem - dststart)) { memcpy(&dst[dststart], src, srclen*sizeof(wchar_t)); dst[dststart+srclen] = '\0'; return ret; } MSVCRT_INVALID_PMT("dst[elem] is too small", ERANGE); dst[0] = '\0'; return ERANGE; }
/******************************************** * API - return the data length of a mailbox * IN: * mb_id - handle for the mailbox ********************************************/ size_t mb_len(mb_id_t mb_id) { mb_handle_t* mbhdl; mb_hdr_t* mbhdr; mbhdl = (mb_handle_t*) mb_id; if (MB_BAD(mbhdl)) { _set_errno(EINVAL); return (MB_ERROR); } mbhdr = (mb_hdr_t*) mbhdl->maddr; return mbhdr->ttSize - sizeof(mb_hdr_t); }
/****************************************************************** * _wcsupr_s (MSVCRT.@) * */ INT CDECL _wcsupr_s( wchar_t* str, size_t n ) { wchar_t* ptr = str; if (!str || !n) { if (str) *str = '\0'; _set_errno(EINVAL); return EINVAL; } while (n--) { if (!*ptr) return 0; *ptr = toupperW(*ptr); ptr++; } /* MSDN claims that the function should return and set errno to * ERANGE, which doesn't seem to be true based on the tests. */ *str = '\0'; _set_errno(EINVAL); return EINVAL; }
/* * @implemented * * _chdrive (MSVCRT.@) * * Change the current drive. * * PARAMS * newdrive [I] Drive number to change to (1 = 'A', 2 = 'B', ...) * * RETURNS * Success: 0. The current drive is set to newdrive. * Failure: -1. errno indicates the error. * * NOTES * See SetCurrentDirectoryA. */ int _chdrive(int newdrive) { WCHAR buffer[] = L"A:"; buffer[0] += newdrive - 1; if (!SetCurrentDirectoryW( buffer )) { _dosmaperr(GetLastError()); if (newdrive <= 0) { _set_errno(EACCES); } return -1; } return 0; }
/********************************************* * API - get current state of a mailbox * IN: * mb_id - handle for the mailbox * OUT: * state - current mailbox state **********************************************/ mb_error_t mb_getstate(mb_id_t mb_id, mb_state_t* state) { mb_handle_t* mbhdl; mb_hdr_t* mbhdr; mbhdl = (mb_handle_t*) mb_id; if (MB_BAD(mbhdl)) { _set_errno(EINVAL); return (MB_ERROR); } mbhdr = (mb_hdr_t*) mbhdl->maddr; *state = mbhdr->curState; return (MB_OK); }
int __statfsx64 (const char *file, struct statfsx64 *buf) { char *RootDirectory = rootdir (file); int res = 0; if ((RootDirectory == NULL) ) {// || access (file, F_OK)) { // || (GetDriveType(RootDirectory) == DRIVE_REMOVABLE)) { // fprintf(stderr, "__statfsx64: RootDirectory: %s\n", RootDirectory); buf = NULL; _set_errno(ENOENT); res = -1; } else res = __rstatfsx64 ((const char *) RootDirectory, buf); free (RootDirectory); return res; }
int __cdecl vfwprintf_s(FILE* file, const wchar_t *format, va_list argptr) { int ret; if(!MSVCRT_CHECK_PMT( file != NULL)) { _set_errno(EINVAL); return -1; } _lock_file(file); ret = wstreamout(file, format, argptr); _unlock_file(file); return ret; }
/*********************************** * API - set requested state * IN: * mb_id - handle for the mailbox * state - current mailbox state ***********************************/ mb_error_t mb_setexpectedstate(mb_id_t mb_id, mb_state_t state) { mb_handle_t* mbhdl; mb_hdr_t* mbhdr; mbhdl = (mb_handle_t*) mb_id; if (MB_BAD(mbhdl)) { _set_errno(EINVAL); return (MB_ERROR); } mbhdr = (mb_hdr_t*) mbhdl->maddr; mbhdr->rqstState = state; return (MB_OK); }
int lstat(const char *path, struct stat *buffer) { WIN32_FILE_ATTRIBUTE_DATA fdata; if (GetFileAttributesEx(path, GetFileExInfoStandard, &fdata)) { int fMode = S_IREAD; if (fdata.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY) fMode |= S_IFDIR; else fMode |= S_IFREG; if (!(fdata.dwFileAttributes & FILE_ATTRIBUTE_READONLY)) fMode |= S_IWRITE; buffer->st_ino = 0; buffer->st_gid = 0; buffer->st_uid = 0; buffer->st_nlink = 1; buffer->st_mode = fMode; buffer->st_size = fdata.nFileSizeLow; buffer->st_dev = buffer->st_rdev = 0; buffer->st_atime = filetime_to_time_t(&fdata.ftLastAccessTime); buffer->st_mtime = filetime_to_time_t(&fdata.ftLastWriteTime); buffer->st_ctime = filetime_to_time_t(&fdata.ftCreationTime); if (fdata.dwFileAttributes & FILE_ATTRIBUTE_REPARSE_POINT) { WIN32_FIND_DATA find; HANDLE handle = FindFirstFile(path, &find); if (handle != INVALID_HANDLE_VALUE) { if ((find.dwFileAttributes & FILE_ATTRIBUTE_REPARSE_POINT) && (find.dwReserved0 == IO_REPARSE_TAG_SYMLINK)) { buffer->st_mode = S_IFLNK; } } } _set_errno(0); return 0; } errno = winErrorToPosix(GetLastError()); return -1; }
/* * INTERNAL */ int access_dirT(const _TCHAR *_path) { DWORD Attributes = GetFileAttributes(_path); TRACE(MK_STR(is_dirT)"('%"sT"')\n", _path); if (Attributes == (DWORD)-1) { _dosmaperr(GetLastError()); return -1; } if ((Attributes & FILE_ATTRIBUTE_DIRECTORY) != FILE_ATTRIBUTE_DIRECTORY) { _set_errno(EACCES); return -1; } return 0; }
mb_error_t mb_gettimestamp(mb_id_t mb_id, struct timeval *timestamp) { mb_handle_t* mbhdl; mb_hdr_t* mbhdr; mbhdl = (mb_handle_t*) mb_id; if (MB_BAD(mbhdl)) { _set_errno(EINVAL); return (MB_ERROR); } mbhdr = (mb_hdr_t*) mbhdl->maddr; timestamp->tv_sec = mbhdr->tmStamp; timestamp->tv_usec = 0; return (MB_OK); }
/* {{{ data_available */ int data_available(ftpbuf_t *ftp, php_socket_t s) { int n; n = php_pollfd_for_ms(s, PHP_POLLREADABLE, 1000); if (n < 1) { #ifdef PHP_WIN32 if (n == 0) { _set_errno(ETIMEDOUT); } #elif !(defined(NETWARE) && defined(USE_WINSOCK)) if (n == 0) { errno = ETIMEDOUT; } #endif return 0; } return 1; }
/********************************************************************* * wcstok_s (MSVCRT.@) */ wchar_t * CDECL wcstok_s( wchar_t *str, const wchar_t *delim, wchar_t **next_token ) { wchar_t *ret; if (!MSVCRT_CHECK_PMT(delim != NULL) || !MSVCRT_CHECK_PMT(next_token != NULL) || !MSVCRT_CHECK_PMT(str != NULL || *next_token != NULL)) { _set_errno(EINVAL); return NULL; } if (!str) str = *next_token; while (*str && strchrW( delim, *str )) str++; if (!*str) return NULL; ret = str++; while (*str && !strchrW( delim, *str )) str++; if (*str) *str++ = 0; *next_token = str; return ret; }
/******************************************** * API - multicast the mailbox * IN: * mb_id - handle for the mailbox ********************************************/ mb_error_t mb_broadcast(mb_id_t mb_id) { mb_handle_t* mbhdl; mb_hdr_t* mbhdr; mbhdl = (mb_handle_t*) mb_id; if (MB_BAD(mbhdl)) { _set_errno(EINVAL); return (MB_ERROR); } mbhdr = (mb_hdr_t*) mbhdl->maddr; if (lockf(mbhdl->fd, F_LOCK, 0) == -1) { return (MB_ERROR); } _mb_net_send(mbhdr); (void) lockf(mbhdl->fd, F_ULOCK, 0); return (MB_OK); }
/* {{{ data_writeable */ int data_writeable(ftpbuf_t *ftp, php_socket_t s) { int n; n = php_pollfd_for_ms(s, POLLOUT, 1000); if (n < 1) { #ifdef PHP_WIN32 if (n == 0) { _set_errno(ETIMEDOUT); } #else if (n == 0) { errno = ETIMEDOUT; } #endif return 0; } return 1; }
/* {{{ my_accept */ int my_accept(ftpbuf_t *ftp, php_socket_t s, struct sockaddr *addr, socklen_t *addrlen) { int n; n = php_pollfd_for_ms(s, PHP_POLLREADABLE, ftp->timeout_sec * 1000); if (n < 1) { #ifdef PHP_WIN32 if (n == 0) { _set_errno(ETIMEDOUT); } #else if (n == 0) { errno = ETIMEDOUT; } #endif return -1; } return accept(s, addr, addrlen); }
/*********************************** * API - Close a mailbox * IN: * mb_id - handle for the mailbox ***********************************/ mb_error_t mb_close(mb_id_t mb_id) { mb_handle_t* mbhdl; mb_hdr_t* mbhdr; mbhdl = (mb_handle_t*) mb_id; if (MB_BAD(mbhdl)) { _set_errno(EINVAL); return (MB_ERROR); } if (lockf(mbhdl->fd, F_LOCK, 0) == -1) { return (MB_ERROR); } mbhdr = (mb_hdr_t*) mbhdl->maddr; munmap(mbhdl->maddr, mbhdr->ttSize); mbhdl->valid = 0; close(mbhdl->fd); free(mbhdl); return (MB_OK); }