Embedded Artistry libc
C Standard Library Support for Bare-metal Systems
stdlib.h File Reference
#include <stddef.h>
#include <stdint.h>
#include <wctype.h>
Include dependency graph for stdlib.h:
This graph shows which files directly or indirectly include this file:

Go to the source code of this file.

Classes

struct  div_t
 Division type for integers. More...
 
struct  ldiv_t
 Division type for long integers. More...
 
struct  lldiv_t
 Division type for long long integers. More...
 
struct  imaxdiv_t
 Division type for maximal integer storage. More...
 

Macros

#define EXIT_FAILURE   1
 
#define EXIT_SUCCESS   0
 
#define RAND_MAX   (0x7fffffff)
 
#define MB_CUR_MAX   sizeof(wchar_t)
 

Functions

void abort (void) __attribute__((noreturn))
 
int atexit (void(*)(void))
 
void exit (int) __attribute__((noreturn))
 
void _Exit (int) __attribute__((noreturn))
 
int at_quick_exit (void(*)(void))
 
void quick_exit (int) __attribute__((noreturn))
 
int cxa_atexit (void(*)(void *), void *, void *)
 
char * getenv (const char *)
 
int system (const char *)
 
int mblen (const char *, size_t)
 
int mbtowc (wchar_t *__restrict, const char *__restrict, size_t)
 
int wctomb (char *, wchar_t)
 
size_t mbstowcs (wchar_t *__restrict, const char *__restrict, size_t)
 
size_t wcstombs (char *__restrict, const wchar_t *__restrict, size_t)
 
int atoi (const char *str)
 Interprets an integer value in a byte string pointed to by str. More...
 
long atol (const char *str)
 Interprets a Long value in a byte string pointed to by str. More...
 
long long atoll (const char *str)
 Interprets a Long Long value in a byte string pointed to by str. More...
 
double atof (const char *str)
 Interprets a floating-point value in a byte string pointed to by str. More...
 
float strtof (const char *__restrict str, char **__restrict str_end)
 Interprets a floating-point value in a byte string pointed to by str. More...
 
double strtod (const char *__restrict str, char **__restrict str_end)
 Interprets a floating-point value in a byte string pointed to by str. More...
 
long strtol (const char *__restrict str, char **__restrict str_end, int base)
 Interprets an long value in a byte string pointed to by str. More...
 
unsigned long strtoul (const char *__restrict str, char **__restrict str_end, int base)
 Interprets an unsigned long value in a byte string pointed to by str. More...
 
long long strtoll (const char *__restrict str, char **__restrict str_end, int base)
 Interprets an long long value in a byte string pointed to by str. More...
 
unsigned long long strtoull (const char *__restrict str, char **__restrict str_end, int base)
 Interprets an unsigned long long value in a byte string pointed to by str. More...
 
long double strtold (const char *__restrict, char **__restrict)
 
int abs (int n)
 Computes the absolute value of an integer number. More...
 
long labs (long n)
 Computes the absolute value of an long number. More...
 
long long llabs (long long n)
 Computes the absolute value of an long long number. More...
 
intmax_t imaxabs (intmax_t j)
 Computes the absolute value of an intmax_t number. More...
 
div_t div (int x, int y)
 Computes both the quotient and the remainder of the division of the numerator x by the denominator y. More...
 
ldiv_t ldiv (long x, long y)
 Computes both the quotient and the remainder of the division of the numerator x by the denominator y. More...
 
lldiv_t lldiv (long long x, long long y)
 Computes both the quotient and the remainder of the division of the numerator x by the denominator y. More...
 
imaxdiv_t imaxdiv (intmax_t numer, intmax_t denom)
 Computes both the quotient and the remainder of the division of the numerator x by the denominator y. More...
 
int rand_r (unsigned int *ctx)
 
int rand (void)
 Returns a pseudo-random integer value between ​0​ and. More...
 
void srand (unsigned seed)
 Seeds the pseudo-random number generator used by. More...
 
int heapsort (void *vbase, size_t nmemb, size_t size, int(*compar)(const void *, const void *))
 Sorts the given array pointed to by vbase in ascending order. More...
 
int heapsort_r (void *vbase, size_t nmemb, size_t size, void *thunk, int(*compar)(void *, const void *, const void *))
 Sorts the given array pointed to by vbase in ascending order. More...
 
void * bsearch (const void *key, const void *ptr, size_t count, size_t size, int(*comp)(const void *, const void *))
 Finds an element equal to element pointed to by key in an array pointed to by ptr. More...
 
void qsort_r (void *a, size_t n, size_t es, void *thunk, int(*cmp)(void *, const void *, const void *))
 Sorts the given array pointed to by ptr in ascending order. More...
 
void qsort (void *a, size_t n, size_t es, int(*compar)(const void *, const void *))
 Sorts the given array pointed to by ptr in ascending order. More...
 
void * malloc (size_t size)
 Allocates size bytes of uninitialized storage. More...
 
void free (void *ptr)
 Deallocates allocated memory space. More...
 
void * calloc (size_t num, size_t size)
 Allocates memory for an array of given number objects of size and initializes all bytes in the allocated storage to zero. More...
 
void * realloc (void *ptr, size_t size)
 Reallocates the given area of memory. More...
 
void * reallocf (void *ptr, size_t size)
 Reallocates the given area of memory. More...
 

Class Documentation

◆ div_t

struct div_t

Division type for integers.

Definition at line 15 of file stdlib.h.

Collaboration diagram for div_t:
Collaboration graph
Class Members
int quot

The quotient

int rem

The remainder

◆ ldiv_t

struct ldiv_t

Division type for long integers.

Definition at line 22 of file stdlib.h.

Collaboration diagram for ldiv_t:
Collaboration graph
Class Members
long quot

The quotient

long rem

The remainder

◆ lldiv_t

struct lldiv_t

Division type for long long integers.

Definition at line 29 of file stdlib.h.

Collaboration diagram for lldiv_t:
Collaboration graph
Class Members
long long quot

The quotient

long long rem

The remainder

◆ imaxdiv_t

struct imaxdiv_t

Division type for maximal integer storage.

Definition at line 36 of file stdlib.h.

Collaboration diagram for imaxdiv_t:
Collaboration graph
Class Members
intmax_t quot

The quotient

intmax_t rem

The remainder

Macro Definition Documentation

◆ EXIT_FAILURE

#define EXIT_FAILURE   1

Expands to 1. Indicates program execution execution status.

Definition at line 43 of file stdlib.h.

◆ EXIT_SUCCESS

#define EXIT_SUCCESS   0

Expands to 0. indicates program execution execution status.

Definition at line 46 of file stdlib.h.

◆ MB_CUR_MAX

#define MB_CUR_MAX   sizeof(wchar_t)

Length of the current locale's multi-byte character. We are faking this for now, since we don't provide locale support.

Definition at line 55 of file stdlib.h.

◆ RAND_MAX

#define RAND_MAX   (0x7fffffff)

Expands to an integer constant expression equal to the maximum value returned by the function

See also
rand() It's guaranteed that this value is at least 32767

Definition at line 51 of file stdlib.h.

Function Documentation

◆ _Exit()

void _Exit ( int  )

TODO: CLEANUP DOC Terminates the process normally by returning control to the host environment, but without performing any of the regular cleanup tasks for terminating processes (as function exit does). No object destructors, nor functions registered by atexit or at_quick_exit are called

Referenced by __attribute__().

◆ abort()

void abort ( void  )

Referenced by __attribute__().

◆ abs()

int abs ( int  n)

Computes the absolute value of an integer number.

Computes the absolute value of an integer number. The behavior is undefined if the result cannot be represented by the return type.

Parameters
ninteger value
Returns
The absolute value of n (i.e. |n|), if it is representable.

Definition at line 32 of file abs.c.

33 {
34  return (j < 0 ? -j : j);
35 }

◆ at_quick_exit()

int at_quick_exit ( void(*)(void)  )

◆ atexit()

int atexit ( void(*)(void)  )

◆ atof()

double atof ( const char *  str)

Interprets a floating-point value in a byte string pointed to by str.

Interprets a floating-point value in a byte string pointed to by str. Function discards any whitespace characters (as determined by

See also
isspace() until first non-whitespace character is found. Then it takes as many characters as possible to form a valid floating-point representation and converts them to a floating-point value. The valid floating-point value can be one of the following: 1) decimal floating-point expression. It consists of the following parts: a) (optional) plus or minus sign b) nonempty sequence of decimal digits optionally containing decimal-point character c) (optional) e or E followed with optional minus or plus sign and nonempty sequence of decimal digits (defines exponent)
Parameters
strpointer to the null-terminated byte string to be interpreted
Returns
double value corresponding to the contents of str on success. If the converted value falls out of range of corresponding return type, the return value is undefined. If no conversion can be performed, ​0​.0 is returned.

Definition at line 57 of file atof.c.

58 {
59  return (strtod(ascii, NULL));
60 }
#define NULL
Definition: stddef.h:15
double strtod(const char *__restrict str, char **__restrict str_end)
Interprets a floating-point value in a byte string pointed to by str.

References NULL, and strtod().

◆ atoi()

int atoi ( const char *  str)

Interprets an integer value in a byte string pointed to by str.

Interprets an integer value in a byte string pointed to by str. Discards any whitespace characters until the first non-whitespace character is found, then takes as many characters as possible to form a valid integer number representation and converts them to an integer value. The valid integer value consists of the following parts: a) (optional) plus or minus sign b) numeric digits

Parameters
strpointer to the null-terminated byte string to be interpreted
Returns
Integer value corresponding to the contents of str on success. If the converted value falls out of range of corresponding return type, the return value is undefined. If no conversion can be performed, ​0​ is returned.

Definition at line 5 of file atoi.c.

6 {
7  bool neg = false;
8  int val = 0;
9 
10  switch(*str)
11  {
12  case '-':
13  neg = true;
14  // intentional fallthrough to advance str
15  case '+':
16  str++;
17  }
18 
19  while(isdigit(*str))
20  {
21  val = (10 * val) + (*str++ - '0');
22  }
23 
24  return (neg ? -val : val);
25 }
int isdigit(int ch)
Checks if the given character is a numeric character.
Definition: isdigit.c:5

References isdigit().

◆ atol()

long atol ( const char *  str)

Interprets a Long value in a byte string pointed to by str.

Interprets a Long value in a byte string pointed to by str. Discards any whitespace characters until the first non-whitespace character is found, then takes as many characters as possible to form a valid long number representation and converts them to an long value. The valid Long value consists of the following parts: a) (optional) plus or minus sign b) numeric digits

Parameters
strpointer to the null-terminated byte string to be interpreted
Returns
long value corresponding to the contents of str on success. If the converted value falls out of range of corresponding return type, the return value is undefined. If no conversion can be performed, ​0​ is returned.

Definition at line 5 of file atol.c.

6 {
7  long val = 0;
8  bool neg = false;
9 
10  while(isspace(*str))
11  {
12  str++;
13  }
14 
15  switch(*str)
16  {
17  case '-':
18  neg = true;
19  // intentional fallthrough
20  case '+':
21  str++;
22  }
23 
24  while(isdigit(*str))
25  {
26  val = (10 * val) + (*str++ - '0');
27  }
28 
29  return neg ? -val : val;
30 }
int isspace(int ch)
Checks if the given character is a whitespace character.
Definition: isspace.c:5
int isdigit(int ch)
Checks if the given character is a numeric character.
Definition: isdigit.c:5

References isdigit(), and isspace().

◆ atoll()

long long atoll ( const char *  str)

Interprets a Long Long value in a byte string pointed to by str.

Interprets a Long Long value in a byte string pointed to by str. Discards any whitespace characters until the first non-whitespace character is found, then takes as many characters as possible to form a valid long number representation and converts them to an long long value. The valid Long long value consists of the following parts: a) (optional) plus or minus sign b) numeric digits

Parameters
strpointer to the null-terminated byte string to be interpreted
Returns
long value corresponding to the contents of str on success. If the converted value falls out of range of corresponding return type, the return value is undefined. If no conversion can be performed, ​0​ is returned.

Definition at line 5 of file atoll.c.

6 {
7  long long val = 0;
8  bool neg = false;
9 
10  while(isspace(*str))
11  {
12  str++;
13  }
14 
15  switch(*str)
16  {
17  case '-':
18  neg = true;
19  // Intentional fallthrough
20  case '+':
21  str++;
22  }
23 
24  while(isdigit(*str))
25  {
26  val = (10 * val) + (*str++ - '0');
27  }
28  return neg ? -val : val;
29 }
int isspace(int ch)
Checks if the given character is a whitespace character.
Definition: isspace.c:5
int isdigit(int ch)
Checks if the given character is a numeric character.
Definition: isdigit.c:5

References isdigit(), and isspace().

◆ bsearch()

void* bsearch ( const void *  key,
const void *  ptr,
size_t  count,
size_t  size,
int(*)(const void *, const void *)  comp 
)

Finds an element equal to element pointed to by key in an array pointed to by ptr.

Parameters
keypointer to the element to search for
ptrpointer to the array to examine
countnumber of element in the array
sizesize of each element in the array in bytes
compcomparison function which returns ​a negative integer value if the first argument is less than the second,a positive integer value if the first argument is greater than the second and zero if the arguments are equal. key is passed as the first argument, an element from the array as the second. The signature of the comparison function should be equivalent to the following: int cmp(const void *a, const void *b); The function must not modify the objects passed to it and must return consistent results when called for the same objects, regardless of their positions in the array.
Returns
Pointer to an element in the array that compares equal to *key, or null pointer if such element has not been found.

◆ calloc()

void* calloc ( size_t  num,
size_t  size 
)

Allocates memory for an array of given number objects of size and initializes all bytes in the allocated storage to zero.

Allocates memory for an array of num objects of size size and initializes all bytes in the allocated storage to zero.

Parameters
numnumber of objects
sizesize of the array
Returns
(None)

Definition at line 11 of file calloc.c.

12 {
13  /* num * size unsigned integer wrapping check */
14  if((num >= MUL_NO_OVERFLOW || size >= MUL_NO_OVERFLOW) && num > 0 && SIZE_MAX / num < size)
15  {
16  return NULL;
17  }
18 
19  size_t total_size = num * size;
20  void* ptr = malloc(total_size);
21 
22  if(ptr)
23  {
24  memset(ptr, 0, total_size);
25  }
26 
27  return ptr;
28 }
#define MUL_NO_OVERFLOW
Definition: calloc.c:9
#define NULL
Definition: stddef.h:15
void * memset(void *dest, int c, size_t n)
Copies the value c into each of the first n characters of the object pointed to by dest.
#define SIZE_MAX
Definition: stdint.h:279
void * malloc(size_t size)
Allocates size bytes of uninitialized storage.

References malloc(), memset(), MUL_NO_OVERFLOW, NULL, and SIZE_MAX.

◆ cxa_atexit()

int cxa_atexit ( void(*)(void *)  ,
void *  ,
void *   
)

◆ div()

div_t div ( int  x,
int  y 
)

Computes both the quotient and the remainder of the division of the numerator x by the denominator y.

Computes both the quotient and the remainder of the division of the numerator x by the denominator y.

Parameters
xinteger values
yinteger values
Returns
If both the remainder and the quotient can be represented as objects of the corresponding type (int, long, long long, imaxdiv_t, respectively), returns both as an object of type
See also
div_t,
ldiv_t,
lldiv_t,
imaxdiv_t.

If either the remainder or the quotient cannot be represented, the behavior is undefined.

Definition at line 35 of file div.c.

36 {
37  div_t r;
38 
39  r.quot = num / denom;
40  r.rem = num % denom;
41  /*
42  * The ANSI standard says that |r.quot| <= |n/d|, where
43  * n/d is to be computed in infinite precision. In other
44  * words, we should always truncate the quotient towards
45  * 0, never -infinity.
46  *
47  * Machine division and remainer may work either way when
48  * one or both of n or d is negative. If only one is
49  * negative and r.quot has been truncated towards -inf,
50  * r.rem will have the same sign as denom and the opposite
51  * sign of num; if both are negative and r.quot has been
52  * truncated towards -inf, r.rem will be positive (will
53  * have the opposite sign of num). These are considered
54  * `wrong'.
55  *
56  * If both are num and denom are positive, r will always
57  * be positive.
58  *
59  * This all boils down to:
60  * if num >= 0, but r.rem < 0, we got the wrong answer.
61  * In that case, to get the right answer, add 1 to r.quot and
62  * subtract denom from r.rem.
63  */
64  if(num >= 0 && r.rem < 0)
65  {
66  r.quot++;
67  r.rem -= denom;
68  }
69  return (r);
70 }
int quot
Definition: stdlib.h:17
int rem
Definition: stdlib.h:18
Division type for integers.
Definition: stdlib.h:15

References div_t::quot, and div_t::rem.

◆ exit()

void exit ( int  )

◆ free()

void free ( void *  ptr)

Deallocates allocated memory space.

Deallocates the space previously allocated by

See also
malloc,
calloc,
realloc. If ptr is a null pointer, the function does nothing.

The behavior is undefined if the value of ptr does not equal a value returned earlier by

See also
malloc,
calloc,
realloc.

The behavior is undefined if the memory area referred to by ptr has already been deallocated, that is, free() or realloc has already been called with ptr as the argument and no calls to malloc, calloc or realloc resulted in a pointer equal to ptr afterwards.

The behavior is undefined if after free returns, an access is made through the pointer ptr (unless another allocation function happened to result in a pointer value equal to ptr)

Parameters
ptrpointer to the memory to deallocate

Referenced by heapsort(), heapsort_r(), realloc(), and reallocf().

◆ getenv()

char* getenv ( const char *  )

◆ heapsort()

int heapsort ( void *  vbase,
size_t  nmemb,
size_t  size,
int(*)(const void *, const void *)  compar 
)

Sorts the given array pointed to by vbase in ascending order.

The heapsort() function is a modified selection sort. It sorts an array of nmemb objects, the initial member of which is pointed to by vbase. The size of each object is specified by size.

The contents of the array base are sorted in ascending order according to a comparison function pointed to by compar, which requires two arguments pointing to the objects being compared.

Parameters
vbasea pointer to the array to sort
nmembthe number of objects to sort
sizethe size of each object in the array
comparcomparison function which returns ​a negative integer value if the first argument is less than the second,a positive integer value if the first argument is greater than the second and zero if the arguments are equal. key is passed as the first argument, an element from the array as the second. The signature of the comparison function should be equivalent to the following: int cmp(const void *a, const void *b);
Returns
the value 0 if successful; otherwise the value -1 is returned and the global variable errno is set to indicate the error.

Referenced by _qsort().

◆ heapsort_r()

int heapsort_r ( void *  vbase,
size_t  nmemb,
size_t  size,
void *  thunk,
int(*)(void *, const void *, const void *)  compar 
)

Sorts the given array pointed to by vbase in ascending order.

It sorts an array of nmemb objects, the initial member of which is pointed to by vbase. The size of each object is specified by size. The heapsort_r() function behaves identically to

See also
heapsort(), except that it takes an additional argument, thunk, which is passed unchanged as the first argument to function pointed to compar. This allows the comparison function to access additional data without using global variables, and thus heapsort_r() is suitable for use in functions which must be reentrant. And is therefore reentrant and safe to use in threads.

The contents of the array base are sorted in ascending order according to a comparison function pointed to by compar, which requires two arguments pointing to the objects being compared.

Parameters
vbasea pointer to the array to sort
nmembthe number of objects to sort
sizethe size of each object in the array
thunkadditional data(variable) for compar
comparcomparison function which returns ​a negative integer value if the first argument is less than the second,a positive integer value if the first argument is greater than the second and zero if the arguments are equal. key is passed as the first argument, an element from the array as the second. The signature of the comparison function should be equivalent to the following: int cmp(const void *a, const void *b);
Returns
the value 0 if successful; otherwise the value -1 is returned and the global variable errno is set to indicate the error.

Referenced by _qsort().

◆ imaxabs()

intmax_t imaxabs ( intmax_t  j)

Computes the absolute value of an intmax_t number.

Computes the absolute value of an intmax_t number. The behavior is undefined if the result cannot be represented by the return type.

Parameters
jvalue
Returns
The absolute value of n (i.e. |n|), if it is representable.

Definition at line 29 of file imaxabs.c.

30 {
31  return (j < 0 ? -j : j);
32 }

◆ imaxdiv()

imaxdiv_t imaxdiv ( intmax_t  numer,
intmax_t  denom 
)

Computes both the quotient and the remainder of the division of the numerator x by the denominator y.

Computes both the quotient and the remainder of the division of the numerator x by the denominator y. Computes quotient and remainder simultaneously. The quotient is the algebraic quotient with any fractional part discarded (truncated towards zero). The remainder is such that quot * y + rem == x.

Computes the quotient (the result of the expression x/y) and remainder (the result of the expression xy) simultaneously.

Parameters
numerintmax_t values (numerator)
denomintmax_t values (denominator)
Returns
If both the remainder and the quotient can be represented as objects of the corresponding type (int, long, long long, imaxdiv_t, respectively), returns both as an object of type
See also
div_t,
ldiv_t,
lldiv_t,
imaxdiv_t.

If either the remainder or the quotient cannot be represented, the behavior is undefined.

Definition at line 30 of file imaxdiv.c.

31 {
32  imaxdiv_t retval;
33 
34  retval.quot = numer / denom;
35  retval.rem = numer % denom;
36  if(numer >= 0 && retval.rem < 0)
37  {
38  retval.quot++;
39  retval.rem -= denom;
40  }
41  return (retval);
42 }
intmax_t rem
Definition: stdlib.h:39
intmax_t quot
Definition: stdlib.h:38
Division type for maximal integer storage.
Definition: stdlib.h:36

References imaxdiv_t::quot, and imaxdiv_t::rem.

◆ labs()

long labs ( long  n)

Computes the absolute value of an long number.

Computes the absolute value of an long number. The behavior is undefined if the result cannot be represented by the return type.

Parameters
nlong value
Returns
The absolute value of n (i.e. |n|), if it is representable.

Definition at line 32 of file labs.c.

33 {
34  return (j < 0 ? -j : j);
35 }

◆ ldiv()

ldiv_t ldiv ( long  x,
long  y 
)

Computes both the quotient and the remainder of the division of the numerator x by the denominator y.

Computes both the quotient and the remainder of the division of the numerator x by the denominator y. Computes quotient and remainder simultaneously. The quotient is the algebraic quotient with any fractional part discarded (truncated towards zero). The remainder is such that quot * y + rem == x.

Computes the quotient (the result of the expression x/y) and remainder (the result of the expression xy) simultaneously.

Parameters
xinteger values
yinteger values
Returns
If both the remainder and the quotient can be represented as objects of the corresponding type (int, long, long long, imaxdiv_t, respectively), returns both as an object of type
See also
div_t,
ldiv_t,
lldiv_t,
imaxdiv_t.

If either the remainder or the quotient cannot be represented, the behavior is undefined.

Definition at line 35 of file ldiv.c.

36 {
37  ldiv_t r;
38 
39  /* see div.c for comments */
40 
41  r.quot = num / denom;
42  r.rem = num % denom;
43  if(num >= 0 && r.rem < 0)
44  {
45  r.quot++;
46  r.rem -= denom;
47  }
48  return (r);
49 }
Division type for long integers.
Definition: stdlib.h:22
long rem
Definition: stdlib.h:25
long quot
Definition: stdlib.h:24

References ldiv_t::quot, and ldiv_t::rem.

◆ llabs()

long long llabs ( long long  n)

Computes the absolute value of an long long number.

Computes the absolute value of an long long number. The behavior is undefined if the result cannot be represented by the return type.

Parameters
nlong long value
Returns
The absolute value of n (i.e. |n|), if it is representable.

Definition at line 29 of file llabs.c.

30 {
31  return (j < 0 ? -j : j);
32 }

◆ lldiv()

lldiv_t lldiv ( long long  x,
long long  y 
)

Computes both the quotient and the remainder of the division of the numerator x by the denominator y.

Computes both the quotient and the remainder of the division of the numerator x by the denominator y. Computes quotient and remainder simultaneously. The quotient is the algebraic quotient with any fractional part discarded (truncated towards zero). The remainder is such that quot * y + rem == x.

Computes the quotient (the result of the expression x/y) and remainder (the result of the expression xy) simultaneously.

Parameters
xinteger values
yinteger values
Returns
If both the remainder and the quotient can be represented as objects of the corresponding type (int, long, long long, imaxdiv_t, respectively), returns both as an object of type
See also
div_t,
ldiv_t,
lldiv_t,
imaxdiv_t.

If either the remainder or the quotient cannot be represented, the behavior is undefined.

Definition at line 30 of file lldiv.c.

31 {
32  lldiv_t retval;
33 
34  retval.quot = numer / denom;
35  retval.rem = numer % denom;
36  if(numer >= 0 && retval.rem < 0)
37  {
38  retval.quot++;
39  retval.rem -= denom;
40  }
41  return (retval);
42 }
long long quot
Definition: stdlib.h:31
Division type for long long integers.
Definition: stdlib.h:29
long long rem
Definition: stdlib.h:32

References lldiv_t::quot, and lldiv_t::rem.

◆ malloc()

void* malloc ( size_t  size)

Allocates size bytes of uninitialized storage.

If size is zero, the behavior is implementation defined (null pointer may be returned, or some non-null pointer may be returned that may not be used to access storage, but has to be passed to

See also
free). malloc is thread-safe: it behaves as though only accessing the memory locations visible through its argument, and not any static storage.
Parameters
sizenumber of bytes to allocate
Returns
On success, returns the pointer to the beginning of newly allocated memory. The returned pointer must be deallocated with
See also
free() or
realloc(). On failure, returns a null pointer.

Referenced by calloc(), heapsort(), heapsort_r(), realloc(), strdup(), strndup(), and vasprintf().

◆ mblen()

int mblen ( const char *  ,
size_t   
)

◆ mbstowcs()

size_t mbstowcs ( wchar_t *  __restrict,
const char *  __restrict,
size_t   
)

◆ mbtowc()

int mbtowc ( wchar_t *  __restrict,
const char *  __restrict,
size_t   
)

◆ qsort()

void qsort ( void *  a,
size_t  n,
size_t  es,
int(*)(const void *, const void *)  compar 
)

Sorts the given array pointed to by ptr in ascending order.

Sorts the given array pointed to by ptr in ascending order. The array contains count elements of size bytes. Function pointed to by comp is used for object comparison.

Parameters
apointer to the element to sort
nnumber of element in the array
essize of each element in the array in bytes
comparcomparison function which returns ​a negative integer value if the first argument is less than the second,a positive integer value if the first argument is greater than the second and zero if the arguments are equal. key is passed as the first argument, an element from the array as the second. The signature of the comparison function should be equivalent to the following: int cmp(const void *a, const void *b); The function must not modify the objects passed to it and must return consistent

◆ qsort_r()

void qsort_r ( void *  a,
size_t  n,
size_t  es,
void *  thunk,
int(*)(void *, const void *, const void *)  cmp 
)

Sorts the given array pointed to by ptr in ascending order.

The qsort_r() function behaves identically to

See also
qsort(), except that it takes an additional argument, thunk, which is passed unchanged as the first argument to function pointed to compar. This allows the comparison function to access additional data without using global variables, and thus qsort_r() is suitable for use in functions which must be reentrant. And is therefore reentrant and safe to use in threads.
Parameters
apointer to the element to sort
nnumber of element in the array
essize of each element in the array in bytes
thunkadditional data(variable) for cmp
cmpcomparison function which returns ​a negative integer value if the first argument is less than the second,a positive integer value if the first argument is greater than the second and zero if the arguments are equal. key is passed as the first argument, an element from the array as the second. The signature of the comparison function should be equivalent to the following: int cmp(const void *a, const void *b); The function must not modify the objects passed to it and must return consistent

◆ quick_exit()

void quick_exit ( int  )

◆ rand()

int rand ( void  )

Returns a pseudo-random integer value between ​0​ and.

See also
RAND_MAX (0 and
RAND_MAX included).

Returns a pseudo-random integer value between ​0​ and

See also
RAND_MAX (0 and
RAND_MAX included).
srand() seeds the pseudo-random number generator used by rand. If rand is used before any calls to
srand, rand behaves as if it was seeded with
srand. Each time rand is seeded with srand, it must produce the same sequence of values.

rand is not guaranteed to be thread-safe.

Returns
Pseudo-random integer value between ​0​ and RAND_MAX, inclusive.

◆ rand_r()

int rand_r ( unsigned int *  ctx)

◆ realloc()

void* realloc ( void *  ptr,
size_t  size 
)

Reallocates the given area of memory.

Reallocates the given area of memory. It must be previously allocated by

See also
malloc,
calloc or
realloc and not yet freed with a call to
free or
realloc. Otherwise, the results are undefined.

The reallocation is done by either: 1) expanding or contracting the existing area pointed to by ptr, if possible. The contents of the area remain unchanged up to the lesser of the new and old sizes. If the area is expanded, the contents of the new part of the array are undefined. 2) allocating a new memory block of size size bytes, copying memory area with size equal the lesser of the new and the old sizes, and freeing the old block.

If there is not enough memory, the old memory block is not freed and null pointer is returned. If ptr is NULL, the behavior is the same as calling malloc(size).

Parameters
ptrpointer to the memory area to be reallocated
sizenew size of the array
Returns
pointer to new memory allocation. If size is zero (e.g. realloc(ptr,0)) then returns NULL

Definition at line 4 of file realloc.c.

5 {
6  void* new_data = NULL;
7 
8  if(size)
9  {
10  if(!ptr)
11  {
12  return malloc(size);
13  }
14 
15  new_data = malloc(size);
16  if(new_data)
17  {
18  memcpy(new_data, ptr, size); // TODO: unsafe copy...
19  free(ptr); // we always move the data. free.
20  }
21  }
22 
23  return new_data;
24 }
void free(void *ptr)
Deallocates allocated memory space.
#define NULL
Definition: stddef.h:15
void * malloc(size_t size)
Allocates size bytes of uninitialized storage.
void * memcpy(void *__restrict dest, const void *__restrict src, size_t n)
Copies n characters from the object pointed to by src to the object pointed to by dest.

References free(), malloc(), memcpy(), and NULL.

Referenced by reallocf().

Here is the caller graph for this function:

◆ reallocf()

void* reallocf ( void *  ptr,
size_t  size 
)

Reallocates the given area of memory.

Reallocates the given area of memory. It must be previously allocated by

See also
malloc,
calloc or
realloc and not yet freed with a call to
free or
realloc. Otherwise, the results are undefined.

reallocf is a FreeBSD extension to realloc that frees the input pointer if an error occurrs

This library does not handle the BSD case where realloc(ptr,0) frees the ptr

Parameters
ptrpointer to the memory area to be reallocated
sizenew size of the array
Returns
pointer to new memory allocation. If size is zero (e.g. realloc(ptr,0)) then returns NULL

Definition at line 26 of file realloc.c.

27 {
28  void* p = realloc(ptr, size);
29 
30  if((p == NULL) && (ptr != NULL))
31  {
32  free(ptr);
33  }
34 
35  return p;
36 }
void free(void *ptr)
Deallocates allocated memory space.
#define NULL
Definition: stddef.h:15
void * realloc(void *ptr, size_t size)
Reallocates the given area of memory.
Definition: realloc.c:4

References free(), NULL, and realloc().

◆ srand()

void srand ( unsigned  seed)

Seeds the pseudo-random number generator used by.

See also
rand with the value seed.

Seeds the pseudo-random number generator used by

See also
rand with the value seed. If rand() is used before any calls to srand,
rand behaves as if it was seeded with srand. Each time rand is seeded with the same seed, it must produce the same sequence of values.

srand() is not guaranteed to be thread-safe.

Parameters
seedthe seed value

◆ strtod()

double strtod ( const char *__restrict  str,
char **__restrict  str_end 
)

Interprets a floating-point value in a byte string pointed to by str.

Interprets a floating-point value in a byte string pointed to by str. Function discards any whitespace characters (as determined by

See also
isspace()) until first non-whitespace character is found. Then it takes as many characters as possible to form a valid floating-point representation and converts them to a floating-point value. The valid floating-point value can be one of the following: a) decimal floating-point expression. It consists of the following parts: 1) (optional) plus or minus sign 2) nonempty sequence of decimal digits optionally containing decimal-point character (as determined by the current C locale) (defines significand) 3) (optional) e or E followed with optional minus or plus sign and nonempty sequence of decimal digits (defines exponent) b) binary floating-point expression. It consists of the following parts: 1) (optional) plus or minus sign 2) 0x or 0X c) infinity expression. It consists of the following parts: 1) (optional) plus or minus sign 2) INF or INFINITY ignoring case d) not-a-number expression. It consists of the following parts: 1) (optional) plus or minus sign 2) NAN or NAN(char_sequence) ignoring case of the NAN part. char_sequence can only contain digits, Latin letters, and underscores. The result is a quiet NaN floating-point value.

The functions sets the pointer pointed to by str_end to point to the character past the last character interpreted. If str_end is NULL, it is ignored.

Parameters
stra pointer to the null-terminated byte string to be interpreted
str_endpointer to a pointer to character
Returns
Floating-point value corresponding to the contents of str on success. If the converted value falls out of range of corresponding return type, range error occurs and HUGE_VAL, HUGE_VALF or HUGE_VALL is returned. If no conversion can be performed, ​0​ is returned.

Referenced by atof(), and strtold().

◆ strtof()

float strtof ( const char *__restrict  str,
char **__restrict  str_end 
)

Interprets a floating-point value in a byte string pointed to by str.

Interprets a floating-point value in a byte string pointed to by str. Function discards any whitespace characters (as determined by

See also
isspace()) until first non-whitespace character is found. Then it takes as many characters as possible to form a valid floating-point representation and converts them to a floating-point value. The valid floating-point value can be one of the following: a) decimal floating-point expression. It consists of the following parts: 1) (optional) plus or minus sign 2) nonempty sequence of decimal digits optionally containing decimal-point character (as determined by the current C locale) (defines significand) 3) (optional) e or E followed with optional minus or plus sign and nonempty sequence of decimal digits (defines exponent) b) binary floating-point expression. It consists of the following parts: 1) (optional) plus or minus sign 2) 0x or 0X c) infinity expression. It consists of the following parts: 1) (optional) plus or minus sign 2) INF or INFINITY ignoring case d) not-a-number expression. It consists of the following parts: 1) (optional) plus or minus sign 2) NAN or NAN(char_sequence) ignoring case of the NAN part. char_sequence can only contain digits, Latin letters, and underscores. The result is a quiet NaN floating-point value.

The functions sets the pointer pointed to by str_end to point to the character past the last character interpreted. If str_end is NULL, it is ignored.

Parameters
stra pointer to the null-terminated byte string to be interpreted
str_endpointer to a pointer to character
Returns
Floating-point value corresponding to the contents of str on success. If the converted value falls out of range of corresponding return type, range error occurs and HUGE_VAL, HUGE_VALF or HUGE_VALL is returned. If no conversion can be performed, ​0​ is returned.

◆ strtol()

long strtol ( const char *__restrict  str,
char **__restrict  str_end,
int  base 
)

Interprets an long value in a byte string pointed to by str.

Interprets an long value in a byte string pointed to by str. Discards any whitespace characters (as identified by calling

See also
isspace()) until the first non-whitespace character is found, then takes as many characters as possible to form a valid base-n (where n=base) long number representation and converts them to an long value. The valid long value consists of the following parts:
 1) (optional) plus or minus sign
 2) (optional) prefix (0) indicating octal base (applies only when the base is 8 or ​0​)
 3) (optional) prefix (0x or 0X) indicating hexadecimal base (applies only when the base is
16 or ​0​) 4) a sequence of digits

The set of valid values for base is {0,2,3,...,36}. The set of valid digits for base-2 integers is {0,1}, for base-3 integers is {0,1,2}, and so on. For bases larger than 10, valid digits include alphabetic characters, starting from Aa for base-11 integer, to Zz for base-36 integer. The case of the characters is ignored.

If the value of base is ​0​, the numeric base is auto-detected: if the prefix is 0, the base is octal, if the prefix is 0x or 0X, the base is hexadecimal, otherwise the base is decimal.

If the minus sign was part of the input sequence, the numeric value calculated from the sequence of digits is negated as if by unary minus in the result type.

The functions sets the pointer pointed to by str_end to point to the character past the last character interpreted. If str_end is NULL, it is ignored.

If the str is empty or does not have the expected form, no conversion is performed, and (if str_end is not NULL) the value of str is stored in the object pointed to by str_end.

Parameters
strpointer to the null-terminated byte string to be interpreted
str_endpointer to a pointer to character.
basebase of the interpreted integer value
Returns
returns the result of the conversion, unless the value would underflow or overflow. If the converted value falls out of range of corresponding return type, a range error occurs (setting errno to ERANGE) and LONG_MAX, LONG_MIN, LLONG_MAX or LLONG_MIN is returned. If no conversion can be performed, ​0​ is returned.

◆ strtold()

long double strtold ( const char *  __restrict,
char **  __restrict 
)

◆ strtoll()

long long strtoll ( const char *__restrict  str,
char **__restrict  str_end,
int  base 
)

Interprets an long long value in a byte string pointed to by str.

Interprets an long value in a byte string pointed to by str. Discards any whitespace characters (as identified by calling

See also
isspace()) until the first non-whitespace character is found, then takes as many characters as possible to form a valid base-n (where n=base) long number representation and converts them to an long long value. The valid long value consists of the following parts:
 1) (optional) plus or minus sign
 2) (optional) prefix (0) indicating octal base (applies only when the base is 8 or ​0​)
 3) (optional) prefix (0x or 0X) indicating hexadecimal base (applies only when the base is
16 or ​0​) 4) a sequence of digits

The set of valid values for base is {0,2,3,...,36}. The set of valid digits for base-2 integers is {0,1}, for base-3 integers is {0,1,2}, and so on. For bases larger than 10, valid digits include alphabetic characters, starting from Aa for base-11 integer, to Zz for base-36 integer. The case of the characters is ignored.

If the value of base is ​0​, the numeric base is auto-detected: if the prefix is 0, the base is octal, if the prefix is 0x or 0X, the base is hexadecimal, otherwise the base is decimal.

If the minus sign was part of the input sequence, the numeric value calculated from the sequence of digits is negated as if by unary minus in the result type.

The functions sets the pointer pointed to by str_end to point to the character past the last character interpreted. If str_end is NULL, it is ignored.

If the str is empty or does not have the expected form, no conversion is performed, and (if str_end is not NULL) the value of str is stored in the object pointed to by str_end.

Parameters
strpointer to the null-terminated byte string to be interpreted
str_endpointer to a pointer to character.
basebase of the interpreted integer value
Returns
returns the result of the conversion, unless the value would underflow or overflow. If the converted value falls out of range of corresponding return type, a range error occurs (setting errno to ERANGE) and LONG_MAX, LONG_MIN, LLONG_MAX or LLONG_MIN is returned. If no conversion can be performed, ​0​ is returned.

◆ strtoul()

unsigned long strtoul ( const char *__restrict  str,
char **__restrict  str_end,
int  base 
)

Interprets an unsigned long value in a byte string pointed to by str.

Interprets an unsigned long value in a byte string pointed to by str. Discards any whitespace characters (as identified by calling

See also
isspace()) until the first non-whitespace character is found, then takes as many characters as possible to form a valid base-n (where n=base) long number representation and converts them to an unsigned long value. The valid long value consists of the following parts:
 1) (optional) plus or minus sign
 2) (optional) prefix (0) indicating octal base (applies only when the base is 8 or ​0​)
 3) (optional) prefix (0x or 0X) indicating hexadecimal base (applies only when the base is
16 or ​0​) 4) a sequence of digits

The set of valid values for base is {0,2,3,...,36}. The set of valid digits for base-2 integers is {0,1}, for base-3 integers is {0,1,2}, and so on. For bases larger than 10, valid digits include alphabetic characters, starting from Aa for base-11 integer, to Zz for base-36 integer. The case of the characters is ignored.

If the value of base is ​0​, the numeric base is auto-detected: if the prefix is 0, the base is octal, if the prefix is 0x or 0X, the base is hexadecimal, otherwise the base is decimal.

If the minus sign was part of the input sequence, the numeric value calculated from the sequence of digits is negated as if by unary minus in the result type.

If the str is empty or does not have the expected form, no conversion is performed, and (if str_end is not NULL) the value of str is stored in the object pointed to by str_end.

Parameters
strpointer to the null-terminated byte string to be interpreted
str_endpointer to a pointer to character.
basebase of the interpreted integer value
Returns
returns the result of the conversion, unless the value would underflow or overflow. If the converted value falls out of range of corresponding return type, a range error occurs (setting errno to ERANGE) and LONG_MAX, LONG_MIN, LLONG_MAX or LLONG_MIN is returned. If no conversion can be performed, ​0​ is returned.

◆ strtoull()

unsigned long long strtoull ( const char *__restrict  str,
char **__restrict  str_end,
int  base 
)

Interprets an unsigned long long value in a byte string pointed to by str.

Interprets an unsigned long long value in a byte string pointed to by str. Discards any whitespace characters (as identified by calling

See also
isspace()) until the first non-whitespace character is found, then takes as many characters as possible to form a valid base-n (where n=base) long number representation and converts them to an unsigned long long value. The valid long value consists of the following parts:
 1) (optional) plus or minus sign
 2) (optional) prefix (0) indicating octal base (applies only when the base is 8 or ​0​)
 3) (optional) prefix (0x or 0X) indicating hexadecimal base (applies only when the base is
16 or ​0​) 4) a sequence of digits

The set of valid values for base is {0,2,3,...,36}. The set of valid digits for base-2 integers is {0,1}, for base-3 integers is {0,1,2}, and so on. For bases larger than 10, valid digits include alphabetic characters, starting from Aa for base-11 integer, to Zz for base-36 integer. The case of the characters is ignored.

If the value of base is ​0​, the numeric base is auto-detected: if the prefix is 0, the base is octal, if the prefix is 0x or 0X, the base is hexadecimal, otherwise the base is decimal.

If the minus sign was part of the input sequence, the numeric value calculated from the sequence of digits is negated as if by unary minus in the result type.

If the str is empty or does not have the expected form, no conversion is performed, and (if str_end is not NULL) the value of str is stored in the object pointed to by str_end.

Parameters
strpointer to the null-terminated byte string to be interpreted
str_endpointer to a pointer to character.
basebase of the interpreted integer value
Returns
returns the result of the conversion, unless the value would underflow or overflow. If the converted value falls out of range of corresponding return type, a range error occurs (setting errno to ERANGE) and LONG_MAX, LONG_MIN, LLONG_MAX or LLONG_MIN is returned. If no conversion can be performed, ​0​ is returned.

Definition at line 44 of file strtoull.c.

45 {
46  const char* s;
47  unsigned long long acc;
48  char c;
49  unsigned long long cutoff;
50  int neg, any, cutlim;
51 
52  /*
53  * See strtoq for comments as to the logic used.
54  */
55  s = nptr;
56  do
57  {
58  c = *s++;
59  } while(isspace((unsigned char)c));
60  if(c == '-')
61  {
62  neg = 1;
63  c = *s++;
64  }
65  else
66  {
67  neg = 0;
68  if(c == '+')
69  {
70  {
71  c = *s++;
72  }
73  }
74  }
75  if((base == 0 || base == 16) && c == '0' && (*s == 'x' || *s == 'X'))
76  {
77  c = s[1];
78  s += 2;
79  base = 16;
80  }
81  if(base == 0)
82  {
83  {
84  base = c == '0' ? 8 : 10;
85  }
86  }
87  acc = any = 0;
88  if(base < 2 || base > 36)
89  {
90  {
91  goto noconv;
92  }
93  }
94 
95  cutoff = ULLONG_MAX / (unsigned long long)base;
96  cutlim = (int)(ULLONG_MAX % (unsigned long long)base);
97  for(;; c = *s++)
98  {
99  if(c >= '0' && c <= '9')
100  {
101  {
102  c -= '0';
103  }
104  }
105  else if(c >= 'A' && c <= 'Z')
106  {
107  {
108  c -= 'A' - 10;
109  }
110  }
111  else if(c >= 'a' && c <= 'z')
112  {
113  {
114  c -= 'a' - 10;
115  }
116  }
117  else
118  {
119  {
120  break;
121  }
122  }
123  if(c >= base)
124  {
125  {
126  break;
127  }
128  }
129  if(any < 0 || acc > cutoff || (acc == cutoff && c > cutlim))
130  {
131  {
132  any = -1;
133  }
134  }
135  else
136  {
137  any = 1;
138  acc *= (unsigned long)base;
139  acc += (unsigned long)c;
140  }
141  }
142  if(any < 0)
143  {
144  acc = ULLONG_MAX;
145  // errno = ERANGE;
146  }
147  else if(!any)
148  {
149  noconv:
150  // PJ: nonstandard, but since no errno, acc = 0
151  acc = 0;
152  // errno = EINVAL;
153  }
154  else if(neg)
155  {
156  acc = -acc;
157  }
158  if(endptr != NULL)
159  {
160  *endptr = (char*)(uintptr_t)(any ? s - 1 : nptr);
161  }
162  return (acc);
163 }
#define NULL
Definition: stddef.h:15
int isspace(int ch)
Checks if the given character is a whitespace character.
Definition: isspace.c:5
#define ULLONG_MAX
Definition: limits.h:67

References isspace(), NULL, and ULLONG_MAX.

◆ system()

int system ( const char *  )

◆ wcstombs()

size_t wcstombs ( char *  __restrict,
const wchar_t *  __restrict,
size_t   
)

◆ wctomb()

int wctomb ( char *  ,
wchar_t   
)