Reactos
at master 72 lines 2.2 kB view raw
1/*** 2*perror.c - print system error message 3* 4* Copyright (c) Microsoft Corporation. All rights reserved. 5* 6*Purpose: 7* defines perror() - print system error message 8* System error message are indexed by errno; conforms to XENIX 9* standard, with much compatability with 1983 uniforum draft standard. 10* 11*******************************************************************************/ 12#include <corecrt_internal.h> 13#include <corecrt_internal_lowio.h> 14#include <corecrt_internal_ptd_propagation.h> 15#include <io.h> 16#include <stdio.h> 17#include <stdlib.h> 18#include <string.h> 19 20#pragma warning(disable:__WARNING_RETVAL_IGNORED_FUNC_COULD_FAIL) // 6031 return value ignored 21 22/*** 23*void perror(message) - print system error message 24* 25*Purpose: 26* prints user's error message, then follows it with ": ", then the system 27* error message, then a newline. All output goes to stderr. If user's 28* message is nullptr or a null string, only the system error message is 29* printer. If errno is weird, prints "Unknown error". 30* 31*Entry: 32* const char *message - users message to prefix system error message 33* 34*Exit: 35* Prints message; no return value. 36* 37*Exceptions: 38* 39*******************************************************************************/ 40 41static void __cdecl _perror_internal(char const* const user_prefix, __crt_cached_ptd_host& ptd) 42{ 43 int const fh = 2; 44 45 __acrt_lowio_lock_fh(fh); 46 __try 47 { 48 if (user_prefix != nullptr && user_prefix[0] != '\0') 49 { 50 _write_nolock(fh, user_prefix, static_cast<unsigned>(strlen(user_prefix)), ptd); 51 _write_nolock(fh, ": ", 2, ptd); 52 } 53 54 // Use PTD directly to access previously set errno value. 55 char const* const system_message = _get_sys_err_msg(ptd.get_raw_ptd()->_terrno); 56 57 _write_nolock(fh, system_message, static_cast<unsigned>(strlen(system_message)), ptd); 58 _write_nolock(fh, "\n", 1, ptd); 59 60 } 61 __finally 62 { 63 __acrt_lowio_unlock_fh( fh ); 64 } 65 __endtry 66} 67 68extern "C" void __cdecl perror(char const* const user_prefix) 69{ 70 __crt_cached_ptd_host ptd; 71 return _perror_internal(user_prefix, ptd); 72}