The manual
ginnOS
The reference: libc, the libraries and the kernel
Loading...
Searching...
No Matches
stdlib.h File Reference

This file contains the declarations of general utility functions. More...

#include <stddef.h>
#include <stdint.h>
#include <sys/types.h>

Go to the source code of this file.

Data Structures

struct  div_t
struct  ldiv_t

Macros

#define RAND_MAX   0x7fffffff
#define EXIT_SUCCESS   0
#define EXIT_FAILURE   1
#define MB_CUR_MAX   ((size_t)4)

Functions

void * malloc (size_t size)
 allocate memory.
void * calloc (size_t nmemb, size_t size)
 allocate zero-initialized memory.
void * realloc (void *ptr, size_t size)
 resize a previously allocated block.
void free (void *ptr)
 free a previously allocated block.
int atoi (const char *s)
 convert a string to an integer.
double strtod (const char *str, char **end)
 a floating-point number at the start of str, after spaces: an optional sign, then decimal digits with an optional point and exponent (1.5e3), hexadecimal digits with "0x" and an optional "p" exponent (0x1.8p1), "inf", "infinity" or "nan".
float strtof (const char *str, char **end)
long double strtold (const char *str, char **end)
double atof (const char *str)
 strtod(str, NULL).
int system (const char *command)
 run a command with "/bin/sh -c" and wait for it.
int mkstemp (char *template)
 make and open a new file with a name no other file has.
char * getenv (const char *name)
 look up an environment variable.
int atexit (void(*function)(void))
 register a function for exit() to call.
long atol (const char *s)
long long atoll (const char *s)
long strtol (const char *s, char **end, int base)
unsigned long strtoul (const char *s, char **end, int base)
long long strtoll (const char *s, char **end, int base)
unsigned long long strtoull (const char *s, char **end, int base)
int abs (int value)
long labs (long value)
long long llabs (long long value)
div_t div (int numerator, int denominator)
ldiv_t ldiv (long numerator, long denominator)
int rand (void)
void srand (unsigned int seed)
uint32_t arc4random (void)
 random numbers good for keys, from the kernel's generator (sys/random.h).
void arc4random_buf (void *buf, size_t length)
uint32_t arc4random_uniform (uint32_t bound)
 a random number below bound, each one equally likely
void qsort (void *base, size_t count, size_t size, int(*compare)(const void *, const void *))
 sort count elements of size bytes (heap sort: not stable).
void * bsearch (const void *key, const void *base, size_t count, size_t size, int(*compare)(const void *, const void *))
 find key in a sorted array.
int setenv (const char *name, const char *value, int overwrite)
int unsetenv (const char *name)
int putenv (char *entry)
int posix_openpt (int flags)
 pseudo-terminals (the master side, /dev/ptmx).
int grantpt (int fd)
int unlockpt (int fd)
char * ptsname (int fd)
int ptsname_r (int fd, char *buf, size_t size)
void abort (void) __attribute__((noreturn))
 end the process with SIGABRT.
void exit (int status) __attribute__((noreturn))
 run the atexit handlers, write out buffered stdio output, then end the process with an exit code.
void _Exit (int status) __attribute__((noreturn))
 end the process at once: no atexit functions, no stream flush (C99)
int mblen (const char *s, size_t n)
 the bytes of the character at s (at most n), 0 for "\0", or -1
int mbtowc (wchar_t *wc, const char *s, size_t n)
 the character at s (at most n bytes) into *wc: its bytes, 0 or -1
int wctomb (char *s, wchar_t wc)
 wc as UTF-8 at s: the bytes, or -1
char * realpath (const char *restrict path, char *restrict resolved)
 the absolute path of path, with no "." or ".." and no double slashes, into resolved (PATH_MAX bytes), or into new memory when resolved is NULL.
char * mkdtemp (char *template)
 make a new folder (0700) named template with its six X's replaced

Variables

char ** environ
 the environment: a NULL-terminated array of "KEY=VALUE" strings.

Detailed Description

This file contains the declarations of general utility functions.

Macro Definition Documentation

◆ RAND_MAX

#define RAND_MAX   0x7fffffff

◆ EXIT_SUCCESS

#define EXIT_SUCCESS   0

◆ EXIT_FAILURE

#define EXIT_FAILURE   1

◆ MB_CUR_MAX

#define MB_CUR_MAX   ((size_t)4)

Function Documentation

◆ malloc()

void * malloc ( size_t size)

allocate memory.

Parameters
sizenumber of bytes to allocate.
Returns
pointer to allocated memory, or NULL on failure.

◆ calloc()

void * calloc ( size_t nmemb,
size_t size )

allocate zero-initialized memory.

Parameters
nmembnumber of elements.
sizesize of each element in bytes.
Returns
pointer to allocated memory, or NULL on failure.

◆ realloc()

void * realloc ( void * ptr,
size_t size )

resize a previously allocated block.

Parameters
ptrpointer returned by malloc/calloc/realloc, or NULL.
sizenew size in bytes. if 0, the block is freed and NULL is returned.
Returns
pointer to the resized block, or NULL on failure (original block unchanged).

◆ free()

void free ( void * ptr)

free a previously allocated block.

Parameters
ptrpointer returned by malloc/calloc/realloc, or NULL (no-op).

◆ atoi()

int atoi ( const char * s)

convert a string to an integer.

Parameters
snull-terminated string.
Returns
the integer value.

◆ strtod()

double strtod ( const char * str,
char ** end )

a floating-point number at the start of str, after spaces: an optional sign, then decimal digits with an optional point and exponent (1.5e3), hexadecimal digits with "0x" and an optional "p" exponent (0x1.8p1), "inf", "infinity" or "nan".

the result is rounded correctly.

Parameters
endreceives the first character after the number (may be NULL).
Returns
the number, or 0 if there is none (then *end is str). a number past the largest gives HUGE_VAL, below the smallest 0; both set errno to ERANGE.

◆ strtof()

float strtof ( const char * str,
char ** end )

◆ strtold()

long double strtold ( const char * str,
char ** end )

◆ atof()

double atof ( const char * str)

strtod(str, NULL).

◆ system()

int system ( const char * command)

run a command with "/bin/sh -c" and wait for it.

Parameters
commandthe command line, or NULL to ask whether there is a shell.
Returns
the wait status of the shell, or -1 if it could not run (for NULL: non-zero if /bin/sh exists).

◆ mkstemp()

int mkstemp ( char * template)

make and open a new file with a name no other file has.

the last six characters of template must be "XXXXXX"; they are changed to the name that was used.

Returns
an fd open for reading and writing (mode 0600), or -1 (errno set).

◆ getenv()

char * getenv ( const char * name)

look up an environment variable.

Parameters
namethe variable name (without '=').
Returns
its value, or NULL if it is not set.

◆ atexit()

int atexit ( void(* function )(void))

register a function for exit() to call.

handlers run in the reverse order of registration; _exit() skips them.

Parameters
functionthe handler.
Returns
0, or -1 if 32 handlers are registered already.

◆ atol()

long atol ( const char * s)

◆ atoll()

long long atoll ( const char * s)

◆ strtol()

long strtol ( const char * s,
char ** end,
int base )

◆ strtoul()

unsigned long strtoul ( const char * s,
char ** end,
int base )

◆ strtoll()

long long strtoll ( const char * s,
char ** end,
int base )

◆ strtoull()

unsigned long long strtoull ( const char * s,
char ** end,
int base )

◆ abs()

int abs ( int value)

◆ labs()

long labs ( long value)

◆ llabs()

long long llabs ( long long value)

◆ div()

div_t div ( int numerator,
int denominator )

◆ ldiv()

ldiv_t ldiv ( long numerator,
long denominator )

◆ rand()

int rand ( void )

◆ srand()

void srand ( unsigned int seed)

◆ arc4random()

uint32_t arc4random ( void )

random numbers good for keys, from the kernel's generator (sys/random.h).

they cannot fail.

◆ arc4random_buf()

void arc4random_buf ( void * buf,
size_t length )

◆ arc4random_uniform()

uint32_t arc4random_uniform ( uint32_t bound)

a random number below bound, each one equally likely

◆ qsort()

void qsort ( void * base,
size_t count,
size_t size,
int(* compare )(const void *, const void *) )

sort count elements of size bytes (heap sort: not stable).

◆ bsearch()

void * bsearch ( const void * key,
const void * base,
size_t count,
size_t size,
int(* compare )(const void *, const void *) )

find key in a sorted array.

Returns
the element, or NULL.

◆ setenv()

int setenv ( const char * name,
const char * value,
int overwrite )

◆ unsetenv()

int unsetenv ( const char * name)

◆ putenv()

int putenv ( char * entry)

◆ posix_openpt()

int posix_openpt ( int flags)

pseudo-terminals (the master side, /dev/ptmx).

posix_openpt opens a new master; unlockpt lets the slave be opened; ptsname gives the slave's path (in a static buffer). grantpt does nothing: the slaves are open to all. see also <pty.h>.

◆ grantpt()

int grantpt ( int fd)

◆ unlockpt()

int unlockpt ( int fd)

◆ ptsname()

char * ptsname ( int fd)

◆ ptsname_r()

int ptsname_r ( int fd,
char * buf,
size_t size )

◆ abort()

void abort ( void )

end the process with SIGABRT.

◆ exit()

void exit ( int status)

run the atexit handlers, write out buffered stdio output, then end the process with an exit code.

Parameters
statusexit code.

◆ _Exit()

void _Exit ( int status)

end the process at once: no atexit functions, no stream flush (C99)

◆ mblen()

int mblen ( const char * s,
size_t n )

the bytes of the character at s (at most n), 0 for "\0", or -1

◆ mbtowc()

int mbtowc ( wchar_t * wc,
const char * s,
size_t n )

the character at s (at most n bytes) into *wc: its bytes, 0 or -1

◆ wctomb()

int wctomb ( char * s,
wchar_t wc )

wc as UTF-8 at s: the bytes, or -1

◆ realpath()

char * realpath ( const char *restrict path,
char *restrict resolved )

the absolute path of path, with no "." or ".." and no double slashes, into resolved (PATH_MAX bytes), or into new memory when resolved is NULL.

the file must exist. (ginnOS has no symbolic links yet.)

Returns
resolved, or NULL with errno.

◆ mkdtemp()

char * mkdtemp ( char * template)

make a new folder (0700) named template with its six X's replaced

Variable Documentation

◆ environ

char** environ
extern

the environment: a NULL-terminated array of "KEY=VALUE" strings.