/* define normal version that locks/unlocks, validates fh */ int __cdecl _write ( int fh, const void *buf, unsigned cnt ) { int r; /* return value */ /* validate handle */ _CHECK_FH_CLEAR_OSSERR_RETURN( fh, EBADF, -1 ); _VALIDATE_CLEAR_OSSERR_RETURN((fh >= 0 && (unsigned)fh < (unsigned)_nhandle), EBADF, -1); _VALIDATE_CLEAR_OSSERR_RETURN((_osfile(fh) & FOPEN), EBADF, -1); _lock_fh(fh); /* lock file */ __try { if ( _osfile(fh) & FOPEN ) r = _write_nolock(fh, buf, cnt); /* write bytes */ else { errno = EBADF; _doserrno = 0; /* not o.s. error */ r = -1; _ASSERTE(("Invalid file descriptor. File possibly closed by a different thread",0)); } } __finally { _unlock_fh(fh); /* unlock file */ } return r; }
void __cdecl _wperror ( const wchar_t *wmessage ) { int fh = 2; size_t size = 0; char *amessage; const char *sysmessage; /* convert WCS string into ASCII string */ if ( wmessage && *wmessage ) { _ERRCHECK_EINVAL_ERANGE(wcstombs_s( &size, NULL, 0, wmessage, INT_MAX)); if ( size==0 || (amessage = (char *)_calloc_crt(size, sizeof(char))) == NULL ) return; if ( _ERRCHECK_EINVAL_ERANGE(wcstombs_s(NULL, amessage, size, wmessage, _TRUNCATE)) != 0) { _free_crt(amessage); return; } } else amessage = NULL; _lock_fh( fh ); /* acquire file handle lock */ __try { if ( amessage ) { _write_nolock(fh,(char *)amessage,(unsigned)strlen(amessage)); _write_nolock(fh,": ",2); } _free_crt(amessage); /* note: freeing NULL is legal and benign */ sysmessage = _get_sys_err_msg( errno ); _write_nolock(fh, sysmessage,(unsigned)strlen(sysmessage)); _write_nolock(fh,"\n",1); } __finally { _unlock_fh( fh ); /* release file handle lock */ } }
extern "C" void __cdecl perror(char const* const user_prefix) { int const fh = 2; __acrt_lowio_lock_fh(fh); __try { if (user_prefix != nullptr && user_prefix[0] != '\0') { _write_nolock(fh, user_prefix, static_cast<unsigned>(strlen(user_prefix))); _write_nolock(fh, ": ", 2); } char const* const system_message = _get_sys_err_msg(errno); _write_nolock(fh, system_message, static_cast<unsigned>(strlen(system_message))); _write_nolock(fh, "\n", 1); } __finally { __acrt_lowio_unlock_fh( fh ); } }