Delete all Trailing spaces in code.
[reactos.git] / reactos / include / crt / stdlib.h
1 /*
2 * stdlib.h
3 * This file has no copyright assigned and is placed in the Public Domain.
4 * This file is a part of the mingw-runtime package.
5 * No warranty is given; refer to the file DISCLAIMER within the package.
6 *
7 * Definitions for common types, variables, and functions.
8 *
9 */
10
11 #ifndef _STDLIB_H_
12 #define _STDLIB_H_
13
14 /* All the headers include this file. */
15 #include <_mingw.h>
16
17 #define __need_size_t
18 #define __need_wchar_t
19 #define __need_NULL
20 #ifndef RC_INVOKED
21 #include <stddef.h>
22 #endif /* RC_INVOKED */
23
24 #include <stdint.h> /* For uintptr_t */
25
26 /*
27 * RAND_MAX is the maximum value that may be returned by rand.
28 * The minimum is zero.
29 */
30 #define RAND_MAX 0x7FFF
31
32 /*
33 * These values may be used as exit status codes.
34 */
35 #define EXIT_SUCCESS 0
36 #define EXIT_FAILURE 1
37
38 /*
39 * Definitions for path name functions.
40 * NOTE: All of these values have simply been chosen to be conservatively high.
41 * Remember that with long file names we can no longer depend on
42 * extensions being short.
43 */
44 #ifndef __STRICT_ANSI__
45
46 #ifndef MAX_PATH
47 #define MAX_PATH (260)
48 #endif
49
50 #define _MAX_PATH MAX_PATH
51 #define _MAX_DRIVE (3)
52 #define _MAX_DIR 256
53 #define _MAX_FNAME 256
54 #define _MAX_EXT 256
55
56 #endif /* Not __STRICT_ANSI__ */
57
58
59 #ifndef RC_INVOKED
60
61 #ifdef __cplusplus
62 extern "C" {
63 #endif
64
65 #if !defined (__STRICT_ANSI__)
66
67 /*
68 * This seems like a convenient place to declare these variables, which
69 * give programs using WinMain (or main for that matter) access to main-ish
70 * argc and argv. environ is a pointer to a table of environment variables.
71 * NOTE: Strings in _argv and environ are ANSI strings.
72 */
73 extern int _argc;
74 extern char** _argv;
75
76 /* imports from runtime dll of the above variables */
77 #ifdef __MSVCRT__
78
79 extern int* __cdecl __p___argc(void);
80 extern char*** __cdecl __p___argv(void);
81 extern wchar_t*** __cdecl __p___wargv(void);
82
83 #define __argc (*__p___argc())
84 #define __argv (*__p___argv())
85 #define __wargv (*__p___wargv())
86
87 #else /* !MSVCRT */
88
89 #ifndef __DECLSPEC_SUPPORTED
90
91 extern int* _imp____argc_dll;
92 extern char*** _imp____argv_dll;
93 #define __argc (*_imp____argc_dll)
94 #define __argv (*_imp____argv_dll)
95
96 #else /* __DECLSPEC_SUPPORTED */
97
98 __MINGW_IMPORT int __argc_dll;
99 __MINGW_IMPORT char** __argv_dll;
100 #define __argc __argc_dll
101 #define __argv __argv_dll
102
103 #endif /* __DECLSPEC_SUPPORTED */
104
105 #endif /* __MSVCRT */
106 #endif /* __STRICT_ANSI__ */
107 /*
108 * Also defined in ctype.h.
109 */
110 #ifndef MB_CUR_MAX
111 #ifdef __DECLSPEC_SUPPORTED
112 # ifdef __MSVCRT__
113 # define MB_CUR_MAX __mb_cur_max
114 __MINGW_IMPORT int __mb_cur_max;
115 # else /* not __MSVCRT */
116 # define MB_CUR_MAX __mb_cur_max_dll
117 __MINGW_IMPORT int __mb_cur_max_dll;
118 # endif /* not __MSVCRT */
119
120 #else /* ! __DECLSPEC_SUPPORTED */
121 # ifdef __MSVCRT__
122 extern int* _imp____mbcur_max;
123 # define MB_CUR_MAX (*_imp____mb_cur_max)
124 # else /* not __MSVCRT */
125 extern int* _imp____mbcur_max_dll;
126 # define MB_CUR_MAX (*_imp____mb_cur_max_dll)
127 # endif /* not __MSVCRT */
128 #endif /* __DECLSPEC_SUPPORTED */
129 #endif /* MB_CUR_MAX */
130
131 /*
132 * MS likes to declare errno in stdlib.h as well.
133 */
134
135 #ifdef _UWIN
136 #undef errno
137 extern int errno;
138 #else
139 _CRTIMP int* __cdecl _errno(void);
140 #define errno (*_errno())
141 #endif
142 _CRTIMP int* __cdecl __doserrno(void);
143 #define _doserrno (*__doserrno())
144
145 #if !defined (__STRICT_ANSI__)
146 /*
147 * Use environ from the DLL, not as a global.
148 */
149
150 #ifdef __MSVCRT__
151 extern _CRTIMP char *** __cdecl __p__environ(void);
152 extern _CRTIMP wchar_t *** __cdecl __p__wenviron(void);
153 # define _environ (*__p__environ())
154 # define _wenviron (*__p__wenviron())
155 #else /* ! __MSVCRT__ */
156 # ifndef __DECLSPEC_SUPPORTED
157 extern char *** _imp___environ_dll;
158 # define _environ (*_imp___environ_dll)
159 # else /* __DECLSPEC_SUPPORTED */
160 __MINGW_IMPORT char ** _environ_dll;
161 # define _environ _environ_dll
162 # endif /* __DECLSPEC_SUPPORTED */
163 #endif /* ! __MSVCRT__ */
164
165 #define environ _environ
166
167 #ifdef __MSVCRT__
168 /* One of the MSVCRTxx libraries */
169
170 #ifndef __DECLSPEC_SUPPORTED
171 extern int* _imp___sys_nerr;
172 # define sys_nerr (*_imp___sys_nerr)
173 #else /* __DECLSPEC_SUPPORTED */
174 __MINGW_IMPORT int _sys_nerr;
175 # ifndef _UWIN
176 # define sys_nerr _sys_nerr
177 # endif /* _UWIN */
178 #endif /* __DECLSPEC_SUPPORTED */
179
180 #else /* ! __MSVCRT__ */
181
182 /* CRTDLL run time library */
183
184 #ifndef __DECLSPEC_SUPPORTED
185 extern int* _imp___sys_nerr_dll;
186 # define sys_nerr (*_imp___sys_nerr_dll)
187 #else /* __DECLSPEC_SUPPORTED */
188 __MINGW_IMPORT int _sys_nerr_dll;
189 # define sys_nerr _sys_nerr_dll
190 #endif /* __DECLSPEC_SUPPORTED */
191
192 #endif /* ! __MSVCRT__ */
193
194 #ifndef __DECLSPEC_SUPPORTED
195 extern char*** _imp__sys_errlist;
196 #define sys_errlist (*_imp___sys_errlist)
197 #else /* __DECLSPEC_SUPPORTED */
198 __MINGW_IMPORT char* _sys_errlist[];
199 #ifndef _UWIN
200 #define sys_errlist _sys_errlist
201 #endif /* _UWIN */
202 #endif /* __DECLSPEC_SUPPORTED */
203
204 /*
205 * OS version and such constants.
206 */
207
208 #ifdef __MSVCRT__
209 /* msvcrtxx.dll */
210
211 extern _CRTIMP unsigned __cdecl int* __p__osver(void);
212 extern _CRTIMP unsigned __cdecl int* __p__winver(void);
213 extern _CRTIMP unsigned __cdecl int* __p__winmajor(void);
214 extern _CRTIMP unsigned __cdecl int* __p__winminor(void);
215
216 #ifndef __DECLSPEC_SUPPORTED
217 # define _osver (*__p__osver())
218 # define _winver (*__p__winver())
219 # define _winmajor (*__p__winmajor())
220 # define _winminor (*__p__winminor())
221 #else
222 __MINGW_IMPORT unsigned int _osver;
223 __MINGW_IMPORT unsigned int _winver;
224 __MINGW_IMPORT unsigned int _winmajor;
225 __MINGW_IMPORT unsigned int _winminor;
226 #endif /* __DECLSPEC_SUPPORTED */
227
228 #else
229 /* Not msvcrtxx.dll, thus crtdll.dll */
230
231 #ifndef __DECLSPEC_SUPPORTED
232
233 extern unsigned int* _imp___osver_dll;
234 extern unsigned int* _imp___winver_dll;
235 extern unsigned int* _imp___winmajor_dll;
236 extern unsigned int* _imp___winminor_dll;
237
238 #define _osver (*_imp___osver_dll)
239 #define _winver (*_imp___winver_dll)
240 #define _winmajor (*_imp___winmajor_dll)
241 #define _winminor (*_imp___winminor_dll)
242
243 #else /* __DECLSPEC_SUPPORTED */
244
245 __MINGW_IMPORT unsigned int _osver_dll;
246 __MINGW_IMPORT unsigned int _winver_dll;
247 __MINGW_IMPORT unsigned int _winmajor_dll;
248 __MINGW_IMPORT unsigned int _winminor_dll;
249
250 #define _osver _osver_dll
251 #define _winver _winver_dll
252 #define _winmajor _winmajor_dll
253 #define _winminor _winminor_dll
254
255 #endif /* __DECLSPEC_SUPPORTED */
256
257 #endif
258
259 #if defined __MSVCRT__
260 /* although the _pgmptr is exported as DATA,
261 * be safe and use the access function __p__pgmptr() to get it. */
262 _CRTIMP char** __cdecl __p__pgmptr(void);
263 #define _pgmptr (*__p__pgmptr())
264 _CRTIMP wchar_t** __cdecl __p__wpgmptr(void);
265 #define _wpgmptr (*__p__wpgmptr())
266 #else /* ! __MSVCRT__ */
267 # ifndef __DECLSPEC_SUPPORTED
268 extern char** __imp__pgmptr_dll;
269 # define _pgmptr (*_imp___pgmptr_dll)
270 # else /* __DECLSPEC_SUPPORTED */
271 __MINGW_IMPORT char* _pgmptr_dll;
272 # define _pgmptr _pgmptr_dll
273 # endif /* __DECLSPEC_SUPPORTED */
274 /* no wide version in CRTDLL */
275 #endif /* __MSVCRT__ */
276
277 /*
278 * This variable determines the default file mode.
279 * TODO: Which flags work?
280 */
281 #if !defined (__DECLSPEC_SUPPORTED) || defined (__IN_MINGW_RUNTIME)
282
283 #ifdef __MSVCRT__
284 extern int* _imp___fmode;
285 #define _fmode (*_imp___fmode)
286 #else
287 /* CRTDLL */
288 extern int* _imp___fmode_dll;
289 #define _fmode (*_imp___fmode_dll)
290 #endif
291
292 #else /* __DECLSPEC_SUPPORTED */
293
294 #ifdef __MSVCRT__
295 __MINGW_IMPORT int _fmode;
296 #else /* ! __MSVCRT__ */
297 __MINGW_IMPORT int _fmode_dll;
298 #define _fmode _fmode_dll
299 #endif /* ! __MSVCRT__ */
300
301 #endif /* __DECLSPEC_SUPPORTED */
302
303 #endif /* Not __STRICT_ANSI__ */
304
305 _CRTIMP double __cdecl atof (const char*);
306 _CRTIMP int __cdecl atoi (const char*);
307 _CRTIMP long __cdecl atol (const char*);
308 #if !defined (__STRICT_ANSI__)
309 _CRTIMP int __cdecl _wtoi (const wchar_t *);
310 _CRTIMP long __cdecl _wtol (const wchar_t *);
311 #endif
312 _CRTIMP double __cdecl strtod (const char*, char**);
313 #if !defined __NO_ISOCEXT /* in libmingwex.a */
314 float __cdecl strtof (const char * __restrict__, char ** __restrict__);
315 long double __cdecl strtold (const char * __restrict__, char ** __restrict__);
316 #endif /* __NO_ISOCEXT */
317
318 _CRTIMP long __cdecl strtol (const char*, char**, int);
319 _CRTIMP unsigned long __cdecl strtoul (const char*, char**, int);
320
321 #ifndef _WSTDLIB_DEFINED
322 /* also declared in wchar.h */
323 _CRTIMP double __cdecl wcstod (const wchar_t*, wchar_t**);
324 #if !defined __NO_ISOCEXT /* in libmingwex.a */
325 float __cdecl wcstof( const wchar_t * __restrict__, wchar_t ** __restrict__);
326 long double __cdecl wcstold (const wchar_t * __restrict__, wchar_t ** __restrict__);
327 #endif /* __NO_ISOCEXT */
328
329 _CRTIMP long __cdecl wcstol (const wchar_t*, wchar_t**, int);
330 _CRTIMP unsigned long __cdecl wcstoul (const wchar_t*, wchar_t**, int);
331 #define _WSTDLIB_DEFINED
332 #endif
333
334 _CRTIMP size_t __cdecl wcstombs (char*, const wchar_t*, size_t);
335 _CRTIMP int __cdecl wctomb (char*, wchar_t);
336
337 _CRTIMP int __cdecl mblen (const char*, size_t);
338 _CRTIMP size_t __cdecl mbstowcs (wchar_t*, const char*, size_t);
339 _CRTIMP int __cdecl mbtowc (wchar_t*, const char*, size_t);
340
341 _CRTIMP int __cdecl rand (void);
342 _CRTIMP void __cdecl srand (unsigned int);
343
344 _CRTIMP void* __cdecl calloc (size_t, size_t) __MINGW_ATTRIB_MALLOC;
345 _CRTIMP void* __cdecl malloc (size_t) __MINGW_ATTRIB_MALLOC;
346 _CRTIMP void* __cdecl realloc (void*, size_t);
347 _CRTIMP void __cdecl free (void*);
348 _CRTIMP void __cdecl abort (void) __MINGW_ATTRIB_NORETURN;
349 _CRTIMP void __cdecl exit (int) __MINGW_ATTRIB_NORETURN;
350
351 /* Note: This is in startup code, not imported directly from dll */
352 int __cdecl atexit (void (*)(void));
353
354 _CRTIMP int __cdecl system (const char*);
355 _CRTIMP char* __cdecl getenv (const char*);
356
357 /* bsearch and qsort are also in non-ANSI header search.h */
358 _CRTIMP void* __cdecl bsearch (const void*, const void*, size_t, size_t,
359 int (*)(const void*, const void*));
360 _CRTIMP void __cdecl qsort (void*, size_t, size_t,
361 int (*)(const void*, const void*));
362
363 _CRTIMP int __cdecl abs (int) __MINGW_ATTRIB_CONST;
364 _CRTIMP long __cdecl labs (long) __MINGW_ATTRIB_CONST;
365
366 /*
367 * div_t and ldiv_t are structures used to return the results of div and
368 * ldiv.
369 *
370 * NOTE: div and ldiv appear not to work correctly unless
371 * -fno-pcc-struct-return is specified. This is included in the
372 * mingw32 specs file.
373 */
374 typedef struct { int quot, rem; } div_t;
375 typedef struct { long quot, rem; } ldiv_t;
376
377 _CRTIMP div_t __cdecl div (int, int) __MINGW_ATTRIB_CONST;
378 _CRTIMP ldiv_t __cdecl ldiv (long, long) __MINGW_ATTRIB_CONST;
379
380 #if !defined (__STRICT_ANSI__)
381
382 /*
383 * NOTE: Officially the three following functions are obsolete. The Win32 API
384 * functions SetErrorMode, Beep and Sleep are their replacements.
385 */
386 _CRTIMP void __cdecl _beep (unsigned int, unsigned int) __MINGW_ATTRIB_DEPRECATED;
387 /* Not to be confused with _set_error_mode (int). */
388 _CRTIMP void __cdecl _seterrormode (int) __MINGW_ATTRIB_DEPRECATED;
389 _CRTIMP void __cdecl _sleep (unsigned long) __MINGW_ATTRIB_DEPRECATED;
390
391 _CRTIMP void __cdecl _exit (int) __MINGW_ATTRIB_NORETURN;
392
393 /* _onexit is MS extension. Use atexit for portability. */
394 /* Note: This is in startup code, not imported directly from dll */
395 typedef int (* _onexit_t)(void);
396 _onexit_t __cdecl _onexit( _onexit_t );
397
398 _CRTIMP int __cdecl _putenv (const char*);
399 _CRTIMP void __cdecl _searchenv (const char*, const char*, char*);
400
401
402 _CRTIMP char* __cdecl _ecvt (double, int, int*, int*);
403 _CRTIMP char* __cdecl _fcvt (double, int, int*, int*);
404 _CRTIMP char* __cdecl _gcvt (double, int, char*);
405
406 _CRTIMP void __cdecl _makepath (char*, const char*, const char*, const char*, const char*);
407 _CRTIMP void __cdecl _splitpath (const char*, char*, char*, char*, char*);
408 _CRTIMP char* __cdecl _fullpath (char*, const char*, size_t);
409
410 _CRTIMP char* __cdecl _itoa (int, char*, int);
411 _CRTIMP char* __cdecl _ltoa (long, char*, int);
412 _CRTIMP char* __cdecl _ultoa(unsigned long, char*, int);
413 _CRTIMP wchar_t* __cdecl _itow (int, wchar_t*, int);
414 _CRTIMP wchar_t* __cdecl _ltow (long, wchar_t*, int);
415 _CRTIMP wchar_t* __cdecl _ultow (unsigned long, wchar_t*, int);
416
417 #ifdef __MSVCRT__
418 _CRTIMP __int64 __cdecl _atoi64(const char *);
419 _CRTIMP char* __cdecl _i64toa(__int64, char *, int);
420 _CRTIMP char* __cdecl _ui64toa(unsigned __int64, char *, int);
421 _CRTIMP __int64 __cdecl _wtoi64(const wchar_t *);
422 _CRTIMP wchar_t* __cdecl _i64tow(__int64, wchar_t *, int);
423 _CRTIMP wchar_t* __cdecl _ui64tow(unsigned __int64, wchar_t *, int);
424
425 _CRTIMP wchar_t* __cdecl _wgetenv(const wchar_t*);
426 _CRTIMP int __cdecl _wputenv(const wchar_t*);
427 _CRTIMP void __cdecl _wsearchenv(const wchar_t*, const wchar_t*, wchar_t*);
428 _CRTIMP void __cdecl _wmakepath(wchar_t*, const wchar_t*, const wchar_t*, const wchar_t*, const wchar_t*);
429 _CRTIMP void __cdecl _wsplitpath (const wchar_t*, wchar_t*, wchar_t*, wchar_t*, wchar_t*);
430 _CRTIMP wchar_t* __cdecl _wfullpath (wchar_t*, const wchar_t*, size_t);
431
432 _CRTIMP unsigned int __cdecl _rotl(unsigned int, int) __MINGW_ATTRIB_CONST;
433 _CRTIMP unsigned int __cdecl _rotr(unsigned int, int) __MINGW_ATTRIB_CONST;
434 _CRTIMP unsigned long __cdecl _lrotl(unsigned long, int) __MINGW_ATTRIB_CONST;
435 _CRTIMP unsigned long __cdecl _lrotr(unsigned long, int) __MINGW_ATTRIB_CONST;
436
437 _CRTIMP int __cdecl _set_error_mode (int);
438 #define _OUT_TO_DEFAULT 0
439 #define _OUT_TO_STDERR 1
440 #define _OUT_TO_MSGBOX 2
441 #define _REPORT_ERRMODE 3
442
443 #if __MSVCRT_VERSION__ >= 0x800
444 _CRTIMP unsigned int __cdecl _set_abort_behavior (unsigned int, unsigned int);
445 /* These masks work with msvcr80.dll version 8.0.50215.44 (a beta release). */
446 #define _WRITE_ABORT_MSG 1
447 #define _CALL_REPORTFAULT 2
448
449 typedef void (* _invalid_parameter_handler) (const wchar_t *,
450 const wchar_t *,
451 const wchar_t *,
452 unsigned int,
453 uintptr_t);
454 _invalid_parameter_handler _set_invalid_parameter_handler (_invalid_parameter_handler);
455 #endif /* __MSVCRT_VERSION__ >= 0x800 */
456 #endif /* __MSVCRT__ */
457
458 #ifndef _NO_OLDNAMES
459
460 _CRTIMP int __cdecl putenv (const char*);
461 _CRTIMP void __cdecl searchenv (const char*, const char*, char*);
462
463 _CRTIMP char* __cdecl itoa (int, char*, int);
464 _CRTIMP char* __cdecl ltoa (long, char*, int);
465
466 #ifndef _UWIN
467 _CRTIMP char* __cdecl ecvt (double, int, int*, int*);
468 _CRTIMP char* __cdecl fcvt (double, int, int*, int*);
469 _CRTIMP char* __cdecl gcvt (double, int, char*);
470 #endif /* _UWIN */
471 #endif /* Not _NO_OLDNAMES */
472
473 #endif /* Not __STRICT_ANSI__ */
474
475 /* C99 names */
476
477 #if !defined __NO_ISOCEXT /* externs in static libmingwex.a */
478
479 /* C99 name for _exit */
480 void __cdecl _Exit(int) __MINGW_ATTRIB_NORETURN;
481 #ifndef __STRICT_ANSI__ /* inline using non-ansi functions */
482 __CRT_INLINE void __cdecl _Exit(int __status)
483 { _exit (__status); }
484 #endif
485
486 typedef struct { long long quot, rem; } lldiv_t;
487
488 lldiv_t __cdecl lldiv (long long, long long) __MINGW_ATTRIB_CONST;
489
490 long long __cdecl llabs(long long);
491 __CRT_INLINE long long __cdecl llabs(long long _j)
492 {return (_j >= 0 ? _j : -_j);}
493
494 long long __cdecl strtoll (const char* __restrict__, char** __restrict, int);
495 unsigned long long __cdecl strtoull (const char* __restrict__, char** __restrict__, int);
496
497 #if defined (__MSVCRT__) /* these are stubs for MS _i64 versions */
498 long long __cdecl atoll (const char *);
499
500 #if !defined (__STRICT_ANSI__)
501 long long __cdecl wtoll (const wchar_t *);
502 char* __cdecl lltoa (long long, char *, int);
503 char* __cdecl ulltoa (unsigned long long , char *, int);
504 wchar_t* __cdecl lltow (long long, wchar_t *, int);
505 wchar_t* __cdecl ulltow (unsigned long long, wchar_t *, int);
506
507 /* inline using non-ansi functions */
508 __CRT_INLINE long long __cdecl atoll (const char * _c)
509 { return _atoi64 (_c); }
510 __CRT_INLINE char* __cdecl lltoa (long long _n, char * _c, int _i)
511 { return _i64toa (_n, _c, _i); }
512 __CRT_INLINE char* __cdecl ulltoa (unsigned long long _n, char * _c, int _i)
513 { return _ui64toa (_n, _c, _i); }
514 __CRT_INLINE long long __cdecl wtoll (const wchar_t * _w)
515 { return _wtoi64 (_w); }
516 __CRT_INLINE wchar_t* __cdecl lltow (long long _n, wchar_t * _w, int _i)
517 { return _i64tow (_n, _w, _i); }
518 __CRT_INLINE wchar_t* __cdecl ulltow (unsigned long long _n, wchar_t * _w, int _i)
519 { return _ui64tow (_n, _w, _i); }
520 #endif /* (__STRICT_ANSI__) */
521
522 #endif /* __MSVCRT__ */
523
524 #endif /* !__NO_ISOCEXT */
525
526
527 #ifdef __cplusplus
528 }
529 #endif
530
531 #endif /* Not RC_INVOKED */
532
533 #endif /* Not _STDLIB_H_ */
534