ReactOS  0.4.14-dev-317-g96040ec
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 
23 char __syserr00[] = "No Error";
24 char __syserr01[] = "Operation not permitted (EPERM)";
25 char __syserr02[] = "No such file or directory (ENOENT)";
26 char __syserr03[] = "No such process (ESRCH)";
27 char __syserr04[] = "Interrupted system call (EINTR)";
28 char __syserr05[] = "Input or output error (EIO)";
29 char __syserr06[] = "No such device or address (ENXIO)";
30 char __syserr07[] = "Argument list too long (E2BIG)";
31 char __syserr08[] = "Unable to execute file (ENOEXEC)";
32 char __syserr09[] = "Bad file descriptor (EBADF)";
33 char __syserr10[] = "No child processes (ECHILD)";
34 char __syserr11[] = "Resource temporarily unavailable (EAGAIN)";
35 char __syserr12[] = "Not enough memory (ENOMEM)";
36 char __syserr13[] = "Permission denied (EACCES)";
37 char __syserr14[] = "Bad address (EFAULT)";
38 char __syserr15[] = "Unknown Error: 15";
39 char __syserr16[] = "Resource busy (EBUSY)";
40 char __syserr17[] = "File exists (EEXIST)";
41 char __syserr18[] = "Improper link (EXDEV)";
42 char __syserr19[] = "No such device (ENODEV)";
43 char __syserr20[] = "Not a directory (ENOTDIR)";
44 char __syserr21[] = "Is a directory (EISDIR)";
45 char __syserr22[] = "Invalid argument (EINVAL)";
46 char __syserr23[] = "Too many open files in system (ENFILE)";
47 char __syserr24[] = "Too many open files (EMFILE)";
48 char __syserr25[] = "Inappropriate I/O control operation (ENOTTY)";
49 char __syserr26[] = "Unknown error: 26";
50 char __syserr27[] = "File too large (EFBIG)";
51 char __syserr28[] = "No space left on drive (ENOSPC)";
52 char __syserr29[] = "Invalid seek (ESPIPE)";
53 char __syserr30[] = "Read-only file system (EROFS)";
54 char __syserr31[] = "Too many links (EMLINK)";
55 char __syserr32[] = "Broken pipe (EPIPE)";
56 char __syserr33[] = "Input to function out of range (EDOM)";
57 char __syserr34[] = "Output of function out of range (ERANGE)";
58 char __syserr35[] = "Unknown error: 35";
59 char __syserr36[] = "Resource deadlock avoided (EDEADLK)";
60 char __syserr37[] = "Unknown error: 37";
61 char __syserr38[] = "File name too long (ENAMETOOLONG)";
62 char __syserr39[] = "No locks available (ENOLCK)";
63 char __syserr40[] = "Function not implemented (ENOSYS)";
64 char __syserr41[] = "Directory not empty (ENOTEMPTY)";
65 char __syserr42[] = "Illegal byte sequence (EILSEQ)";
66 
67 char *_sys_errlist[] = {
77 };
78 
79 int _sys_nerr = sizeof(_sys_errlist) / sizeof(_sys_errlist[0]) - 1;
80 
81 /*********************************************************************
82  * strerror (MSVCRT.@)
83  */
84 char* CDECL strerror(int err)
85 {
87 
88  if (!data->strerror_buffer)
89  if (!(data->strerror_buffer = malloc(256))) return NULL;
90 
91  if (err < 0 || err > _sys_nerr) err = _sys_nerr;
92  strcpy( data->strerror_buffer, _sys_errlist[err] );
93  return data->strerror_buffer;
94 }
95 
96 /**********************************************************************
97  * strerror_s (MSVCRT.@)
98  */
99 int CDECL strerror_s(char *buffer, size_t numberOfElements, int errnum)
100 {
101  char *ptr;
102 
103  if (!buffer || !numberOfElements)
104  {
105  *_errno() = EINVAL;
106  return EINVAL;
107  }
108 
109  if (errnum < 0 || errnum > _sys_nerr)
110  errnum = _sys_nerr;
111 
112  ptr = _sys_errlist[errnum];
113  while (*ptr && numberOfElements > 1)
114  {
115  *buffer++ = *ptr++;
117  }
118 
119  *buffer = '\0';
120  return 0;
121 }
122 
123 /**********************************************************************
124  * _strerror (MSVCRT.@)
125  */
126 char* CDECL _strerror(const char* str)
127 {
129  int err;
130 
131  if (!data->strerror_buffer)
132  if (!(data->strerror_buffer = malloc(256))) return NULL;
133 
134  err = data->thread_errno;
135  if (err < 0 || err > _sys_nerr) err = _sys_nerr;
136 
137  if (str && *str)
138  sprintf( data->strerror_buffer, "%s: %s\n", str, _sys_errlist[err] );
139  else
140  sprintf( data->strerror_buffer, "%s\n", _sys_errlist[err] );
141 
142  return data->strerror_buffer;
143 }
144 
145 /*********************************************************************
146  * perror (MSVCRT.@)
147  */
148 void CDECL perror(const char* str)
149 {
150  int err = *_errno();
151  if (err < 0 || err > _sys_nerr) err = _sys_nerr;
152 
153  if (str && *str)
154  {
155  _write( 2, str, strlen(str) );
156  _write( 2, ": ", 2 );
157  }
159  _write( 2, "\n", 1 );
160 }
161 
162 /*********************************************************************
163  * _wcserror_s (MSVCRT.@)
164  */
165 int CDECL _wcserror_s(wchar_t* buffer, size_t nc, int err)
166 {
167  if (!MSVCRT_CHECK_PMT(buffer != NULL) || !MSVCRT_CHECK_PMT(nc > 0))
168  {
170  return EINVAL;
171  }
172  if (err < 0 || err > _sys_nerr) err = _sys_nerr;
174  return 0;
175 }
176 
177 /*********************************************************************
178  * _wcserror (MSVCRT.@)
179  */
180 wchar_t* CDECL _wcserror(int err)
181 {
183 
184  if (!data->wcserror_buffer)
185  if (!(data->wcserror_buffer = malloc(256 * sizeof(wchar_t)))) return NULL;
186  _wcserror_s(data->wcserror_buffer, 256, err);
187  return data->wcserror_buffer;
188 }
189 
190 /**********************************************************************
191  * __wcserror_s (MSVCRT.@)
192  */
193 int CDECL __wcserror_s(wchar_t* buffer, size_t nc, const wchar_t* str)
194 {
195  int err;
196  static const WCHAR colonW[] = {':', ' ', '\0'};
197  static const WCHAR nlW[] = {'\n', '\0'};
198  size_t len;
199 
200  err = *_errno();
201  if (err < 0 || err > _sys_nerr) err = _sys_nerr;
202 
203  len = MultiByteToWideChar(CP_ACP, 0, _sys_errlist[err], -1, NULL, 0) + 1 /* \n */;
204  if (str && *str) len += lstrlenW(str) + 2 /* ': ' */;
205  if (len > nc)
206  {
207  MSVCRT_INVALID_PMT("buffer[nc] is too small", ERANGE);
208  return ERANGE;
209  }
210  if (str && *str)
211  {
212  lstrcpyW(buffer, str);
213  lstrcatW(buffer, colonW);
214  }
215  else buffer[0] = '\0';
216  len = lstrlenW(buffer);
218  lstrcatW(buffer, nlW);
219 
220  return 0;
221 }
222 
223 /**********************************************************************
224  * __wcserror (MSVCRT.@)
225  */
226 wchar_t* CDECL __wcserror(const wchar_t* str)
227 {
229  int err;
230 
231  if (!data->wcserror_buffer)
232  if (!(data->wcserror_buffer = malloc(256 * sizeof(wchar_t)))) return NULL;
233 
234  err = __wcserror_s(data->wcserror_buffer, 256, str);
235  if (err) FIXME("bad wcserror call (%d)\n", err);
236 
237  return data->wcserror_buffer;
238 }
errno_t __cdecl _set_errno(_In_ int _Value)
char __syserr05[]
Definition: strerror.c:28
int CDECL _wcserror_s(wchar_t *buffer, size_t nc, int err)
Definition: strerror.c:165
_CRTIMP int __cdecl _write(_In_ int _FileHandle, _In_reads_bytes_(_MaxCharCount) const void *_Buf, _In_ unsigned int _MaxCharCount)
char __syserr19[]
Definition: strerror.c:42
char __syserr22[]
Definition: strerror.c:45
char __syserr38[]
Definition: strerror.c:61
char __syserr40[]
Definition: strerror.c:63
ACPI_SIZE strlen(const char *String)
Definition: utclib.c:269
#define CP_ACP
Definition: compat.h:99
char __syserr15[]
Definition: strerror.c:38
Definition: arc.h:39
char __syserr10[]
Definition: strerror.c:33
char __syserr09[]
Definition: strerror.c:32
char __syserr35[]
Definition: strerror.c:58
char __syserr06[]
Definition: strerror.c:29
char __syserr13[]
Definition: strerror.c:36
GLuint buffer
Definition: glext.h:5915
char __syserr36[]
Definition: strerror.c:59
char __syserr18[]
Definition: strerror.c:41
char __syserr16[]
Definition: strerror.c:39
char __syserr20[]
Definition: strerror.c:43
#define lstrlenW
Definition: compat.h:407
char __syserr27[]
Definition: strerror.c:50
char __syserr37[]
Definition: strerror.c:60
char __syserr39[]
Definition: strerror.c:62
static size_t numberOfElements
Definition: string.c:87
char *CDECL _strerror(const char *str)
Definition: strerror.c:126
char __syserr32[]
Definition: strerror.c:55
#define sprintf(buf, format,...)
Definition: sprintf.c:55
char __syserr28[]
Definition: strerror.c:51
char __syserr41[]
Definition: strerror.c:64
char __syserr33[]
Definition: strerror.c:56
char __syserr08[]
Definition: strerror.c:31
#define FIXME(fmt,...)
Definition: debug.h:110
static PVOID ptr
Definition: dispmode.c:27
#define MSVCRT_INVALID_PMT(x)
Definition: mbstowcs_s.c:25
const WCHAR * str
smooth NULL
Definition: ftsmooth.c:416
int _sys_nerr
Definition: strerror.c:79
char __syserr17[]
Definition: strerror.c:40
char *CDECL strerror(int err)
Definition: strerror.c:84
char * _sys_errlist[]
Definition: strerror.c:67
char __syserr25[]
Definition: strerror.c:48
__wchar_t WCHAR
Definition: xmlstorage.h:180
char __syserr30[]
Definition: strerror.c:53
char __syserr21[]
Definition: strerror.c:44
char __syserr03[]
Definition: strerror.c:26
char __syserr02[]
Definition: strerror.c:25
char __syserr24[]
Definition: strerror.c:47
GLint GLenum GLsizei GLsizei GLsizei GLint GLsizei const GLvoid * data
Definition: gl.h:1950
_CRTIMP int *__cdecl _errno(void)
Definition: errno.c:19
#define ERANGE
Definition: acclib.h:92
int CDECL __wcserror_s(wchar_t *buffer, size_t nc, const wchar_t *str)
Definition: strerror.c:193
#define MSVCRT_CHECK_PMT(x)
Definition: mbstowcs_s.c:26
GLenum GLsizei len
Definition: glext.h:6722
#define err(...)
char __syserr29[]
Definition: strerror.c:52
thread_data_t * msvcrt_get_thread_data(void)
Definition: tls.c:31
char __syserr04[]
Definition: strerror.c:27
wchar_t *CDECL __wcserror(const wchar_t *str)
Definition: strerror.c:226
char __syserr42[]
Definition: strerror.c:65
int CDECL strerror_s(char *buffer, size_t numberOfElements, int errnum)
Definition: strerror.c:99
#define lstrcpyW
Definition: compat.h:406
#define CDECL
Definition: compat.h:21
char __syserr31[]
Definition: strerror.c:54
char __syserr14[]
Definition: strerror.c:37
wchar_t *CDECL _wcserror(int err)
Definition: strerror.c:180
char __syserr34[]
Definition: strerror.c:57
#define MultiByteToWideChar
Definition: compat.h:100
char __syserr07[]
Definition: strerror.c:30
char __syserr00[]
Definition: strerror.c:23
char * strcpy(char *DstString, const char *SrcString)
Definition: utclib.c:388
char __syserr26[]
Definition: strerror.c:49
char __syserr23[]
Definition: strerror.c:46
#define malloc
Definition: debug_ros.c:4
LPWSTR WINAPI lstrcatW(LPWSTR lpString1, LPCWSTR lpString2)
Definition: lstring.c:274
char __syserr11[]
Definition: strerror.c:34
char __syserr01[]
Definition: strerror.c:24
char __syserr12[]
Definition: strerror.c:35
void CDECL perror(const char *str)
Definition: strerror.c:148