ReactOS 0.4.16-dev-852-gcfcc8d8
perror.cpp
Go to the documentation of this file.
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>
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
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 }
62 {
64 }
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}
ACPI_SIZE strlen(const char *String)
Definition: utclib.c:269
#define __cdecl
Definition: accygwin.h:79
_Ret_z_ __inline char const * _get_sys_err_msg(size_t const m)
_Check_return_ int __cdecl _write_nolock(_In_ int _FileHandle, _In_reads_bytes_(_MaxCharCount) const void *_Buf, _In_ unsigned int _MaxCharCount, __crt_cached_ptd_host &_Ptd)
void __cdecl __acrt_lowio_lock_fh(_In_ int _FileHandle)
void __cdecl __acrt_lowio_unlock_fh(_In_ int _FileHandle)
_In_ size_t const _In_ int _In_ bool const _In_ unsigned const _In_ __acrt_rounding_mode const _Inout_ __crt_cached_ptd_host & ptd
Definition: cvt.cpp:355
static void __cdecl _perror_internal(char const *const user_prefix, __crt_cached_ptd_host &ptd)
Definition: perror.cpp:41
void __cdecl perror(char const *const user_prefix)
Definition: perror.cpp:68
#define __try
Definition: pseh2_64.h:172
#define __endtry
Definition: pseh2_64.h:175
#define __finally
Definition: pseh2_64.h:174