2005-04-30 15:48:48 +02:00
|
|
|
#ifndef STDLIB_H
|
|
|
|
#define STDLIB_H
|
|
|
|
|
2009-05-01 16:41:06 +02:00
|
|
|
FILE_LICENCE ( GPL2_OR_LATER );
|
|
|
|
|
2007-01-19 01:39:12 +01:00
|
|
|
#include <stdint.h>
|
2007-01-19 03:02:59 +01:00
|
|
|
#include <assert.h>
|
2010-07-18 17:52:43 +02:00
|
|
|
#include <ctype.h>
|
2007-01-19 03:02:59 +01:00
|
|
|
|
|
|
|
/*****************************************************************************
|
|
|
|
*
|
|
|
|
* Numeric parsing
|
|
|
|
*
|
|
|
|
****************************************************************************
|
|
|
|
*/
|
2007-01-19 01:39:12 +01:00
|
|
|
|
2010-07-18 17:52:43 +02:00
|
|
|
static inline int strtoul_base ( const char **pp, int base )
|
|
|
|
{
|
|
|
|
const char *p = *pp;
|
|
|
|
|
|
|
|
while ( isspace ( *p ) )
|
|
|
|
p++;
|
|
|
|
|
|
|
|
if ( base == 0 ) {
|
|
|
|
base = 10;
|
|
|
|
if ( *p == '0' ) {
|
|
|
|
p++;
|
|
|
|
base = 8;
|
|
|
|
if ( ( *p | 0x20 ) == 'x' ) {
|
|
|
|
p++;
|
|
|
|
base = 16;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
*pp = p;
|
|
|
|
|
|
|
|
return base;
|
|
|
|
}
|
|
|
|
|
|
|
|
static inline unsigned int strtoul_charval ( unsigned int charval )
|
|
|
|
{
|
|
|
|
if ( charval >= 'a' ) {
|
|
|
|
charval = ( charval - 'a' + 10 );
|
|
|
|
} else if ( charval >= 'A' ) {
|
|
|
|
charval = ( charval - 'A' + 10 );
|
|
|
|
} else if ( charval <= '9' ) {
|
|
|
|
charval = ( charval - '0' );
|
|
|
|
}
|
|
|
|
|
|
|
|
return charval;
|
|
|
|
}
|
|
|
|
|
2006-04-30 13:45:38 +02:00
|
|
|
extern unsigned long strtoul ( const char *p, char **endp, int base );
|
2010-07-18 17:52:43 +02:00
|
|
|
extern unsigned long long strtoull ( const char *p, char **endp, int base );
|
|
|
|
|
2007-01-19 03:02:59 +01:00
|
|
|
|
|
|
|
/*****************************************************************************
|
|
|
|
*
|
|
|
|
* Memory allocation
|
|
|
|
*
|
|
|
|
****************************************************************************
|
|
|
|
*/
|
|
|
|
|
2007-08-20 20:29:50 +02:00
|
|
|
extern void * __malloc malloc ( size_t size );
|
2007-01-19 03:02:59 +01:00
|
|
|
extern void * realloc ( void *old_ptr, size_t new_size );
|
2006-12-02 21:10:21 +01:00
|
|
|
extern void free ( void *ptr );
|
2007-08-20 20:29:50 +02:00
|
|
|
extern void * __malloc zalloc ( size_t len );
|
2007-01-18 13:54:18 +01:00
|
|
|
|
2006-12-02 21:10:21 +01:00
|
|
|
/**
|
|
|
|
* Allocate cleared memory
|
|
|
|
*
|
|
|
|
* @v nmemb Number of members
|
|
|
|
* @v size Size of each member
|
|
|
|
* @ret ptr Allocated memory
|
|
|
|
*
|
|
|
|
* Allocate memory as per malloc(), and zero it.
|
|
|
|
*
|
2007-01-18 13:54:18 +01:00
|
|
|
* This is implemented as a static inline, with the body of the
|
2007-06-11 22:36:10 +02:00
|
|
|
* function in zalloc(), since in most cases @c nmemb will be 1 and
|
2007-01-18 13:54:18 +01:00
|
|
|
* doing the multiply is just wasteful.
|
2006-12-02 21:10:21 +01:00
|
|
|
*/
|
2007-08-20 20:29:50 +02:00
|
|
|
static inline void * __malloc calloc ( size_t nmemb, size_t size ) {
|
2007-06-11 22:36:10 +02:00
|
|
|
return zalloc ( nmemb * size );
|
2006-12-02 21:10:21 +01:00
|
|
|
}
|
2005-04-30 15:48:48 +02:00
|
|
|
|
2007-01-19 03:02:59 +01:00
|
|
|
/*****************************************************************************
|
|
|
|
*
|
|
|
|
* Random number generation
|
|
|
|
*
|
|
|
|
****************************************************************************
|
|
|
|
*/
|
|
|
|
|
|
|
|
extern long int random ( void );
|
|
|
|
extern void srandom ( unsigned int seed );
|
|
|
|
|
|
|
|
static inline int rand ( void ) {
|
|
|
|
return random();
|
|
|
|
}
|
|
|
|
|
|
|
|
static inline void srand ( unsigned int seed ) {
|
|
|
|
srandom ( seed );
|
|
|
|
}
|
|
|
|
|
|
|
|
/*****************************************************************************
|
|
|
|
*
|
|
|
|
* Miscellaneous
|
|
|
|
*
|
|
|
|
****************************************************************************
|
|
|
|
*/
|
|
|
|
|
|
|
|
extern int system ( const char *command );
|
2008-11-19 01:18:32 +01:00
|
|
|
extern __asmcall int main ( void );
|
2007-01-19 03:02:59 +01:00
|
|
|
|
2005-04-30 15:48:48 +02:00
|
|
|
#endif /* STDLIB_H */
|