• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /**
2  * @defgroup stdlib Stdlib
3  * @ingroup libc
4  */
5 
6 #ifndef _STDLIB_H
7 #define _STDLIB_H
8 
9 #ifdef __cplusplus
10 extern "C" {
11 #endif
12 
13 #ifdef __LITEOS__
14 #include <malloc.h>
15 #endif
16 
17 #include <features.h>
18 
19 #ifdef __cplusplus
20 #define NULL 0L
21 #else
22 #ifndef NULL
23 #define NULL ((void*)0)
24 #endif
25 #endif
26 
27 #define __NEED_size_t
28 #define __NEED_wchar_t
29 
30 #include <bits/alltypes.h>
31 
32 /**
33  *@ingroup stdlib
34  *
35  *@par Description:
36  *This API is used to convert a string to an integer.
37  *    The call is equivalent to strtol(str, (char **)NULL, 10).
38  *    If the value cannot be represented, the behavior is undefined.
39  *@attention
40  *<ul>
41  *<li> None.</li>
42  *</ul>
43  *
44  *@retval  int     converted value if the value can be represented.
45  *
46  *@par Dependency:
47  *<ul><li>stdlib.h</li></ul>
48  *@see strtol
49  */
50 int atoi (const char *);
51 
52 /**
53  *@ingroup stdlib
54  *
55  *@par Description:
56  *This API is used to convert a string to a long integer.
57  *    The call is equivalent to strtol(str, (char **)NULL, 10).
58  *    If the value cannot be represented, the behavior is undefined.
59  *@attention
60  *<ul>
61  *<li> None.</li>
62  *</ul>
63  *
64  *@retval  long     converted value if the value can be represented.
65  *
66  *@par Dependency:
67  *<ul><li>stdlib.h</li></ul>
68  *@see strtol
69  */
70 long atol (const char *);
71 
72 /**
73  *@ingroup stdlib
74  *
75  *@par Description:
76  *This API is used to convert a string to a long long integer.
77  *    The call is equivalent to strtol(str, (char **)NULL, 10).
78  *    If the value cannot be represented, the behavior is undefined.
79  *@attention
80  *<ul>
81  *<li> None.</li>
82  *</ul>
83  *
84  *@retval  long-long     converted value if the value can be represented.
85  *
86  *@par Dependency:
87  *<ul><li>stdlib.h</li></ul>
88  *@see strtol
89  */
90 long long atoll (const char *);
91 
92 /**
93  *@ingroup stdlib
94  *
95  *@par Description:
96  *This API is used to convert a string to a double-precision number.
97  *    The call is equivalent to strtod(str, (char **)NULL).
98  *    If the value cannot be represented, the behavior is undefined.
99  *@attention
100  *<ul>
101  *<li> None.</li>
102  *</ul>
103  *
104  *@retval  double  converted value if the value can be represented.
105  *
106  *@par Errors
107  *<ul>
108  *<li><b>ERANGE</b>: The value to be returned would cause overflow  or underflow.</li>
109  *</ul>
110  *
111  *@par Dependency:
112  *<ul><li>stdlib.h</li></ul>
113  *@see strtod
114  */
115 double atof (const char *);
116 
117 /**
118  *@ingroup stdlib
119  *
120  *@par Description:
121  *This API is used to convert a string to a float-precision number.
122  *    If the value cannot be represented, the behavior is undefined.
123  *@attention
124  *<ul>
125  *<li> None.</li>
126  *</ul>
127  *
128  *@retval  float     converted value if the value can be represented.
129  *
130  *@par Errors
131  *<ul>
132  *<li><b>ERANGE</b>: The value to be returned would cause overflow  or underflow.</li>
133  *</ul>
134  *
135  *@par Dependency:
136  *<ul><li>stdlib.h</li></ul>
137  *@see strtod | strtold
138  */
139 float strtof (const char *__restrict, char **__restrict);
140 
141 /**
142  *@ingroup stdlib
143  *
144  *@par Description:
145  *This API is used to convert a string to a double-precision number.
146  *    If the value cannot be represented, the behavior is undefined.
147  *@attention
148  *<ul>
149  *<li> None.</li>
150  *</ul>
151  *
152  *@retval  double     converted value if the value can be represented.
153  *
154  *@par Errors
155  *<ul>
156  *<li><b>ERANGE</b>: The value to be returned would cause overflow  or underflow.</li>
157  *</ul>
158  *
159  *@par Dependency:
160  *<ul><li>stdlib.h</li></ul>
161  *@see strtof | strtold
162  */
163 double strtod (const char *__restrict, char **__restrict);
164 
165 /**
166  *@ingroup stdlib
167  *
168  *@par Description:
169  *This API is used to convert a string to a long-double-precision number.
170  *    If the value cannot be represented, the behavior is undefined.
171  *@attention
172  *<ul>
173  *<li> None.</li>
174  *</ul>
175  *
176  *@retval  long-double     converted value if the value can be represented.
177  *
178  *@par Errors
179  *<ul>
180  *<li><b>ERANGE</b>: The value to be returned would cause overflow  or underflow.</li>
181  *</ul>
182  *
183  *@par Dependency:
184  *<ul><li>stdlib.h</li></ul>
185  *@see strtod | strtof
186  */
187 long double strtold (const char *__restrict, char **__restrict);
188 
189 /**
190  *@ingroup stdlib
191  *
192  *@par Description:
193  *This API is used to convert a string to a long integer.
194  *    Ensure that "base" is between 2 and 36 inclusive, or the special value of 0.
195  *@attention
196  *<ul>
197  *<li> None.</li>
198  *</ul>
199  *
200  *@retval  non-zero  The converted value, successful completion.
201  *@retval  0    Fails to convert, with any of the following error codes in errno.
202  *
203  *@par Errors
204  *<ul>
205  *<li><b>ERANGE</b>: The converted value is out of range {LONG_MIN, LONG_MAX}.</li>
206  *<li><b>EINVAL</b>: The value of "base" is not supported.</li>
207  *</ul>
208  *
209  *@par Dependency:
210  *<ul><li>stdlib.h</li></ul>
211  *@see strtoll | strtoul | strtoull
212  */
213 long strtol (const char *__restrict, char **__restrict, int);
214 
215 /**
216  *@ingroup stdlib
217  *
218  *@par Description:
219  *This API is used to convert a string to a unsigned long integer.
220  *    Ensure that "base" is between 2 and 36 inclusive, or the special value of 0.
221  *@attention
222  *<ul>
223  *<li> None.</li>
224  *</ul>
225  *
226  *@retval  non-zero  The converted value, successful completion.
227  *@retval  0    Fails to convert, with any of the following error codes in errno.
228  *
229  *@par Errors
230  *<ul>
231  *<li><b>ERANGE</b>: The converted value is out of range ULONG_MAX.</li>
232  *<li><b>EINVAL</b>: The value of "base" is not supported.</li>
233  *</ul>
234  *
235  *@par Dependency:
236  *<ul><li>stdlib.h</li></ul>
237  *@see strtoll | strtoul | strtoull
238  */
239 unsigned long strtoul (const char *__restrict, char **__restrict, int);
240 
241 
242 /**
243  *@ingroup stdlib
244  *
245  *@par Description:
246  *This API is used to convert a string to a long long integer.
247  *    Ensure that "base" is between 2 and 36 inclusive, or the special value of 0.
248  *@attention
249  *<ul>
250  *<li> None.</li>
251  *</ul>
252  *
253  *@retval  non-zero  The converted value, successful completion.
254  *@retval  0    Fails to convert, with any of the following error codes in errno.
255  *
256  *@par Errors
257  *<ul>
258  *<li><b>ERANGE</b>: The converted value is out of range {LLONG_MIN, LLONG_MAX}.</li>
259  *<li><b>EINVAL</b>: The value of "base" is not supported.</li>
260  *</ul>
261  *
262  *@par Dependency:
263  *<ul><li>stdlib.h</li></ul>
264  *@see strtoll | strtoul | strtoull
265  */
266 long long strtoll (const char *__restrict, char **__restrict, int);
267 
268 
269 /**
270  *@ingroup stdlib
271  *
272  *@par Description:
273  *This API is used to convert a string to a unsigned long long integer.
274  *    Ensure that "base" is between 2 and 36 inclusive, or the special value of 0.
275  *@attention
276  *<ul>
277  *<li> None.</li>
278  *</ul>
279  *
280  *@retval  non-zero  The converted value, successful completion.
281  *@retval  0    Fails to convert, with any of the following error codes in errno.
282  *
283  *@par Errors
284  *<ul>
285  *<li><b>ERANGE</b>: The converted value is out of range ULLONG_MAX.</li>
286  *<li><b>EINVAL</b>: The value of "base" is not supported.</li>
287  *</ul>
288  *
289  *@par Dependency:
290  *<ul><li>stdlib.h</li></ul>
291  *@see strtoll | strtoul | strtoull
292  */
293 unsigned long long strtoull (const char *__restrict, char **__restrict, int);
294 
295 /**
296  *@ingroup stdlib
297  *
298  *@par Description:
299  *This API is used to compute a sequence of pseudo-random integers in the range [0, RAND_MAX].
300  *@attention
301  *<ul>
302  *<li> None.</li>
303  *</ul>
304  *
305  *@par Dependency:
306  *<ul><li>stdlib.h</li></ul>
307  *@see random
308  */
309 int rand (void);
310 
311 /**
312  *@ingroup stdlib
313  *
314  *@par Description:
315  *This API is used to set  the argument as a seed for a new sequence of pseudo-random
316  *    numbers to be returned by subsequent calls to rand().
317  *@attention
318  *<ul>
319  *<li> None.</li>
320  *</ul>
321  *
322  *@par Dependency:
323  *<ul><li>stdlib.h</li></ul>
324  *@see srandom
325  */
326 void srand (unsigned);
327 
328 void *malloc (size_t);
329 void *calloc (size_t, size_t);
330 void *realloc (void *, size_t);
331 void free (void *);
332 void *aligned_alloc(size_t, size_t);
333 
334 /**
335  *@ingroup stdlib
336  *
337  *@par Description:
338  *This API is used to abort the system, the system enters swi exception, print backtrace and then
339  *    Entering an infinite loop.
340  *@attention
341  *<ul>
342  *<li> Normal interrupts will not be responded, only NMI exception can be triggered.</li>
343  *</ul>
344  *
345  *@par Dependency:
346  *<ul><li>stdlib.h</li></ul>
347  *@see exit
348  */
349 _Noreturn void abort (void);
350 
351 /**
352  *@ingroup stdlib
353  *
354  *@par Description:
355  *This API is defined, but just return 0.
356  *@attention
357  *<ul>
358  *<li> None.</li>
359  *</ul>
360  *
361  *@par Dependency:
362  *<ul><li>stdlib.h</li></ul>
363  *@see abort | exit
364  */
365 int atexit (void (*) (void));
366 
367 /**
368  *@ingroup stdlib
369  *
370  *@par Description:
371  *This API is used to terminate a thread, the current thread enters an infinite loop.
372  *@attention
373  *<ul>
374  *<li> The current thread enters an infinite loop, the thread with a lower priority than
375       the current thread can not obtain cpu resources.</li>
376  *<li> CPU occupancy rate will up to 100%.</li>
377  *</ul>
378  *
379  *@par Dependency:
380  *<ul><li>stdlib.h</li></ul>
381  *@see abort
382  */
383 _Noreturn void exit (int);
384 _Noreturn void _Exit (int);
385 int at_quick_exit (void (*) (void));
386 _Noreturn void quick_exit (int);
387 
388 /**
389  *@ingroup stdlib
390  *
391  *@par Description:
392  *This API is defined, but just return NULL.
393  *@attention
394  *<ul>
395  *<li> None.</li>
396  *</ul>
397  *
398  *@par Dependency:
399  *<ul><li>stdlib.h</li></ul>
400  *@see putenv | setenv | unsetenv
401  */
402 char *getenv (const char *);
403 
404 /**
405  *@ingroup stdlib
406  *
407  *@par Description:
408  *This API is defined, but just return 0.
409  *@attention
410  *<ul>
411  *<li> None.</li>
412  *</ul>
413  *
414  *@par Dependency:
415  *<ul><li>stdlib.h</li></ul>
416  *@see None.
417  */
418 int system (const char *);
419 
420 /**
421  *@ingroup stdlib
422  *
423  *@par Description:
424  *This API is used to search an array of "nmemb" objects, the initial element of which
425  *    is pointed to by base0", for an element that matches the object pointed to by "key".
426  *    The size of each element in the array is specified by "size".
427  *@attention
428  *<ul>
429  *<li> None.</li>
430  *</ul>
431  *
432  *@retval  NULL    No match is found.
433  *@retval  void*   A pointer to a matching member of the array.
434  *
435  *@par Dependency:
436  *<ul><li>stdlib.h</li></ul>
437  *@see None.
438  */
439 void *bsearch (const void *, const void *, size_t, size_t, int (*)(const void *, const void *));
440 
441 /**
442  *@ingroup stdlib
443  *
444  *@par Description:
445  *This API is used to sort an array of "nmemb", the initial element of which
446  *    is pointed to by base", The size of each object, in bytes, is specified by the "size".
447  *    If the "nmemb" has the value zero, the comparison function pointed to by "compar"
448  *    would not be called and no rearrangement will take place.
449  *@attention
450  *<ul>
451  *<li> None.</li>
452  *</ul>
453  *
454  *@par Dependency:
455  *<ul><li>stdlib.h</li></ul>
456  *@see None.
457  */
458 void qsort (void *, size_t, size_t, int (*)(const void *, const void *));
459 
460 /**
461  *@ingroup stdlib
462  *
463  *@par Description:
464  *This API is used to compute the absolute value of its integer operand "j".
465  *    If the value cannot be represented, the behavior is undefined.
466  *@attention
467  *<ul>
468  *<li> None.</li>
469  *</ul>
470  *
471  *@retval  int      The absolute value of its integer operand.
472  *
473  *@par Dependency:
474  *<ul><li>stdlib.h</li></ul>
475  *@see labs | llabs
476  */
477 int abs (int);
478 
479 /**
480  *@ingroup stdlib
481  *
482  *@par Description:
483  *This API is used to compute the absolute value of the long integer operand "j".
484  *    If the value cannot be represented, the behavior is undefined.
485  *@attention
486  *<ul>
487  *<li> None.</li>
488  *</ul>
489  *
490  *@retval  long      The absolute value of its integer operand.
491  *
492  *@par Dependency:
493  *<ul><li>stdlib.h</li></ul>
494  *@see abs | llabs
495  */
496 long labs (long);
497 
498 /**
499  *@ingroup stdlib
500  *
501  *@par Description:
502  *This API is used to compute the absolute value of the long long integer operand "j".
503  *    If the value cannot be represented, the behavior is undefined.
504  *@attention
505  *<ul>
506  *<li> None.</li>
507  *</ul>
508  *
509  *@retval  long-long      The absolute value of its integer operand.
510  *
511  *@par Dependency:
512  *<ul><li>stdlib.h</li></ul>
513  *@see labs | abs
514  */
515 long long llabs (long long);
516 
517 typedef struct { int quot, rem; } div_t;
518 typedef struct { long quot, rem; } ldiv_t;
519 typedef struct { long long quot, rem; } lldiv_t;
520 
521 div_t div (int, int);
522 ldiv_t ldiv (long, long);
523 lldiv_t lldiv (long long, long long);
524 
525 int mblen (const char *, size_t);
526 int mbtowc (wchar_t *__restrict, const char *__restrict, size_t);
527 int wctomb (char *, wchar_t);
528 size_t mbstowcs (wchar_t *__restrict, const char *__restrict, size_t);
529 size_t wcstombs (char *__restrict, const wchar_t *__restrict, size_t);
530 
531 /**
532  * @ingroup stdlib
533  * Define unsuccessful termination to 1.
534  */
535 #define EXIT_FAILURE 1
536 
537 /**
538  * @ingroup stdlib
539  * Define successful termination to 0.
540  */
541 #define EXIT_SUCCESS 0
542 
543 size_t __ctype_get_mb_cur_max(void);
544 #define MB_CUR_MAX (__ctype_get_mb_cur_max())
545 
546 /**
547  * @ingroup stdlib
548  * Define Maximum value returned by rand() to 0x7fffffff.
549  */
550 #define RAND_MAX (0x7fffffff)
551 
552 
553 #if defined(_POSIX_SOURCE) || defined(_POSIX_C_SOURCE) \
554  || defined(_XOPEN_SOURCE) || defined(_GNU_SOURCE) \
555  || defined(_BSD_SOURCE)
556 
557 #define WNOHANG    1
558 #define WUNTRACED  2
559 
560 #define WEXITSTATUS(s) (((s) & 0xff00) >> 8)
561 #define WTERMSIG(s) ((s) & 0x7f)
562 #define WSTOPSIG(s) WEXITSTATUS(s)
563 #define WIFEXITED(s) (!WTERMSIG(s))
564 #define WIFSTOPPED(s) ((short)((((s)&0xffff)*0x10001)>>8) > 0x7f00)
565 #define WIFSIGNALED(s) (((s)&0xffff)-1U < 0xffu)
566 
567 /**
568  *@ingroup stdlib
569  *
570  *@par Description:
571  *This API is used to allocate "size" bytes  memory aligned on a boundary specified by "alignment".
572  *The value of "alignment" shall be a power of two and  multiple of sizeof(void *).
573  *The value of errno would not be modified.
574  *@attention
575  *<ul>
576  *<li> None.</li>
577  *</ul>
578  *
579  *@retval  0  Success to allocate memory.
580  *@retval  EINVAL    The value of "alignment" shall be a power of two and  multiple of sizeof(void *).
581  *@retval  ENOMEM    There is no enough memory.
582  *
583  *@par Dependency:
584  *<ul><li>stdlib.h</li></ul>
585  *@see malloc | free
586  */
587 int posix_memalign (void **, size_t, size_t);
588 int setenv (const char *, const char *, int);
589 int unsetenv (const char *);
590 
591 /**
592  *@ingroup stdlib
593  *
594  *@par Description:
595  *This API is defined, but just return -1.
596  *@attention
597  *<ul>
598  *<li> None.</li>
599  *</ul>
600  *
601  *@par Dependency:
602  *<ul><li>stdlib.h</li></ul>
603  *@see mktemp | mkstemps
604  */
605 int mkstemp (char *);
606 int mkostemp (char *, int);
607 char *mkdtemp (char *);
608 int getsubopt (char **, char *const *, char **);
609 int rand_r (unsigned *);
610 
611 #endif
612 
613 
614 #if defined(_XOPEN_SOURCE) || defined(_GNU_SOURCE) \
615  || defined(_BSD_SOURCE)
616 char *realpath (const char *__restrict, char *__restrict);
617 
618 /**
619  *@ingroup stdlib
620  *
621  *@par Description:
622  *This API is used to generate a pseudo-random integer in the range [0, RAND_MAX].
623  *@attention
624  *<ul>
625  *<li> None.</li>
626  *</ul>
627  *
628  *@par Dependency:
629  *<ul><li>stdlib.h</li></ul>
630  *@see srandom
631  */
632 long int random (void);
633 
634 /**
635  *@ingroup stdlib
636  *
637  *@par Description:
638  *This API is used to set  the argument as a seed for a new sequence of pseudo-random
639  *    numbers to be returned by subsequent calls to random().
640  *@attention
641  *<ul>
642  *<li> None.</li>
643  *</ul>
644  *
645  *@par Dependency:
646  *<ul><li>stdlib.h</li></ul>
647  *@see srandom
648  */
649 void srandom (unsigned int);
650 char *initstate (unsigned int, char *, size_t);
651 char *setstate (char *);
652 int putenv (char *);
653 int posix_openpt (int);
654 int grantpt (int);
655 int unlockpt (int);
656 char *ptsname (int);
657 char *l64a (long);
658 long a64l (const char *);
659 void setkey (const char *);
660 double drand48 (void);
661 double erand48 (unsigned short [3]);
662 long int lrand48 (void);
663 long int nrand48 (unsigned short [3]);
664 long mrand48 (void);
665 long jrand48 (unsigned short [3]);
666 void srand48 (long);
667 unsigned short *seed48 (unsigned short [3]);
668 void lcong48 (unsigned short [7]);
669 #endif
670 
671 #if defined(_GNU_SOURCE) || defined(_BSD_SOURCE)
672 #include <alloca.h>
673 
674 /**
675  *@ingroup stdlib
676  *
677  *@par Description:
678  *This API is defined, but just return NULL.
679  *@attention
680  *<ul>
681  *<li> None.</li>
682  *</ul>
683  *
684  *@par Dependency:
685  *<ul><li>stdlib.h</li></ul>
686  *@see mkstemp
687  */
688 char *mktemp (char *);
689 
690 /**
691  *@ingroup stdlib
692  *
693  *@par Description:
694  *This API is defined, but just return -1.
695  *@attention
696  *<ul>
697  *<li> None.</li>
698  *</ul>
699  *
700  *@par Dependency:
701  *<ul><li>stdlib.h</li></ul>
702  *@see mkstemp | mkstemps
703  */
704 int mkstemps (char *, int);
705 int mkostemps (char *, int, int);
706 void *valloc (size_t);
707 void *memalign(size_t, size_t);
708 int getloadavg(double *, int);
709 int clearenv(void);
710 #define WCOREDUMP(s) ((s) & 0x80)
711 #define WIFCONTINUED(s) ((s) == 0xffff)
712 #endif
713 
714 #ifdef _GNU_SOURCE
715 int ptsname_r(int, char *, size_t);
716 char *ecvt(double, int, int *, int *);
717 char *fcvt(double, int, int *, int *);
718 char *gcvt(double, int, char *);
719 char *secure_getenv(const char *);
720 struct __locale_struct;
721 #ifndef __LITEOS__
722 float strtof_l(const char *__restrict, char **__restrict, struct __locale_struct *);
723 double strtod_l(const char *__restrict, char **__restrict, struct __locale_struct *);
724 long double strtold_l(const char *__restrict, char **__restrict, struct __locale_struct *);
725 #endif
726 #endif
727 
728 #if defined(_LARGEFILE64_SOURCE) || defined(_GNU_SOURCE)
729 #define mkstemp64 mkstemp
730 #define mkostemp64 mkostemp
731 #if defined(_GNU_SOURCE) || defined(_BSD_SOURCE)
732 #define mkstemps64 mkstemps
733 #define mkostemps64 mkostemps
734 #endif
735 #endif
736 
737 #ifdef __cplusplus
738 }
739 #endif
740 
741 #endif
742