//----------------------------------------------------------------
//
// NeuroChip NM6403, C++ library
// 1997-1999 (c) RC Module Inc.
// cstdlib,
//
//----------------------------------------------------------------


#ifndef _CSTDLIB_Included
#define _CSTDLIB_Included


#include <_nsstd.h>

#include <cstddef>
#include <cerrno>


_NS_STD_BEGIN


    // The maximum value returned by rand()
#define RAND_MAX 32767

    // Typical return values of exit() function
#define EXIT_SUCCESS 0
#define EXIT_FAILURE 1

typedef struct {
        int     quot;
        int     rem;
} div_t;

typedef struct {
        long    quot;
        long    rem;
} ldiv_t;

#ifdef __cplusplus
extern "C" {
#endif

    // The variable containing the last runtime error code

extern  int    errno;

    // Program termination functions

void            exit(int __status);
int             atexit(void ( *__func)(void));
void            abort(void);
void            _exit(int __status);

    // Arithmetic functions

int             abs(int __x);
long            labs(long __x);
div_t           div(int __numer, int __denom);
ldiv_t          ldiv(long __numer, long __denom);

    // string to integer conversion functions

int             atoi(const char  *__s);
long            atol(const char  *__s);
long            strtol(const char  *__s, char  * *__endptr,int __radix);
unsigned long   strtoul(const char  *__s, char  * *__endptr,
                 int __radix);

    // string to double conversion functions

double          atof(const char  *__s);
double          strtod(const char  *__s, char  * *__endptr);

    // Dynamic memory management functions

void *          malloc(size_t __size);
void *          malloc0(size_t __size);
void *          malloc1(size_t __size);
void *          malloc2(size_t __size);
void *          malloc3(size_t __size);

void *          realloc(void * __ptr, size_t __size);
void *          realloc0(void * __ptr, size_t __size);
void *          realloc1(void * __ptr, size_t __size);
void *          realloc2(void * __ptr, size_t __size);
void *          realloc3(void * __ptr, size_t __size);

void *          calloc(size_t __nmemb, size_t __size);
void *          calloc0(size_t __nmemb, size_t __size);
void *          calloc1(size_t __nmemb, size_t __size);
void *          calloc2(size_t __nmemb, size_t __size);
void *          calloc3(size_t __nmemb, size_t __size);

void            free(void * __ptr);
void            free0(void * __ptr);
void            free1(void * __ptr);
void            free2(void * __ptr);
void            free3(void * __ptr);


    // Random number generator functions

int             rand(void);
void            srand(unsigned __seed);

    // Search and sorting functions

/*
void  *         bsearch(const void  *__key, const void  *__base,
                     size_t __nelem, size_t __width,
                     int ( *fcmp)(const void  *,
                     const void  *));
void            qsort(void  *__base, size_t __nelem, size_t __width,
                      int  (* __fcmp)(const void  *, const void  *));
*/

#ifdef __cplusplus
}
#endif

    // Library extensions, specified by the C++ Standard

#ifdef __cplusplus

    // Overloaded declaration: long   abs(long);        // labs()

inline long abs (long a)
{
    return labs(a);
}

#endif


_NS_STD_END


#endif  // _CSTDLIB_Included
