/*
 *   stdlib.h -- ANSI 
 *
 *   Functions, types, and macros that are of general utility.
 *
 *           Copyright (c) 1990, MetaWare Incorporated
 */

#ifndef _STDLIB_H
#define _STDLIB_H

#ifndef _SIZE_T_DEFINED
#define _SIZE_T_DEFINED
typedef unsigned int size_t;
#endif

#ifndef _WCHAR_T_DEFINED
#define _WCHAR_T_DEFINED
typedef char wchar_t;
#endif

#ifndef NULL
#define NULL ((void *)0)
#endif

/* MSVERSION has max and min defined here */

#define _MAX_PATH 144
#define _MAX_DRIVE 3
#define _MAX_DIR 130
#define _MAX_FNAME 9
#define _MAX_EXT 5

#ifdef __HIGHC__
extern int errno; /* errno is ansi, but doesn't belong in stdlib! */
#endif
extern int _doserrno;
extern char * _sys_errlist[];
extern int _sys_nerr;

extern char ** _environ;
#ifdef __HIGHC__
extern char ** environ;
#pragma alias(environ,"_environ");
#endif
extern unsigned int _psp;
extern int _fmode;

extern unsigned char _osmajor;
extern unsigned char _osminor;

#ifdef __HIGHC__
#define DOS_MODE 0
#define OS2_MODE 1
#endif

/* String conversions */
extern double atof(const char *__nptr);
extern int atoi(const char *__nptr);
extern long atol(const char *__nptr);
extern double strtod(const char *__nptr, char **__endptr);
extern long strtol(const char *__nptr, char **__endptr, int __base);
extern unsigned long strtoul(const char *__nptr, char **__endptr, int __base);
#ifdef __HIGHC__
extern void swab(char *,char *,int );

#ifndef tolower
extern int tolower(int );
#endif

#ifndef toupper
extern int toupper(int );
#endif
#endif


/* Random number generators */

#define RAND_MAX (sizeof(int) == 2 ? 32767 : 2147483647)
#define MB_CUR_MAX      1

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

/* Memory management (Heap) functions */
extern void *calloc(unsigned int __nelem, size_t __elsize);
extern void free(void *__ptr);
extern void *malloc(size_t __size);
extern void *realloc(void *__ptr, size_t __size);

/* Communication with the environment */
extern void abort(void);
extern void exit(int __status);
extern int atexit(void (*__func)(void));
extern void _exit(int __status);
#define EXIT_FAILURE (-1)
#define EXIT_SUCCESS 0
extern char *getenv(const char *__name);
extern int system(const char *__string);
extern int _putenv(char *);
extern void perror(const char *);
extern void _searchenv(char *,char *,char *);
extern void _splitpath(char *,char *,char *,char *,char *);
extern void _makepath(char *,char *,char *,char *,char *);

#undef abs
#undef labs
extern int abs(int __i);
extern long int labs(long int __i);
#ifdef __HIGHC__ 
#define _abs(i) (abs(i))
#define labs(i) (abs(i))
extern char * itoa(int, char *, int);
extern char * ltoa(long ,char *, int);
extern char * ultoa(unsigned long ,char *, int);
extern void perror(const char *);
extern int  putenv(char *);
#endif
extern unsigned long _lrotl(unsigned long ,int );
extern unsigned long _lrotr(unsigned long ,int );
extern unsigned short _srotl(unsigned short ,int );
extern unsigned short _srotr(unsigned short ,int );
extern unsigned int _rotl(unsigned int ,int );
extern unsigned int _rotr(unsigned int ,int );
extern unsigned char _crotl(unsigned char ,int );
extern unsigned char _crotr(unsigned char ,int );
extern void _swab(char *__from_ptr,char *__to_ptr,int __kount);
extern char * _itoa(int, char *, int);
extern char * _ltoa(long ,char *, int);
extern char * _ultoa(unsigned long ,char *, int);

#ifndef _DIV_T_DEFINED
#define _DIV_T_DEFINED
typedef struct { int quot; int rem; } div_t;
typedef struct { long quot; long rem; } ldiv_t;
#endif

ldiv_t ldiv(long int __numerator, long int __denominator);
div_t div(int __numerator, int __denominator);

extern void qsort(void *__base, size_t __nmemb, size_t __size,
		int (*__compar) (const void*, const void*));
extern void *bsearch(const void *__key, const void *__base,
	size_t __nmemb, size_t __size,
	int (*__compar) (const void *, const void *));

/* Multibyte character functions */
extern  int mblen(const char *__s, size_t __n);
extern  int mbtowc(wchar_t *__pwc, const char *__s, size_t __n);
extern  int wctomb(char *__s, wchar_t __wchar);

/* Multibyte string functions */
extern  size_t mbstowcs(wchar_t *__pwcs, const char *__s, size_t __n);
extern  size_t wcstombs(char *__s, const wchar_t *__pwcs, size_t __n);

/* onexit has been removed from the standard and replaced with atexit. */
/* We will eventually remove onexit, so modify your programs to use atexit. */
#ifdef __HIGHC__
typedef void (*(*onexit_t)(void) )(void);
/* want to say: typedef onexit_t (*onexit_t) (void); */
extern onexit_t onexit(onexit_t (*func)(void));
#endif

#endif /* _STDLIB_H */
