Functions from built-in libc (firmware).
Usage: #include <swilib.h> or #include <swilib/libc.h>
- Warning
- For GCC: all of these functions are disabled by default due to being external
libc.so is available.
-
External libc is more compatible with standarts and helpful for porting existing code.
-
But you always have the option to use built-in libc functions (e.g. for smaller memory usage).
-
Just add
-DSWILIB_LIBC to your definitions and remove -lc from libs.
Also, some non-standard functions are available even if the built-in library is disabled:
|
| void * | malloc (size_t size) |
| | Allocate memory block.
|
| |
| void * | calloc (size_t count, size_t size) |
| | Allocate and zero-initialize array.
|
| |
| void * | realloc (void *ptr, size_t size) |
| | Reallocate memory block.
|
| |
| void | free (void *ptr) |
| | Deallocate memory block.
|
| |
| void | mfree (void *ptr) |
| | Free previously allocated memory.
|
| |
| void * | malloc_adr (void) |
| | Pointer to the function malloc().
|
| |
| void * | mfree_adr (void) |
| | Pointer to the function free().
|
| |
| void * | realloc_adr (void) |
| | Pointer to the function realloc().
|
| |
|
| int | memcmp (const void *ptr1, const void *ptr2, size_t num) |
| | Compare two blocks of memory.
|
| |
| void * | memchr (const void *str, int c, size_t num) |
| | Locate character in block of memory.
|
| |
| void | zeromem (void *ptr, int num) |
| | Fill block of memory with zero.
|
| |
| void | bzero (void *ptr, int num) |
| | Fill block of memory with zero.
|
| |
| void * | memset (void *ptr, int val, size_t num) |
| | Fill block of memory with zero.
|
| |
| void * | memcpy (void *destination, const void *source, size_t num) |
| | Copy block of memory.
|
| |
| void * | memmove (void *destination, const void *source, size_t num) |
| | Move block of memory.
|
| |
|
| void | srand (uint32_t seed) |
| | Initialize random number generator.
|
| |
| int | rand (void) |
| | Generate random number.
|
| |
|
| int | snprintf (char *s, size_t n, const char *format,...) |
| | Write formatted output to sized buffer.
|
| |
| int | sprintf (char *s, const char *format,...) |
| | Write formatted output to buffer.
|
| |
| int | vsprintf (char *s, const char *format, va_list args) |
| | Write formatted output to buffer using a variable argument list.
|
| |
| int | vsnprintf (char *s, size_t n, const char *format, va_list args) |
| | Write formatted output to a sized buffer using a variable argument list.
|
| |
| int | sscanf (const char *s, const char *format,...) |
| | Read formatted data from string.
|
| |
|
| char * | strchr (const char *str, int character) |
| | Locate first occurrence of character in string.
|
| |
| int | strcmp (const char *str1, const char *str2) |
| | Compare two strings.
|
| |
| int | strcasecmp (const char *str1, const char *str2) |
| | Compare two strings without case sensitivity.
|
| |
| int | strcmpi (const char *str1, const char *str2) |
| | Compare two strings without case sensitivity.
|
| |
| size_t | strlen (const char *str) |
| | Get string length.
|
| |
| size_t | strcspn (const char *str1, const char *str2) |
| | Get span until character in string.
|
| |
| char * | strcat (char *destination, const char *source) |
| | Concatenate strings.
|
| |
| char * | strncat (char *destination, const char *source, size_t num) |
| | Append characters from string.
|
| |
| int | strncmp (const char *str1, const char *str2, size_t num) |
| | Compare characters of two strings.
|
| |
| char * | strcpy (char *destination, const char *source) |
| | Copy string.
|
| |
| char * | strncpy (char *destination, const char *source, size_t num) |
| | Copy characters from string.
|
| |
| char * | strrchr (const char *str, int character) |
| | Locate last occurrence of character in string.
|
| |
| char * | strstr (const char *str1, const char *str2) |
| | Locate substring.
|
| |
| long int | strtol (const char *str, char **endptr, int base) |
| | Convert string to long integer.
|
| |
| unsigned long | strtoul (const char *str, char **endptr, int base) |
| | Convert string to unsigned long integer.
|
| |
|
| void | qsort (void *first, size_t nitems, size_t size, int(*comparator)(const void *, const void *)) |
| | Sorting array.
|
| |
| void * | bsearch (const void *key, const void *first, size_t nitems, size_t size, int(*comparator)(const void *, const void *)) |
| | Searchign in array.
|
| |
|
| int | setenv (const char *name, const char *value, int replace) |
| | Set a value for the environment variable.
|
| |
| int | unsetenv (const char *name) |
| | Remove environment variable.
|
| |
| char * | getenv (const char *name) |
| | Get value for the environment variable.
|
| |
| int | clearenv (void) |
| | Remove all environment variables.
|
| |
|
| int | dlopen (const char *name) |
| | Load the dynamic shared object (.so).
|
| |
| void * | dlsym (int handle, const char *name) |
| | Get a pointer to the symbol from the previously loaded dynamic object.
|
| |
| int | dlclose (int handle) |
| | Close previously loaded dynamic object.
|
| |
| const char * | dlerror (void) |
| | Get a string representation of the last error in _dlopen.
|
| |
◆ __JMPBUF_DECL
◆ jmp_buf
◆ malloc()
| void * malloc |
( |
size_t | size | ) |
|
Allocate memory block.
- Parameters
-
| size | Size of the memory block, in bytes. |
- Returns
- On success, a pointer to the memory block allocated by the function.
-
If the function failed to allocate the requested block of memory, a null pointer is returned.
◆ calloc()
| void * calloc |
( |
size_t | count, |
|
|
size_t | size ) |
Allocate and zero-initialize array.
- Parameters
-
| count | Number of elements to allocate. |
| size | Size of each element. |
- Returns
- On success, a pointer to the memory block allocated by the function.
-
If the function failed to allocate the requested block of memory, a null pointer is returned.
◆ realloc()
| void * realloc |
( |
void * | ptr, |
|
|
size_t | size ) |
Reallocate memory block.
- Parameters
-
| ptr | Pointer to a memory block previously allocated with malloc, calloc or realloc. Alternatively, this can be a null pointer, in which case a new block is allocated (as if malloc was called). |
| size | New size for the memory block, in bytes. |
- Returns
- A pointer to the reallocated memory block, which may be either the same as ptr or a new location.
-
A null-pointer indicates that the function failed to allocate storage, and thus the block pointed by ptr was not modified.
◆ free()
Deallocate memory block.
- Parameters
-
◆ mfree()
Free previously allocated memory.
- Parameters
-
| ptr | pointer returned by malloc/calloc/realloc |
◆ malloc_adr()
| void * malloc_adr |
( |
void | | ) |
|
Pointer to the function malloc().
- Returns
- pointer to the function
◆ mfree_adr()
| void * mfree_adr |
( |
void | | ) |
|
Pointer to the function free().
- Returns
- pointer to the function
◆ realloc_adr()
| void * realloc_adr |
( |
void | | ) |
|
Pointer to the function realloc().
- Returns
- pointer to the function
◆ memcmp()
| int memcmp |
( |
const void * | ptr1, |
|
|
const void * | ptr2, |
|
|
size_t | num ) |
Compare two blocks of memory.
- Parameters
-
| ptr1 | Pointer to block of memory. |
| ptr2 | Pointer to block of memory. |
| num | Number of bytes to compare. |
- Returns
- <0 the first byte that does not match in both memory blocks has a lower value in ptr1 than in ptr2 (if evaluated as unsigned char values)
-
0 the contents of both memory blocks are equal
-
>0 the first byte that does not match in both memory blocks has a greater value in ptr1 than in ptr2 (if evaluated as unsigned char values)
◆ memchr()
| void * memchr |
( |
const void * | str, |
|
|
int | c, |
|
|
size_t | num ) |
Locate character in block of memory.
- Parameters
-
| str | This is the pointer to the block of memory where the search is performed. |
| c | This is the value to be passed as an int, but the function performs a byte per byte search using the unsigned char conversion of this value. |
| num | This is the number of bytes to be analyzed. |
- Returns
- This function returns a pointer to the matching byte or NULL if the character does not occur in the given memory area.
◆ zeromem()
| void zeromem |
( |
void * | ptr, |
|
|
int | num ) |
Fill block of memory with zero.
- Parameters
-
| ptr | Pointer to the block of memory to fill. |
| num | Number of bytes to be set to the zero. |
◆ bzero()
| void bzero |
( |
void * | ptr, |
|
|
int | num ) |
Fill block of memory with zero.
- Parameters
-
| ptr | Pointer to the block of memory to fill. |
| num | Number of bytes to be set to the zero. |
- Deprecated
- use memset
◆ memset()
| void * memset |
( |
void * | ptr, |
|
|
int | val, |
|
|
size_t | num ) |
Fill block of memory with zero.
- Parameters
-
| ptr | Pointer to the block of memory to fill. |
| val | Value to be set. The value is passed as an int, but the function fills the block of memory using the unsigned char conversion of this value. |
| num | Number of bytes to be set to the value. |
- Returns
- ptr is returned.
◆ memcpy()
| void * memcpy |
( |
void * | destination, |
|
|
const void * | source, |
|
|
size_t | num ) |
Copy block of memory.
- Parameters
-
| destination | Pointer to the destination array where the content is to be copied, type-casted to a pointer of type void*. |
| source | Pointer to the source of data to be copied, type-casted to a pointer of type const void*. |
| num | Number of bytes to copy. |
- Returns
- destination is returned.
◆ memmove()
| void * memmove |
( |
void * | destination, |
|
|
const void * | source, |
|
|
size_t | num ) |
Move block of memory.
- Parameters
-
| destination | Pointer to the destination array where the content is to be copied, type-casted to a pointer of type void*. |
| source | Pointer to the source of data to be copied, type-casted to a pointer of type const void*. |
| num | Number of bytes to copy. |
- Returns
- destination is returned.
◆ setjmp()
Save calling environment for long jump.
- Parameters
-
| env | Object of type jmp_buf where the environment information is stored. |
- Returns
- This macro may return more than once: A first time, on its direct invocation; In this case it always returns zero.
-
When longjmp is called with the information set to env, the macro returns again; this time it returns the value passed to longjmp as second argument if this is different from zero, or 1 if it is zero.
◆ longjmp()
| void longjmp |
( |
jmp_buf | env, |
|
|
int | val ) |
Long jump.
- Parameters
-
| env | Object of type jmp_buf filled by a previous call to setjmp that contains information to restore the environment to that point. |
| val | Value to which the setjmp expression evaluates. If this is zero, the expression evaluates as 1. |
◆ srand()
| void srand |
( |
uint32_t | seed | ) |
|
Initialize random number generator.
- Parameters
-
| seed | An integer value to be used as seed by the pseudo-random number generator algorithm. |
◆ rand()
Generate random number.
- Returns
- An integer value between 0 and RAND_MAX.
◆ snprintf()
| int snprintf |
( |
char * | s, |
|
|
size_t | n, |
|
|
const char * | format, |
|
|
| ... ) |
Write formatted output to sized buffer.
- Parameters
-
| s | Pointer to a buffer where the resulting C-string is stored. The buffer should have a size of at least n characters. |
| n | Maximum number of bytes to be used in the buffer. The generated string has a length of at most n-1, leaving space for the additional terminating null character. |
| format | C string that contains a format string that follows the same specifications as standart format in any sprintf/printf. |
- Returns
- The number of characters that would have been written if n had been sufficiently large, not counting the terminating null character.
-
If an encoding error occurs, a negative number is returned.
-
Notice that only when this returned value is non-negative and less than n, the string has been completely written.
◆ sprintf()
| int sprintf |
( |
char * | s, |
|
|
const char * | format, |
|
|
| ... ) |
Write formatted output to buffer.
- Parameters
-
| s | Pointer to a buffer where the resulting C-string is stored. The buffer should be large enough to contain the resulting string. |
| format | C string that contains a format string that follows the same specifications as standart format in any sprintf/printf. |
- Returns
- The number of characters that would have been written if n had been sufficiently large, not counting the terminating null character.
-
If an encoding error occurs, a negative number is returned.
-
Notice that only when this returned value is non-negative and less than n, the string has been completely written.
◆ vsprintf()
| int vsprintf |
( |
char * | s, |
|
|
const char * | format, |
|
|
va_list | args ) |
Write formatted output to buffer using a variable argument list.
- Parameters
-
| s | Pointer to the destination buffer. |
| format | C format string. |
| args | Variable argument list initialized by va_start. |
- Returns
- Number of characters written, excluding the terminating null character, or a negative value on error.
◆ vsnprintf()
| int vsnprintf |
( |
char * | s, |
|
|
size_t | n, |
|
|
const char * | format, |
|
|
va_list | args ) |
Write formatted output to a sized buffer using a variable argument list.
- Parameters
-
| s | Pointer to the destination buffer. |
| n | Size of the destination buffer. |
| format | C format string. |
| args | Variable argument list initialized by va_start. |
- Returns
- Number of characters that would have been written, excluding the terminating null character, or a negative value on error.
◆ sscanf()
| int sscanf |
( |
const char * | s, |
|
|
const char * | format, |
|
|
| ... ) |
Read formatted data from string.
- Parameters
-
| s | C string that the function processes as its source to retrieve the data. |
| format | C string that contains a format string that follows the same specifications as standart format in any scanf/sscanf. |
- Returns
- On success, the function returns the number of items in the argument list successfully filled.
-
This count can match the expected number of items or be less (even zero) in the case of a matching failure.
-
In the case of an input failure before any data could be successfully interpreted, EOF is returned.
◆ strchr()
| char * strchr |
( |
const char * | str, |
|
|
int | character ) |
Locate first occurrence of character in string.
- Parameters
-
| str | C string. |
| character | Character to be located. It is passed as its int promotion, but it is internally converted back to char for the comparison. |
- Returns
- A pointer to the first occurrence of character in str. If the character is not found, the function returns a null pointer.
◆ strcmp()
| int strcmp |
( |
const char * | str1, |
|
|
const char * | str2 ) |
Compare two strings.
- Parameters
-
| str1 | C string to be compared. |
| str2 | C string to be compared. |
- Returns
- <0 - the first character that does not match has a lower value in ptr1 than in ptr2
-
0 - the contents of both strings are equal
-
>0 - the first character that does not match has a greater value in ptr1 than in ptr2
◆ strcasecmp()
| int strcasecmp |
( |
const char * | str1, |
|
|
const char * | str2 ) |
Compare two strings without case sensitivity.
- Parameters
-
| str1 | C string to be compared. |
| str2 | C string to be compared. |
- Returns
- <0 - the first character that does not match has a lower value in ptr1 than in ptr2
-
0 - the contents of both strings are equal
-
>0 - the first character that does not match has a greater value in ptr1 than in ptr2
◆ strcmpi()
| int strcmpi |
( |
const char * | str1, |
|
|
const char * | str2 ) |
Compare two strings without case sensitivity.
- Parameters
-
| str1 | C string to be compared. |
| str2 | C string to be compared. |
- Returns
- <0 - the first character that does not match has a lower value in ptr1 than in ptr2
-
0 - the contents of both strings are equal
-
>0 - the first character that does not match has a greater value in ptr1 than in ptr2
◆ strlen()
| size_t strlen |
( |
const char * | str | ) |
|
Get string length.
- Parameters
-
- Returns
- The length of string.
◆ strcspn()
| size_t strcspn |
( |
const char * | str1, |
|
|
const char * | str2 ) |
Get span until character in string.
- Parameters
-
| str1 | C string to be scanned. |
| str2 | C string containing the characters to match. |
- Returns
- The length of the initial part of str1 not containing any of the characters that are part of str2.
-
This is the length of str1 if none of the characters in str2 are found in str1.
◆ strcat()
| char * strcat |
( |
char * | destination, |
|
|
const char * | source ) |
Concatenate strings.
- Parameters
-
| destination | Pointer to the destination array, which should contain a C string, and be large enough to contain the concatenated resulting string. |
| source | C string to be appended. This should not overlap destination. |
- Returns
- destination is returned.
◆ strncat()
| char * strncat |
( |
char * | destination, |
|
|
const char * | source, |
|
|
size_t | num ) |
Append characters from string.
- Parameters
-
| destination | Pointer to the destination array, which should contain a C string, and be large enough to contain the concatenated resulting string, including the additional null-character. |
| source | C string to be appended. |
| num | Maximum number of characters to be appended. |
- Returns
- destination is returned.
◆ strncmp()
| int strncmp |
( |
const char * | str1, |
|
|
const char * | str2, |
|
|
size_t | num ) |
Compare characters of two strings.
- Parameters
-
| str1 | C string to be compared. |
| str2 | C string to be compared. |
| num | Maximum number of characters to compare. |
- Returns
- destination is returned.
◆ strcpy()
| char * strcpy |
( |
char * | destination, |
|
|
const char * | source ) |
Copy string.
- Parameters
-
| destination | Pointer to the destination array where the content is to be copied. |
| source | C string to be copied. |
- Returns
- destination is returned.
◆ strncpy()
| char * strncpy |
( |
char * | destination, |
|
|
const char * | source, |
|
|
size_t | num ) |
Copy characters from string.
- Parameters
-
| destination | Pointer to the destination array where the content is to be copied. |
| source | C string to be copied. |
| num | Maximum number of characters to be copied from source. |
- Returns
- destination is returned.
◆ strrchr()
| char * strrchr |
( |
const char * | str, |
|
|
int | character ) |
Locate last occurrence of character in string.
- Parameters
-
| str | C string. |
| character | Character to be located. It is passed as its int promotion, but it is internally converted back to char. |
- Returns
- A pointer to the last occurrence of character in str. If the character is not found, the function returns a null pointer.
◆ strstr()
| char * strstr |
( |
const char * | str1, |
|
|
const char * | str2 ) |
Locate substring.
- Parameters
-
| str1 | C string to be scanned. |
| str2 | C string containing the sequence of characters to match. |
- Returns
- A pointer to the first occurrence in str1 of the entire sequence of characters specified in str2, or a null pointer if the sequence is not present in str1.
◆ strtol()
| long int strtol |
( |
const char * | str, |
|
|
char ** | endptr, |
|
|
int | base ) |
Convert string to long integer.
- Parameters
-
| str | C-string beginning with the representation of an integral number. |
| endptr | Reference to an object of type char*, whose value is set by the function to the next character in str after the numerical value. This parameter can also be a null pointer, in which case it is not used. |
| base | Numerical base (radix) that determines the valid characters and their interpretation. If this is 0, the base used is determined by the format in the sequence (see above). |
- Returns
- On success, the function returns the converted integral number as a long int value. If no valid conversion could be performed, a zero value is returned (0L).
◆ strtoul()
| unsigned long strtoul |
( |
const char * | str, |
|
|
char ** | endptr, |
|
|
int | base ) |
Convert string to unsigned long integer.
- Parameters
-
| str | C-string containing the representation of an integral number. |
| endptr | Reference to an object of type char*, whose value is set by the function to the next character in str after the numerical value. This parameter can also be a null pointer, in which case it is not used. |
| base | Numerical base (radix) that determines the valid characters and their interpretation. If this is 0, the base used is determined by the format in the sequence (see strtol for details). |
- Returns
- On success, the function returns the converted integral number as an unsigned long int value. If no valid conversion could be performed, a zero value is returned.
◆ qsort()
| void qsort |
( |
void * | first, |
|
|
size_t | nitems, |
|
|
size_t | size, |
|
|
int(* | comparator )(const void *, const void *) ) |
Sorting array.
- Parameters
-
| first | This is the pointer to the first element of the array to be sorted. |
| nitems | This is the number of elements in the array pointed by base. |
| size | This is the size in bytes of each element in the array. |
| comparator | This is the function that compares two elements. |
◆ bsearch()
| void * bsearch |
( |
const void * | key, |
|
|
const void * | first, |
|
|
size_t | nitems, |
|
|
size_t | size, |
|
|
int(* | comparator )(const void *, const void *) ) |
Searchign in array.
- Parameters
-
| key | This is the pointer to the object that serves as key for the search, type-casted as a void*. |
| first | This is the pointer to the first object of the array where the search is performed, type-casted as a void*. |
| nitems | This is the number of elements in the array pointed by base. |
| size | This is the size in bytes of each element in the array. |
| comparator | This is the function that compares two elements. |
- Returns
- This function returns a pointer to an entry in the array that matches the search key. If key is not found, a NULL pointer is returned.
◆ setenv()
| int setenv |
( |
const char * | name, |
|
|
const char * | value, |
|
|
int | replace ) |
Set a value for the environment variable.
- Parameters
-
| name | name of the variable |
| value | new value for the variable |
| replace | allow overwriting already existing variables |
- Returns
- 0 or error
- Builtin on platforms:
- ELKA, NSG, X75, SG
◆ unsetenv()
| int unsetenv |
( |
const char * | name | ) |
|
Remove environment variable.
- Parameters
-
- Returns
- 0 or error
- Builtin on platforms:
- ELKA, NSG, X75, SG
◆ getenv()
| char * getenv |
( |
const char * | name | ) |
|
Get value for the environment variable.
- Parameters
-
- Returns
- C-string
- Builtin on platforms:
- ELKA, NSG, X75, SG
◆ clearenv()
Remove all environment variables.
- Returns
- 0 or error
- Builtin on platforms:
- ELKA, NSG, X75, SG
◆ dlopen()
| int dlopen |
( |
const char * | name | ) |
|
Load the dynamic shared object (.so).
- Parameters
-
| name | path to the .so file or only name (libNAME.so) |
- Returns
- 0 or error
- Builtin on platforms:
- ELKA, NSG, X75, SG
◆ dlsym()
| void * dlsym |
( |
int | handle, |
|
|
const char * | name ) |
Get a pointer to the symbol from the previously loaded dynamic object.
- Parameters
-
| handle | descriptor obtained from _dlopen |
| name | name of the symbol |
- Returns
- pointer to the symbol
- Builtin on platforms:
- ELKA, NSG, X75, SG
◆ dlclose()
| int dlclose |
( |
int | handle | ) |
|
Close previously loaded dynamic object.
- Parameters
-
| handle | descriptor obtained from _dlopen |
- Returns
- 0 or error
- Builtin on platforms:
- ELKA, NSG, X75, SG
◆ dlerror()
| const char * dlerror |
( |
void | | ) |
|
Get a string representation of the last error in _dlopen.
- Returns
- C-string
- Builtin on platforms:
- ELKA, NSG, X75, SG