stdlib.h 32 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915
  1. /* Copyright (C) 1991-1999, 2000, 2001 Free Software Foundation, Inc.
  2. This file is part of the GNU C Library.
  3. The GNU C Library is free software; you can redistribute it and/or
  4. modify it under the terms of the GNU Lesser General Public
  5. License as published by the Free Software Foundation; either
  6. version 2.1 of the License, or (at your option) any later version.
  7. The GNU C Library is distributed in the hope that it will be useful,
  8. but WITHOUT ANY WARRANTY; without even the implied warranty of
  9. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  10. Lesser General Public License for more details.
  11. You should have received a copy of the GNU Lesser General Public
  12. License along with the GNU C Library; if not, write to the Free
  13. Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
  14. 02111-1307 USA. */
  15. /*
  16. * ISO C99 Standard: 7.20 General utilities <stdlib.h>
  17. */
  18. #ifndef _STDLIB_H
  19. #include <features.h>
  20. /* Get size_t, wchar_t and NULL from <stddef.h>. */
  21. #define __need_size_t
  22. #ifndef __need_malloc_and_calloc
  23. #ifdef __UCLIBC_HAS_WCHAR__
  24. # define __need_wchar_t
  25. #endif
  26. # define __need_NULL
  27. #endif
  28. #include <stddef.h>
  29. __BEGIN_DECLS
  30. #ifndef __need_malloc_and_calloc
  31. #define _STDLIB_H 1
  32. #if defined __USE_XOPEN && !defined _SYS_WAIT_H
  33. /* XPG requires a few symbols from <sys/wait.h> being defined. */
  34. # include <bits/waitflags.h>
  35. # include <bits/waitstatus.h>
  36. # ifdef __USE_BSD
  37. /* Lots of hair to allow traditional BSD use of `union wait'
  38. as well as POSIX.1 use of `int' for the status word. */
  39. # if defined __GNUC__ && !defined __cplusplus
  40. # define __WAIT_INT(status) \
  41. (__extension__ ({ union { __typeof(status) __in; int __i; } __u; \
  42. __u.__in = (status); __u.__i; }))
  43. # else
  44. # define __WAIT_INT(status) (*(int *) &(status))
  45. # endif
  46. /* This is the type of the argument to `wait'. The funky union
  47. causes redeclarations with ether `int *' or `union wait *' to be
  48. allowed without complaint. __WAIT_STATUS_DEFN is the type used in
  49. the actual function definitions. */
  50. # if !defined __GNUC__ || __GNUC__ < 2 || defined __cplusplus
  51. # define __WAIT_STATUS void *
  52. # define __WAIT_STATUS_DEFN void *
  53. # else
  54. /* This works in GCC 2.6.1 and later. */
  55. typedef union
  56. {
  57. union wait *__uptr;
  58. int *__iptr;
  59. } __WAIT_STATUS __attribute__ ((__transparent_union__));
  60. # define __WAIT_STATUS_DEFN int *
  61. # endif
  62. # else /* Don't use BSD. */
  63. # define __WAIT_INT(status) (status)
  64. # define __WAIT_STATUS int *
  65. # define __WAIT_STATUS_DEFN int *
  66. # endif /* Use BSD. */
  67. /* Define the macros <sys/wait.h> also would define this way. */
  68. # define WEXITSTATUS(status) __WEXITSTATUS(__WAIT_INT(status))
  69. # define WTERMSIG(status) __WTERMSIG(__WAIT_INT(status))
  70. # define WSTOPSIG(status) __WSTOPSIG(__WAIT_INT(status))
  71. # define WIFEXITED(status) __WIFEXITED(__WAIT_INT(status))
  72. # define WIFSIGNALED(status) __WIFSIGNALED(__WAIT_INT(status))
  73. # define WIFSTOPPED(status) __WIFSTOPPED(__WAIT_INT(status))
  74. #endif /* X/Open and <sys/wait.h> not included. */
  75. /* Returned by `div'. */
  76. typedef struct
  77. {
  78. int quot; /* Quotient. */
  79. int rem; /* Remainder. */
  80. } div_t;
  81. /* Returned by `ldiv'. */
  82. #ifndef __ldiv_t_defined
  83. typedef struct
  84. {
  85. long int quot; /* Quotient. */
  86. long int rem; /* Remainder. */
  87. } ldiv_t;
  88. # define __ldiv_t_defined 1
  89. #endif
  90. #if defined __USE_ISOC99 && !defined __lldiv_t_defined
  91. /* Returned by `lldiv'. */
  92. __extension__ typedef struct
  93. {
  94. long long int quot; /* Quotient. */
  95. long long int rem; /* Remainder. */
  96. } lldiv_t;
  97. # define __lldiv_t_defined 1
  98. #endif
  99. /* The largest number rand will return (same as INT_MAX). */
  100. #define RAND_MAX 2147483647
  101. /* We define these the same for all machines.
  102. Changes from this to the outside world should be done in `_exit'. */
  103. #define EXIT_FAILURE 1 /* Failing exit status. */
  104. #define EXIT_SUCCESS 0 /* Successful exit status. */
  105. #ifdef __UCLIBC_HAS_WCHAR__
  106. /* Maximum length of a multibyte character in the current locale. */
  107. #define MB_CUR_MAX (_stdlib_mb_cur_max ())
  108. extern size_t _stdlib_mb_cur_max (void) __THROW;
  109. #endif
  110. #ifdef __UCLIBC_HAS_FLOATS__
  111. /* Convert a string to a floating-point number. */
  112. extern double atof (__const char *__nptr) __THROW __attribute_pure__;
  113. #endif /* __UCLIBC_HAS_FLOATS__ */
  114. /* Convert a string to an integer. */
  115. extern int atoi (__const char *__nptr) __THROW __attribute_pure__;
  116. /* Convert a string to a long integer. */
  117. extern long int atol (__const char *__nptr) __THROW __attribute_pure__;
  118. #if defined __USE_ISOC99 || (defined __GNUC__ && defined __USE_MISC)
  119. /* Convert a string to a long long integer. */
  120. __extension__ extern long long int atoll (__const char *__nptr)
  121. __THROW __attribute_pure__;
  122. #endif
  123. #ifdef __UCLIBC_HAS_FLOATS__
  124. /* Convert a string to a floating-point number. */
  125. extern double strtod (__const char *__restrict __nptr,
  126. char **__restrict __endptr) __THROW;
  127. #ifdef __USE_ISOC99
  128. /* Likewise for `float' and `long double' sizes of floating-point numbers. */
  129. extern float strtof (__const char *__restrict __nptr,
  130. char **__restrict __endptr) __THROW;
  131. extern long double strtold (__const char *__restrict __nptr,
  132. char **__restrict __endptr) __THROW;
  133. #endif
  134. #endif /* __UCLIBC_HAS_FLOATS__ */
  135. /* Convert a string to a long integer. */
  136. extern long int strtol (__const char *__restrict __nptr,
  137. char **__restrict __endptr, int __base) __THROW;
  138. /* Convert a string to an unsigned long integer. */
  139. extern unsigned long int strtoul (__const char *__restrict __nptr,
  140. char **__restrict __endptr, int __base)
  141. __THROW;
  142. #if defined __GNUC__ && defined __USE_BSD
  143. /* Convert a string to a quadword integer. */
  144. __extension__
  145. extern long long int strtoq (__const char *__restrict __nptr,
  146. char **__restrict __endptr, int __base) __THROW;
  147. /* Convert a string to an unsigned quadword integer. */
  148. __extension__
  149. extern unsigned long long int strtouq (__const char *__restrict __nptr,
  150. char **__restrict __endptr, int __base)
  151. __THROW;
  152. #endif /* GCC and use BSD. */
  153. #if defined __USE_ISOC99 || (defined __GNUC__ && defined __USE_MISC)
  154. /* These functions will part of the standard C library in ISO C99. */
  155. /* Convert a string to a quadword integer. */
  156. __extension__
  157. extern long long int strtoll (__const char *__restrict __nptr,
  158. char **__restrict __endptr, int __base) __THROW;
  159. /* Convert a string to an unsigned quadword integer. */
  160. __extension__
  161. extern unsigned long long int strtoull (__const char *__restrict __nptr,
  162. char **__restrict __endptr, int __base)
  163. __THROW;
  164. #endif /* ISO C99 or GCC and use MISC. */
  165. #if 0
  166. #ifdef __USE_GNU
  167. /* The concept of one static locale per category is not very well
  168. thought out. Many applications will need to process its data using
  169. information from several different locales. Another application is
  170. the implementation of the internationalization handling in the
  171. upcoming ISO C++ standard library. To support this another set of
  172. the functions using locale data exist which have an additional
  173. argument.
  174. Attention: all these functions are *not* standardized in any form.
  175. This is a proof-of-concept implementation. */
  176. /* Structure for reentrant locale using functions. This is an
  177. (almost) opaque type for the user level programs. */
  178. # include <xlocale.h>
  179. /* Special versions of the functions above which take the locale to
  180. use as an additional parameter. */
  181. extern long int __strtol_l (__const char *__restrict __nptr,
  182. char **__restrict __endptr, int __base,
  183. __locale_t __loc) __THROW;
  184. extern unsigned long int __strtoul_l (__const char *__restrict __nptr,
  185. char **__restrict __endptr,
  186. int __base, __locale_t __loc) __THROW;
  187. __extension__
  188. extern long long int __strtoll_l (__const char *__restrict __nptr,
  189. char **__restrict __endptr, int __base,
  190. __locale_t __loc) __THROW;
  191. __extension__
  192. extern unsigned long long int __strtoull_l (__const char *__restrict __nptr,
  193. char **__restrict __endptr,
  194. int __base, __locale_t __loc)
  195. __THROW;
  196. extern double __strtod_l (__const char *__restrict __nptr,
  197. char **__restrict __endptr, __locale_t __loc)
  198. __THROW;
  199. extern float __strtof_l (__const char *__restrict __nptr,
  200. char **__restrict __endptr, __locale_t __loc) __THROW;
  201. extern long double __strtold_l (__const char *__restrict __nptr,
  202. char **__restrict __endptr,
  203. __locale_t __loc) __THROW;
  204. #endif /* GNU */
  205. #endif /* 0 */
  206. #if 0
  207. /* The internal entry points for `strtoX' take an extra flag argument
  208. saying whether or not to parse locale-dependent number grouping. */
  209. #ifdef __UCLIBC_HAS_FLOATS__
  210. extern double __strtod_internal (__const char *__restrict __nptr,
  211. char **__restrict __endptr, int __group)
  212. __THROW;
  213. extern float __strtof_internal (__const char *__restrict __nptr,
  214. char **__restrict __endptr, int __group)
  215. __THROW;
  216. extern long double __strtold_internal (__const char *__restrict __nptr,
  217. char **__restrict __endptr,
  218. int __group) __THROW;
  219. #endif /* __UCLIBC_HAS_FLOATS__ */
  220. #ifndef __strtol_internal_defined
  221. extern long int __strtol_internal (__const char *__restrict __nptr,
  222. char **__restrict __endptr,
  223. int __base, int __group) __THROW;
  224. # define __strtol_internal_defined 1
  225. #endif
  226. #ifndef __strtoul_internal_defined
  227. extern unsigned long int __strtoul_internal (__const char *__restrict __nptr,
  228. char **__restrict __endptr,
  229. int __base, int __group) __THROW;
  230. # define __strtoul_internal_defined 1
  231. #endif
  232. #if defined __GNUC__ || defined __USE_ISOC99
  233. # ifndef __strtoll_internal_defined
  234. __extension__
  235. extern long long int __strtoll_internal (__const char *__restrict __nptr,
  236. char **__restrict __endptr,
  237. int __base, int __group) __THROW;
  238. # define __strtoll_internal_defined 1
  239. # endif
  240. # ifndef __strtoull_internal_defined
  241. __extension__
  242. extern unsigned long long int __strtoull_internal (__const char *
  243. __restrict __nptr,
  244. char **__restrict __endptr,
  245. int __base, int __group)
  246. __THROW;
  247. # define __strtoull_internal_defined 1
  248. # endif
  249. #endif /* GCC */
  250. #endif /* 0 */
  251. #ifdef __USE_EXTERN_INLINES
  252. #if 0
  253. /* Define inline functions which call the internal entry points. */
  254. extern __inline double
  255. strtod (__const char *__restrict __nptr, char **__restrict __endptr) __THROW
  256. {
  257. return __strtod_internal (__nptr, __endptr, 0);
  258. }
  259. extern __inline long int
  260. strtol (__const char *__restrict __nptr, char **__restrict __endptr,
  261. int __base) __THROW
  262. {
  263. return __strtol_internal (__nptr, __endptr, __base, 0);
  264. }
  265. extern __inline unsigned long int
  266. strtoul (__const char *__restrict __nptr, char **__restrict __endptr,
  267. int __base) __THROW
  268. {
  269. return __strtoul_internal (__nptr, __endptr, __base, 0);
  270. }
  271. # ifdef __USE_ISOC99
  272. extern __inline float
  273. strtof (__const char *__restrict __nptr, char **__restrict __endptr) __THROW
  274. {
  275. return __strtof_internal (__nptr, __endptr, 0);
  276. }
  277. extern __inline long double
  278. strtold (__const char *__restrict __nptr, char **__restrict __endptr) __THROW
  279. {
  280. return __strtold_internal (__nptr, __endptr, 0);
  281. }
  282. # endif
  283. # ifdef __USE_BSD
  284. __extension__ extern __inline long long int
  285. strtoq (__const char *__restrict __nptr, char **__restrict __endptr,
  286. int __base) __THROW
  287. {
  288. return __strtoll_internal (__nptr, __endptr, __base, 0);
  289. }
  290. __extension__ extern __inline unsigned long long int
  291. strtouq (__const char *__restrict __nptr, char **__restrict __endptr,
  292. int __base) __THROW
  293. {
  294. return __strtoull_internal (__nptr, __endptr, __base, 0);
  295. }
  296. # endif
  297. # if defined __USE_MISC || defined __USE_ISOC99
  298. __extension__ extern __inline long long int
  299. strtoll (__const char *__restrict __nptr, char **__restrict __endptr,
  300. int __base) __THROW
  301. {
  302. return __strtoll_internal (__nptr, __endptr, __base, 0);
  303. }
  304. __extension__ extern __inline unsigned long long int
  305. strtoull (__const char * __restrict __nptr, char **__restrict __endptr,
  306. int __base) __THROW
  307. {
  308. return __strtoull_internal (__nptr, __endptr, __base, 0);
  309. }
  310. # endif
  311. #endif /* 0 */
  312. extern __inline double
  313. atof (__const char *__nptr) __THROW
  314. {
  315. return strtod (__nptr, (char **) NULL);
  316. }
  317. extern __inline int
  318. atoi (__const char *__nptr) __THROW
  319. {
  320. return (int) strtol (__nptr, (char **) NULL, 10);
  321. }
  322. extern __inline long int
  323. atol (__const char *__nptr) __THROW
  324. {
  325. return strtol (__nptr, (char **) NULL, 10);
  326. }
  327. # if defined __USE_MISC || defined __USE_ISOC99
  328. __extension__ extern __inline long long int
  329. atoll (__const char *__nptr) __THROW
  330. {
  331. return strtoll (__nptr, (char **) NULL, 10);
  332. }
  333. # endif
  334. #endif /* Optimizing and Inlining. */
  335. #if defined __USE_SVID || defined __USE_XOPEN_EXTENDED
  336. /* Convert N to base 64 using the digits "./0-9A-Za-z", least-significant
  337. digit first. Returns a pointer to static storage overwritten by the
  338. next call. */
  339. extern char *l64a (long int __n) __THROW;
  340. /* Read a number from a string S in base 64 as above. */
  341. extern long int a64l (__const char *__s) __THROW __attribute_pure__;
  342. # include <sys/types.h> /* we need int32_t... */
  343. /* These are the functions that actually do things. The `random', `srandom',
  344. `initstate' and `setstate' functions are those from BSD Unices.
  345. The `rand' and `srand' functions are required by the ANSI standard.
  346. We provide both interfaces to the same random number generator. */
  347. /* Return a random long integer between 0 and RAND_MAX inclusive. */
  348. extern long int random (void) __THROW;
  349. /* Seed the random number generator with the given number. */
  350. extern void srandom (unsigned int __seed) __THROW;
  351. /* Initialize the random number generator to use state buffer STATEBUF,
  352. of length STATELEN, and seed it with SEED. Optimal lengths are 8, 16,
  353. 32, 64, 128 and 256, the bigger the better; values less than 8 will
  354. cause an error and values greater than 256 will be rounded down. */
  355. extern char *initstate (unsigned int __seed, char *__statebuf,
  356. size_t __statelen) __THROW;
  357. /* Switch the random number generator to state buffer STATEBUF,
  358. which should have been previously initialized by `initstate'. */
  359. extern char *setstate (char *__statebuf) __THROW;
  360. # ifdef __USE_MISC
  361. /* Reentrant versions of the `random' family of functions.
  362. These functions all use the following data structure to contain
  363. state, rather than global state variables. */
  364. struct random_data
  365. {
  366. int32_t *fptr; /* Front pointer. */
  367. int32_t *rptr; /* Rear pointer. */
  368. int32_t *state; /* Array of state values. */
  369. int rand_type; /* Type of random number generator. */
  370. int rand_deg; /* Degree of random number generator. */
  371. int rand_sep; /* Distance between front and rear. */
  372. int32_t *end_ptr; /* Pointer behind state table. */
  373. };
  374. extern int random_r (struct random_data *__restrict __buf,
  375. int32_t *__restrict __result) __THROW;
  376. extern int srandom_r (unsigned int __seed, struct random_data *__buf) __THROW;
  377. extern int initstate_r (unsigned int __seed, char *__restrict __statebuf,
  378. size_t __statelen,
  379. struct random_data *__restrict __buf) __THROW;
  380. extern int setstate_r (char *__restrict __statebuf,
  381. struct random_data *__restrict __buf) __THROW;
  382. # endif /* Use misc. */
  383. #endif /* Use SVID || extended X/Open. */
  384. /* Return a random integer between 0 and RAND_MAX inclusive. */
  385. extern int rand (void) __THROW;
  386. /* Seed the random number generator with the given number. */
  387. extern void srand (unsigned int __seed) __THROW;
  388. #ifdef __USE_POSIX
  389. /* Reentrant interface according to POSIX.1. */
  390. extern int rand_r (unsigned int *__seed) __THROW;
  391. #endif
  392. #if defined __USE_SVID || defined __USE_XOPEN
  393. /* System V style 48-bit random number generator functions. */
  394. #ifdef __UCLIBC_HAS_FLOATS__
  395. /* Return non-negative, double-precision floating-point value in [0.0,1.0). */
  396. extern double drand48 (void) __THROW;
  397. extern double erand48 (unsigned short int __xsubi[3]) __THROW;
  398. #endif /* __UCLIBC_HAS_FLOATS__ */
  399. /* Return non-negative, long integer in [0,2^31). */
  400. extern long int lrand48 (void) __THROW;
  401. extern long int nrand48 (unsigned short int __xsubi[3]) __THROW;
  402. /* Return signed, long integers in [-2^31,2^31). */
  403. extern long int mrand48 (void) __THROW;
  404. extern long int jrand48 (unsigned short int __xsubi[3]) __THROW;
  405. /* Seed random number generator. */
  406. extern void srand48 (long int __seedval) __THROW;
  407. extern unsigned short int *seed48 (unsigned short int __seed16v[3]) __THROW;
  408. extern void lcong48 (unsigned short int __param[7]) __THROW;
  409. # ifdef __USE_MISC
  410. /* Data structure for communication with thread safe versions. This
  411. type is to be regarded as opaque. It's only exported because users
  412. have to allocate objects of this type. */
  413. struct drand48_data
  414. {
  415. unsigned short int __x[3]; /* Current state. */
  416. unsigned short int __old_x[3]; /* Old state. */
  417. unsigned short int __c; /* Additive const. in congruential formula. */
  418. unsigned short int __init; /* Flag for initializing. */
  419. unsigned long long int __a; /* Factor in congruential formula. */
  420. };
  421. #ifdef __UCLIBC_HAS_FLOATS__
  422. /* Return non-negative, double-precision floating-point value in [0.0,1.0). */
  423. extern int drand48_r (struct drand48_data *__restrict __buffer,
  424. double *__restrict __result) __THROW;
  425. extern int erand48_r (unsigned short int __xsubi[3],
  426. struct drand48_data *__restrict __buffer,
  427. double *__restrict __result) __THROW;
  428. #endif /* __UCLIBC_HAS_FLOATS__ */
  429. /* Return non-negative, long integer in [0,2^31). */
  430. extern int lrand48_r (struct drand48_data *__restrict __buffer,
  431. long int *__restrict __result) __THROW;
  432. extern int nrand48_r (unsigned short int __xsubi[3],
  433. struct drand48_data *__restrict __buffer,
  434. long int *__restrict __result) __THROW;
  435. /* Return signed, long integers in [-2^31,2^31). */
  436. extern int mrand48_r (struct drand48_data *__restrict __buffer,
  437. long int *__restrict __result) __THROW;
  438. extern int jrand48_r (unsigned short int __xsubi[3],
  439. struct drand48_data *__restrict __buffer,
  440. long int *__restrict __result) __THROW;
  441. /* Seed random number generator. */
  442. extern int srand48_r (long int __seedval, struct drand48_data *__buffer)
  443. __THROW;
  444. extern int seed48_r (unsigned short int __seed16v[3],
  445. struct drand48_data *__buffer) __THROW;
  446. extern int lcong48_r (unsigned short int __param[7],
  447. struct drand48_data *__buffer) __THROW;
  448. # endif /* Use misc. */
  449. #endif /* Use SVID or X/Open. */
  450. #endif /* don't just need malloc and calloc */
  451. #ifndef __malloc_and_calloc_defined
  452. #define __malloc_and_calloc_defined
  453. /* Allocate SIZE bytes of memory. */
  454. extern void *malloc (size_t __size) __THROW __attribute_malloc__;
  455. /* Allocate NMEMB elements of SIZE bytes each, all initialized to 0. */
  456. extern void *calloc (size_t __nmemb, size_t __size)
  457. __THROW __attribute_malloc__;
  458. #if 0
  459. /* Cope with autoconf's broken AC_FUNC_MALLOC macro, which
  460. * redefines malloc to rpl_malloc if it does not detect glibc
  461. * style returning-a-valid-pointer-for-malloc(0) behavior. This
  462. * calls malloc() as usual, but if __size is zero, we allocate and
  463. * return a 1-byte block instead.... sigh... */
  464. static __inline void *rpl_malloc (size_t __size)
  465. {
  466. if (__size == 0) {
  467. __size++;
  468. }
  469. return malloc(__size);
  470. }
  471. #endif
  472. #endif
  473. #ifndef __need_malloc_and_calloc
  474. /* Re-allocate the previously allocated block
  475. in PTR, making the new block SIZE bytes long. */
  476. extern void *realloc (void *__ptr, size_t __size) __THROW __attribute_malloc__;
  477. /* Free a block allocated by `malloc', `realloc' or `calloc'. */
  478. extern void free (void *__ptr) __THROW;
  479. #ifdef __USE_MISC
  480. /* Free a block. An alias for `free'. (Sun Unices). */
  481. extern void cfree (void *__ptr) __THROW;
  482. #endif /* Use misc. */
  483. #if defined __USE_GNU || defined __USE_BSD || defined __USE_MISC
  484. # include <alloca.h>
  485. #endif /* Use GNU, BSD, or misc. */
  486. #if defined __USE_BSD || defined __USE_XOPEN_EXTENDED
  487. /* Allocate SIZE bytes on a page boundary. The storage cannot be freed. */
  488. extern void *valloc (size_t __size) __THROW __attribute_malloc__;
  489. #endif
  490. #ifdef __USE_XOPEN2K
  491. /* Allocate memory of SIZE bytes with an alignment of ALIGNMENT. */
  492. extern int posix_memalign (void **__memptr, size_t __alignment, size_t __size)
  493. __THROW __attribute_malloc__;
  494. #endif
  495. /* Abort execution and generate a core-dump. */
  496. extern void abort (void) __THROW __attribute__ ((__noreturn__));
  497. /* Register a function to be called when `exit' is called. */
  498. extern int atexit (void (*__func) (void)) __THROW;
  499. /* The following is used by uClibc in atexit.c and sysconf.c */
  500. /* We have no limit when __UCLIBC_DYNAMIC_ATEXIT__ is enabled. */
  501. #ifdef __UCLIBC_DYNAMIC_ATEXIT__
  502. # define __UCLIBC_MAX_ATEXIT INT_MAX
  503. #else
  504. # define __UCLIBC_MAX_ATEXIT 20
  505. #endif
  506. #ifdef __USE_MISC
  507. /* Register a function to be called with the status
  508. given to `exit' and the given argument. */
  509. extern int on_exit (void (*__func) (int __status, void *__arg), void *__arg)
  510. __THROW;
  511. #endif
  512. /* Call all functions registered with `atexit' and `on_exit',
  513. in the reverse of the order in which they were registered
  514. perform stdio cleanup, and terminate program execution with STATUS. */
  515. extern void exit (int __status) __THROW __attribute__ ((__noreturn__));
  516. #ifdef __USE_ISOC99
  517. /* Terminate the program with STATUS without calling any of the
  518. functions registered with `atexit' or `on_exit'. */
  519. extern void _Exit (int __status) __THROW __attribute__ ((__noreturn__));
  520. #endif
  521. /* Return the value of envariable NAME, or NULL if it doesn't exist. */
  522. extern char *getenv (__const char *__name) __THROW;
  523. /* This function is similar to the above but returns NULL if the
  524. programs is running with SUID or SGID enabled. */
  525. extern char *__secure_getenv (__const char *__name) __THROW;
  526. #if defined __USE_SVID || defined __USE_XOPEN
  527. /* The SVID says this is in <stdio.h>, but this seems a better place. */
  528. /* Put STRING, which is of the form "NAME=VALUE", in the environment.
  529. If there is no `=', remove NAME from the environment. */
  530. extern int putenv (char *__string) __THROW;
  531. #endif
  532. #if defined __USE_BSD || defined __USE_XOPEN2K
  533. /* Set NAME to VALUE in the environment.
  534. If REPLACE is nonzero, overwrite an existing value. */
  535. extern int setenv (__const char *__name, __const char *__value, int __replace)
  536. __THROW;
  537. /* Remove the variable NAME from the environment. */
  538. extern int unsetenv (__const char *__name) __THROW;
  539. #endif
  540. #ifdef __USE_MISC
  541. /* The `clearenv' was planned to be added to POSIX.1 but probably
  542. never made it. Nevertheless the POSIX.9 standard (POSIX bindings
  543. for Fortran 77) requires this function. */
  544. extern int clearenv (void) __THROW;
  545. #endif
  546. #if defined __USE_MISC || defined __USE_XOPEN_EXTENDED
  547. /* Generate a unique temporary file name from TEMPLATE.
  548. The last six characters of TEMPLATE must be "XXXXXX";
  549. they are replaced with a string that makes the file name unique.
  550. Returns TEMPLATE, or a null pointer if it cannot get a unique file name. */
  551. extern char *mktemp (char *__template) __THROW;
  552. /* Generate a unique temporary file name from TEMPLATE.
  553. The last six characters of TEMPLATE must be "XXXXXX";
  554. they are replaced with a string that makes the filename unique.
  555. Returns a file descriptor open on the file for reading and writing,
  556. or -1 if it cannot create a uniquely-named file. */
  557. # ifndef __USE_FILE_OFFSET64
  558. extern int mkstemp (char *__template) __THROW;
  559. # else
  560. # ifdef __REDIRECT
  561. extern int __REDIRECT (mkstemp, (char *__template) __THROW, mkstemp64);
  562. # else
  563. # define mkstemp mkstemp64
  564. # endif
  565. # endif
  566. # ifdef __USE_LARGEFILE64
  567. extern int mkstemp64 (char *__template) __THROW;
  568. # endif
  569. #endif
  570. #ifdef __USE_BSD
  571. /* Create a unique temporary directory from TEMPLATE.
  572. The last six characters of TEMPLATE must be "XXXXXX";
  573. they are replaced with a string that makes the directory name unique.
  574. Returns TEMPLATE, or a null pointer if it cannot get a unique name.
  575. The directory is created mode 700. */
  576. extern char *mkdtemp (char *__template) __THROW;
  577. #endif
  578. /* Execute the given line as a shell command. */
  579. extern int system (__const char *__command) __THROW;
  580. #if 0
  581. /*#ifdef __USE_GNU*/
  582. /* Return a malloc'd string containing the canonical absolute name of the
  583. named file. The last file name component need not exist, and may be a
  584. symlink to a nonexistent file. */
  585. extern char *canonicalize_file_name (__const char *__name) __THROW;
  586. #endif
  587. #if defined __USE_BSD || defined __USE_XOPEN_EXTENDED
  588. /* Return the canonical absolute name of file NAME. The last file name
  589. component need not exist, and may be a symlink to a nonexistent file.
  590. If RESOLVED is null, the result is malloc'd; otherwise, if the canonical
  591. name is PATH_MAX chars or more, returns null with `errno' set to
  592. ENAMETOOLONG; if the name fits in fewer than PATH_MAX chars, returns the
  593. name in RESOLVED. */
  594. extern char *realpath (__const char *__restrict __name,
  595. char *__restrict __resolved) __THROW;
  596. #endif
  597. /* Shorthand for type of comparison functions. */
  598. #ifndef __COMPAR_FN_T
  599. # define __COMPAR_FN_T
  600. typedef int (*__compar_fn_t) (__const void *, __const void *);
  601. # ifdef __USE_GNU
  602. typedef __compar_fn_t comparison_fn_t;
  603. # endif
  604. #endif
  605. /* Do a binary search for KEY in BASE, which consists of NMEMB elements
  606. of SIZE bytes each, using COMPAR to perform the comparisons. */
  607. extern void *bsearch (__const void *__key, __const void *__base,
  608. size_t __nmemb, size_t __size, __compar_fn_t __compar);
  609. /* Sort NMEMB elements of BASE, of SIZE bytes each,
  610. using COMPAR to perform the comparisons. */
  611. extern void qsort (void *__base, size_t __nmemb, size_t __size,
  612. __compar_fn_t __compar);
  613. /* Return the absolute value of X. */
  614. extern int abs (int __x) __THROW __attribute__ ((__const__));
  615. extern long int labs (long int __x) __THROW __attribute__ ((__const__));
  616. #ifdef __USE_ISOC99
  617. __extension__ extern long long int llabs (long long int __x)
  618. __THROW __attribute__ ((__const__));
  619. #endif
  620. /* Return the `div_t', `ldiv_t' or `lldiv_t' representation
  621. of the value of NUMER over DENOM. */
  622. /* GCC may have built-ins for these someday. */
  623. extern div_t div (int __numer, int __denom)
  624. __THROW __attribute__ ((__const__));
  625. extern ldiv_t ldiv (long int __numer, long int __denom)
  626. __THROW __attribute__ ((__const__));
  627. #ifdef __USE_ISOC99
  628. __extension__ extern lldiv_t lldiv (long long int __numer,
  629. long long int __denom)
  630. __THROW __attribute__ ((__const__));
  631. #endif
  632. #if 0
  633. #ifdef __UCLIBC_HAS_FLOATS__
  634. #if defined __USE_SVID || defined __USE_XOPEN_EXTENDED
  635. /* Convert floating point numbers to strings. The returned values are
  636. valid only until another call to the same function. */
  637. /* Convert VALUE to a string with NDIGIT digits and return a pointer to
  638. this. Set *DECPT with the position of the decimal character and *SIGN
  639. with the sign of the number. */
  640. extern char *ecvt (double __value, int __ndigit, int *__restrict __decpt,
  641. int *__restrict __sign) __THROW;
  642. /* Convert VALUE to a string rounded to NDIGIT decimal digits. Set *DECPT
  643. with the position of the decimal character and *SIGN with the sign of
  644. the number. */
  645. extern char *fcvt (double __value, int __ndigit, int *__restrict __decpt,
  646. int *__restrict __sign) __THROW;
  647. /* If possible convert VALUE to a string with NDIGIT significant digits.
  648. Otherwise use exponential representation. The resulting string will
  649. be written to BUF. */
  650. extern char *gcvt (double __value, int __ndigit, char *__buf) __THROW;
  651. # ifdef __USE_MISC
  652. /* Long double versions of above functions. */
  653. extern char *qecvt (long double __value, int __ndigit,
  654. int *__restrict __decpt, int *__restrict __sign) __THROW;
  655. extern char *qfcvt (long double __value, int __ndigit,
  656. int *__restrict __decpt, int *__restrict __sign) __THROW;
  657. extern char *qgcvt (long double __value, int __ndigit, char *__buf) __THROW;
  658. /* Reentrant version of the functions above which provide their own
  659. buffers. */
  660. extern int ecvt_r (double __value, int __ndigit, int *__restrict __decpt,
  661. int *__restrict __sign, char *__restrict __buf,
  662. size_t __len) __THROW;
  663. extern int fcvt_r (double __value, int __ndigit, int *__restrict __decpt,
  664. int *__restrict __sign, char *__restrict __buf,
  665. size_t __len) __THROW;
  666. extern int qecvt_r (long double __value, int __ndigit,
  667. int *__restrict __decpt, int *__restrict __sign,
  668. char *__restrict __buf, size_t __len) __THROW;
  669. extern int qfcvt_r (long double __value, int __ndigit,
  670. int *__restrict __decpt, int *__restrict __sign,
  671. char *__restrict __buf, size_t __len) __THROW;
  672. # endif /* misc */
  673. #endif /* use MISC || use X/Open Unix */
  674. #endif /* __UCLIBC_HAS_FLOATS__ */
  675. #endif
  676. #ifdef __UCLIBC_HAS_WCHAR__
  677. /* Return the length of the multibyte character
  678. in S, which is no longer than N. */
  679. extern int mblen (__const char *__s, size_t __n) __THROW;
  680. /* Return the length of the given multibyte character,
  681. putting its `wchar_t' representation in *PWC. */
  682. extern int mbtowc (wchar_t *__restrict __pwc,
  683. __const char *__restrict __s, size_t __n) __THROW;
  684. /* Put the multibyte character represented
  685. by WCHAR in S, returning its length. */
  686. extern int wctomb (char *__s, wchar_t __wchar) __THROW;
  687. /* Convert a multibyte string to a wide char string. */
  688. extern size_t mbstowcs (wchar_t *__restrict __pwcs,
  689. __const char *__restrict __s, size_t __n) __THROW;
  690. /* Convert a wide char string to multibyte string. */
  691. extern size_t wcstombs (char *__restrict __s,
  692. __const wchar_t *__restrict __pwcs, size_t __n)
  693. __THROW;
  694. #endif /* def __UCLIBC_HAS_WCHAR__ */
  695. #ifdef __USE_SVID
  696. /* Determine whether the string value of RESPONSE matches the affirmation
  697. or negative response expression as specified by the LC_MESSAGES category
  698. in the program's current locale. Returns 1 if affirmative, 0 if
  699. negative, and -1 if not matching. */
  700. extern int rpmatch (__const char *__response) __THROW;
  701. #endif
  702. #ifdef __USE_XOPEN_EXTENDED
  703. /* Parse comma separated suboption from *OPTIONP and match against
  704. strings in TOKENS. If found return index and set *VALUEP to
  705. optional value introduced by an equal sign. If the suboption is
  706. not part of TOKENS return in *VALUEP beginning of unknown
  707. suboption. On exit *OPTIONP is set to the beginning of the next
  708. token or at the terminating NUL character. */
  709. extern int getsubopt (char **__restrict __optionp,
  710. char *__const *__restrict __tokens,
  711. char **__restrict __valuep) __THROW;
  712. #endif
  713. #ifdef __USE_XOPEN
  714. /* Setup DES tables according KEY. */
  715. extern void setkey (__const char *__key) __THROW;
  716. #endif
  717. /* X/Open pseudo terminal handling. */
  718. #ifdef __USE_XOPEN2K
  719. /* Return a master pseudo-terminal handle. */
  720. extern int posix_openpt (int __oflag) __THROW;
  721. #endif
  722. #ifdef __USE_XOPEN
  723. /* The next four functions all take a master pseudo-tty fd and
  724. perform an operation on the associated slave: */
  725. /* Chown the slave to the calling user. */
  726. extern int grantpt (int __fd) __THROW;
  727. /* Release an internal lock so the slave can be opened.
  728. Call after grantpt(). */
  729. extern int unlockpt (int __fd) __THROW;
  730. /* Return the pathname of the pseudo terminal slave assoicated with
  731. the master FD is open on, or NULL on errors.
  732. The returned storage is good until the next call to this function. */
  733. extern char *ptsname (int __fd) __THROW;
  734. #endif
  735. #ifdef __USE_GNU
  736. /* Store at most BUFLEN characters of the pathname of the slave pseudo
  737. terminal associated with the master FD is open on in BUF.
  738. Return 0 on success, otherwise an error number. */
  739. #if 0
  740. extern int ptsname_r (int __fd, char *__buf, size_t __buflen) __THROW;
  741. #endif
  742. /* Open a master pseudo terminal and return its file descriptor. */
  743. extern int getpt (void) __THROW;
  744. #endif
  745. #ifdef __USE_BSD
  746. /* Put the 1 minute, 5 minute and 15 minute load averages into the first
  747. NELEM elements of LOADAVG. Return the number written (never more than
  748. three, but may be less than NELEM), or -1 if an error occurred. */
  749. extern int getloadavg (double __loadavg[], int __nelem) __THROW;
  750. #endif
  751. #endif /* don't just need malloc and calloc */
  752. #undef __need_malloc_and_calloc
  753. __END_DECLS
  754. #endif /* stdlib.h */