user32: Move EnumProps16 to wnd16.c.
[wine] / dlls / msvcrt / errno.c
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 <stdio.h>
22 #include <string.h>
23
24 #include "msvcrt.h"
25 #include "wine/debug.h"
26
27 WINE_DEFAULT_DEBUG_CHANNEL(msvcrt);
28
29 /* error strings generated with glibc strerror */
30 static char str_success[]       = "Success";
31 static char str_EPERM[]         = "Operation not permitted";
32 static char str_ENOENT[]        = "No such file or directory";
33 static char str_ESRCH[]         = "No such process";
34 static char str_EINTR[]         = "Interrupted system call";
35 static char str_EIO[]           = "Input/output error";
36 static char str_ENXIO[]         = "No such device or address";
37 static char str_E2BIG[]         = "Argument list too long";
38 static char str_ENOEXEC[]       = "Exec format error";
39 static char str_EBADF[]         = "Bad file descriptor";
40 static char str_ECHILD[]        = "No child processes";
41 static char str_EAGAIN[]        = "Resource temporarily unavailable";
42 static char str_ENOMEM[]        = "Cannot allocate memory";
43 static char str_EACCES[]        = "Permission denied";
44 static char str_EFAULT[]        = "Bad address";
45 static char str_EBUSY[]         = "Device or resource busy";
46 static char str_EEXIST[]        = "File exists";
47 static char str_EXDEV[]         = "Invalid cross-device link";
48 static char str_ENODEV[]        = "No such device";
49 static char str_ENOTDIR[]       = "Not a directory";
50 static char str_EISDIR[]        = "Is a directory";
51 static char str_EINVAL[]        = "Invalid argument";
52 static char str_ENFILE[]        = "Too many open files in system";
53 static char str_EMFILE[]        = "Too many open files";
54 static char str_ENOTTY[]        = "Inappropriate ioctl for device";
55 static char str_EFBIG[]         = "File too large";
56 static char str_ENOSPC[]        = "No space left on device";
57 static char str_ESPIPE[]        = "Illegal seek";
58 static char str_EROFS[]         = "Read-only file system";
59 static char str_EMLINK[]        = "Too many links";
60 static char str_EPIPE[]         = "Broken pipe";
61 static char str_EDOM[]          = "Numerical argument out of domain";
62 static char str_ERANGE[]        = "Numerical result out of range";
63 static char str_EDEADLK[]       = "Resource deadlock avoided";
64 static char str_ENAMETOOLONG[]  = "File name too long";
65 static char str_ENOLCK[]        = "No locks available";
66 static char str_ENOSYS[]        = "Function not implemented";
67 static char str_ENOTEMPTY[]     = "Directory not empty";
68 static char str_EILSEQ[]        = "Invalid or incomplete multibyte or wide character";
69 static char str_generic_error[] = "Unknown error";
70
71 char *MSVCRT__sys_errlist[] =
72 {
73     str_success,
74     str_EPERM,
75     str_ENOENT,
76     str_ESRCH,
77     str_EINTR,
78     str_EIO,
79     str_ENXIO,
80     str_E2BIG,
81     str_ENOEXEC,
82     str_EBADF,
83     str_ECHILD,
84     str_EAGAIN,
85     str_ENOMEM,
86     str_EACCES,
87     str_EFAULT,
88     str_generic_error,
89     str_EBUSY,
90     str_EEXIST,
91     str_EXDEV,
92     str_ENODEV,
93     str_ENOTDIR,
94     str_EISDIR,
95     str_EINVAL,
96     str_ENFILE,
97     str_EMFILE,
98     str_ENOTTY,
99     str_generic_error,
100     str_EFBIG,
101     str_ENOSPC,
102     str_ESPIPE,
103     str_EROFS,
104     str_EMLINK,
105     str_EPIPE,
106     str_EDOM,
107     str_ERANGE,
108     str_generic_error,
109     str_EDEADLK,
110     str_generic_error,
111     str_ENAMETOOLONG,
112     str_ENOLCK,
113     str_ENOSYS,
114     str_ENOTEMPTY,
115     str_EILSEQ,
116     str_generic_error
117 };
118
119 unsigned int MSVCRT__sys_nerr = sizeof(MSVCRT__sys_errlist)/sizeof(MSVCRT__sys_errlist[0]) - 1;
120
121 /* INTERNAL: Set the crt and dos errno's from the OS error given. */
122 void msvcrt_set_errno(int err)
123 {
124   int *errno = MSVCRT__errno();
125   MSVCRT_ulong *doserrno = MSVCRT___doserrno();
126
127   *doserrno = err;
128
129   switch(err)
130   {
131 #define ERR_CASE(oserr) case oserr:
132 #define ERR_MAPS(oserr, crterr) case oserr: *errno = crterr; break
133     ERR_CASE(ERROR_ACCESS_DENIED)
134     ERR_CASE(ERROR_NETWORK_ACCESS_DENIED)
135     ERR_CASE(ERROR_CANNOT_MAKE)
136     ERR_CASE(ERROR_SEEK_ON_DEVICE)
137     ERR_CASE(ERROR_LOCK_FAILED)
138     ERR_CASE(ERROR_FAIL_I24)
139     ERR_CASE(ERROR_CURRENT_DIRECTORY)
140     ERR_CASE(ERROR_DRIVE_LOCKED)
141     ERR_CASE(ERROR_NOT_LOCKED)
142     ERR_CASE(ERROR_INVALID_ACCESS)
143     ERR_CASE(ERROR_SHARING_VIOLATION)
144     ERR_MAPS(ERROR_LOCK_VIOLATION,       MSVCRT_EACCES);
145     ERR_CASE(ERROR_FILE_NOT_FOUND)
146     ERR_CASE(ERROR_NO_MORE_FILES)
147     ERR_CASE(ERROR_BAD_PATHNAME)
148     ERR_CASE(ERROR_BAD_NETPATH)
149     ERR_CASE(ERROR_INVALID_DRIVE)
150     ERR_CASE(ERROR_BAD_NET_NAME)
151     ERR_CASE(ERROR_FILENAME_EXCED_RANGE)
152     ERR_MAPS(ERROR_PATH_NOT_FOUND,       MSVCRT_ENOENT);
153     ERR_MAPS(ERROR_IO_DEVICE,            MSVCRT_EIO);
154     ERR_MAPS(ERROR_BAD_FORMAT,           MSVCRT_ENOEXEC);
155     ERR_MAPS(ERROR_INVALID_HANDLE,       MSVCRT_EBADF);
156     ERR_CASE(ERROR_OUTOFMEMORY)
157     ERR_CASE(ERROR_INVALID_BLOCK)
158     ERR_CASE(ERROR_NOT_ENOUGH_QUOTA);
159     ERR_MAPS(ERROR_ARENA_TRASHED,        MSVCRT_ENOMEM);
160     ERR_MAPS(ERROR_BUSY,                 MSVCRT_EBUSY);
161     ERR_CASE(ERROR_ALREADY_EXISTS)
162     ERR_MAPS(ERROR_FILE_EXISTS,          MSVCRT_EEXIST);
163     ERR_MAPS(ERROR_BAD_DEVICE,           MSVCRT_ENODEV);
164     ERR_MAPS(ERROR_TOO_MANY_OPEN_FILES,  MSVCRT_EMFILE);
165     ERR_MAPS(ERROR_DISK_FULL,            MSVCRT_ENOSPC);
166     ERR_MAPS(ERROR_BROKEN_PIPE,          MSVCRT_EPIPE);
167     ERR_MAPS(ERROR_POSSIBLE_DEADLOCK,    MSVCRT_EDEADLK);
168     ERR_MAPS(ERROR_DIR_NOT_EMPTY,        MSVCRT_ENOTEMPTY);
169     ERR_MAPS(ERROR_BAD_ENVIRONMENT,      MSVCRT_E2BIG);
170     ERR_CASE(ERROR_WAIT_NO_CHILDREN)
171     ERR_MAPS(ERROR_CHILD_NOT_COMPLETE,   MSVCRT_ECHILD);
172     ERR_CASE(ERROR_NO_PROC_SLOTS)
173     ERR_CASE(ERROR_MAX_THRDS_REACHED)
174     ERR_MAPS(ERROR_NESTING_NOT_ALLOWED,  MSVCRT_EAGAIN);
175   default:
176     /*  Remaining cases map to EINVAL */
177     /* FIXME: may be missing some errors above */
178     *errno = MSVCRT_EINVAL;
179   }
180 }
181
182 /*********************************************************************
183  *              _errno (MSVCRT.@)
184  */
185 int* CDECL MSVCRT__errno(void)
186 {
187     return &msvcrt_get_thread_data()->thread_errno;
188 }
189
190 /*********************************************************************
191  *              __doserrno (MSVCRT.@)
192  */
193 MSVCRT_ulong* CDECL MSVCRT___doserrno(void)
194 {
195     return &msvcrt_get_thread_data()->thread_doserrno;
196 }
197
198 /*********************************************************************
199  *              strerror (MSVCRT.@)
200  */
201 char* CDECL MSVCRT_strerror(int err)
202 {
203     thread_data_t *data = msvcrt_get_thread_data();
204
205     if (!data->strerror_buffer)
206         if (!(data->strerror_buffer = MSVCRT_malloc(256))) return NULL;
207
208     if (err < 0 || err > MSVCRT__sys_nerr) err = MSVCRT__sys_nerr;
209     strcpy( data->strerror_buffer, MSVCRT__sys_errlist[err] );
210     return data->strerror_buffer;
211 }
212
213 /**********************************************************************
214  *              _strerror       (MSVCRT.@)
215  */
216 char* CDECL _strerror(const char* str)
217 {
218     thread_data_t *data = msvcrt_get_thread_data();
219     int err;
220
221     if (!data->strerror_buffer)
222         if (!(data->strerror_buffer = MSVCRT_malloc(256))) return NULL;
223
224     err = data->thread_errno;
225     if (err < 0 || err > MSVCRT__sys_nerr) err = MSVCRT__sys_nerr;
226
227     if (str && *str)
228         sprintf( data->strerror_buffer, "%s: %s\n", str, MSVCRT__sys_errlist[err] );
229     else
230         sprintf( data->strerror_buffer, "%s\n", MSVCRT__sys_errlist[err] );
231
232     return data->strerror_buffer;
233 }
234
235 /*********************************************************************
236  *              perror (MSVCRT.@)
237  */
238 void CDECL MSVCRT_perror(const char* str)
239 {
240     int err = *MSVCRT__errno();
241     if (err < 0 || err > MSVCRT__sys_nerr) err = MSVCRT__sys_nerr;
242
243     if (str && *str)
244     {
245         MSVCRT__write( 2, str, strlen(str) );
246         MSVCRT__write( 2, ": ", 2 );
247     }
248     MSVCRT__write( 2, MSVCRT__sys_errlist[err], strlen(MSVCRT__sys_errlist[err]) );
249     MSVCRT__write( 2, "\n", 1 );
250 }
251
252 /******************************************************************************
253  *              _set_error_mode (MSVCRT.@)
254  *
255  * Set the error mode, which describes where the C run-time writes error
256  * messages.
257  *
258  * PARAMS
259  *   mode - the new error mode
260  *
261  * RETURNS
262  *   The old error mode.
263  *
264  * TODO
265  *  This function does not have a proper implementation; the error mode is
266  *  never used.
267  */
268 int CDECL _set_error_mode(int mode)
269 {
270   static int current_mode = MSVCRT__OUT_TO_DEFAULT;
271
272   const int old = current_mode;
273   if ( MSVCRT__REPORT_ERRMODE != mode ) {
274     current_mode = mode;
275     FIXME("dummy implementation (old mode: %d, new mode: %d)\n",
276           old, mode);
277   }
278   return old;
279 }
280
281 /******************************************************************************
282  *              _seterrormode (MSVCRT.@)
283  */
284 void CDECL _seterrormode(int mode)
285 {
286     SetErrorMode( mode );
287 }