ReactOS 0.4.16-dev-329-g9223134
strerror.c
Go to the documentation of this file.
1/*
2 * msvcrt.dll errno functions
3 *
4 * Copyright 2000 Jon Griffiths
5 *
6 * This library is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Lesser General Public
8 * License as published by the Free Software Foundation; either
9 * version 2.1 of the License, or (at your option) any later version.
10 *
11 * This library is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * Lesser General Public License for more details.
15 *
16 * You should have received a copy of the GNU Lesser General Public
17 * License along with this library; if not, write to the Free Software
18 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
19 */
20
21#include <precomp.h>
22
23char __syserr00[] = "No Error";
24char __syserr01[] = "Operation not permitted (EPERM)";
25char __syserr02[] = "No such file or directory (ENOENT)";
26char __syserr03[] = "No such process (ESRCH)";
27char __syserr04[] = "Interrupted system call (EINTR)";
28char __syserr05[] = "Input or output error (EIO)";
29char __syserr06[] = "No such device or address (ENXIO)";
30char __syserr07[] = "Argument list too long (E2BIG)";
31char __syserr08[] = "Unable to execute file (ENOEXEC)";
32char __syserr09[] = "Bad file descriptor (EBADF)";
33char __syserr10[] = "No child processes (ECHILD)";
34char __syserr11[] = "Resource temporarily unavailable (EAGAIN)";
35char __syserr12[] = "Not enough memory (ENOMEM)";
36char __syserr13[] = "Permission denied (EACCES)";
37char __syserr14[] = "Bad address (EFAULT)";
38char __syserr15[] = "Unknown Error: 15";
39char __syserr16[] = "Resource busy (EBUSY)";
40char __syserr17[] = "File exists (EEXIST)";
41char __syserr18[] = "Improper link (EXDEV)";
42char __syserr19[] = "No such device (ENODEV)";
43char __syserr20[] = "Not a directory (ENOTDIR)";
44char __syserr21[] = "Is a directory (EISDIR)";
45char __syserr22[] = "Invalid argument (EINVAL)";
46char __syserr23[] = "Too many open files in system (ENFILE)";
47char __syserr24[] = "Too many open files (EMFILE)";
48char __syserr25[] = "Inappropriate I/O control operation (ENOTTY)";
49char __syserr26[] = "Unknown error: 26";
50char __syserr27[] = "File too large (EFBIG)";
51char __syserr28[] = "No space left on drive (ENOSPC)";
52char __syserr29[] = "Invalid seek (ESPIPE)";
53char __syserr30[] = "Read-only file system (EROFS)";
54char __syserr31[] = "Too many links (EMLINK)";
55char __syserr32[] = "Broken pipe (EPIPE)";
56char __syserr33[] = "Input to function out of range (EDOM)";
57char __syserr34[] = "Output of function out of range (ERANGE)";
58char __syserr35[] = "Unknown error: 35";
59char __syserr36[] = "Resource deadlock avoided (EDEADLK)";
60char __syserr37[] = "Unknown error: 37";
61char __syserr38[] = "File name too long (ENAMETOOLONG)";
62char __syserr39[] = "No locks available (ENOLCK)";
63char __syserr40[] = "Function not implemented (ENOSYS)";
64char __syserr41[] = "Directory not empty (ENOTEMPTY)";
65char __syserr42[] = "Illegal byte sequence (EILSEQ)";
66char __syserr43[] = "Unknown/generic error";
67
68char *_sys_errlist[] = {
78};
79
80int _sys_nerr = sizeof(_sys_errlist) / sizeof(_sys_errlist[0]) - 1;
81
82/*********************************************************************
83 * strerror (MSVCRT.@)
84 */
85char* CDECL strerror(int err)
86{
88
89 if (!data->strerror_buffer)
90 if (!(data->strerror_buffer = malloc(256))) return NULL;
91
92 if (err < 0 || err > _sys_nerr) err = _sys_nerr;
93 strcpy( data->strerror_buffer, _sys_errlist[err] );
94 return data->strerror_buffer;
95}
96
97/**********************************************************************
98 * strerror_s (MSVCRT.@)
99 */
100int CDECL strerror_s(char *buffer, size_t numberOfElements, int errnum)
101{
102 char *ptr;
103
104 if (!buffer || !numberOfElements)
105 {
106 *_errno() = EINVAL;
107 return EINVAL;
108 }
109
110 if (errnum < 0 || errnum > _sys_nerr)
111 errnum = _sys_nerr;
112
113 ptr = _sys_errlist[errnum];
114 while (*ptr && numberOfElements > 1)
115 {
116 *buffer++ = *ptr++;
118 }
119
120 *buffer = '\0';
121 return 0;
122}
123
124/**********************************************************************
125 * _strerror (MSVCRT.@)
126 */
127char* CDECL _strerror(const char* str)
128{
130 int err;
131
132 if (!data->strerror_buffer)
133 if (!(data->strerror_buffer = malloc(256))) return NULL;
134
135 err = data->thread_errno;
136 if (err < 0 || err > _sys_nerr) err = _sys_nerr;
137
138 if (str && *str)
139 sprintf( data->strerror_buffer, "%s: %s\n", str, _sys_errlist[err] );
140 else
141 sprintf( data->strerror_buffer, "%s\n", _sys_errlist[err] );
142
143 return data->strerror_buffer;
144}
145
146/*********************************************************************
147 * perror (MSVCRT.@)
148 */
149void CDECL perror(const char* str)
150{
151 int err = *_errno();
152 if (err < 0 || err > _sys_nerr) err = _sys_nerr;
153
154 if (str && *str)
155 {
156 _write( 2, str, strlen(str) );
157 _write( 2, ": ", 2 );
158 }
160 _write( 2, "\n", 1 );
161}
162
163/*********************************************************************
164 * _wcserror_s (MSVCRT.@)
165 */
166int CDECL _wcserror_s(wchar_t* buffer, size_t nc, int err)
167{
168 if (!MSVCRT_CHECK_PMT(buffer != NULL) || !MSVCRT_CHECK_PMT(nc > 0))
169 {
171 return EINVAL;
172 }
173 if (err < 0 || err > _sys_nerr) err = _sys_nerr;
175 return 0;
176}
177
178/*********************************************************************
179 * _wcserror (MSVCRT.@)
180 */
181wchar_t* CDECL _wcserror(int err)
182{
184
185 if (!data->wcserror_buffer)
186 if (!(data->wcserror_buffer = malloc(256 * sizeof(wchar_t)))) return NULL;
187 _wcserror_s(data->wcserror_buffer, 256, err);
188 return data->wcserror_buffer;
189}
190
191/**********************************************************************
192 * __wcserror_s (MSVCRT.@)
193 */
194int CDECL __wcserror_s(wchar_t* buffer, size_t nc, const wchar_t* str)
195{
196 int err;
197 static const WCHAR colonW[] = {':', ' ', '\0'};
198 static const WCHAR nlW[] = {'\n', '\0'};
199 size_t len;
200
201 err = *_errno();
202 if (err < 0 || err > _sys_nerr) err = _sys_nerr;
203
204 len = MultiByteToWideChar(CP_ACP, 0, _sys_errlist[err], -1, NULL, 0) + 1 /* \n */;
205 if (str && *str) len += lstrlenW(str) + 2 /* ': ' */;
206 if (len > nc)
207 {
208 MSVCRT_INVALID_PMT("buffer[nc] is too small", ERANGE);
209 return ERANGE;
210 }
211 if (str && *str)
212 {
214 lstrcatW(buffer, colonW);
215 }
216 else buffer[0] = '\0';
219 lstrcatW(buffer, nlW);
220
221 return 0;
222}
223
224/**********************************************************************
225 * __wcserror (MSVCRT.@)
226 */
227wchar_t* CDECL __wcserror(const wchar_t* str)
228{
230 int err;
231
232 if (!data->wcserror_buffer)
233 if (!(data->wcserror_buffer = malloc(256 * sizeof(wchar_t)))) return NULL;
234
235 err = __wcserror_s(data->wcserror_buffer, 256, str);
236 if (err) FIXME("bad wcserror call (%d)\n", err);
237
238 return data->wcserror_buffer;
239}
#define EINVAL
Definition: acclib.h:90
ACPI_SIZE strlen(const char *String)
Definition: utclib.c:269
#define ERANGE
Definition: acclib.h:92
char * strcpy(char *DstString, const char *SrcString)
Definition: utclib.c:388
#define FIXME(fmt,...)
Definition: precomp.h:53
#define malloc
Definition: debug_ros.c:4
#define NULL
Definition: types.h:112
#define CDECL
Definition: compat.h:29
#define CP_ACP
Definition: compat.h:109
#define lstrcpyW
Definition: compat.h:749
#define MultiByteToWideChar
Definition: compat.h:110
#define lstrlenW
Definition: compat.h:750
GLint GLenum GLsizei GLsizei GLsizei GLint GLsizei const GLvoid * data
Definition: gl.h:1950
GLuint buffer
Definition: glext.h:5915
GLenum GLsizei len
Definition: glext.h:6722
LPWSTR WINAPI lstrcatW(LPWSTR lpString1, LPCWSTR lpString2)
Definition: lstring.c:274
static PVOID ptr
Definition: dispmode.c:27
#define MSVCRT_INVALID_PMT(x)
Definition: mbstowcs_s.c:25
#define MSVCRT_CHECK_PMT(x)
Definition: mbstowcs_s.c:26
#define sprintf(buf, format,...)
Definition: sprintf.c:55
thread_data_t * msvcrt_get_thread_data(void)
Definition: tls.c:31
static size_t numberOfElements
Definition: string.c:87
#define err(...)
const WCHAR * str
_CRTIMP int *__cdecl _errno(void)
Definition: errno.c:17
errno_t __cdecl _set_errno(_In_ int _Value)
_CRTIMP int __cdecl _write(_In_ int _FileHandle, _In_reads_bytes_(_MaxCharCount) const void *_Buf, _In_ unsigned int _MaxCharCount)
char __syserr32[]
Definition: strerror.c:55
char __syserr13[]
Definition: strerror.c:36
char __syserr30[]
Definition: strerror.c:53
char __syserr10[]
Definition: strerror.c:33
char __syserr37[]
Definition: strerror.c:60
char __syserr17[]
Definition: strerror.c:40
char *CDECL strerror(int err)
Definition: strerror.c:85
char __syserr29[]
Definition: strerror.c:52
char __syserr36[]
Definition: strerror.c:59
char __syserr18[]
Definition: strerror.c:41
char __syserr43[]
Definition: strerror.c:66
char __syserr01[]
Definition: strerror.c:24
char __syserr24[]
Definition: strerror.c:47
char __syserr23[]
Definition: strerror.c:46
wchar_t *CDECL _wcserror(int err)
Definition: strerror.c:181
char __syserr25[]
Definition: strerror.c:48
void CDECL perror(const char *str)
Definition: strerror.c:149
char __syserr20[]
Definition: strerror.c:43
char __syserr04[]
Definition: strerror.c:27
char __syserr00[]
Definition: strerror.c:23
wchar_t *CDECL __wcserror(const wchar_t *str)
Definition: strerror.c:227
char __syserr05[]
Definition: strerror.c:28
char __syserr33[]
Definition: strerror.c:56
char __syserr09[]
Definition: strerror.c:32
int CDECL strerror_s(char *buffer, size_t numberOfElements, int errnum)
Definition: strerror.c:100
char __syserr12[]
Definition: strerror.c:35
char __syserr34[]
Definition: strerror.c:57
int CDECL __wcserror_s(wchar_t *buffer, size_t nc, const wchar_t *str)
Definition: strerror.c:194
char __syserr42[]
Definition: strerror.c:65
char __syserr40[]
Definition: strerror.c:63
char __syserr11[]
Definition: strerror.c:34
char __syserr35[]
Definition: strerror.c:58
char __syserr19[]
Definition: strerror.c:42
int _sys_nerr
Definition: strerror.c:80
char __syserr22[]
Definition: strerror.c:45
char __syserr15[]
Definition: strerror.c:38
int CDECL _wcserror_s(wchar_t *buffer, size_t nc, int err)
Definition: strerror.c:166
char __syserr21[]
Definition: strerror.c:44
char * _sys_errlist[]
Definition: strerror.c:68
char __syserr41[]
Definition: strerror.c:64
char __syserr16[]
Definition: strerror.c:39
char __syserr02[]
Definition: strerror.c:25
char __syserr28[]
Definition: strerror.c:51
char __syserr14[]
Definition: strerror.c:37
char __syserr26[]
Definition: strerror.c:49
char __syserr27[]
Definition: strerror.c:50
char __syserr06[]
Definition: strerror.c:29
char __syserr38[]
Definition: strerror.c:61
char __syserr31[]
Definition: strerror.c:54
char *CDECL _strerror(const char *str)
Definition: strerror.c:127
char __syserr07[]
Definition: strerror.c:30
char __syserr39[]
Definition: strerror.c:62
char __syserr08[]
Definition: strerror.c:31
char __syserr03[]
Definition: strerror.c:26
__wchar_t WCHAR
Definition: xmlstorage.h:180