The manual
ginnOS
The reference: libc, the libraries and the kernel
Loading...
Searching...
No Matches
stdlib.h
Go to the documentation of this file.
1#pragma once
2
3#include <stddef.h>
4#include <stdint.h>
5#include <sys/types.h>
6
7/**
8 * @file stdlib.h
9 * @brief This file contains the declarations of general utility functions.
10 */
11
12#ifndef NULL
13#define NULL ((void *)0)
14#endif
15
16/**
17 * allocate memory.
18 * @param size number of bytes to allocate.
19 * @return pointer to allocated memory, or NULL on failure.
20 */
21void *malloc(size_t size);
22
23/**
24 * allocate zero-initialized memory.
25 * @param nmemb number of elements.
26 * @param size size of each element in bytes.
27 * @return pointer to allocated memory, or NULL on failure.
28 */
29void *calloc(size_t nmemb, size_t size);
30
31/**
32 * resize a previously allocated block.
33 * @param ptr pointer returned by malloc/calloc/realloc, or NULL.
34 * @param size new size in bytes. if 0, the block is freed and NULL is returned.
35 * @return pointer to the resized block, or NULL on failure (original block unchanged).
36 */
37void *realloc(void *ptr, size_t size);
38
39/**
40 * free a previously allocated block.
41 * @param ptr pointer returned by malloc/calloc/realloc, or NULL (no-op).
42 */
43void free(void *ptr);
44
45/**
46 * convert a string to an integer.
47 * @param s null-terminated string.
48 * @return the integer value.
49 */
50int atoi(const char *s);
51
52/**
53 * a floating-point number at the start of str, after spaces: an optional
54 * sign, then decimal digits with an optional point and exponent (1.5e3),
55 * hexadecimal digits with "0x" and an optional "p" exponent (0x1.8p1),
56 * "inf", "infinity" or "nan". the result is rounded correctly.
57 * @param end receives the first character after the number (may be NULL).
58 * @return the number, or 0 if there is none (then *end is str). a number
59 * past the largest gives HUGE_VAL, below the smallest 0; both set
60 * errno to ERANGE.
61 */
62double strtod(const char *str, char **end);
63float strtof(const char *str, char **end);
64long double strtold(const char *str, char **end);
65
66/**
67 * strtod(str, NULL).
68 */
69double atof(const char *str);
70
71/**
72 * run a command with "/bin/sh -c" and wait for it.
73 * @param command the command line, or NULL to ask whether there is a shell.
74 * @return the wait status of the shell, or -1 if it could not run (for
75 * NULL: non-zero if /bin/sh exists).
76 */
77int system(const char *command);
78
79/**
80 * make and open a new file with a name no other file has. the last six
81 * characters of template must be "XXXXXX"; they are changed to the name
82 * that was used.
83 * @return an fd open for reading and writing (mode 0600), or -1 (errno set).
84 */
85int mkstemp(char *template);
86
87/**
88 * the environment: a NULL-terminated array of "KEY=VALUE" strings.
89 */
90extern char **environ;
91
92/**
93 * look up an environment variable.
94 * @param name the variable name (without '=').
95 * @return its value, or NULL if it is not set.
96 */
97char *getenv(const char *name);
98
99/**
100 * register a function for exit() to call. handlers run in the reverse
101 * order of registration; _exit() skips them.
102 * @param function the handler.
103 * @return 0, or -1 if 32 handlers are registered already.
104 */
105int atexit(void (*function)(void));
106
107typedef struct
108{
109 int quot;
110 int rem;
111} div_t;
112
113typedef struct
114{
115 long quot;
116 long rem;
117} ldiv_t;
118
119#define RAND_MAX 0x7fffffff
120#define EXIT_SUCCESS 0
121#define EXIT_FAILURE 1
122
123long atol(const char *s);
124long long atoll(const char *s);
125long strtol(const char *s, char **end, int base);
126unsigned long strtoul(const char *s, char **end, int base);
127long long strtoll(const char *s, char **end, int base);
128unsigned long long strtoull(const char *s, char **end, int base);
129
130int abs(int value);
131long labs(long value);
132long long llabs(long long value);
133div_t div(int numerator, int denominator);
134ldiv_t ldiv(long numerator, long denominator);
135
136int rand(void);
137void srand(unsigned int seed);
138
139/**
140 * random numbers good for keys, from the kernel's generator
141 * (sys/random.h). they cannot fail.
142 */
144void arc4random_buf(void *buf, size_t length);
145/** a random number below bound, each one equally likely */
147
148/**
149 * sort count elements of size bytes (heap sort: not stable).
150 */
151void qsort(void *base, size_t count, size_t size, int (*compare)(const void *, const void *));
152
153/**
154 * find key in a sorted array.
155 * @return the element, or NULL.
156 */
157void *bsearch(const void *key, const void *base, size_t count, size_t size,
158 int (*compare)(const void *, const void *));
159
160int setenv(const char *name, const char *value, int overwrite);
161int unsetenv(const char *name);
162int putenv(char *entry);
163
164/**
165 * pseudo-terminals (the master side, /dev/ptmx). posix_openpt opens a new
166 * master; unlockpt lets the slave be opened; ptsname gives the slave's
167 * path (in a static buffer). grantpt does nothing: the slaves are open to
168 * all. see also <pty.h>.
169 */
171int grantpt(int fd);
172int unlockpt(int fd);
173char *ptsname(int fd);
174int ptsname_r(int fd, char *buf, size_t size);
175
176/**
177 * end the process with SIGABRT.
178 */
179void abort(void) __attribute__((noreturn));
180
181/**
182 * run the atexit handlers, write out buffered stdio output, then end the
183 * process with an exit code.
184 * @param status exit code.
185 */
186void exit(int status) __attribute__((noreturn));
187
188/** end the process at once: no atexit functions, no stream flush (C99) */
189void _Exit(int status) __attribute__((noreturn));
190
191// characters of more than one byte: UTF-8 (see <wchar.h>)
192#define MB_CUR_MAX ((size_t)4)
193
194/** the bytes of the character at s (at most n), 0 for "\0", or -1 */
195int mblen(const char *s, size_t n);
196/** the character at s (at most n bytes) into *wc: its bytes, 0 or -1 */
197int mbtowc(wchar_t *wc, const char *s, size_t n);
198/** wc as UTF-8 at s: the bytes, or -1 */
199int wctomb(char *s, wchar_t wc);
200
201/**
202 * the absolute path of path, with no "." or ".." and no double slashes,
203 * into resolved (PATH_MAX bytes), or into new memory when resolved is
204 * NULL. the file must exist. (ginnOS has no symbolic links yet.)
205 * @return resolved, or NULL with errno.
206 */
207char *realpath(const char *restrict path, char *restrict resolved);
208/** make a new folder (0700) named template with its six X's replaced */
209char *mkdtemp(char *template);
uint32_t base
Definition gdt.c:1
uint8_t flags
Definition idt.c:3
unsigned int uint32_t
Definition fb.h:17
NULL, size_t, ptrdiff_t, offsetof.
integer types of fixed width.
uint32_t arc4random(void)
random numbers good for keys, from the kernel's generator (sys/random.h).
int system(const char *command)
run a command with "/bin/sh -c" and wait for it.
int mblen(const char *s, size_t n)
the bytes of the character at s (at most n), 0 for "\0", or -1
uint32_t arc4random_uniform(uint32_t bound)
a random number below bound, each one equally likely
char * ptsname(int fd)
int unsetenv(const char *name)
int setenv(const char *name, const char *value, int overwrite)
void * calloc(size_t nmemb, size_t size)
allocate zero-initialized memory.
int atoi(const char *s)
convert a string to an integer.
int grantpt(int fd)
long labs(long value)
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...
void exit(int status) __attribute__((noreturn))
run the atexit handlers, write out buffered stdio output, then end the process with an exit code.
long strtol(const char *s, char **end, int base)
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),...
long long atoll(const char *s)
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 abs(int value)
double atof(const char *str)
strtod(str, NULL).
long long strtoll(const char *s, char **end, int base)
int mkstemp(char *template)
make and open a new file with a name no other file has.
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).
long atol(const char *s)
void srand(unsigned int seed)
div_t div(int numerator, int denominator)
char * mkdtemp(char *template)
make a new folder (0700) named template with its six X's replaced
void * malloc(size_t size)
allocate memory.
ldiv_t ldiv(long numerator, long denominator)
void arc4random_buf(void *buf, size_t length)
char ** environ
the environment: a NULL-terminated array of "KEY=VALUE" strings.
long long llabs(long long value)
unsigned long strtoul(const char *s, char **end, int base)
char * getenv(const char *name)
look up an environment variable.
int wctomb(char *s, wchar_t wc)
wc as UTF-8 at s: the bytes, or -1
int unlockpt(int fd)
int atexit(void(*function)(void))
register a function for exit() to call.
void abort(void) __attribute__((noreturn))
end the process with SIGABRT.
unsigned long long strtoull(const char *s, char **end, int base)
int ptsname_r(int fd, char *buf, size_t size)
int putenv(char *entry)
void * realloc(void *ptr, size_t size)
resize a previously allocated block.
int rand(void)
long double strtold(const char *str, char **end)
void _Exit(int status) __attribute__((noreturn))
end the process at once: no atexit functions, no stream flush (C99)
float strtof(const char *str, char **end)
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.
void free(void *ptr)
free a previously allocated block.
int posix_openpt(int flags)
pseudo-terminals (the master side, /dev/ptmx).
ELF32 file header.
Definition elf.h:51
Definition stdlib.h:108
int quot
Definition stdlib.h:109
int rem
Definition stdlib.h:110
Definition stdlib.h:114
long rem
Definition stdlib.h:116
long quot
Definition stdlib.h:115
basic system types.