/*
 * Copyright (c) 1996-2006 MIPS Technologies, Inc.
 * 
 * All rights reserved.
 * 
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions
 * are met:
 * 
 *      * Redistributions of source code must retain the above copyright
 *        notice, this list of conditions and the following disclaimer.
 *      * Redistributions in binary form must reproduce the above
 *      copyright
 *        notice, this list of conditions and the following disclaimer
 *        in the documentation and/or other materials provided with
 *        the distribution.
 *      * Neither the name of MIPS Technologies Inc. nor the names of its
 *        contributors may be used to endorse or promote products derived
 *        from this software without specific prior written permission.
 * 
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 */

/*
 * stdlib.h : ANSI stdlib definitions
 */


#ifndef __STDLIB_H
#ifdef __cplusplus
extern "C" {
#endif
#define __STDLIB_H

/* 
 * This is a ANSI-C header file only
*/

/* --- Inclusions --- */
#include <stddef.h>		/* wrong -- offsetof again */

/* --- Constants --- */
#define RAND_MAX	0x7fffffff

#define EXIT_SUCCESS	0
#define EXIT_FAILURE	1

typedef struct {int quot; int rem;} div_t;
typedef struct {long int quot; long int rem;} ldiv_t;
#ifndef __STRICT_ANSI__
typedef struct {long long int quot; long long int rem;} lldiv_t;
#endif

double	atof	(const char *);
int	atoi	(const char *);
long	atol	(const char *);
double	strtod	(const char *, char **);
long	strtol	(const char *, char **, int);
unsigned long strtoul (const char *, char **, int);
#ifndef __STRICT_ANSI__
float	strtof	(const char *, char **);
long long atoll	(const char *);
long long strtoll (const char *, char **, int);
unsigned long long strtoull (const char *, char **, int);
#endif

/* --- Pseudo-random sequence generation --- */
int	rand	(void);
int	rand_r	(unsigned int *);
void	srand	(unsigned int);
#if !defined(_ANSI_SOURCE) && !defined(_POSIX_SOURCE)
unsigned int arc4random (void);
void	arc4random_stir (void);
void	arc4random_addrandom (unsigned char *, int);
#endif

/* --- Memory management --- */
void	free	(void *);
void	*malloc	(size_t);
void	*realloc (void *, size_t);

void 	*calloc	(size_t, size_t);
void	cfree(void *);

/* system hooks */
void	abort	(void) __attribute__ ((noreturn));
void	exit	(int) __attribute__ ((noreturn));
int	atexit	(void (*)(void));
int	system	(const char *);

/* enviroment hooks */
char	*getenv	(const char *);

#if !defined(_ANSI_SOURCE) && !defined(_POSIX_SOURCE)
/* Not ANSI / POSIX defined */
int	putenv	(const char *);
int	setenv (const char *, const char *, int);
void	unsetenv (const char *);
void	deleteenv (const char *);
#endif

  /* --- Searching and sorting --- */
void	*bsearch (const void *, const void *, 
		  size_t, size_t,
		  int (*)(const void *, const void *));

void	qsort	(void *base,
		 size_t, size_t , 
		 int (*)(const void *, const void *));

/* --- Integer arithmetic --- */
int	abs	(int);
div_t	div	(int, int);

long int labs	(long int);
ldiv_t	ldiv	(long int, long int);

#ifndef __STRICT_ANSI__
long long int llabs	(long long int);
lldiv_t	lldiv	(long long int, long long int);
#endif

/* --- Multibyte characters --- */
extern int __mb_cur_max;
#define MB_CUR_MAX	__mb_cur_max 
int	mblen (const char *, size_t);
int	mbtowc (wchar_t *, const char *, size_t);
int	mbstowc (wchar_t *, const char *, size_t);
size_t	mbstowcs (wchar_t *, const char *, size_t);
int	wctomb (char *, wchar_t);
size_t	wcstombs (char *, const wchar_t *, size_t);

/* --- Macros --- */
#if defined(__OPTIMIZE__) && ! defined(__OPTIMIZE_SIZE__)
#define atoi(nptr)	((int) strtol((nptr), 0, 10))
#define atol(nptr)	strtol((nptr), 0, 10)
#define atof(nptr)	strtod((nptr), 0)
#ifndef __STRICT_ANSI__
#define atoll(nptr)	strtoll((nptr), NULL, 10)
#endif
#endif

#ifdef __cplusplus
}
#endif
#endif /* !defined __STDLIB_H */
