2008-11-17 00:36:30 +08:00
|
|
|
/****************************************************************************
|
|
|
|
* include/stdlib.h
|
2007-02-18 07:21:28 +08:00
|
|
|
*
|
2020-04-12 01:17:55 +08:00
|
|
|
* Licensed to the Apache Software Foundation (ASF) under one or more
|
|
|
|
* contributor license agreements. See the NOTICE file distributed with
|
|
|
|
* this work for additional information regarding copyright ownership. The
|
|
|
|
* ASF licenses this file to you under the Apache License, Version 2.0 (the
|
|
|
|
* "License"); you may not use this file except in compliance with the
|
|
|
|
* License. You may obtain a copy of the License at
|
2007-02-18 07:21:28 +08:00
|
|
|
*
|
2020-04-12 01:17:55 +08:00
|
|
|
* http://www.apache.org/licenses/LICENSE-2.0
|
2007-02-18 07:21:28 +08:00
|
|
|
*
|
2020-04-12 01:17:55 +08:00
|
|
|
* Unless required by applicable law or agreed to in writing, software
|
|
|
|
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
|
|
|
|
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
|
|
|
|
* License for the specific language governing permissions and limitations
|
|
|
|
* under the License.
|
2007-02-18 07:21:28 +08:00
|
|
|
*
|
2008-11-17 00:36:30 +08:00
|
|
|
****************************************************************************/
|
2007-02-18 07:21:28 +08:00
|
|
|
|
2008-11-17 00:36:30 +08:00
|
|
|
#ifndef __INCLUDE_STDLIB_H
|
|
|
|
#define __INCLUDE_STDLIB_H
|
2007-02-18 07:21:28 +08:00
|
|
|
|
2008-11-17 00:36:30 +08:00
|
|
|
/****************************************************************************
|
2007-02-18 07:21:28 +08:00
|
|
|
* Included Files
|
2008-11-17 00:36:30 +08:00
|
|
|
****************************************************************************/
|
2007-02-18 07:21:28 +08:00
|
|
|
|
2007-07-01 01:05:44 +08:00
|
|
|
#include <nuttx/config.h>
|
2010-10-10 05:12:49 +08:00
|
|
|
#include <nuttx/compiler.h>
|
2016-10-19 09:11:09 +08:00
|
|
|
|
2007-02-18 07:21:28 +08:00
|
|
|
#include <sys/types.h>
|
2010-10-10 05:12:49 +08:00
|
|
|
#include <stdint.h>
|
2018-07-20 01:21:49 +08:00
|
|
|
#include <limits.h>
|
2007-02-18 07:21:28 +08:00
|
|
|
|
2008-11-17 00:36:30 +08:00
|
|
|
/****************************************************************************
|
2014-09-29 00:15:33 +08:00
|
|
|
* Pre-processor Definitions
|
2008-11-17 00:36:30 +08:00
|
|
|
****************************************************************************/
|
2007-02-18 07:21:28 +08:00
|
|
|
|
2015-08-04 01:01:41 +08:00
|
|
|
/* The C standard specifies two constants, EXIT_SUCCESS and EXIT_FAILURE,
|
|
|
|
* that may be passed to exit() to indicate successful or unsuccessful
|
|
|
|
* termination, respectively.
|
2007-02-18 07:21:28 +08:00
|
|
|
*/
|
|
|
|
|
|
|
|
#define EXIT_SUCCESS 0
|
|
|
|
#define EXIT_FAILURE 1
|
|
|
|
|
2011-10-31 01:28:54 +08:00
|
|
|
/* The NULL pointer should be defined in this file but is currently defined
|
|
|
|
* in sys/types.h.
|
|
|
|
*/
|
|
|
|
|
2018-07-20 01:21:49 +08:00
|
|
|
/* Maximum value returned by rand(). Must be a minimum of 32767. */
|
2011-10-31 01:28:54 +08:00
|
|
|
|
2018-07-20 01:21:49 +08:00
|
|
|
#define RAND_MAX INT_MAX
|
2011-10-31 01:28:54 +08:00
|
|
|
|
|
|
|
/* Integer expression whose value is the maximum number of bytes in a
|
2014-04-14 04:32:20 +08:00
|
|
|
* character specified by the current locale.
|
2011-10-31 01:28:54 +08:00
|
|
|
*/
|
|
|
|
|
2022-05-29 16:27:52 +08:00
|
|
|
#define MB_CUR_MAX 4
|
2011-10-31 01:28:54 +08:00
|
|
|
|
2015-08-04 01:01:41 +08:00
|
|
|
/* The environ variable, normally 'char **environ;' is not implemented as a
|
|
|
|
* function call. However, get_environ_ptr() can be used in its place.
|
2007-07-01 01:05:44 +08:00
|
|
|
*/
|
|
|
|
|
2022-04-21 15:25:49 +08:00
|
|
|
#ifdef CONFIG_DISABLE_ENVIRON
|
|
|
|
# define environ NULL
|
|
|
|
#else
|
|
|
|
# define environ get_environ_ptr()
|
|
|
|
#endif
|
2007-07-01 01:05:44 +08:00
|
|
|
|
2023-02-25 16:02:11 +08:00
|
|
|
#if defined(CONFIG_FS_LARGEFILE)
|
2021-07-21 11:46:21 +08:00
|
|
|
# define mkstemp64 mkstemp
|
|
|
|
# define mkostemp64 mkostemp
|
|
|
|
# define mkstemps64 mkstemps
|
|
|
|
# define mkostemps64 mkostemps
|
|
|
|
#endif
|
|
|
|
|
2021-07-28 22:44:44 +08:00
|
|
|
#define strtof_l(s, e, l) strtof(s, e)
|
|
|
|
#define strtod_l(s, e, l) strtod(s, e)
|
|
|
|
#define strtold_l(s, e, l) strtold(s, e)
|
|
|
|
#define strtoll_l(s, e, b, l) strtoll(s, e, b)
|
|
|
|
#define strtoull_l(s, e, b, l) strtoull(s, e, b)
|
|
|
|
|
2008-11-17 00:36:30 +08:00
|
|
|
/****************************************************************************
|
2015-08-04 01:01:41 +08:00
|
|
|
* Public Type Definitions
|
2008-11-17 00:36:30 +08:00
|
|
|
****************************************************************************/
|
2007-02-18 07:21:28 +08:00
|
|
|
|
2015-08-14 22:35:01 +08:00
|
|
|
/* Structure type returned by the div() function. */
|
|
|
|
|
|
|
|
struct div_s
|
|
|
|
{
|
|
|
|
int quot; /* Quotient */
|
|
|
|
int rem; /* Remainder */
|
|
|
|
};
|
|
|
|
|
|
|
|
typedef struct div_s div_t;
|
|
|
|
|
2015-08-14 22:45:59 +08:00
|
|
|
/* Structure type returned by the ldiv() function. */
|
|
|
|
|
|
|
|
struct ldiv_s
|
|
|
|
{
|
|
|
|
long quot; /* Quotient */
|
|
|
|
long rem; /* Remainder */
|
|
|
|
};
|
|
|
|
|
|
|
|
typedef struct ldiv_s ldiv_t;
|
|
|
|
|
|
|
|
/* Structure type returned by the lldiv() function. */
|
|
|
|
|
|
|
|
struct lldiv_s
|
|
|
|
{
|
|
|
|
long quot; /* Quotient */
|
|
|
|
long rem; /* Remainder */
|
|
|
|
};
|
|
|
|
|
|
|
|
typedef struct lldiv_s lldiv_t;
|
|
|
|
|
2008-11-17 00:36:30 +08:00
|
|
|
/****************************************************************************
|
2015-08-04 01:01:41 +08:00
|
|
|
* Public Function Prototypes
|
2008-11-17 00:36:30 +08:00
|
|
|
****************************************************************************/
|
2007-02-18 07:21:28 +08:00
|
|
|
|
|
|
|
#undef EXTERN
|
|
|
|
#if defined(__cplusplus)
|
|
|
|
#define EXTERN extern "C"
|
2013-03-09 04:36:18 +08:00
|
|
|
extern "C"
|
|
|
|
{
|
2007-02-18 07:21:28 +08:00
|
|
|
#else
|
|
|
|
#define EXTERN extern
|
|
|
|
#endif
|
|
|
|
|
|
|
|
/* Random number generation */
|
2007-02-28 05:17:21 +08:00
|
|
|
|
2013-03-09 04:36:18 +08:00
|
|
|
void srand(unsigned int seed);
|
|
|
|
int rand(void);
|
2023-06-28 12:29:03 +08:00
|
|
|
int rand_r(FAR unsigned int *seedp);
|
2021-12-21 22:48:02 +08:00
|
|
|
void lcong48(FAR unsigned short int param[7]);
|
|
|
|
FAR unsigned short int *seed48(FAR unsigned short int seed16v[3]);
|
|
|
|
void srand48(long int seedval);
|
|
|
|
#ifdef CONFIG_HAVE_LONG_LONG
|
|
|
|
long int jrand48(FAR unsigned short int xsubi[3]);
|
|
|
|
long int lrand48(void);
|
|
|
|
long int mrand48(void);
|
|
|
|
long int nrand48(FAR unsigned short int xsubi[3]);
|
|
|
|
# ifdef CONFIG_HAVE_DOUBLE
|
|
|
|
double drand48(void);
|
|
|
|
double erand48(FAR unsigned short int xsubi[3]);
|
|
|
|
# endif
|
|
|
|
#endif
|
2007-02-18 07:21:28 +08:00
|
|
|
|
2018-07-20 01:21:49 +08:00
|
|
|
#define srandom(s) srand(s)
|
|
|
|
long random(void);
|
|
|
|
|
2020-12-12 14:16:07 +08:00
|
|
|
#ifdef CONFIG_CRYPTO_RANDOM_POOL
|
|
|
|
void arc4random_buf(FAR void *bytes, size_t nbytes);
|
2022-11-09 11:41:22 +08:00
|
|
|
uint32_t arc4random(void);
|
2020-12-12 14:16:07 +08:00
|
|
|
#endif
|
|
|
|
|
2007-02-18 07:21:28 +08:00
|
|
|
/* Environment variable support */
|
2007-02-28 05:17:21 +08:00
|
|
|
|
2015-11-19 03:22:43 +08:00
|
|
|
FAR char **get_environ_ptr(void);
|
2013-03-09 04:36:18 +08:00
|
|
|
FAR char *getenv(FAR const char *name);
|
|
|
|
int putenv(FAR const char *string);
|
|
|
|
int clearenv(void);
|
2015-11-19 03:22:43 +08:00
|
|
|
int setenv(FAR const char *name, FAR const char *value, int overwrite);
|
|
|
|
int unsetenv(FAR const char *name);
|
2007-02-18 07:21:28 +08:00
|
|
|
|
|
|
|
/* Process exit functions */
|
2007-02-28 05:17:21 +08:00
|
|
|
|
2013-03-09 04:36:18 +08:00
|
|
|
void exit(int status) noreturn_function;
|
2023-01-21 00:23:16 +08:00
|
|
|
void quick_exit(int status) noreturn_function;
|
2013-03-09 04:36:18 +08:00
|
|
|
void abort(void) noreturn_function;
|
|
|
|
int atexit(CODE void (*func)(void));
|
2023-01-21 00:23:16 +08:00
|
|
|
int at_quick_exit(CODE void (*func)(void));
|
2013-03-09 04:36:18 +08:00
|
|
|
int on_exit(CODE void (*func)(int, FAR void *), FAR void *arg);
|
2007-02-18 07:21:28 +08:00
|
|
|
|
2013-04-19 06:13:38 +08:00
|
|
|
/* _Exit() is a stdlib.h equivalent to the unistd.h _exit() function */
|
|
|
|
|
2020-06-26 14:40:13 +08:00
|
|
|
void _Exit(int status) noreturn_function;
|
2013-04-19 06:13:38 +08:00
|
|
|
|
2016-08-26 01:28:09 +08:00
|
|
|
/* System() command is not implemented in the NuttX libc because it is so
|
|
|
|
* entangled with shell logic. There is an experimental version at
|
|
|
|
* apps/system/system. system() is prototyped here, however, for
|
2016-08-26 02:36:19 +08:00
|
|
|
* standards compatibility.
|
2016-08-26 01:28:09 +08:00
|
|
|
*/
|
|
|
|
|
2016-08-26 02:36:19 +08:00
|
|
|
#ifndef __KERNEL__
|
2018-08-23 20:40:59 +08:00
|
|
|
int system(FAR const char *cmd);
|
2016-08-26 02:36:19 +08:00
|
|
|
#endif
|
2016-08-26 01:28:09 +08:00
|
|
|
|
2020-06-22 20:43:21 +08:00
|
|
|
FAR char *realpath(FAR const char *path, FAR char *resolved);
|
|
|
|
|
2007-02-18 07:21:28 +08:00
|
|
|
/* String to binary conversions */
|
2007-02-28 05:17:21 +08:00
|
|
|
|
2015-11-19 03:22:43 +08:00
|
|
|
long strtol(FAR const char *nptr, FAR char **endptr, int base);
|
|
|
|
unsigned long strtoul(FAR const char *nptr, FAR char **endptr, int base);
|
2009-06-14 23:36:18 +08:00
|
|
|
#ifdef CONFIG_HAVE_LONG_LONG
|
2015-11-19 03:22:43 +08:00
|
|
|
long long strtoll(FAR const char *nptr, FAR char **endptr, int base);
|
|
|
|
unsigned long long strtoull(FAR const char *nptr, FAR char **endptr,
|
|
|
|
int base);
|
2009-06-14 23:36:18 +08:00
|
|
|
#endif
|
2016-10-21 02:07:12 +08:00
|
|
|
float strtof(FAR const char *str, FAR char **endptr);
|
2016-10-23 03:02:55 +08:00
|
|
|
#ifdef CONFIG_HAVE_DOUBLE
|
|
|
|
double strtod(FAR const char *str, FAR char **endptr);
|
|
|
|
#endif
|
|
|
|
#ifdef CONFIG_HAVE_LONG_DOUBLE
|
|
|
|
long double strtold(FAR const char *str, FAR char **endptr);
|
|
|
|
#endif
|
2009-06-14 23:36:18 +08:00
|
|
|
|
2020-08-29 11:03:19 +08:00
|
|
|
int atoi(FAR const char *nptr);
|
|
|
|
long atol(FAR const char *nptr);
|
2009-06-14 23:36:18 +08:00
|
|
|
#ifdef CONFIG_HAVE_LONG_LONG
|
2020-06-26 14:40:13 +08:00
|
|
|
long long atoll(FAR const char *nptr);
|
2020-06-01 14:19:26 +08:00
|
|
|
#endif
|
2017-02-10 23:32:55 +08:00
|
|
|
#ifdef CONFIG_HAVE_DOUBLE
|
2020-08-29 11:03:19 +08:00
|
|
|
double atof(FAR const char *nptr);
|
2017-02-10 23:32:55 +08:00
|
|
|
#endif
|
2007-02-18 07:21:28 +08:00
|
|
|
|
2013-03-15 02:30:06 +08:00
|
|
|
/* Binary to string conversions */
|
|
|
|
|
2015-11-19 03:22:43 +08:00
|
|
|
FAR char *itoa(int val, FAR char *str, int base);
|
2013-03-15 02:30:06 +08:00
|
|
|
|
2016-10-19 09:11:09 +08:00
|
|
|
/* Wide character operations */
|
|
|
|
|
2020-06-02 13:54:30 +08:00
|
|
|
int mblen(FAR const char *s, size_t n);
|
2016-10-19 09:11:09 +08:00
|
|
|
int mbtowc(FAR wchar_t *pwc, FAR const char *s, size_t n);
|
2020-06-02 13:54:30 +08:00
|
|
|
size_t mbstowcs(FAR wchar_t *dst, FAR const char *src, size_t len);
|
2016-10-19 09:11:09 +08:00
|
|
|
int wctomb(FAR char *s, wchar_t wchar);
|
2020-06-02 13:54:30 +08:00
|
|
|
size_t wcstombs(FAR char *dst, FAR const wchar_t *src, size_t len);
|
2016-10-19 09:11:09 +08:00
|
|
|
|
2007-02-18 07:21:28 +08:00
|
|
|
/* Memory Management */
|
2007-02-28 05:17:21 +08:00
|
|
|
|
2022-12-12 16:03:43 +08:00
|
|
|
FAR void *malloc(size_t) malloc_like1(1);
|
|
|
|
FAR void *valloc(size_t) malloc_like1(1);
|
2015-11-19 03:22:43 +08:00
|
|
|
void free(FAR void *);
|
2022-12-12 16:03:43 +08:00
|
|
|
FAR void *realloc(FAR void *, size_t) realloc_like(2);
|
2024-04-15 21:47:41 +08:00
|
|
|
FAR void *reallocarray(FAR void *, size_t, size_t) realloc_like2(2, 3);
|
2022-12-12 16:03:43 +08:00
|
|
|
FAR void *memalign(size_t, size_t) malloc_like1(2);
|
|
|
|
FAR void *zalloc(size_t) malloc_like1(1);
|
|
|
|
FAR void *calloc(size_t, size_t) malloc_like2(1, 2);
|
|
|
|
FAR void *aligned_alloc(size_t, size_t) malloc_like1(2);
|
2020-08-29 11:03:19 +08:00
|
|
|
int posix_memalign(FAR void **, size_t, size_t);
|
2020-06-01 14:21:15 +08:00
|
|
|
|
2016-07-15 22:33:47 +08:00
|
|
|
/* Pseudo-Terminals */
|
|
|
|
|
2021-05-07 02:34:12 +08:00
|
|
|
#ifdef CONFIG_PSEUDOTERM
|
2022-02-03 01:42:18 +08:00
|
|
|
int posix_openpt(int oflag);
|
2016-07-15 22:33:47 +08:00
|
|
|
FAR char *ptsname(int fd);
|
2020-08-29 11:03:19 +08:00
|
|
|
int ptsname_r(int fd, FAR char *buf, size_t buflen);
|
|
|
|
int unlockpt(int fd);
|
2016-07-15 23:39:33 +08:00
|
|
|
|
|
|
|
/* int grantpt(int fd); Not implemented */
|
|
|
|
|
|
|
|
#define grantpt(fd) (0)
|
2016-07-15 22:33:47 +08:00
|
|
|
#endif
|
|
|
|
|
2015-08-14 22:35:01 +08:00
|
|
|
/* Arithmetic */
|
2010-10-10 04:46:14 +08:00
|
|
|
|
2020-08-29 11:03:19 +08:00
|
|
|
int abs(int j);
|
|
|
|
long int labs(long int j);
|
2015-08-15 02:04:57 +08:00
|
|
|
#ifdef CONFIG_HAVE_LONG_LONG
|
|
|
|
long long int llabs(long long int j);
|
|
|
|
#endif
|
|
|
|
|
2020-08-29 11:03:19 +08:00
|
|
|
div_t div(int number, int denom);
|
|
|
|
ldiv_t ldiv(long number, long denom);
|
2015-08-14 22:45:59 +08:00
|
|
|
#ifdef CONFIG_HAVE_LONG_LONG
|
2020-08-29 11:03:19 +08:00
|
|
|
lldiv_t lldiv(long long number, long long denom);
|
2015-08-14 22:45:59 +08:00
|
|
|
#endif
|
2015-08-14 22:35:01 +08:00
|
|
|
|
|
|
|
/* Temporary files */
|
|
|
|
|
2020-09-17 16:03:38 +08:00
|
|
|
FAR char *mktemp(FAR char *path_template);
|
2020-08-29 11:03:19 +08:00
|
|
|
int mkstemp(FAR char *path_template);
|
2020-08-29 12:18:13 +08:00
|
|
|
FAR char *mkdtemp(FAR char *path_template);
|
2010-10-10 04:46:14 +08:00
|
|
|
|
2009-06-21 02:22:47 +08:00
|
|
|
/* Sorting */
|
|
|
|
|
2020-08-29 11:03:19 +08:00
|
|
|
void qsort(FAR void *base, size_t nel, size_t width,
|
|
|
|
CODE int (*compar)(FAR const void *, FAR const void *));
|
2009-06-21 02:22:47 +08:00
|
|
|
|
2015-10-03 02:33:58 +08:00
|
|
|
/* Binary search */
|
|
|
|
|
2020-08-29 11:03:19 +08:00
|
|
|
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 *));
|
2007-02-18 07:21:28 +08:00
|
|
|
|
2021-11-29 12:46:00 +08:00
|
|
|
/* Current program name manipulation */
|
|
|
|
|
|
|
|
FAR const char *getprogname(void);
|
|
|
|
|
2023-03-21 14:36:42 +08:00
|
|
|
/* Registers a destructor function to be called by exit() */
|
|
|
|
|
|
|
|
int __cxa_atexit(CODE void (*func)(FAR void *), FAR void *arg,
|
|
|
|
FAR void *dso_handle);
|
|
|
|
|
2023-05-23 06:46:19 +08:00
|
|
|
#if CONFIG_FORTIFY_SOURCE > 0
|
|
|
|
fortify_function(realpath) FAR char *realpath(FAR const char *path,
|
|
|
|
FAR char *resolved)
|
|
|
|
{
|
|
|
|
FAR char *ret = __real_realpath(path, resolved);
|
|
|
|
if (ret != NULL && resolved != NULL)
|
|
|
|
{
|
|
|
|
size_t len = 1;
|
|
|
|
FAR char *p;
|
|
|
|
|
|
|
|
p = ret;
|
|
|
|
while (*p++ != '\0')
|
|
|
|
{
|
|
|
|
len++;
|
|
|
|
}
|
|
|
|
|
|
|
|
fortify_assert(len <= fortify_size(resolved, 0));
|
|
|
|
}
|
|
|
|
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2007-02-18 07:21:28 +08:00
|
|
|
#undef EXTERN
|
|
|
|
#if defined(__cplusplus)
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2008-11-17 00:36:30 +08:00
|
|
|
#endif /* __INCLUDE_STDLIB_H */
|