ReactOS 0.4.16-dev-963-g182f353
_strerr.cpp
Go to the documentation of this file.
1/***
2*_strerr.c - routine for indexing into system error list
3*
4* Copyright (c) Microsoft Corporation. All rights reserved.
5*
6*Purpose:
7* Returns system error message index by errno; conforms to the
8* XENIX standard, much compatibility with 1983 uniforum draft standard.
9*
10*******************************************************************************/
11
12#include <corecrt_internal.h>
14#include <malloc.h>
15#include <stdlib.h>
16#include <string.h>
17
18
19
20/***
21*char *_strerror(message) - get system error message
22*
23*Purpose:
24* builds an error message consisting of the users error message
25* (the message parameter), followed by ": ", followed by the system
26* error message (index through errno), followed by a newline. If
27* message is nullptr or a null string, returns a pointer to just
28* the system error message.
29*
30*Entry:
31* char *message - user's message to prefix system error message
32*
33*Exit:
34* returns pointer to static memory containing error message.
35* returns nullptr if malloc() fails in multi-thread versions.
36*
37*Exceptions:
38*
39*******************************************************************************/
40
41static char*& __cdecl get_strerror_buffer(__acrt_ptd* const ptd, char) throw()
42{
43 return ptd->_strerror_buffer;
44}
45
46static wchar_t*& __cdecl get_strerror_buffer(__acrt_ptd* const ptd, wchar_t) throw()
47{
48 return ptd->_wcserror_buffer;
49}
50
53 size_t const buffer_count,
54 char const* const message
55 ) throw()
56{
58}
59
62 size_t const buffer_count,
63 char const* const message
64 ) throw()
65{
66 size_t const buffer_length = wcslen(buffer);
67 return mbstowcs_s(
68 nullptr,
69 buffer + buffer_length,
70 buffer_count - buffer_length,
71 message,
72 buffer_count - buffer_length - 2);
73}
74
75template <typename Character>
77_Success_(return != 0)
78static Character* __cdecl common_strerror(
79 Character const* const message
80 ) throw()
81{
82 using traits = __crt_char_traits<Character>;
83
84 errno_t const original_errno_value = errno;
85
87 if (!ptd)
88 return nullptr;
89
90 Character*& buffer = get_strerror_buffer(ptd, Character());
91 if (!buffer)
92 buffer = _calloc_crt_t(Character, strerror_buffer_count).detach();
93
94 if (!buffer)
95 return nullptr;
96
97 buffer[0] = '\0';
98
99 // CRT_REFACTOR TODO AppCRT/DesktopCRT Dependencies
100 // (We should be using mbs* functions here).
101 if (message && message[0] != '\0')
102 {
103 Character const colon[] = { ':', ' ', '\0' };
104
105 // Leave room for the ": \n\0"
106 _ERRCHECK(traits::tcsncat_s(buffer, strerror_buffer_count, message, strerror_buffer_count - 4));
107 _ERRCHECK(traits::tcscat_s(buffer, strerror_buffer_count, colon));
108 }
109
110 char const* const system_message = _get_sys_err_msg(original_errno_value);
111 _ERRCHECK(append_message(buffer, strerror_buffer_count, system_message));
112
113 Character const newline[] = { '\n', '\0' };
114 _ERRCHECK(traits::tcscat_s(buffer, strerror_buffer_count, newline));
115
116 return buffer;
117}
118
119extern "C" char* __cdecl _strerror(char const* const message)
120{
121 return common_strerror(message);
122}
123
124extern "C" wchar_t* __cdecl __wcserror(wchar_t const* const message)
125{
126 return common_strerror(message);
127}
128
129
130
131/***
132*errno_t _strerror_s(buffer, sizeInTChars, message) - get system error message
133*
134*Purpose:
135* builds an error message consisting of the users error message
136* (the message parameter), followed by ": ", followed by the system
137* error message (index through errno), followed by a newline. If
138* message is nullptr or a null string, returns a pointer to just
139* the system error message.
140*
141*Entry:
142* TCHAR * buffer - Destination buffer.
143* size_t sizeInTChars - Size of the destination buffer.
144* TCHAR * message - user's message to prefix system error message
145*
146*Exit:
147* The error code.
148*
149*Exceptions:
150* Input parameters are validated. Refer to the validation section of the function.
151*
152*******************************************************************************/
153
154size_t const minimum_message_length = 5;
155
156template <typename Character>
158 _Out_writes_z_(buffer_count) Character* const buffer,
159 size_t const buffer_count,
160 Character const* const message
161 ) throw()
162{
163 using traits = __crt_char_traits<Character>;
164
165 errno_t const original_errno_value = errno;
166
169 buffer[0] = '\0';
170
171 if (message &&
172 message[0] != '\0' &&
173 traits::tcslen(message) < (buffer_count - 2 - minimum_message_length))
174 {
175 Character const colon[] = { ':', ' ', '\0' };
176 _ERRCHECK(traits::tcscpy_s(buffer, buffer_count, message));
177 _ERRCHECK(traits::tcscat_s(buffer, buffer_count, colon));
178 }
179
180 // Append the error message at the end of the buffer:
181 return traits::tcserror_s(
182 buffer + traits::tcslen(buffer),
183 buffer_count - traits::tcslen(buffer),
184 original_errno_value);
185}
186
188 char* const buffer,
189 size_t const buffer_count,
190 char const* const message
191 )
192{
194}
195
197 wchar_t* const buffer,
198 size_t const buffer_count,
199 wchar_t const* const message
200 )
201{
203}
char *__cdecl _strerror(char const *const message)
Definition: _strerr.cpp:119
static errno_t __cdecl append_message(_Inout_updates_z_(buffer_count) char *const buffer, size_t const buffer_count, char const *const message)
Definition: _strerr.cpp:51
errno_t __cdecl __wcserror_s(wchar_t *const buffer, size_t const buffer_count, wchar_t const *const message)
Definition: _strerr.cpp:196
size_t const minimum_message_length
Definition: _strerr.cpp:154
wchar_t *__cdecl __wcserror(wchar_t const *const message)
Definition: _strerr.cpp:124
static errno_t __cdecl common_strerror_s(_Out_writes_z_(buffer_count) Character *const buffer, size_t const buffer_count, Character const *const message)
Definition: _strerr.cpp:157
static char *&__cdecl get_strerror_buffer(__acrt_ptd *const ptd, char)
Definition: _strerr.cpp:41
errno_t __cdecl _strerror_s(char *const buffer, size_t const buffer_count, char const *const message)
Definition: _strerr.cpp:187
#define EINVAL
Definition: acclib.h:90
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)
#define _ERRCHECK(e)
__acrt_ptd *__cdecl __acrt_getptd_noexit(void)
result_buffer_count char *const _In_ int const _In_ bool const _In_ unsigned const _In_ STRFLT const _In_ bool const _Inout_ __crt_cached_ptd_host &ptd throw()
Definition: cvt.cpp:119
_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
GLuint buffer
Definition: glext.h:5915
_CRTIMP size_t __cdecl wcslen(_In_z_ const wchar_t *_Str)
#define _VALIDATE_RETURN_ERRCODE(expr, errorcode)
errno_t mbstowcs_s(size_t *cchConverted, wchar_t *widechar, size_t charoutct, const char *multibyte, size_t count)
Definition: mbstowcs.cpp:316
#define _Out_writes_z_(s)
Definition: no_sal2.h:180
#define _Success_(c)
Definition: no_sal2.h:84
#define _Inout_updates_z_(s)
Definition: no_sal2.h:186
#define _Ret_z_
Definition: no_sal2.h:306
#define errno
Definition: errno.h:18
_In_opt_ _Locale strncat_s
Definition: string.h:263
static _Ret_z_ Character *__cdecl common_strerror(int const error_number)
Definition: strerror.cpp:79
Definition: tftpd.h:60
int errno_t
Definition: corecrt.h:615
size_t const buffer_count
Definition: xtoa.cpp:36
#define const
Definition: zconf.h:233