HelenOS sources
This source file includes following definitions.
- strtold
- rand
- srand
- atexit
- exit
- at_quick_exit
- quick_exit
- _Exit
- abort
- getenv
- system
- abs
- labs
- llabs
#include <adt/list.h>
#include <fibril_synch.h>
#include <stdlib.h>
#include <errno.h>
#include "private/libc.h"
#include "private/scanf.h"
#include "private/stdlib.h"
#include "private/stdio.h"
#include "private/sstream.h"
static int glbl_seed = 1;
static LIST_INITIALIZE(exit_handlers);
static FIBRIL_MUTEX_INITIALIZE(exit_handlers_lock);
static LIST_INITIALIZE(quick_exit_handlers);
static FIBRIL_MUTEX_INITIALIZE(quick_exit_handlers_lock);
long double strtold(const char *nptr, char **endptr)
{
int numchar;
long double ld;
errno_t rc;
FILE f;
numchar = 0;
__sstream_init(nptr, &f);
rc = __fstrtold(&f, &numchar, SIZE_MAX, &ld);
if (rc != EOK) {
ld = 0;
if (endptr != NULL)
*endptr = (char *) nptr;
errno = rc;
} else {
if (endptr != NULL)
*endptr = (char *) __sstream_getpos(&f);
}
return ld;
}
int rand(void)
{
return glbl_seed = ((1366 * glbl_seed + 150889) % RAND_MAX);
}
void srand(unsigned int seed)
{
glbl_seed = seed % RAND_MAX;
}
int atexit(void (*func)(void))
{
__exit_handler_t *entry;
entry = malloc(sizeof(__exit_handler_t));
if (entry == NULL)
return -1;
entry->func = func;
fibril_mutex_lock(&exit_handlers_lock);
list_prepend(&entry->llist, &exit_handlers);
fibril_mutex_unlock(&exit_handlers_lock);
return 0;
}
void exit(int status)
{
link_t *link;
__exit_handler_t *eh;
fibril_mutex_lock(&exit_handlers_lock);
while (!list_empty(&exit_handlers)) {
link = list_first(&exit_handlers);
list_remove(link);
fibril_mutex_unlock(&exit_handlers_lock);
eh = list_get_instance(link, __exit_handler_t, llist);
eh->func();
free(eh);
fibril_mutex_lock(&exit_handlers_lock);
}
fibril_mutex_unlock(&exit_handlers_lock);
_Exit(status);
}
int at_quick_exit(void (*func)(void))
{
__exit_handler_t *entry;
entry = malloc(sizeof(__exit_handler_t));
if (entry == NULL)
return -1;
entry->func = func;
fibril_mutex_lock(&exit_handlers_lock);
list_prepend(&entry->llist, &exit_handlers);
fibril_mutex_unlock(&exit_handlers_lock);
return 0;
}
void quick_exit(int status)
{
link_t *link;
__exit_handler_t *eh;
fibril_mutex_lock(&quick_exit_handlers_lock);
while (!list_empty(&quick_exit_handlers)) {
link = list_first(&quick_exit_handlers);
list_remove(link);
fibril_mutex_unlock(&quick_exit_handlers_lock);
eh = list_get_instance(link, __exit_handler_t, llist);
eh->func();
free(eh);
fibril_mutex_lock(&quick_exit_handlers_lock);
}
fibril_mutex_unlock(&quick_exit_handlers_lock);
_Exit(status);
}
void _Exit(int status)
{
__libc_exit(status);
}
void abort(void)
{
__libc_abort();
}
char *getenv(const char *name)
{
(void) name;
return NULL;
}
int system(const char *string)
{
if (string == NULL)
return 0;
return 1;
}
int abs(int j)
{
int aj;
if (j < 0) {
aj = -j;
assert(aj >= 0);
} else {
aj = j;
}
return aj;
}
long labs(long j)
{
long aj;
if (j < 0) {
aj = -j;
assert(aj >= 0);
} else {
aj = j;
}
return aj;
}
long long llabs(long long j)
{
long long aj;
if (j < 0) {
aj = -j;
assert(aj >= 0);
} else {
aj = j;
}
return aj;
}
div_t div(int numer, int denom)
{
div_t d;
d.quot = numer / denom;
d.rem = numer % denom;
return d;
}
ldiv_t ldiv(long numer, long denom)
{
ldiv_t d;
d.quot = numer / denom;
d.rem = numer % denom;
return d;
}
lldiv_t lldiv(long long numer, long long denom)
{
lldiv_t d;
d.quot = numer / denom;
d.rem = numer % denom;
return d;
}
HelenOS homepage, sources at GitHub