TizenRT Libs&Environment  v2.0 M2
stdlib.h
Go to the documentation of this file.
1 /****************************************************************************
2  *
3  * Copyright 2016 Samsung Electronics All Rights Reserved.
4  *
5  * Licensed under the Apache License, Version 2.0 (the "License");
6  * you may not use this file except in compliance with the License.
7  * You may obtain a copy of the License at
8  *
9  * http://www.apache.org/licenses/LICENSE-2.0
10  *
11  * Unless required by applicable law or agreed to in writing,
12  * software distributed under the License is distributed on an
13  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND,
14  * either express or implied. See the License for the specific
15  * language governing permissions and limitations under the License.
16  *
17  ****************************************************************************/
18 /****************************************************************************
19  * include/stdlib.h
20  *
21  * Copyright (C) 2007-2014 Gregory Nutt. All rights reserved.
22  * Author: Gregory Nutt <gnutt@nuttx.org>
23  *
24  * Redistribution and use in source and binary forms, with or without
25  * modification, are permitted provided that the following conditions
26  * are met:
27  *
28  * 1. Redistributions of source code must retain the above copyright
29  * notice, this list of conditions and the following disclaimer.
30  * 2. Redistributions in binary form must reproduce the above copyright
31  * notice, this list of conditions and the following disclaimer in
32  * the documentation and/or other materials provided with the
33  * distribution.
34  * 3. Neither the name NuttX nor the names of its contributors may be
35  * used to endorse or promote products derived from this software
36  * without specific prior written permission.
37  *
38  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
39  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
40  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
41  * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
42  * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
43  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
44  * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
45  * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
46  * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
47  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
48  * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
49  * POSSIBILITY OF SUCH DAMAGE.
50  *
51  ****************************************************************************/
59 
62 #ifndef __INCLUDE_STDLIB_H
63 #define __INCLUDE_STDLIB_H
64 
65 /****************************************************************************
66  * Included Files
67  ****************************************************************************/
68 
69 #include <tinyara/config.h>
70 #include <tinyara/compiler.h>
71 
72 #include <sys/types.h>
73 #include <stdint.h>
74 
75 /****************************************************************************
76  * Pre-processor Definitions
77  ****************************************************************************/
78 
79 /* The C standard specifies two constants, EXIT_SUCCESS and
80  * EXIT_FAILURE, that may be passed to exit() to indicate
81  * successful or unsucessful termination, respectively.
82  */
83 
84 #define EXIT_SUCCESS 0
85 #define EXIT_FAILURE 1
86 
87 /* The NULL pointer should be defined in this file but is currently defined
88  * in sys/types.h.
89  */
90 
91 /* Maximum value returned by rand(). Must be a minimum of 32767. */
92 
93 #define RAND_MAX INT_MAX
94 
95 /* Integer expression whose value is the maximum number of bytes in a
96  * character specified by the current locale.
97  */
98 
99 #define MB_CUR_MAX 1
100 
101 /* The environ variable, normally 'extern char **environ;' is
102  * not implemented as a function call. However, get_environ_ptr()
103  * can be used in its place.
104  */
105 
106 #ifndef CONFIG_DISABLE_ENVIRON
107 #define environ get_environ_ptr()
108 #endif
109 
110 /****************************************************************************
111  * Global Type Definitions
112  ****************************************************************************/
114 struct mallinfo {
115  int arena; /* This is the total size of memory allocated
116  * for use by malloc in bytes. */
117  int ordblks; /* This is the number of free (not in use) chunks */
118  int mxordblk; /* Size of the largest free (not in use) chunk */
119  int uordblks; /* This is the total size of memory occupied by
120  * chunks handed out by malloc. */
121  int fordblks; /* This is the total size of memory occupied
122  * by free (not in use) chunks.*/
123 };
124 
125 /* Structure type returned by the div() function. */
126 
127 struct div_s {
128  int quot; /* Quotient */
129  int rem; /* Remainder */
130 };
131 
132 typedef struct div_s div_t;
133 
134 /* Structure type returned by the ldiv() function. */
135 
136 struct ldiv_s {
137  long quot; /* Quotient */
138  long rem; /* Remainder */
139 };
140 
141 typedef struct ldiv_s ldiv_t;
142 
143 /* Structure type returned by the lldiv() function. */
144 
145 struct lldiv_s {
146  long quot; /* Quotient */
147  long rem; /* Remainder */
148 };
149 
150 typedef struct lldiv_s lldiv_t;
155 /****************************************************************************
156  * Global Function Prototypes
157  ****************************************************************************/
158 
159 /****************************************************************************
160  * Global Function Prototypes
161  ****************************************************************************/
162 
163 #undef EXTERN
164 #if defined(__cplusplus)
165 #define EXTERN extern "C"
166 extern "C" {
167 #else
168 #define EXTERN extern
169 #endif
170 
171 /* Random number generation */
181 void srand(unsigned int seed);
189 int rand(void);
190 
191 #define srandom(s) srand(s)
192 
200 long random(void);
201 
202 /* Environment variable support */
203 
204 #ifndef CONFIG_DISABLE_ENVIRON
205 
215 FAR char *get_environ_ptr(size_t *envsize);
224 FAR char *getenv(FAR const char *name);
233 int putenv(FAR const char *string);
242 int clearenv(void);
251 int setenv(const char *name, const char *value, int overwrite);
260 int unsetenv(const char *name);
261 #endif
262 
263 /* Process exit functions */
272 void exit(int status) noreturn_function;
280 void abort(void) noreturn_function;
281 #ifdef CONFIG_SCHED_ATEXIT
282 
290 int atexit(CODE void (*func)(void));
291 #endif
292 #ifdef CONFIG_SCHED_ONEXIT
293 
305 int on_exit(CODE void (*func)(int, FAR void *), FAR void *arg);
306 #endif
307 
308 /* _Exit() is a stdlib.h equivalent to the unistd.h _exit() function */
313 void _exit(int status); /* See unistd.h */
317 #define _Exit(s) _exit(s)
318 
319 /* String to binary conversions */
327 long strtol(const char *, char **, int);
335 unsigned long strtoul(const char *, char **, int);
336 #ifdef CONFIG_HAVE_LONG_LONG
337 
344 long long strtoll(const char *, char **, int);
352 unsigned long long strtoull(const char *, char **, int);
353 #endif
354 #ifdef CONFIG_HAVE_DOUBLE
355 
362 double strtod(FAR const char *str, FAR char **endptr);
363 #endif
364 #ifdef CONFIG_HAVE_LONG_DOUBLE
365 
369 long double strtold(FAR const char *str, FAR char **endptr);
370 #endif
371 
374 float strtof(FAR const char *str, FAR char **endptr);
386 #define atoi(nptr) strtol((nptr), NULL, 10)
387 
394 #define atol(nptr) strtol((nptr), NULL, 10)
395 #ifdef CONFIG_HAVE_LONG_LONG
396 
403 #define atoll(nptr) strtoll((nptr), NULL, 10)
404 #endif
405 
412 #define atof(nptr) strtod((nptr), NULL)
413 
414 /* Binary to string conversions */
426 char *itoa(int value, char *str, int base);
427 
428 /* Wide character operations */
429 
430 #ifdef CONFIG_LIBC_WCHAR
431 
435 int mbtowc(FAR wchar_t *pwc, FAR const char *s, size_t n);
439 int wctomb(FAR char *s, wchar_t wchar);
443 #endif
444 
445 /* Memory Management */
453 FAR void *malloc(size_t);
461 void free(FAR void *);
469 FAR void *realloc(FAR void *, size_t);
480 FAR void *memalign(size_t, size_t);
490 FAR void *zalloc(size_t);
498 FAR void *calloc(size_t, size_t);
499 
500 /* Misc */
508 int abs(int j);
516 long int labs(long int j);
517 #ifdef CONFIG_HAVE_LONG_LONG
518 
525 long long int llabs(long long int j);
526 #endif
527 
528 #ifdef CONFIG_CAN_PASS_STRUCTS
529 
536 div_t div(int numer, int denom);
541 ldiv_t ldiv(long numer, long denom);
542 #ifdef CONFIG_HAVE_LONG_LONG
543 
546 lldiv_t lldiv(long long numer, long long denom);
547 #endif
548 #endif
549 
560 FAR char *mktemp(FAR char *path_template);
561 
569 int mkstemp(FAR char *path_template);
570 
571 /* Sorting */
579 void qsort(void *base, size_t nmemb, size_t size, int (*compar)(const void *, const void *));
580 
581 /* Binary search */
589 FAR void *bsearch(FAR const void *key, FAR const void *base, size_t nel, size_t width, CODE int (*compar)(FAR const void *, FAR const void *));
590 
591 #ifdef CONFIG_CAN_PASS_STRUCTS
592 
599 struct mallinfo mallinfo(void);
600 #else
601 
609 int mallinfo(struct mallinfo *info);
610 #endif
611 
612 #undef EXTERN
613 #if defined(__cplusplus)
614 }
615 #endif
616 
617 #endif /* __INCLUDE_STDLIB_H */
int arena
Definition: stdlib.h:115
FAR void * bsearch(FAR const void *key, FAR const void *base, size_t nel, size_t width, CODE int(*compar)(FAR const void *, FAR const void *))
binary search a sorted table
FAR void * calloc(size_t, size_t)
a memory allocator
int setenv(const char *name, const char *value, int overwrite)
add or change environment variable
struct mallinfo mallinfo(void)
returns a copy of updated current heap information for the user heap
int on_exit(CODE void(*func)(int, FAR void *), FAR void *arg)
register a function to be called at program exit
int fordblks
Definition: stdlib.h:121
unsigned long long strtoull(const char *, char **, int)
convert a string to an unsigned long
int quot
Definition: stdlib.h:128
int abs(int j)
return an integer absolute value
int rand(void)
pseudo-random number generator
int rem
Definition: stdlib.h:129
long quot
Definition: stdlib.h:137
void abort(void) noreturn_function
generate an abnormal process abort
void free(FAR void *)
free allocated memory
FAR char * get_environ_ptr(size_t *envsize)
Return a pointer to the thread specific environ variable.
int uordblks
Definition: stdlib.h:119
int putenv(FAR const char *string)
change or add a value to an environment
double strtod(FAR const char *str, FAR char **endptr)
convert a string to a double-precision number
void exit(int status) noreturn_function
terminate a process
FAR char * mktemp(FAR char *path_template)
generates a unique temporary filename from template.
unsigned long strtoul(const char *, char **, int)
convert a string to an unsigned long
FAR void * memalign(size_t, size_t)
allocates size bytes and returns a pointer to the allocated memory
Definition: stdlib.h:127
void srand(unsigned int seed)
initialize random number generator
int mkstemp(FAR char *path_template)
create a regular file with a unique name derived from template and return a file descriptor for the f...
long long strtoll(const char *, char **, int)
convert a string to a long integer
int atexit(CODE void(*func)(void))
register a function to run at process termination
long long int llabs(long long int j)
return a long integer absolute value
void qsort(void *base, size_t nmemb, size_t size, int(*compar)(const void *, const void *))
sort a table of data
FAR void * realloc(FAR void *, size_t)
memory reallocator
FAR void * malloc(size_t)
a memory allocator
int unsetenv(const char *name)
remove an environment variable
char * itoa(int value, char *str, int base)
convert integer to string
FAR void * zalloc(size_t)
Allocate and zero memory from the user heap.
Definition: stdlib.h:136
long rem
Definition: stdlib.h:147
div_t div(int numer, int denom)
compute the quotient and remainder of an integer division
int clearenv(void)
clears the environment of all name-value pairs and sets the value of the external variable environ to...
long random(void)
pseudo-random number generator
long int labs(long int j)
return a long integer absolute value
FAR char * getenv(FAR const char *name)
get value of an environment variable
structure of memory information
Definition: stdlib.h:114
int mxordblk
Definition: stdlib.h:118
long quot
Definition: stdlib.h:146
long strtol(const char *, char **, int)
convert a string to a long integer
int ordblks
Definition: stdlib.h:117
long rem
Definition: stdlib.h:138