]> pd.if.org Git - pdclib/blobdiff - includes/stdlib.h
Added realloc().
[pdclib] / includes / stdlib.h
index c0efa68273dff2c5228ec7c517dfb06494ceacb4..4d6fc53c91d1c4107ae27ec9be0083d7d7130488 100644 (file)
-// ----------------------------------------------------------------------------
-// $Id$
-// ----------------------------------------------------------------------------
-// Public Domain C Library - http://pdclib.sourceforge.net
-// This code is Public Domain. Use, modify, and redistribute at will.
-// ----------------------------------------------------------------------------
-// General utilities
-// ----------------------------------------------------------------------------
-
-#ifndef __STDLIB_H
-#define __STDLIB_H __STDLIB_H
-
-// ----------------------------------------------------------------------------
-// MACROS
-
-#define EXIT_FAILURE // TODO
-#define EXIT_SUCCESS // TODO
-#define MB_CUR_MAX   // TODO
-#define NULL         0
-#define RAND_MAX     // TODO
-
-// ----------------------------------------------------------------------------
-// TYPEDEFS
-
-typedef struct
-{
-    int quotient;
-    int remainder;
-} div_t;
-
-typedef struct
-{
-    long quotient;
-    long remainder;
-} ldiv_t;
+/* $Id$ */
+
+/* Release $Name$ */
+
+/* General utilities <stdlib.h>
+
+   This file is part of the Public Domain C Library (PDCLib).
+   Permission is granted to use, modify, and / or redistribute at will.
+*/
+
+#ifndef _PDCLIB_STDLIB_H
+#define _PDCLIB_STDLIB_H _PDCLIB_STDLIB_H
+
+#ifndef _PDCLIB_INT_H
+#define _PDCLIB_INT_H _PDCLIB_INT_H
+#include <_PDCLIB_int.h>
+#endif
+
+#ifndef _PDCLIB_SIZE_T_DEFINED
+#define _PDCLIB_SIZE_T_DEFINED _PDCLIB_SIZE_T_DEFINED
+typedef _PDCLIB_size_t size_t;
+#endif
+
+#define NULL         _PDCLIB_NULL
+#define EXIT_SUCCESS _PDCLIB_SUCCESS
+#define EXIT_FAILURE _PDCLIB_FAILURE
+
+/* Numeric conversion functions */
+
+/* TODO: atof(), strtof(), strtod(), strtold() */
+
+double atof( const char * nptr );
+double strtod( const char * _PDCLIB_restrict nptr, char * * _PDCLIB_restrict endptr );
+float strtof( const char * _PDCLIB_restrict nptr, char * * _PDCLIB_restrict endptr );
+long double strtold( const char * _PDCLIB_restrict nptr, char * * _PDCLIB_restrict endptr );
+
+/* Seperate the character array nptr into three parts: A (possibly empty)
+   sequence of whitespace characters, a character representation of an integer
+   to the given base, and trailing invalid characters (including the terminating
+   null character). If base is 0, assume it to be 10, unless the integer
+   representation starts with 0x / 0X (setting base to 16) or 0 (setting base to
+   8). If given, base can be anything from 0 to 36, using the 26 letters of the
+   base alphabet (both lowercase and uppercase) as digits 10 through 35.
+   The integer representation is then converted into the return type of the
+   function. It can start with a '+' or '-' sign. If the sign is '-', the result
+   of the conversion is negated.
+   If the conversion is successful, the converted value is returned. If endptr
+   is not a NULL pointer, a pointer to the first trailing invalid character is
+   returned in *endptr.
+   If no conversion could be performed, zero is returned (and nptr in *endptr,
+   if endptr is not a NULL pointer). If the converted value does not fit into
+   the return type, the functions return LONG_MIN, LONG_MAX, ULONG_MAX,
+   LLONG_MIN, LLONG_MAX, or ULLONG_MAX respectively, depending on the sign of
+   the integer representation and the return type, and errno is set to ERANGE.
+*/
+long int strtol( const char * _PDCLIB_restrict nptr, char * * _PDCLIB_restrict endptr, int base );
+long long int strtoll( const char * _PDCLIB_restrict nptr, char * * _PDCLIB_restrict endptr, int base );
+unsigned long int strtoul( const char * _PDCLIB_restrict nptr, char * * _PDCLIB_restrict endptr, int base );
+unsigned long long int strtoull( const char * _PDCLIB_restrict nptr, char * * _PDCLIB_restrict endptr, int base );
+
+/* These functions are the equivalent of (int)strtol( nptr, NULL, 10 ),
+   strtol( nptr, NULL, 10 ) and strtoll(nptr, NULL, 10 ) respectively, with the
+   exception that they do not have to handle overflow situations in any defined
+   way.
+   (PDCLib does not simply forward these to their strtox() equivalents, but
+   provides a simpler atox() function that saves a couple of tests and simply
+   continues with the conversion in case of overflow.)
+*/
+int atoi( const char * nptr );
+long int atol( const char * nptr );
+long long int atoll( const char * nptr );
+
+/* Pseudo-random sequence generation functions */
+
+extern unsigned long int _PDCLIB_seed;
+
+#define RAND_MAX 32767
+
+/* Returns the next number in a pseudo-random sequence, which is between 0 and
+   RAND_MAX.
+   (PDCLib uses the implementation suggested by the standard document, which is
+   next = next * 1103515245 + 12345; return (unsigned int)(next/65536) % 32768;)
+*/
+int rand();
+
+/* Initialize a new pseudo-random sequence with the starting seed. Same seeds
+   result in the same pseudo-random sequence. The default seed is 1.
+*/
+void srand( unsigned int seed );
 
-typedef struct
-{   long long quotient;
-    long long remainder;
-} lldiv_t;
+/* Memory management functions */
 
-typedef size_t; // TODO
+void * malloc( size_t size );
+void * realloc( void * ptr, size_t size );
+void free( void * ptr );
 
-#ifndef __cplusplus
-typedef wchar_t; // TODO
-#endif // __cplusplus
+/* Communication with the environment */
 
-// ----------------------------------------------------------------------------
-// FUNCTIONS - C++
+#define EXIT_SUCCESS _PDCLIB_SUCCESS
+#define EXIT_FAILURE _PDCLIB_FAILURE
 
-#ifdef __cplusplus
+void abort();
+int atexit( void (*func)( void ) ); 
+void exit( int status );
+void _Exit( int status );
+char * getenv( const char * name );
+int system( const char * string );
 
-long abs( long i );
-long long abs( long long i );
+/* Searching and sorting */
 
-ldiv_t div( long numer, long denom );
-lldiv_t div( long long numer, long long denom );
+void * bsearch( const void * key, const void * base, size_t nmemb, size_t size, int (*compar)( const void *, const void * ) );
+void qsort( void * base, size_t nmemb, size_t size, int (*compar)( const void *, const void * ) );
 
-#endif // __cplusplus
+/* Integer arithmetic functions */
 
-// ----------------------------------------------------------------------------
-// FUNCTIONS - Standard C
+int abs( int j );
+long int labs( long int j );
+long long int llabs( long long int j );
 
-int abs( int i );
-long long llabs( long long i );
-long labs( long i );
+typedef struct _PDCLIB_div_t     div_t;
+typedef struct _PDCLIB_ldiv_t   ldiv_t;
+typedef struct _PDCLIB_lldiv_t lldiv_t;
 
 div_t div( int numer, int denom );
-lldiv_t lldiv( long long numer, long long denom );
-ldiv_t ldiv( long numer, long denom );
+ldiv_t ldiv( long int numer, long int denom );
+lldiv_t lldiv( long long int numer, long long int denom );
 
-int rand( void );
-void srand( unsigned int seed );
+/* Multibyte / wide character conversion functions */
 
-double atof( const char * s );
-int atoi( const char * s );
-long atol( const char * s );
-long long atoll( const char * s);
-
-double strtod( const char * restrict s, char * * restrict endptr );
-float strtof( const char * restrict s, char * * restrict endptr );
-long double strtold( const char * restrict s, char * * restrict endptr );
-
-long long strtoll( const char * restrict s, char * * restrict endptr, int base );
-unsigned long long strtoull( const char * restrict s, char * * restrict endptr, int base);
-
-long strtol( const char * restrict s, char * * restrict endptr, int base );
-unsigned long strtoul( const char * restrict s, char * * restrict endptr, int base);
-
-void * calloc( size_t nelem, size_t size );
-void free( void * ptr );
-void * malloc( size_t size );
-void * realloc( void * ptr, size_t size );
+/* TODO: Macro MB_CUR_MAX */
 
+/*
 int mblen( const char * s, size_t n );
-size_t mbstowcs( wchar_t * restrict wcs, const char * restrict s, size_t n );
-int mbtowc( wchar_t * restrict pwc, const char * restrict s, size_t n );
-size_t wcstombs( char * restrict s, const wchar_t * restrict wcs, size_t n );
-int wctomb( char * s, wchar_t wchar );
-
-void _Exit( int status );
-void exit( int status );
-void abort( void );
-char * getenv( const char * name );
-int system( const char * s );
-
-#ifdef __cplusplus
-#define __cppwrapper( x ) extern "C++" x \
-extern "C" x
-#else
-#define __cppwrapper( x ) x
-#endif // __cplusplus
-
-__cppwrapper( int atexit( void (*func) ( void ) ) { /* TODO */ }; )
-__cppwrapper( void * bsearch( const void * key, const void * base, size_t nelem, size_t size, int (*cmp) ( const void * ck, const void * ce) ) { /* TODO */ }; )
-__cppwrapper( void qsort( void * base, size_t nelem, size_t size, int (*cmp) ( const void * e1, const void * e2) ) { /* TODO */ }; )
+int mbtowc( wchar_t * _PDCLIB_restrict pwc, const char * _PDCLIB_restrict s, size_t n );
+int wctomb( char * s, wchar_t wc );
+size_t mbstowcs( wchar_t * _PDCLIB_restrict pwcs, const char * _PDCLIB_restrict s, size_t n );
+size_t wcstombs( char * _PDCLIB_restrict s, const wchar_t * _PDCLIB_restrict pwcs, size_t n );
+*/
 
-#endif // __STDLIB_H
+#endif