SWILIB API (ELKA)    
Loading...
Searching...
No Matches
Libc

Detailed Description

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:

Macros

#define __JMPBUF_DECL
 

Typedefs

typedef int jmp_buf[11]
 

Memory

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().
 

Memory utils

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.
 

Long jump

int setjmp (jmp_buf env)
 Save calling environment for long jump.
 
void longjmp (jmp_buf env, int val)
 Long jump.
 

Random

void srand (uint32_t seed)
 Initialize random number generator.
 
int rand (void)
 Generate random number.
 

Formatting

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.
 

String utils

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.
 

Searching & sorting

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.
 

Environment variables

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.
 

Shared libs

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.
 

Macro Definition Documentation

◆ __JMPBUF_DECL

#define __JMPBUF_DECL

Definition at line 35 of file libc.h.

Typedef Documentation

◆ jmp_buf

typedef int jmp_buf[11]

Definition at line 36 of file libc.h.

Function Documentation

◆ malloc()

void * malloc ( size_t size)

Allocate memory block.

Parameters
sizeSize 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
countNumber of elements to allocate.
sizeSize 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
ptrPointer 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).
sizeNew 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()

void free ( void * ptr)

Deallocate memory block.

Parameters
ptrPointer to a memory block previously allocated with malloc, calloc or realloc.

◆ mfree()

void mfree ( void * ptr)

Free previously allocated memory.

Parameters
ptrpointer 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
ptr1Pointer to block of memory.
ptr2Pointer to block of memory.
numNumber 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
strThis is the pointer to the block of memory where the search is performed.
cThis 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.
numThis 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
ptrPointer to the block of memory to fill.
numNumber of bytes to be set to the zero.

◆ bzero()

void bzero ( void * ptr,
int num )

Fill block of memory with zero.

Parameters
ptrPointer to the block of memory to fill.
numNumber 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
ptrPointer to the block of memory to fill.
valValue 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.
numNumber 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
destinationPointer to the destination array where the content is to be copied, type-casted to a pointer of type void*.
sourcePointer to the source of data to be copied, type-casted to a pointer of type const void*.
numNumber of bytes to copy.
Returns
destination is returned.

◆ memmove()

void * memmove ( void * destination,
const void * source,
size_t num )

Move block of memory.

Parameters
destinationPointer to the destination array where the content is to be copied, type-casted to a pointer of type void*.
sourcePointer to the source of data to be copied, type-casted to a pointer of type const void*.
numNumber of bytes to copy.
Returns
destination is returned.

◆ setjmp()

int setjmp ( jmp_buf env)

Save calling environment for long jump.

Parameters
envObject 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
envObject of type jmp_buf filled by a previous call to setjmp that contains information to restore the environment to that point.
valValue 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
seedAn integer value to be used as seed by the pseudo-random number generator algorithm.

◆ rand()

int rand ( void )

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
sPointer to a buffer where the resulting C-string is stored. The buffer should have a size of at least n characters.
nMaximum 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.
formatC 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
sPointer to a buffer where the resulting C-string is stored. The buffer should be large enough to contain the resulting string.
formatC 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
sPointer to the destination buffer.
formatC format string.
argsVariable 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
sPointer to the destination buffer.
nSize of the destination buffer.
formatC format string.
argsVariable 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
sC string that the function processes as its source to retrieve the data.
formatC 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
strC string.
characterCharacter 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
str1C string to be compared.
str2C 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
str1C string to be compared.
str2C 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
str1C string to be compared.
str2C 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
strC string.
Returns
The length of string.

◆ strcspn()

size_t strcspn ( const char * str1,
const char * str2 )

Get span until character in string.

Parameters
str1C string to be scanned.
str2C 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
destinationPointer to the destination array, which should contain a C string, and be large enough to contain the concatenated resulting string.
sourceC 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
destinationPointer 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.
sourceC string to be appended.
numMaximum 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
str1C string to be compared.
str2C string to be compared.
numMaximum number of characters to compare.
Returns
destination is returned.

◆ strcpy()

char * strcpy ( char * destination,
const char * source )

Copy string.

Parameters
destinationPointer to the destination array where the content is to be copied.
sourceC string to be copied.
Returns
destination is returned.

◆ strncpy()

char * strncpy ( char * destination,
const char * source,
size_t num )

Copy characters from string.

Parameters
destinationPointer to the destination array where the content is to be copied.
sourceC string to be copied.
numMaximum 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
strC string.
characterCharacter 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
str1C string to be scanned.
str2C 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
strC-string beginning with the representation of an integral number.
endptrReference 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.
baseNumerical 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
strC-string containing the representation of an integral number.
endptrReference 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.
baseNumerical 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
firstThis is the pointer to the first element of the array to be sorted.
nitemsThis is the number of elements in the array pointed by base.
sizeThis is the size in bytes of each element in the array.
comparatorThis 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
keyThis is the pointer to the object that serves as key for the search, type-casted as a void*.
firstThis is the pointer to the first object of the array where the search is performed, type-casted as a void*.
nitemsThis is the number of elements in the array pointed by base.
sizeThis is the size in bytes of each element in the array.
comparatorThis 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
namename of the variable
valuenew value for the variable
replaceallow 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
namename of the variable
Returns
0 or error
Builtin on platforms:
ELKA, NSG, X75, SG

◆ getenv()

char * getenv ( const char * name)

Get value for the environment variable.

Parameters
namename of the variable
Returns
C-string
Builtin on platforms:
ELKA, NSG, X75, SG

◆ clearenv()

int clearenv ( void )

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
namepath 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
handledescriptor obtained from _dlopen
namename 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
handledescriptor 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