stdlib.h 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260
  1. /****************************************************************************
  2. * include/stdlib.h
  3. *
  4. * Licensed to the Apache Software Foundation (ASF) under one or more
  5. * contributor license agreements. See the NOTICE file distributed with
  6. * this work for additional information regarding copyright ownership. The
  7. * ASF licenses this file to you under the Apache License, Version 2.0 (the
  8. * "License"); you may not use this file except in compliance with the
  9. * License. You may obtain a copy of the License at
  10. *
  11. * http://www.apache.org/licenses/LICENSE-2.0
  12. *
  13. * Unless required by applicable law or agreed to in writing, software
  14. * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
  15. * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
  16. * License for the specific language governing permissions and limitations
  17. * under the License.
  18. *
  19. ****************************************************************************/
  20. #ifndef __INCLUDE_STDLIB_H
  21. #define __INCLUDE_STDLIB_H
  22. /****************************************************************************
  23. * Included Files
  24. ****************************************************************************/
  25. #include <nuttx/config.h>
  26. #include <nuttx/compiler.h>
  27. #include <sys/types.h>
  28. #include <stdint.h>
  29. #include <limits.h>
  30. /****************************************************************************
  31. * Pre-processor Definitions
  32. ****************************************************************************/
  33. /* The C standard specifies two constants, EXIT_SUCCESS and EXIT_FAILURE,
  34. * that may be passed to exit() to indicate successful or unsuccessful
  35. * termination, respectively.
  36. */
  37. #define EXIT_SUCCESS 0
  38. #define EXIT_FAILURE 1
  39. /* The NULL pointer should be defined in this file but is currently defined
  40. * in sys/types.h.
  41. */
  42. /* Maximum value returned by rand(). Must be a minimum of 32767. */
  43. #define RAND_MAX INT_MAX
  44. /* Integer expression whose value is the maximum number of bytes in a
  45. * character specified by the current locale.
  46. */
  47. #define MB_CUR_MAX 1
  48. /* The environ variable, normally 'char **environ;' is not implemented as a
  49. * function call. However, get_environ_ptr() can be used in its place.
  50. */
  51. #ifndef CONFIG_DISABLE_ENVIRON
  52. # define environ get_environ_ptr()
  53. #endif
  54. /****************************************************************************
  55. * Public Type Definitions
  56. ****************************************************************************/
  57. /* Structure type returned by the div() function. */
  58. struct div_s
  59. {
  60. int quot; /* Quotient */
  61. int rem; /* Remainder */
  62. };
  63. typedef struct div_s div_t;
  64. /* Structure type returned by the ldiv() function. */
  65. struct ldiv_s
  66. {
  67. long quot; /* Quotient */
  68. long rem; /* Remainder */
  69. };
  70. typedef struct ldiv_s ldiv_t;
  71. /* Structure type returned by the lldiv() function. */
  72. struct lldiv_s
  73. {
  74. long quot; /* Quotient */
  75. long rem; /* Remainder */
  76. };
  77. typedef struct lldiv_s lldiv_t;
  78. /****************************************************************************
  79. * Public Function Prototypes
  80. ****************************************************************************/
  81. #undef EXTERN
  82. #if defined(__cplusplus)
  83. #define EXTERN extern "C"
  84. extern "C"
  85. {
  86. #else
  87. #define EXTERN extern
  88. #endif
  89. /* Random number generation */
  90. void srand(unsigned int seed);
  91. int rand(void);
  92. #define srandom(s) srand(s)
  93. long random(void);
  94. #ifdef CONFIG_CRYPTO_RANDOM_POOL
  95. void arc4random_buf(FAR void *bytes, size_t nbytes);
  96. #endif
  97. /* Environment variable support */
  98. FAR char **get_environ_ptr(void);
  99. FAR char *getenv(FAR const char *name);
  100. int putenv(FAR const char *string);
  101. int clearenv(void);
  102. int setenv(FAR const char *name, FAR const char *value, int overwrite);
  103. int unsetenv(FAR const char *name);
  104. /* Process exit functions */
  105. void exit(int status) noreturn_function;
  106. void abort(void) noreturn_function;
  107. int atexit(CODE void (*func)(void));
  108. int on_exit(CODE void (*func)(int, FAR void *), FAR void *arg);
  109. /* _Exit() is a stdlib.h equivalent to the unistd.h _exit() function */
  110. void _Exit(int status) noreturn_function;
  111. /* System() command is not implemented in the NuttX libc because it is so
  112. * entangled with shell logic. There is an experimental version at
  113. * apps/system/system. system() is prototyped here, however, for
  114. * standards compatibility.
  115. */
  116. #ifndef __KERNEL__
  117. int system(FAR const char *cmd);
  118. #endif
  119. FAR char *realpath(FAR const char *path, FAR char *resolved);
  120. /* String to binary conversions */
  121. long strtol(FAR const char *nptr, FAR char **endptr, int base);
  122. unsigned long strtoul(FAR const char *nptr, FAR char **endptr, int base);
  123. #ifdef CONFIG_HAVE_LONG_LONG
  124. long long strtoll(FAR const char *nptr, FAR char **endptr, int base);
  125. unsigned long long strtoull(FAR const char *nptr, FAR char **endptr,
  126. int base);
  127. #endif
  128. float strtof(FAR const char *str, FAR char **endptr);
  129. #ifdef CONFIG_HAVE_DOUBLE
  130. double strtod(FAR const char *str, FAR char **endptr);
  131. #endif
  132. #ifdef CONFIG_HAVE_LONG_DOUBLE
  133. long double strtold(FAR const char *str, FAR char **endptr);
  134. #endif
  135. int atoi(FAR const char *nptr);
  136. long atol(FAR const char *nptr);
  137. #ifdef CONFIG_HAVE_LONG_LONG
  138. long long atoll(FAR const char *nptr);
  139. #endif
  140. #ifdef CONFIG_HAVE_DOUBLE
  141. double atof(FAR const char *nptr);
  142. #endif
  143. /* Binary to string conversions */
  144. FAR char *itoa(int val, FAR char *str, int base);
  145. /* Wide character operations */
  146. #ifdef CONFIG_LIBC_WCHAR
  147. int mblen(FAR const char *s, size_t n);
  148. int mbtowc(FAR wchar_t *pwc, FAR const char *s, size_t n);
  149. size_t mbstowcs(FAR wchar_t *dst, FAR const char *src, size_t len);
  150. int wctomb(FAR char *s, wchar_t wchar);
  151. size_t wcstombs(FAR char *dst, FAR const wchar_t *src, size_t len);
  152. #endif
  153. /* Memory Management */
  154. FAR void *malloc(size_t);
  155. FAR void *valloc(size_t);
  156. void free(FAR void *);
  157. FAR void *realloc(FAR void *, size_t);
  158. FAR void *memalign(size_t, size_t);
  159. FAR void *zalloc(size_t);
  160. FAR void *calloc(size_t, size_t);
  161. FAR void *aligned_alloc(size_t, size_t);
  162. int posix_memalign(FAR void **, size_t, size_t);
  163. /* Pseudo-Terminals */
  164. #ifdef CONFIG_PSEUDOTERM
  165. FAR char *ptsname(int fd);
  166. int ptsname_r(int fd, FAR char *buf, size_t buflen);
  167. int unlockpt(int fd);
  168. /* int grantpt(int fd); Not implemented */
  169. #define grantpt(fd) (0)
  170. #endif
  171. /* Arithmetic */
  172. int abs(int j);
  173. long int labs(long int j);
  174. #ifdef CONFIG_HAVE_LONG_LONG
  175. long long int llabs(long long int j);
  176. #endif
  177. div_t div(int number, int denom);
  178. ldiv_t ldiv(long number, long denom);
  179. #ifdef CONFIG_HAVE_LONG_LONG
  180. lldiv_t lldiv(long long number, long long denom);
  181. #endif
  182. /* Temporary files */
  183. FAR char *mktemp(FAR char *path_template);
  184. int mkstemp(FAR char *path_template);
  185. FAR char *mkdtemp(FAR char *path_template);
  186. /* Sorting */
  187. void qsort(FAR void *base, size_t nel, size_t width,
  188. CODE int (*compar)(FAR const void *, FAR const void *));
  189. /* Binary search */
  190. FAR void *bsearch(FAR const void *key, FAR const void *base, size_t nel,
  191. size_t width, CODE int (*compar)(FAR const void *,
  192. FAR const void *));
  193. #undef EXTERN
  194. #if defined(__cplusplus)
  195. }
  196. #endif
  197. #endif /* __INCLUDE_STDLIB_H */