]> pd.if.org Git - pdclib/blob - includes/stdlib.h
Added realloc().
[pdclib] / includes / stdlib.h
1 /* $Id$ */
2
3 /* Release $Name$ */
4
5 /* General utilities <stdlib.h>
6
7    This file is part of the Public Domain C Library (PDCLib).
8    Permission is granted to use, modify, and / or redistribute at will.
9 */
10
11 #ifndef _PDCLIB_STDLIB_H
12 #define _PDCLIB_STDLIB_H _PDCLIB_STDLIB_H
13
14 #ifndef _PDCLIB_INT_H
15 #define _PDCLIB_INT_H _PDCLIB_INT_H
16 #include <_PDCLIB_int.h>
17 #endif
18
19 #ifndef _PDCLIB_SIZE_T_DEFINED
20 #define _PDCLIB_SIZE_T_DEFINED _PDCLIB_SIZE_T_DEFINED
21 typedef _PDCLIB_size_t size_t;
22 #endif
23
24 #define NULL         _PDCLIB_NULL
25 #define EXIT_SUCCESS _PDCLIB_SUCCESS
26 #define EXIT_FAILURE _PDCLIB_FAILURE
27
28 /* Numeric conversion functions */
29
30 /* TODO: atof(), strtof(), strtod(), strtold() */
31
32 double atof( const char * nptr );
33 double strtod( const char * _PDCLIB_restrict nptr, char * * _PDCLIB_restrict endptr );
34 float strtof( const char * _PDCLIB_restrict nptr, char * * _PDCLIB_restrict endptr );
35 long double strtold( const char * _PDCLIB_restrict nptr, char * * _PDCLIB_restrict endptr );
36
37 /* Seperate the character array nptr into three parts: A (possibly empty)
38    sequence of whitespace characters, a character representation of an integer
39    to the given base, and trailing invalid characters (including the terminating
40    null character). If base is 0, assume it to be 10, unless the integer
41    representation starts with 0x / 0X (setting base to 16) or 0 (setting base to
42    8). If given, base can be anything from 0 to 36, using the 26 letters of the
43    base alphabet (both lowercase and uppercase) as digits 10 through 35.
44    The integer representation is then converted into the return type of the
45    function. It can start with a '+' or '-' sign. If the sign is '-', the result
46    of the conversion is negated.
47    If the conversion is successful, the converted value is returned. If endptr
48    is not a NULL pointer, a pointer to the first trailing invalid character is
49    returned in *endptr.
50    If no conversion could be performed, zero is returned (and nptr in *endptr,
51    if endptr is not a NULL pointer). If the converted value does not fit into
52    the return type, the functions return LONG_MIN, LONG_MAX, ULONG_MAX,
53    LLONG_MIN, LLONG_MAX, or ULLONG_MAX respectively, depending on the sign of
54    the integer representation and the return type, and errno is set to ERANGE.
55 */
56 long int strtol( const char * _PDCLIB_restrict nptr, char * * _PDCLIB_restrict endptr, int base );
57 long long int strtoll( const char * _PDCLIB_restrict nptr, char * * _PDCLIB_restrict endptr, int base );
58 unsigned long int strtoul( const char * _PDCLIB_restrict nptr, char * * _PDCLIB_restrict endptr, int base );
59 unsigned long long int strtoull( const char * _PDCLIB_restrict nptr, char * * _PDCLIB_restrict endptr, int base );
60
61 /* These functions are the equivalent of (int)strtol( nptr, NULL, 10 ),
62    strtol( nptr, NULL, 10 ) and strtoll(nptr, NULL, 10 ) respectively, with the
63    exception that they do not have to handle overflow situations in any defined
64    way.
65    (PDCLib does not simply forward these to their strtox() equivalents, but
66    provides a simpler atox() function that saves a couple of tests and simply
67    continues with the conversion in case of overflow.)
68 */
69 int atoi( const char * nptr );
70 long int atol( const char * nptr );
71 long long int atoll( const char * nptr );
72
73 /* Pseudo-random sequence generation functions */
74
75 extern unsigned long int _PDCLIB_seed;
76
77 #define RAND_MAX 32767
78
79 /* Returns the next number in a pseudo-random sequence, which is between 0 and
80    RAND_MAX.
81    (PDCLib uses the implementation suggested by the standard document, which is
82    next = next * 1103515245 + 12345; return (unsigned int)(next/65536) % 32768;)
83 */
84 int rand();
85
86 /* Initialize a new pseudo-random sequence with the starting seed. Same seeds
87    result in the same pseudo-random sequence. The default seed is 1.
88 */
89 void srand( unsigned int seed );
90
91 /* Memory management functions */
92
93 void * malloc( size_t size );
94 void * realloc( void * ptr, size_t size );
95 void free( void * ptr );
96
97 /* Communication with the environment */
98
99 #define EXIT_SUCCESS _PDCLIB_SUCCESS
100 #define EXIT_FAILURE _PDCLIB_FAILURE
101
102 void abort();
103 int atexit( void (*func)( void ) ); 
104 void exit( int status );
105 void _Exit( int status );
106 char * getenv( const char * name );
107 int system( const char * string );
108
109 /* Searching and sorting */
110
111 void * bsearch( const void * key, const void * base, size_t nmemb, size_t size, int (*compar)( const void *, const void * ) );
112 void qsort( void * base, size_t nmemb, size_t size, int (*compar)( const void *, const void * ) );
113
114 /* Integer arithmetic functions */
115
116 int abs( int j );
117 long int labs( long int j );
118 long long int llabs( long long int j );
119
120 typedef struct _PDCLIB_div_t     div_t;
121 typedef struct _PDCLIB_ldiv_t   ldiv_t;
122 typedef struct _PDCLIB_lldiv_t lldiv_t;
123
124 div_t div( int numer, int denom );
125 ldiv_t ldiv( long int numer, long int denom );
126 lldiv_t lldiv( long long int numer, long long int denom );
127
128 /* Multibyte / wide character conversion functions */
129
130 /* TODO: Macro MB_CUR_MAX */
131
132 /*
133 int mblen( const char * s, size_t n );
134 int mbtowc( wchar_t * _PDCLIB_restrict pwc, const char * _PDCLIB_restrict s, size_t n );
135 int wctomb( char * s, wchar_t wc );
136 size_t mbstowcs( wchar_t * _PDCLIB_restrict pwcs, const char * _PDCLIB_restrict s, size_t n );
137 size_t wcstombs( char * _PDCLIB_restrict s, const wchar_t * _PDCLIB_restrict pwcs, size_t n );
138 */
139
140 #endif