stdlib.h 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482
  1. /*
  2. * stdlib.h
  3. *
  4. * Definitions for common types, variables, and functions.
  5. *
  6. * This file is part of the Mingw32 package.
  7. *
  8. * Contributors:
  9. * Created by Colin Peters <[email protected]>
  10. *
  11. * THIS SOFTWARE IS NOT COPYRIGHTED
  12. *
  13. * This source code is offered for use in the public domain. You may
  14. * use, modify or distribute it freely.
  15. *
  16. * This code is distributed in the hope that it will be useful but
  17. * WITHOUT ANY WARRANTY. ALL WARRANTIES, EXPRESS OR IMPLIED ARE HEREBY
  18. * DISCLAIMED. This includes but is not limited to warranties of
  19. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
  20. *
  21. * $Revision: 1.2 $
  22. * $Author: bellard $
  23. * $Date: 2005/04/17 13:14:29 $
  24. *
  25. */
  26. #ifndef _STDLIB_H_
  27. #define _STDLIB_H_
  28. /* All the headers include this file. */
  29. #include <_mingw.h>
  30. #define __need_size_t
  31. #define __need_wchar_t
  32. #define __need_NULL
  33. #ifndef RC_INVOKED
  34. #include <stddef.h>
  35. #endif /* RC_INVOKED */
  36. /*
  37. * RAND_MAX is the maximum value that may be returned by rand.
  38. * The minimum is zero.
  39. */
  40. #define RAND_MAX 0x7FFF
  41. /*
  42. * These values may be used as exit status codes.
  43. */
  44. #define EXIT_SUCCESS 0
  45. #define EXIT_FAILURE 1
  46. /*
  47. * Definitions for path name functions.
  48. * NOTE: All of these values have simply been chosen to be conservatively high.
  49. * Remember that with long file names we can no longer depend on
  50. * extensions being short.
  51. */
  52. #ifndef __STRICT_ANSI__
  53. #ifndef MAX_PATH
  54. #define MAX_PATH (260)
  55. #endif
  56. #define _MAX_PATH MAX_PATH
  57. #define _MAX_DRIVE (3)
  58. #define _MAX_DIR 256
  59. #define _MAX_FNAME 256
  60. #define _MAX_EXT 256
  61. #endif /* Not __STRICT_ANSI__ */
  62. #ifndef RC_INVOKED
  63. #ifdef __cplusplus
  64. extern "C" {
  65. #endif
  66. /*
  67. * This seems like a convenient place to declare these variables, which
  68. * give programs using WinMain (or main for that matter) access to main-ish
  69. * argc and argv. environ is a pointer to a table of environment variables.
  70. * NOTE: Strings in _argv and environ are ANSI strings.
  71. */
  72. extern int _argc;
  73. extern char** _argv;
  74. /* imports from runtime dll of the above variables */
  75. #ifdef __MSVCRT__
  76. extern int* __p___argc(void);
  77. extern char*** __p___argv(void);
  78. extern wchar_t*** __p___wargv(void);
  79. #define __argc (*__p___argc())
  80. #define __argv (*__p___argv())
  81. #define __wargv (*__p___wargv())
  82. #else /* !MSVCRT */
  83. #ifndef __DECLSPEC_SUPPORTED
  84. extern int* __imp___argc_dll;
  85. extern char*** __imp___argv_dll;
  86. #define __argc (*__imp___argc_dll)
  87. #define __argv (*__imp___argv_dll)
  88. #else /* __DECLSPEC_SUPPORTED */
  89. __MINGW_IMPORT int __argc_dll;
  90. __MINGW_IMPORT char** __argv_dll;
  91. #define __argc __argc_dll
  92. #define __argv __argv_dll
  93. #endif /* __DECLSPEC_SUPPORTED */
  94. #endif /* __MSVCRT */
  95. /*
  96. * Also defined in ctype.h.
  97. */
  98. #ifndef MB_CUR_MAX
  99. # ifdef __MSVCRT__
  100. # define MB_CUR_MAX __mb_cur_max
  101. __MINGW_IMPORT int __mb_cur_max;
  102. # else /* not __MSVCRT */
  103. # define MB_CUR_MAX __mb_cur_max_dll
  104. __MINGW_IMPORT int __mb_cur_max_dll;
  105. # endif /* not __MSVCRT */
  106. #endif /* MB_CUR_MAX */
  107. /*
  108. * MS likes to declare errno in stdlib.h as well.
  109. */
  110. #ifdef _UWIN
  111. #undef errno
  112. extern int errno;
  113. #else
  114. int* _errno(void);
  115. #define errno (*_errno())
  116. #endif
  117. int* __doserrno(void);
  118. #define _doserrno (*__doserrno())
  119. /*
  120. * Use environ from the DLL, not as a global.
  121. */
  122. #ifdef __MSVCRT__
  123. extern char *** __p__environ(void);
  124. extern wchar_t *** __p__wenviron(void);
  125. # define _environ (*__p__environ())
  126. # define _wenviron (*__p__wenviron())
  127. #else /* ! __MSVCRT__ */
  128. # ifndef __DECLSPEC_SUPPORTED
  129. extern char *** __imp__environ_dll;
  130. # define _environ (*__imp__environ_dll)
  131. # else /* __DECLSPEC_SUPPORTED */
  132. __MINGW_IMPORT char ** _environ_dll;
  133. # define _environ _environ_dll
  134. # endif /* __DECLSPEC_SUPPORTED */
  135. #endif /* ! __MSVCRT__ */
  136. #define environ _environ
  137. #ifdef __MSVCRT__
  138. /* One of the MSVCRTxx libraries */
  139. #ifndef __DECLSPEC_SUPPORTED
  140. extern int* __imp__sys_nerr;
  141. # define sys_nerr (*__imp__sys_nerr)
  142. #else /* __DECLSPEC_SUPPORTED */
  143. __MINGW_IMPORT int _sys_nerr;
  144. # ifndef _UWIN
  145. # define sys_nerr _sys_nerr
  146. # endif /* _UWIN */
  147. #endif /* __DECLSPEC_SUPPORTED */
  148. #else /* ! __MSVCRT__ */
  149. /* CRTDLL run time library */
  150. #ifndef __DECLSPEC_SUPPORTED
  151. extern int* __imp__sys_nerr_dll;
  152. # define sys_nerr (*__imp__sys_nerr_dll)
  153. #else /* __DECLSPEC_SUPPORTED */
  154. __MINGW_IMPORT int _sys_nerr_dll;
  155. # define sys_nerr _sys_nerr_dll
  156. #endif /* __DECLSPEC_SUPPORTED */
  157. #endif /* ! __MSVCRT__ */
  158. #ifndef __DECLSPEC_SUPPORTED
  159. extern char*** __imp__sys_errlist;
  160. #define sys_errlist (*__imp__sys_errlist)
  161. #else /* __DECLSPEC_SUPPORTED */
  162. __MINGW_IMPORT char* _sys_errlist[];
  163. #ifndef _UWIN
  164. #define sys_errlist _sys_errlist
  165. #endif /* _UWIN */
  166. #endif /* __DECLSPEC_SUPPORTED */
  167. /*
  168. * OS version and such constants.
  169. */
  170. #ifndef __STRICT_ANSI__
  171. #ifdef __MSVCRT__
  172. /* msvcrtxx.dll */
  173. extern unsigned int* __p__osver(void);
  174. extern unsigned int* __p__winver(void);
  175. extern unsigned int* __p__winmajor(void);
  176. extern unsigned int* __p__winminor(void);
  177. #define _osver (*__p__osver())
  178. #define _winver (*__p__winver())
  179. #define _winmajor (*__p__winmajor())
  180. #define _winminor (*__p__winminor())
  181. #else
  182. /* Not msvcrtxx.dll, thus crtdll.dll */
  183. #ifndef __DECLSPEC_SUPPORTED
  184. extern unsigned int* _imp___osver_dll;
  185. extern unsigned int* _imp___winver_dll;
  186. extern unsigned int* _imp___winmajor_dll;
  187. extern unsigned int* _imp___winminor_dll;
  188. #define _osver (*_imp___osver_dll)
  189. #define _winver (*_imp___winver_dll)
  190. #define _winmajor (*_imp___winmajor_dll)
  191. #define _winminor (*_imp___winminor_dll)
  192. #else /* __DECLSPEC_SUPPORTED */
  193. __MINGW_IMPORT unsigned int _osver_dll;
  194. __MINGW_IMPORT unsigned int _winver_dll;
  195. __MINGW_IMPORT unsigned int _winmajor_dll;
  196. __MINGW_IMPORT unsigned int _winminor_dll;
  197. #define _osver _osver_dll
  198. #define _winver _winver_dll
  199. #define _winmajor _winmajor_dll
  200. #define _winminor _winminor_dll
  201. #endif /* __DECLSPEC_SUPPORTED */
  202. #endif
  203. #if defined __MSVCRT__
  204. /* although the _pgmptr is exported as DATA,
  205. * be safe and use the access function __p__pgmptr() to get it. */
  206. char** __p__pgmptr(void);
  207. #define _pgmptr (*__p__pgmptr())
  208. wchar_t** __p__wpgmptr(void);
  209. #define _wpgmptr (*__p__wpgmptr())
  210. #else /* ! __MSVCRT__ */
  211. # ifndef __DECLSPEC_SUPPORTED
  212. extern char** __imp__pgmptr_dll;
  213. # define _pgmptr (*__imp__pgmptr_dll)
  214. # else /* __DECLSPEC_SUPPORTED */
  215. __MINGW_IMPORT char* _pgmptr_dll;
  216. # define _pgmptr _pgmptr_dll
  217. # endif /* __DECLSPEC_SUPPORTED */
  218. /* no wide version in CRTDLL */
  219. #endif /* __MSVCRT__ */
  220. #endif /* Not __STRICT_ANSI__ */
  221. #ifdef __GNUC__
  222. #define _ATTRIB_NORETURN __attribute__ ((noreturn))
  223. #else /* Not __GNUC__ */
  224. #define _ATTRIB_NORETURN
  225. #endif /* __GNUC__ */
  226. double atof (const char*);
  227. int atoi (const char*);
  228. long atol (const char*);
  229. int _wtoi (const wchar_t *);
  230. long _wtol (const wchar_t *);
  231. double strtod (const char*, char**);
  232. #if !defined __NO_ISOCEXT /* extern stubs in static libmingwex.a */
  233. extern __inline__ float strtof (const char *nptr, char **endptr)
  234. { return (strtod (nptr, endptr));}
  235. #endif /* __NO_ISOCEXT */
  236. long strtol (const char*, char**, int);
  237. unsigned long strtoul (const char*, char**, int);
  238. #ifndef _WSTDLIB_DEFINED
  239. /* also declared in wchar.h */
  240. double wcstod (const wchar_t*, wchar_t**);
  241. #if !defined __NO_ISOCEXT /* extern stub in static libmingwex.a */
  242. extern __inline__ float wcstof( const wchar_t *nptr, wchar_t **endptr)
  243. { return (wcstod(nptr, endptr)); }
  244. #endif /* __NO_ISOCEXT */
  245. long wcstol (const wchar_t*, wchar_t**, int);
  246. unsigned long wcstoul (const wchar_t*, wchar_t**, int);
  247. #define _WSTDLIB_DEFINED
  248. #endif
  249. size_t wcstombs (char*, const wchar_t*, size_t);
  250. int wctomb (char*, wchar_t);
  251. int mblen (const char*, size_t);
  252. size_t mbstowcs (wchar_t*, const char*, size_t);
  253. int mbtowc (wchar_t*, const char*, size_t);
  254. int rand (void);
  255. void srand (unsigned int);
  256. void* calloc (size_t, size_t);
  257. void* malloc (size_t);
  258. void* realloc (void*, size_t);
  259. void free (void*);
  260. void abort (void) _ATTRIB_NORETURN;
  261. void exit (int) _ATTRIB_NORETURN;
  262. int atexit (void (*)(void));
  263. int system (const char*);
  264. char* getenv (const char*);
  265. void* bsearch (const void*, const void*, size_t, size_t,
  266. int (*)(const void*, const void*));
  267. void qsort (const void*, size_t, size_t,
  268. int (*)(const void*, const void*));
  269. int abs (int);
  270. long labs (long);
  271. /*
  272. * div_t and ldiv_t are structures used to return the results of div and
  273. * ldiv.
  274. *
  275. * NOTE: div and ldiv appear not to work correctly unless
  276. * -fno-pcc-struct-return is specified. This is included in the
  277. * mingw32 specs file.
  278. */
  279. typedef struct { int quot, rem; } div_t;
  280. typedef struct { long quot, rem; } ldiv_t;
  281. div_t div (int, int);
  282. ldiv_t ldiv (long, long);
  283. #ifndef __STRICT_ANSI__
  284. /*
  285. * NOTE: Officially the three following functions are obsolete. The Win32 API
  286. * functions SetErrorMode, Beep and Sleep are their replacements.
  287. */
  288. void _beep (unsigned int, unsigned int);
  289. void _seterrormode (int);
  290. void _sleep (unsigned long);
  291. void _exit (int) _ATTRIB_NORETURN;
  292. #if !defined __NO_ISOCEXT /* extern stub in static libmingwex.a */
  293. /* C99 function name */
  294. void _Exit(int) _ATTRIB_NORETURN; /* Declare to get noreturn attribute. */
  295. extern __inline__ void _Exit(int status)
  296. { _exit(status); }
  297. #endif
  298. /* _onexit is MS extension. Use atexit for portability. */
  299. typedef int (* _onexit_t)(void);
  300. _onexit_t _onexit( _onexit_t );
  301. int _putenv (const char*);
  302. void _searchenv (const char*, const char*, char*);
  303. char* _ecvt (double, int, int*, int*);
  304. char* _fcvt (double, int, int*, int*);
  305. char* _gcvt (double, int, char*);
  306. void _makepath (char*, const char*, const char*, const char*, const char*);
  307. void _splitpath (const char*, char*, char*, char*, char*);
  308. char* _fullpath (char*, const char*, size_t);
  309. char* _itoa (int, char*, int);
  310. char* _ltoa (long, char*, int);
  311. char* _ultoa(unsigned long, char*, int);
  312. wchar_t* _itow (int, wchar_t*, int);
  313. wchar_t* _ltow (long, wchar_t*, int);
  314. wchar_t* _ultow (unsigned long, wchar_t*, int);
  315. #ifdef __MSVCRT__
  316. __int64 _atoi64(const char *);
  317. char* _i64toa(__int64, char *, int);
  318. char* _ui64toa(unsigned __int64, char *, int);
  319. __int64 _wtoi64(const wchar_t *);
  320. wchar_t* _i64tow(__int64, wchar_t *, int);
  321. wchar_t* _ui64tow(unsigned __int64, wchar_t *, int);
  322. wchar_t* _wgetenv(const wchar_t*);
  323. int _wputenv(const wchar_t*);
  324. void _wsearchenv(const wchar_t*, const wchar_t*, wchar_t*);
  325. void _wmakepath(wchar_t*, const wchar_t*, const wchar_t*, const wchar_t*, const wchar_t*);
  326. void _wsplitpath (const wchar_t*, wchar_t*, wchar_t*, wchar_t*, wchar_t*);
  327. wchar_t* _wfullpath (wchar_t*, const wchar_t*, size_t);
  328. #endif
  329. #ifndef _NO_OLDNAMES
  330. int putenv (const char*);
  331. void searchenv (const char*, const char*, char*);
  332. char* itoa (int, char*, int);
  333. char* ltoa (long, char*, int);
  334. #ifndef _UWIN
  335. char* ecvt (double, int, int*, int*);
  336. char* fcvt (double, int, int*, int*);
  337. char* gcvt (double, int, char*);
  338. #endif /* _UWIN */
  339. #endif /* Not _NO_OLDNAMES */
  340. #endif /* Not __STRICT_ANSI__ */
  341. /* C99 names */
  342. #if !defined __NO_ISOCEXT /* externs in static libmingwex.a */
  343. typedef struct { long long quot, rem; } lldiv_t;
  344. lldiv_t lldiv (long long, long long);
  345. extern __inline__ long long llabs(long long _j)
  346. {return (_j >= 0 ? _j : -_j);}
  347. long long strtoll (const char* __restrict__, char** __restrict, int);
  348. unsigned long long strtoull (const char* __restrict__, char** __restrict__, int);
  349. #if defined (__MSVCRT__) /* these are stubs for MS _i64 versions */
  350. long long atoll (const char *);
  351. #if !defined (__STRICT_ANSI__)
  352. long long wtoll(const wchar_t *);
  353. char* lltoa(long long, char *, int);
  354. char* ulltoa(unsigned long long , char *, int);
  355. wchar_t* lltow(long long, wchar_t *, int);
  356. wchar_t* ulltow(unsigned long long, wchar_t *, int);
  357. /* inline using non-ansi functions */
  358. extern __inline__ long long atoll (const char * _c)
  359. { return _atoi64 (_c); }
  360. extern __inline__ char* lltoa(long long _n, char * _c, int _i)
  361. { return _i64toa (_n, _c, _i); }
  362. extern __inline__ char* ulltoa(unsigned long long _n, char * _c, int _i)
  363. { return _ui64toa (_n, _c, _i); }
  364. extern __inline__ long long wtoll(const wchar_t * _w)
  365. { return _wtoi64 (_w); }
  366. extern __inline__ wchar_t* lltow(long long _n, wchar_t * _w, int _i)
  367. { return _i64tow (_n, _w, _i); }
  368. extern __inline__ wchar_t* ulltow(unsigned long long _n, wchar_t * _w, int _i)
  369. { return _ui64tow (_n, _w, _i); }
  370. #endif /* (__STRICT_ANSI__) */
  371. #endif /* __MSVCRT__ */
  372. #endif /* !__NO_ISOCEXT */
  373. /*
  374. * Undefine the no return attribute used in some function definitions
  375. */
  376. #undef _ATTRIB_NORETURN
  377. #ifdef __cplusplus
  378. }
  379. #endif
  380. #endif /* Not RC_INVOKED */
  381. #endif /* Not _STDLIB_H_ */