OSDN Git Service

* NEWS: Add and adjust release notes for 4.0.
[mingw/mingw-org-wsl.git] / include / stdlib.h
1 /**
2  * @file stdlib.h
3  * Copyright 2012, 2013 MinGW.org project
4  *
5  * Permission is hereby granted, free of charge, to any person obtaining a
6  * copy of this software and associated documentation files (the "Software"),
7  * to deal in the Software without restriction, including without limitation
8  * the rights to use, copy, modify, merge, publish, distribute, sublicense,
9  * and/or sell copies of the Software, and to permit persons to whom the
10  * Software is furnished to do so, subject to the following conditions:
11  * 
12  * The above copyright notice and this permission notice (including the next
13  * paragraph) shall be included in all copies or substantial portions of the
14  * Software.
15  * 
16  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19  * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20  * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
21  * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
22  * DEALINGS IN THE SOFTWARE.
23  */
24 #ifndef _STDLIB_H
25 #define _STDLIB_H
26 #pragma GCC system_header
27 #include <_mingw.h>
28
29 #define __need_size_t
30 #define __need_wchar_t
31 #define __need_NULL
32
33 #ifndef RC_INVOKED
34 #include <stddef.h>
35 #endif /* RC_INVOKED */
36
37 /*
38  * RAND_MAX is the maximum value that may be returned by rand.
39  * The minimum is zero.
40  */
41 #define RAND_MAX 0x7FFF
42
43 /*
44  * These values may be used as exit status codes.
45  */
46 #define EXIT_SUCCESS 0
47 #define EXIT_FAILURE 1
48
49 /*
50  * Definitions for path name functions.
51  * NOTE: All of these values have simply been chosen to be conservatively high.
52  *       Remember that with long file names we can no longer depend on
53  *       extensions being short.
54  */
55 #ifndef __STRICT_ANSI__
56
57 #ifndef MAX_PATH
58 #define MAX_PATH (260)
59 #endif
60
61 #define _MAX_PATH MAX_PATH
62 #define _MAX_DRIVE (3)
63 #define _MAX_DIR 256
64 #define _MAX_FNAME 256
65 #define _MAX_EXT 256
66 #define _MAX_ENV 32767
67
68 #endif /* Not __STRICT_ANSI__ */
69
70 #ifndef RC_INVOKED
71
72 #ifdef __cplusplus
73 extern "C" {
74 #endif
75
76 #if !defined (__STRICT_ANSI__)
77
78 /*
79  * This seems like a convenient place to declare these variables, which
80  * give programs using WinMain (or main for that matter) access to main-ish
81  * argc and argv. environ is a pointer to a table of environment variables.
82  * NOTE: Strings in _argv and environ are ANSI strings.
83  */
84 extern int _argc;
85 extern char** _argv;
86
87 /* imports from runtime dll of the above variables */
88
89 extern int*  __cdecl __MINGW_NOTHROW   __p___argc(void);
90 extern char*** __cdecl __MINGW_NOTHROW  __p___argv(void);
91 extern wchar_t***  __cdecl __MINGW_NOTHROW __p___wargv(void);
92
93 #define __argc (*__p___argc())
94 #define __argv (*__p___argv())
95 #define __wargv (*__p___wargv())
96
97 #endif /* __STRICT_ANSI__ */
98 /*
99  * Also defined in ctype.h.
100  */
101 #ifndef MB_CUR_MAX
102 #  define MB_CUR_MAX __mb_cur_max
103    __MINGW_IMPORT int __mb_cur_max;
104 #endif  /* MB_CUR_MAX */
105
106 /* 
107  * MS likes to declare errno in stdlib.h as well. 
108  */
109
110  _CRTIMP int* __cdecl __MINGW_NOTHROW _errno(void);
111 #define errno  (*_errno())
112  _CRTIMP int* __cdecl __MINGW_NOTHROW __doserrno(void);
113 #define _doserrno (*__doserrno())
114
115 #if !defined (__STRICT_ANSI__)
116 /*
117  * Use environ from the DLL, not as a global. 
118  */
119
120   extern _CRTIMP char *** __cdecl __MINGW_NOTHROW __p__environ(void);
121   extern _CRTIMP wchar_t *** __cdecl __MINGW_NOTHROW  __p__wenviron(void);
122 # define _environ (*__p__environ())
123 # define _wenviron (*__p__wenviron())
124
125 #define environ _environ
126
127 __MINGW_IMPORT int _sys_nerr;
128 #define sys_nerr _sys_nerr
129
130 __MINGW_IMPORT char* _sys_errlist[];
131 #define sys_errlist _sys_errlist
132
133 /*
134  * OS version and such constants.
135  */
136
137 extern _CRTIMP unsigned __cdecl __MINGW_NOTHROW int* __p__osver(void);
138 extern _CRTIMP unsigned __cdecl __MINGW_NOTHROW int* __p__winver(void);
139 extern _CRTIMP unsigned __cdecl __MINGW_NOTHROW int* __p__winmajor(void);
140 extern _CRTIMP unsigned __cdecl __MINGW_NOTHROW int* __p__winminor(void);
141
142 __MINGW_IMPORT unsigned int _osver;
143 __MINGW_IMPORT unsigned int _winver;
144 __MINGW_IMPORT unsigned int _winmajor;
145 __MINGW_IMPORT unsigned int _winminor;
146
147
148 /* although the _pgmptr is exported as DATA,
149  * be safe and use the access function __p__pgmptr() to get it. */
150 _CRTIMP char** __cdecl __MINGW_NOTHROW __p__pgmptr(void);
151 #define _pgmptr     (*__p__pgmptr())
152 _CRTIMP wchar_t** __cdecl __MINGW_NOTHROW __p__wpgmptr(void);
153 #define _wpgmptr    (*__p__wpgmptr())
154
155 /*
156  * This variable determines the default file mode.
157  * TODO: Which flags work?
158  */
159 #if defined (__IN_MINGW_RUNTIME)
160
161 extern int* _imp___fmode;
162 #define _fmode (*_imp___fmode)
163
164 #else /* ! __IN_MINGW_RUNTIME */
165
166 __MINGW_IMPORT  int _fmode;
167
168 #endif /* ! __IN_MINGW_RUNTIME */
169
170 #endif /* Not __STRICT_ANSI__ */
171
172 _CRTIMP __int64 __cdecl _strtoi64(const char*, char **, int);
173 _CRTIMP __int64 __cdecl _strtoi64_l(const char *, char **, int, _locale_t);
174 _CRTIMP unsigned __int64 __cdecl _strtoui64(const char*, char **, int);
175 _CRTIMP unsigned __int64 __cdecl _strtoui64_l(const char *, char **, int, _locale_t);
176
177 _CRTIMP double __cdecl __MINGW_NOTHROW atof (const char*);
178 _CRTIMP int __cdecl __MINGW_NOTHROW atoi (const char*);
179 _CRTIMP long __cdecl __MINGW_NOTHROW  atol (const char*);
180
181 #if !defined (__STRICT_ANSI__)
182 _CRTIMP double __cdecl __MINGW_NOTHROW _wtof (const wchar_t *);
183 _CRTIMP int __cdecl __MINGW_NOTHROW _wtoi (const wchar_t *);
184 _CRTIMP long __cdecl __MINGW_NOTHROW _wtol (const wchar_t *);
185 #endif
186
187 #if !defined __NO_ISOCEXT  /*  in libmingwex.a */
188 double __cdecl __MINGW_NOTHROW __strtod (const char*, char**);
189 extern double __cdecl __MINGW_NOTHROW
190 strtod (const char* __restrict__ __nptr, char** __restrict__ __endptr);
191 float __cdecl __MINGW_NOTHROW strtof (const char * __restrict__, char ** __restrict__);
192 long double __cdecl __MINGW_NOTHROW strtold (const char * __restrict__, char ** __restrict__);
193 #else
194 _CRTIMP double __cdecl __MINGW_NOTHROW strtod (const char*, char**);
195 #endif /* __NO_ISOCEXT */
196
197 _CRTIMP long __cdecl __MINGW_NOTHROW strtol (const char*, char**, int);
198 _CRTIMP unsigned long __cdecl __MINGW_NOTHROW strtoul (const char*, char**, int);
199
200 #ifndef _WSTDLIB_DEFINED
201 /*  also declared in wchar.h */
202 _CRTIMP __int64 __cdecl _wcstoi64(const wchar_t *, wchar_t **, int);
203 _CRTIMP __int64 __cdecl _wcstoi64_l(const wchar_t *, wchar_t **, int, _locale_t);
204 _CRTIMP unsigned __int64 __cdecl _wcstoui64(const wchar_t *, wchar_t **, int);
205 _CRTIMP unsigned __int64 __cdecl _wcstoui64_l(const wchar_t *, wchar_t **, int, _locale_t);
206 _CRTIMP long __cdecl __MINGW_NOTHROW wcstol (const wchar_t*, wchar_t**, int);
207 _CRTIMP unsigned long __cdecl __MINGW_NOTHROW wcstoul (const wchar_t*, wchar_t**, int);
208 _CRTIMP double __cdecl __MINGW_NOTHROW wcstod (const wchar_t*, wchar_t**);
209
210 #if !defined __NO_ISOCEXT /*  in libmingwex.a */
211 float __cdecl __MINGW_NOTHROW wcstof( const wchar_t * __restrict__, wchar_t ** __restrict__);
212 long double __cdecl __MINGW_NOTHROW wcstold (const wchar_t * __restrict__, wchar_t ** __restrict__);
213 #endif /* __NO_ISOCEXT */
214
215 _CRTIMP wchar_t* __cdecl __MINGW_NOTHROW _wgetenv(const wchar_t*);
216 _CRTIMP int __cdecl __MINGW_NOTHROW _wputenv(const wchar_t*);
217 _CRTIMP void __cdecl __MINGW_NOTHROW _wsearchenv(const wchar_t*, const wchar_t*, wchar_t*);
218 _CRTIMP int __cdecl __MINGW_NOTHROW    _wsystem(const wchar_t*);
219 _CRTIMP void __cdecl __MINGW_NOTHROW    _wmakepath(wchar_t*, const wchar_t*, const wchar_t*, const wchar_t*, const wchar_t*);
220 _CRTIMP void __cdecl __MINGW_NOTHROW _wsplitpath (const wchar_t*, wchar_t*, wchar_t*, wchar_t*, wchar_t*);
221 _CRTIMP wchar_t* __cdecl __MINGW_NOTHROW   _wfullpath (wchar_t*, const wchar_t*, size_t);
222 #define _WSTDLIB_DEFINED
223 #endif /* !_WSTDLIB_DEFINED */
224
225 _CRTIMP size_t __cdecl __MINGW_NOTHROW wcstombs (char*, const wchar_t*, size_t);
226 _CRTIMP int __cdecl __MINGW_NOTHROW wctomb  (char*, wchar_t);
227
228 _CRTIMP int __cdecl __MINGW_NOTHROW mblen  (const char*, size_t);
229 _CRTIMP size_t __cdecl __MINGW_NOTHROW mbstowcs (wchar_t*, const char*, size_t);
230 _CRTIMP int __cdecl __MINGW_NOTHROW mbtowc  (wchar_t*, const char*, size_t);
231
232 _CRTIMP int __cdecl __MINGW_NOTHROW rand (void);
233 _CRTIMP void __cdecl __MINGW_NOTHROW srand (unsigned int);
234
235 _CRTIMP void* __cdecl __MINGW_NOTHROW calloc (size_t, size_t) __MINGW_ATTRIB_MALLOC;
236 _CRTIMP void* __cdecl __MINGW_NOTHROW malloc (size_t) __MINGW_ATTRIB_MALLOC;
237 _CRTIMP void* __cdecl __MINGW_NOTHROW realloc (void*, size_t);
238 _CRTIMP void __cdecl __MINGW_NOTHROW free (void*);
239 _CRTIMP void __cdecl __MINGW_NOTHROW abort (void) __MINGW_ATTRIB_NORETURN;
240 _CRTIMP void __cdecl __MINGW_NOTHROW exit (int) __MINGW_ATTRIB_NORETURN;
241
242 /* Note: This is in startup code, not imported directly from dll */
243 int __cdecl __MINGW_NOTHROW atexit (void (*)(void));
244
245 _CRTIMP int __cdecl __MINGW_NOTHROW system (const char*);
246 _CRTIMP char* __cdecl __MINGW_NOTHROW getenv (const char*);
247
248 /* bsearch and qsort are also in non-ANSI header search.h  */
249 _CRTIMP void* __cdecl bsearch (const void*, const void*, size_t, size_t, 
250           int (*)(const void*, const void*));
251 _CRTIMP void __cdecl qsort(void*, size_t, size_t,
252       int (*)(const void*, const void*));
253
254 _CRTIMP int __cdecl __MINGW_NOTHROW abs (int) __MINGW_ATTRIB_CONST;
255 _CRTIMP long __cdecl __MINGW_NOTHROW labs (long) __MINGW_ATTRIB_CONST;
256
257 /*
258  * div_t and ldiv_t are structures used to return the results of div and
259  * ldiv.
260  *
261  * NOTE: div and ldiv appear not to work correctly unless
262  *       -fno-pcc-struct-return is specified. This is included in the
263  *       mingw32 specs file.
264  */
265 typedef struct { int quot, rem; } div_t;
266 typedef struct { long quot, rem; } ldiv_t;
267
268 _CRTIMP div_t __cdecl __MINGW_NOTHROW div (int, int) __MINGW_ATTRIB_CONST;
269 _CRTIMP ldiv_t __cdecl __MINGW_NOTHROW ldiv (long, long) __MINGW_ATTRIB_CONST;
270
271 #if !defined (__STRICT_ANSI__)
272
273 /*
274  * NOTE: Officially the three following functions are obsolete. The Win32 API
275  *       functions SetErrorMode, Beep and Sleep are their replacements.
276  */
277 _CRTIMP void __cdecl __MINGW_NOTHROW _beep (unsigned int, unsigned int) __MINGW_ATTRIB_DEPRECATED;
278 /* Not to be confused with  _set_error_mode (int).  */
279 _CRTIMP void __cdecl __MINGW_NOTHROW _seterrormode (int) __MINGW_ATTRIB_DEPRECATED;
280 _CRTIMP void __cdecl __MINGW_NOTHROW _sleep (unsigned long) __MINGW_ATTRIB_DEPRECATED;
281
282 _CRTIMP void __cdecl __MINGW_NOTHROW _exit (int) __MINGW_ATTRIB_NORETURN;
283
284 /* _onexit is MS extension. Use atexit for portability.  */
285 /* Note: This is in startup code, not imported directly from dll */
286 typedef  int (* _onexit_t)(void);
287 _onexit_t __cdecl __MINGW_NOTHROW _onexit( _onexit_t );
288
289 _CRTIMP int __cdecl __MINGW_NOTHROW _putenv (const char*);
290 _CRTIMP void __cdecl __MINGW_NOTHROW _searchenv (const char*, const char*, char*);
291
292 _CRTIMP char* __cdecl __MINGW_NOTHROW _ecvt (double, int, int*, int*);
293 _CRTIMP char* __cdecl __MINGW_NOTHROW _fcvt (double, int, int*, int*);
294 _CRTIMP char* __cdecl __MINGW_NOTHROW _gcvt (double, int, char*);
295
296 _CRTIMP void __cdecl __MINGW_NOTHROW _makepath (char*, const char*, const char*, const char*, const char*);
297 _CRTIMP void __cdecl __MINGW_NOTHROW _splitpath (const char*, char*, char*, char*, char*);
298 _CRTIMP char* __cdecl __MINGW_NOTHROW _fullpath (char*, const char*, size_t);
299
300 _CRTIMP char* __cdecl __MINGW_NOTHROW _itoa (int, char*, int);
301 _CRTIMP char* __cdecl __MINGW_NOTHROW _ltoa (long, char*, int);
302 _CRTIMP char* __cdecl __MINGW_NOTHROW   _ultoa(unsigned long, char*, int);
303 _CRTIMP wchar_t* __cdecl __MINGW_NOTHROW  _itow (int, wchar_t*, int);
304 _CRTIMP wchar_t* __cdecl __MINGW_NOTHROW  _ltow (long, wchar_t*, int);
305 _CRTIMP wchar_t* __cdecl __MINGW_NOTHROW  _ultow (unsigned long, wchar_t*, int);
306
307 _CRTIMP __int64 __cdecl __MINGW_NOTHROW _atoi64(const char *);
308 _CRTIMP char* __cdecl __MINGW_NOTHROW _i64toa(__int64, char *, int);
309 _CRTIMP char* __cdecl __MINGW_NOTHROW _ui64toa(unsigned __int64, char *, int);
310 _CRTIMP __int64 __cdecl __MINGW_NOTHROW _wtoi64(const wchar_t *);
311 _CRTIMP wchar_t* __cdecl __MINGW_NOTHROW _i64tow(__int64, wchar_t *, int);
312 _CRTIMP wchar_t* __cdecl __MINGW_NOTHROW _ui64tow(unsigned __int64, wchar_t *, int);
313
314 _CRTIMP unsigned int __cdecl __MINGW_NOTHROW (_rotl)(unsigned int, int) __MINGW_ATTRIB_CONST;
315 _CRTIMP unsigned int __cdecl __MINGW_NOTHROW (_rotr)(unsigned int, int) __MINGW_ATTRIB_CONST;
316 _CRTIMP unsigned long __cdecl __MINGW_NOTHROW (_lrotl)(unsigned long, int) __MINGW_ATTRIB_CONST;
317 _CRTIMP unsigned long __cdecl __MINGW_NOTHROW (_lrotr)(unsigned long, int) __MINGW_ATTRIB_CONST;
318
319 _CRTIMP int __cdecl __MINGW_NOTHROW _set_error_mode (int);
320
321 # define _OUT_TO_DEFAULT 0
322 # define _OUT_TO_STDERR  1
323 # define _OUT_TO_MSGBOX  2
324 # define _REPORT_ERRMODE 3
325
326 # ifndef _UINTPTR_T_DEFINED
327 #  define _UINTPTR_T_DEFINED
328 #  ifdef _WIN64
329      typedef unsigned __int64 uintptr_t;
330 #  else
331      typedef unsigned int uintptr_t;
332 #  endif
333 # endif
334
335 _CRTIMP unsigned int __cdecl __MINGW_NOTHROW _set_abort_behavior (unsigned int, unsigned int);
336
337 /* These masks work with msvcr80.dll version 8.0.50215.44 (a beta release).  */
338 #  define _WRITE_ABORT_MSG 1
339 #  define _CALL_REPORTFAULT 2
340
341 typedef void
342 (* _invalid_parameter_handler) (
343     const wchar_t *,
344     const wchar_t *,
345     const wchar_t *,
346     unsigned int,
347     uintptr_t);
348 _invalid_parameter_handler _set_invalid_parameter_handler (_invalid_parameter_handler);
349
350 #ifndef _NO_OLDNAMES
351
352 _CRTIMP int __cdecl __MINGW_NOTHROW putenv (const char*);
353 _CRTIMP void __cdecl __MINGW_NOTHROW searchenv (const char*, const char*, char*);
354
355 _CRTIMP char* __cdecl __MINGW_NOTHROW itoa (int, char*, int);
356 _CRTIMP char* __cdecl __MINGW_NOTHROW ltoa (long, char*, int);
357
358 _CRTIMP char* __cdecl __MINGW_NOTHROW ecvt (double, int, int*, int*);
359 _CRTIMP char* __cdecl __MINGW_NOTHROW fcvt (double, int, int*, int*);
360 _CRTIMP char* __cdecl __MINGW_NOTHROW gcvt (double, int, char*);
361 #endif /* Not _NO_OLDNAMES */
362
363 #endif /* Not __STRICT_ANSI__ */
364
365 /* C99 names */
366
367 #if !defined __NO_ISOCEXT /* externs in static libmingwex.a */
368
369 /* C99 name for _exit */
370 void __cdecl __MINGW_NOTHROW _Exit(int) __MINGW_ATTRIB_NORETURN;
371 #if !defined __NO_INLINE__ && !defined __STRICT_ANSI__
372 __CRT_INLINE void __cdecl __MINGW_NOTHROW _Exit(int __status)
373  {  _exit (__status); }
374 #endif 
375
376 typedef struct { long long quot, rem; } lldiv_t;
377
378 lldiv_t __cdecl __MINGW_NOTHROW lldiv (long long, long long) __MINGW_ATTRIB_CONST;
379
380 long long __cdecl __MINGW_NOTHROW llabs(long long);
381 #ifndef __NO_INLINE__
382 __CRT_INLINE long long __cdecl __MINGW_NOTHROW llabs(long long _j)
383   {return (_j >= 0 ? _j : -_j);}
384 #endif
385
386 long long  __cdecl __MINGW_NOTHROW strtoll (const char* __restrict__, char** __restrict, int);
387 unsigned long long  __cdecl __MINGW_NOTHROW strtoull (const char* __restrict__, char** __restrict__, int);
388
389 long long  __cdecl __MINGW_NOTHROW atoll (const char *);
390
391 #if !defined (__STRICT_ANSI__)
392 long long  __cdecl __MINGW_NOTHROW wtoll (const wchar_t *);
393 char* __cdecl __MINGW_NOTHROW lltoa (long long, char *, int);
394 char* __cdecl __MINGW_NOTHROW ulltoa (unsigned long long , char *, int);
395 wchar_t* __cdecl __MINGW_NOTHROW lltow (long long, wchar_t *, int);
396 wchar_t* __cdecl __MINGW_NOTHROW ulltow (unsigned long long, wchar_t *, int);
397
398   /* inline using non-ansi functions */
399 #ifndef __NO_INLINE__
400 __CRT_INLINE long long  __cdecl __MINGW_NOTHROW atoll (const char * _c)
401  { return _atoi64 (_c); }
402 __CRT_INLINE char*  __cdecl __MINGW_NOTHROW lltoa (long long _n, char * _c, int _i)
403  { return _i64toa (_n, _c, _i); }
404 __CRT_INLINE char*  __cdecl __MINGW_NOTHROW ulltoa (unsigned long long _n, char * _c, int _i)
405  { return _ui64toa (_n, _c, _i); }
406 __CRT_INLINE long long  __cdecl __MINGW_NOTHROW wtoll (const wchar_t * _w)
407   { return _wtoi64 (_w); }
408 __CRT_INLINE wchar_t*  __cdecl __MINGW_NOTHROW lltow (long long _n, wchar_t * _w, int _i)
409  { return _i64tow (_n, _w, _i); } 
410 __CRT_INLINE wchar_t*  __cdecl __MINGW_NOTHROW ulltow (unsigned long long _n, wchar_t * _w, int _i)
411  { return _ui64tow (_n, _w, _i); } 
412 #endif /* (__NO_INLINE__) */
413 #endif /* (__STRICT_ANSI__)  */
414
415 #endif /* !__NO_ISOCEXT */
416
417
418 #ifdef __cplusplus
419 }
420 #endif
421
422 #endif /* Not RC_INVOKED */
423 #endif /* Not _STDLIB_H */