ReactOS 0.4.16-dev-959-g2ec3a19
wperror.cpp
Go to the documentation of this file.
1/***
2*wperror.c - print system error message (wchar_t version)
3*
4* Copyright (c) Microsoft Corporation. All rights reserved.
5*
6*Purpose:
7* defines _wperror() - print wide system error message
8* System error message are indexed by errno.
9*
10*******************************************************************************/
11#include <corecrt_internal.h>
13#include <io.h>
14#include <limits.h>
15#include <stdio.h>
16#include <stdlib.h>
17#include <string.h>
18
19/***
20*void _wperror(wmessage) - print system error message
21*
22*Purpose:
23* prints user's error message, then follows it with ": ", then the system
24* error message, then a newline. All output goes to stderr. If user's
25* message is nullptr or a null string, only the system error message is
26* printer. If errno is weird, prints "Unknown error".
27*
28*Entry:
29* const wchar_t *wmessage - users message to prefix system error message
30*
31*Exit:
32* Prints message; no return value.
33*
34*Exceptions:
35*
36*******************************************************************************/
37
38extern "C" void __cdecl _wperror(wchar_t const* const wide_user_prefix)
39{
40 // If the user did not provide a prefix, we can just call perror:
41 if (wide_user_prefix == nullptr || wide_user_prefix[0] == L'\0')
42 return perror(nullptr);
43
44 // Otherwise, we need to convert the prefix into a narrow string then pass
45 // it to perror:
46 size_t required_count = 0;
47 _ERRCHECK_EINVAL_ERANGE(wcstombs_s(&required_count, nullptr, 0, wide_user_prefix, INT_MAX));
48 if (required_count == 0)
49 return;
50
51 __crt_unique_heap_ptr<char> const narrow_user_prefix(_calloc_crt_t(char, required_count));
52 if (narrow_user_prefix.get() == nullptr)
53 return;
54
55 errno_t const conversion_result = _ERRCHECK_EINVAL_ERANGE(wcstombs_s(
56 nullptr,
57 narrow_user_prefix.get(),
58 required_count,
59 wide_user_prefix,
60 _TRUNCATE));
61
62 if (conversion_result != 0)
63 return;
64
65 return perror(narrow_user_prefix.get());
66}
#define __cdecl
Definition: accygwin.h:79
#define _ERRCHECK_EINVAL_ERANGE(e)
_CRTIMP void __cdecl perror(_In_opt_z_ const char *_ErrMsg)
#define INT_MAX
Definition: intsafe.h:150
_Check_return_wat_ _CRTIMP errno_t __cdecl wcstombs_s(_Out_opt_ size_t *pcchConverted, _Out_writes_bytes_to_opt_(cjDstSize, *pcchConverted) char *pmbsDst, _In_ size_t cjDstSize, _In_z_ const wchar_t *pwszSrc, _In_ size_t cjMaxCount)
#define L(x)
Definition: ntvdm.h:50
int errno_t
Definition: corecrt.h:615
#define _TRUNCATE
Definition: corecrt.h:278
void __cdecl _wperror(wchar_t const *const wide_user_prefix)
Definition: wperror.cpp:38