stdlib.h 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312
  1. /*****************************************************************************/
  2. /* stdlib.h v8.2.2 */
  3. /* */
  4. /* Copyright (c) 1993-2017 Texas Instruments Incorporated */
  5. /* http://www.ti.com/ */
  6. /* */
  7. /* Redistribution and use in source and binary forms, with or without */
  8. /* modification, are permitted provided that the following conditions */
  9. /* are met: */
  10. /* */
  11. /* Redistributions of source code must retain the above copyright */
  12. /* notice, this list of conditions and the following disclaimer. */
  13. /* */
  14. /* Redistributions in binary form must reproduce the above copyright */
  15. /* notice, this list of conditions and the following disclaimer in */
  16. /* the documentation and/or other materials provided with the */
  17. /* distribution. */
  18. /* */
  19. /* Neither the name of Texas Instruments Incorporated nor the names */
  20. /* of its contributors may be used to endorse or promote products */
  21. /* derived from this software without specific prior written */
  22. /* permission. */
  23. /* */
  24. /* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS */
  25. /* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT */
  26. /* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR */
  27. /* A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT */
  28. /* OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, */
  29. /* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT */
  30. /* LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, */
  31. /* DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY */
  32. /* THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT */
  33. /* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE */
  34. /* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. */
  35. /* */
  36. /*****************************************************************************/
  37. #ifndef _STDLIB
  38. #define _STDLIB
  39. #pragma diag_push
  40. #pragma CHECK_MISRA("-6.3") /* standard types required for standard headers */
  41. #pragma CHECK_MISRA("-8.5") /* need to define inline function */
  42. #pragma CHECK_MISRA("-19.1") /* #includes required for implementation */
  43. #pragma CHECK_MISRA("-19.7") /* need function-like macros */
  44. #pragma CHECK_MISRA("-20.1") /* standard headers must define standard names */
  45. #pragma CHECK_MISRA("-20.2") /* standard headers must define standard names */
  46. /*---------------------------------------------------------------------------*/
  47. /* Attributes are only available in relaxed ANSI mode. */
  48. /*---------------------------------------------------------------------------*/
  49. #ifndef __ATTRIBUTE
  50. #if __TI_STRICT_ANSI_MODE__
  51. #define __ATTRIBUTE(attr)
  52. #else
  53. #define __ATTRIBUTE(attr) __attribute__(attr)
  54. #endif
  55. #endif
  56. #ifdef __cplusplus
  57. /*---------------------------------------------------------------------------*/
  58. /* <cstdlib> IS RECOMMENDED OVER <stdlib.h>. <stdlib.h> IS PROVIDED FOR */
  59. /* COMPATIBILITY WITH C AND THIS USAGE IS DEPRECATED IN C++ */
  60. /*---------------------------------------------------------------------------*/
  61. extern "C" namespace std {
  62. #endif /* !__cplusplus */
  63. #pragma diag_push
  64. #pragma CHECK_MISRA("-5.7") /* keep names intact */
  65. typedef struct { int quot, rem; } div_t;
  66. typedef struct { long quot, rem; } ldiv_t;
  67. #define _LLONG_AVAILABLE 1
  68. typedef struct { long long quot, rem; } lldiv_t;
  69. #pragma diag_pop
  70. #define MB_CUR_MAX 1
  71. #ifndef NULL
  72. #define NULL 0
  73. #endif
  74. #ifndef _SIZE_T
  75. #define _SIZE_T
  76. typedef __SIZE_T_TYPE__ size_t;
  77. #endif
  78. #ifndef __cplusplus
  79. #ifndef _WCHAR_T
  80. #define _WCHAR_T
  81. typedef __WCHAR_T_TYPE__ wchar_t;
  82. #endif
  83. #endif
  84. #define EXIT_FAILURE 1
  85. #define EXIT_SUCCESS 0
  86. #define RAND_MAX 32767
  87. #include <linkage.h>
  88. /*---------------------------------------------------------------*/
  89. /* NOTE - Normally, abs, labs, and fabs are expanded inline, so */
  90. /* no formal definition is really required. However, ANSI */
  91. /* requires that they exist as separate functions, so */
  92. /* they are supplied in the library. The prototype is */
  93. /* here mainly for documentation. */
  94. /*---------------------------------------------------------------*/
  95. #pragma diag_push
  96. #pragma CHECK_MISRA("-16.4") /* false positives due to builtin declarations */
  97. _CODE_ACCESS int abs(int _val);
  98. _CODE_ACCESS long labs(long _val);
  99. #if defined(_LLONG_AVAILABLE)
  100. _CODE_ACCESS long long llabs(long long _val);
  101. #endif
  102. #pragma diag_pop
  103. _CODE_ACCESS int atoi(const char *_st);
  104. _CODE_ACCESS long atol(const char *_st);
  105. #if defined(_LLONG_AVAILABLE)
  106. _CODE_ACCESS long long atoll(const char *_st);
  107. #endif
  108. _CODE_ACCESS int ltoa(long val, char *buffer);
  109. _IDECL double atof(const char *_st);
  110. _CODE_ACCESS long strtol(const char * __restrict _st,
  111. char ** __restrict _endptr, int _base);
  112. _CODE_ACCESS unsigned long strtoul(const char * __restrict _st,
  113. char ** __restrict _endptr, int _base);
  114. #if defined(_LLONG_AVAILABLE)
  115. _CODE_ACCESS long long strtoll(const char * __restrict _st,
  116. char ** __restrict _endptr, int _base);
  117. _CODE_ACCESS unsigned long long strtoull(const char * __restrict _st,
  118. char ** __restrict _endptr,
  119. int _base);
  120. #endif
  121. _CODE_ACCESS float strtof(const char * __restrict _st,
  122. char ** __restrict _endptr);
  123. _CODE_ACCESS double strtod(const char * __restrict _st,
  124. char ** __restrict _endptr);
  125. _CODE_ACCESS long double strtold(const char * __restrict _st,
  126. char ** __restrict _endptr);
  127. _CODE_ACCESS int rand(void);
  128. _CODE_ACCESS void srand(unsigned _seed);
  129. _CODE_ACCESS void *calloc(size_t _num, size_t _size)
  130. __ATTRIBUTE((malloc));
  131. _CODE_ACCESS void *malloc(size_t _size)
  132. __ATTRIBUTE((malloc));
  133. _CODE_ACCESS void *realloc(void *_ptr, size_t _size)
  134. __ATTRIBUTE((malloc));
  135. _CODE_ACCESS void free(void *_ptr);
  136. _CODE_ACCESS void *memalign(size_t _aln, size_t _size)
  137. __ATTRIBUTE((malloc));
  138. _CODE_ACCESS void abort(void);
  139. typedef void (*__TI_atexit_fn)(void);
  140. _CODE_ACCESS int atexit(__TI_atexit_fn _func);
  141. typedef int (*__TI_compar_fn)(const void *_a,const void *_b);
  142. _CODE_ACCESS void *bsearch(const void *_key, const void *_base,
  143. size_t _nmemb, size_t _size,
  144. __TI_compar_fn compar);
  145. _CODE_ACCESS void qsort(void *_base, size_t _nmemb, size_t _size,
  146. __TI_compar_fn compar);
  147. _CODE_ACCESS void exit(int _status);
  148. _CODE_ACCESS div_t div(int _numer, int _denom);
  149. _CODE_ACCESS ldiv_t ldiv(long _numer, long _denom);
  150. #if defined(_LLONG_AVAILABLE)
  151. _CODE_ACCESS lldiv_t lldiv(long long _numer, long long _denom);
  152. #endif
  153. _CODE_ACCESS char *getenv(const char *_string);
  154. _CODE_ACCESS int system(const char *_name);
  155. _CODE_ACCESS int mblen(const char *_s, size_t _n);
  156. _CODE_ACCESS size_t mbstowcs(wchar_t * __restrict _dest,
  157. const char * __restrict _src, size_t _n);
  158. _CODE_ACCESS int mbtowc(wchar_t * __restrict _dest,
  159. const char * __restrict _src, size_t _n);
  160. _CODE_ACCESS size_t wcstombs(char * __restrict _dest,
  161. const wchar_t * __restrict _src, size_t _n);
  162. _CODE_ACCESS int wctomb(char *_s, wchar_t _wc);
  163. #ifdef __cplusplus
  164. } /* extern "C" namespace std */
  165. #endif /* __cplusplus */
  166. #ifdef _INLINE
  167. #ifdef __cplusplus
  168. namespace std {
  169. #endif
  170. static __inline double atof(const char *_st)
  171. {
  172. return strtod(_st, (char **)0);
  173. }
  174. #ifdef __cplusplus
  175. } /* namespace std */
  176. #endif
  177. #endif /* _INLINE */
  178. #ifdef __cplusplus
  179. #if __TI_STRICT_ANSI_MODE__
  180. namespace std {
  181. _CODE_ACCESS inline int atexit(void (*func)(void))
  182. {
  183. return atexit((__TI_atexit_fn)func);
  184. }
  185. _CODE_ACCESS inline void *bsearch(const void *_key, const void *_base,
  186. size_t _nmemb, size_t _size,
  187. int (*compar)(const void *,const void *))
  188. {
  189. return bsearch(_key, _base, _nmemb, _size, (__TI_compar_fn)compar);
  190. }
  191. _CODE_ACCESS inline void qsort(void *_base, size_t _nmemb, size_t _size,
  192. int (*_compar)(const void *, const void *))
  193. {
  194. return qsort(_base, _nmemb, _size, (__TI_compar_fn)_compar);
  195. }
  196. }
  197. #endif
  198. #endif
  199. #pragma diag_pop
  200. #endif /* ! _STDLIB */
  201. #pragma diag_push
  202. /* using declarations must occur outside header guard to support including both
  203. C and C++-wrapped version of header; see _CPP_STYLE_HEADER check */
  204. /* this code is for C++ mode only and thus also not relevant for MISRA */
  205. #pragma CHECK_MISRA("-19.15")
  206. #if defined(__cplusplus) && !defined(_CPP_STYLE_HEADER)
  207. using std::div_t;
  208. using std::ldiv_t;
  209. #if defined(_LLONG_AVAILABLE)
  210. using std::lldiv_t;
  211. #endif
  212. using std::size_t;
  213. using std::abs;
  214. using std::labs;
  215. using std::atoi;
  216. using std::atol;
  217. #if defined(_LLONG_AVAILABLE)
  218. using std::llabs;
  219. using std::atoll;
  220. #endif
  221. using std::atof;
  222. using std::strtol;
  223. using std::strtoul;
  224. #if defined(_LLONG_AVAILABLE)
  225. using std::strtoll;
  226. using std::strtoull;
  227. #endif
  228. using std::strtof;
  229. using std::strtod;
  230. using std::strtold;
  231. using std::rand;
  232. using std::srand;
  233. using std::calloc;
  234. using std::malloc;
  235. using std::realloc;
  236. using std::free;
  237. using std::memalign;
  238. using std::abort;
  239. using std::atexit;
  240. using std::bsearch;
  241. using std::qsort;
  242. using std::exit;
  243. using std::div;
  244. using std::ldiv;
  245. #if defined(_LLONG_AVAILABLE)
  246. using std::lldiv;
  247. #endif
  248. using std::getenv;
  249. using std::system;
  250. using std::mblen;
  251. using std::mbtowc;
  252. using std::wctomb;
  253. using std::mbstowcs;
  254. using std::wcstombs;
  255. #endif /* ! _CPP_STYLE_HEADER */
  256. #pragma diag_pop
  257. #pragma diag_push
  258. /* C2000-specific additions to header implemented with #include */
  259. #pragma CHECK_MISRA("-19.1")
  260. #pragma CHECK_MISRA("-19.15")
  261. #pragma diag_pop