]> pd.if.org Git - pdclib/blob - includes/stdlib.h
Porting current working set from CVS.
[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 #ifndef _PDCLIB_NULL_DEFINED
25 #define _PDCLIB_NULL_DEFINED _PDCLIB_NULL_DEFINED
26 #define NULL _PDCLIB_NULL
27 #endif
28
29 /* Numeric conversion functions */
30
31 /* TODO: atof(), strtof(), strtod(), strtold() */
32
33 double atof( const char * nptr );
34 double strtod( const char * _PDCLIB_restrict nptr, char * * _PDCLIB_restrict endptr );
35 float strtof( const char * _PDCLIB_restrict nptr, char * * _PDCLIB_restrict endptr );
36 long double strtold( const char * _PDCLIB_restrict nptr, char * * _PDCLIB_restrict endptr );
37
38 /* Seperate the character array nptr into three parts: A (possibly empty)
39    sequence of whitespace characters, a character representation of an integer
40    to the given base, and trailing invalid characters (including the terminating
41    null character). If base is 0, assume it to be 10, unless the integer
42    representation starts with 0x / 0X (setting base to 16) or 0 (setting base to
43    8). If given, base can be anything from 0 to 36, using the 26 letters of the
44    base alphabet (both lowercase and uppercase) as digits 10 through 35.
45    The integer representation is then converted into the return type of the
46    function. It can start with a '+' or '-' sign. If the sign is '-', the result
47    of the conversion is negated.
48    If the conversion is successful, the converted value is returned. If endptr
49    is not a NULL pointer, a pointer to the first trailing invalid character is
50    returned in *endptr.
51    If no conversion could be performed, zero is returned (and nptr in *endptr,
52    if endptr is not a NULL pointer). If the converted value does not fit into
53    the return type, the functions return LONG_MIN, LONG_MAX, ULONG_MAX,
54    LLONG_MIN, LLONG_MAX, or ULLONG_MAX respectively, depending on the sign of
55    the integer representation and the return type, and errno is set to ERANGE.
56 */
57 long int strtol( const char * _PDCLIB_restrict nptr, char * * _PDCLIB_restrict endptr, int base );
58 long long int strtoll( const char * _PDCLIB_restrict nptr, char * * _PDCLIB_restrict endptr, int base );
59 unsigned long int strtoul( const char * _PDCLIB_restrict nptr, char * * _PDCLIB_restrict endptr, int base );
60 unsigned long long int strtoull( const char * _PDCLIB_restrict nptr, char * * _PDCLIB_restrict endptr, int base );
61
62 /* These functions are the equivalent of (int)strtol( nptr, NULL, 10 ),
63    strtol( nptr, NULL, 10 ) and strtoll(nptr, NULL, 10 ) respectively, with the
64    exception that they do not have to handle overflow situations in any defined
65    way.
66    (PDCLib does not simply forward these to their strtox() equivalents, but
67    provides a simpler atox() function that saves a couple of tests and simply
68    continues with the conversion in case of overflow.)
69 */
70 int atoi( const char * nptr );
71 long int atol( const char * nptr );
72 long long int atoll( const char * nptr );
73
74 /* Pseudo-random sequence generation functions */
75
76 extern unsigned long int _PDCLIB_seed;
77
78 #define RAND_MAX 32767
79
80 /* Returns the next number in a pseudo-random sequence, which is between 0 and
81    RAND_MAX.
82    (PDCLib uses the implementation suggested by the standard document, which is
83    next = next * 1103515245 + 12345; return (unsigned int)(next/65536) % 32768;)
84 */
85 int rand( void );
86
87 /* Initialize a new pseudo-random sequence with the starting seed. Same seeds
88    result in the same pseudo-random sequence. The default seed is 1.
89 */
90 void srand( unsigned int seed );
91
92 /* Memory management functions */
93
94 /* Allocate a chunk of heap memory of given size. If request could not be
95    satisfied, return NULL. Otherwise, return a pointer to the allocated
96    memory. Memory contents are undefined.
97 */
98 void * malloc( size_t size );
99
100 /* Allocate a chunk of heap memory that is large enough to hold nmemb elements
101    of the given size, and zero-initialize that memory. If request could not be
102    satisfied, return NULL. Otherwise, return a pointer to the allocated
103    memory.
104 */
105 void * calloc( size_t nmemb, size_t size );
106
107 /* De-allocate a chunk of heap memory previously allocated using malloc(),
108    calloc(), or realloc(), and pointed to by ptr. If ptr does not match a
109    pointer previously returned by the mentioned allocation functions, or
110    free() has already been called for this ptr, behaviour is undefined.
111 */
112 void free( void * ptr );
113
114 /* Resize a chunk of memory previously allocated with malloc() and pointed to
115    by ptr to the given size (which might be larger or smaller than the original
116    size). Returns a pointer to the reallocated memory, or NULL if the request
117    could not be satisfied. Note that the resizing might include a memcpy()
118    from the original location to a different one, so the return value might or
119    might not equal ptr. If size is larger than the original size, the value of
120    memory beyond the original size is undefined. If ptr is NULL, realloc()
121    behaves like malloc().
122 */
123 void * realloc( void * ptr, size_t size );
124
125 /* Communication with the environment */
126
127 /* These two can be passed to exit() or _Exit() as status values, to signal
128    successful and unsuccessful program termination, respectively. EXIT_SUCCESS
129    can be replaced by 0. How successful or unsuccessful program termination are
130    signaled to the environment, and what happens if exit() or _Exit() are being
131    called with a value that is neither of the three, is defined by the hosting
132    OS and its glue function.
133 */
134 #define EXIT_SUCCESS _PDCLIB_SUCCESS
135 #define EXIT_FAILURE _PDCLIB_FAILURE
136
137 /* Initiate abnormal process termination, unless programm catches SIGABRT and
138    does not return from the signal handler.
139    This implementantion flushes all streams, closes all files, and removes any
140    temporary files before exiting with EXIT_FAILURE.
141    abort() does not return.
142 */
143 void abort( void );
144
145 /* Register a function that will be called on exit(), _Exit(), or when main()
146    returns. At least 32 functions can be registered this way, and will be
147    called in reverse order of registration (last-in, first-out).
148    Returns zero if registration is successfull, nonzero if it failed.
149 */
150 int atexit( void (*func)( void ) ); 
151
152 /* Normal process termination. Functions registered by atexit() (see above) are
153    called, streams flushed, files closed and temporary files removed before the
154    program is terminated with the given status. (See comment for EXIT_SUCCESS
155    and EXIT_FAILURE above.)
156    exit() does not return.
157 */
158 void exit( int status );
159
160 /* Normal process termination. Functions registered by atexit() (see above) are
161    NOT CALLED. This implementation DOES flush streams, close files and removes
162    temporary files before the program is teminated with the given status. (See
163    comment for EXIT_SUCCESS and EXIT_FAILURE above.)
164    _Exit() does not return.
165 */
166 void _Exit( int status );
167
168 /* Search an environment-provided key-value map for the given key name, and
169    return a pointer to the associated value string (or NULL if key name cannot
170    be found). The value string pointed to might be overwritten by a subsequent
171    call to getenv(). The library never calls getenv() itself.
172    Details on the provided keys and how to set / change them are determined by
173    the hosting OS and its glue function.
174 */
175 char * getenv( const char * name );
176
177 /* If string is a NULL pointer, system() returns nonzero if a command processor
178    is available, and zero otherwise. If string is not a NULL pointer, it is
179    passed to the command processor. If system() returns, it does so with a
180    value that is determined by the hosting OS and its glue function.
181 */
182 int system( const char * string );
183
184 /* Searching and sorting */
185
186 /* Do a binary search for a given key in the array with a given base pointer,
187    which consists of nmemb elements that are of the given size each. To compare
188    the given key with an element from the array, the given function compar is
189    called (with key as first parameter and a pointer to the array member as
190    second parameter); the function should return a value less than, equal to,
191    or greater than 0 if the key is considered to be less than, equal to, or
192    greater than the array element, respectively.
193    The function returns a pointer to the first matching element found, or NULL
194    if no match is found.
195 */
196 void * bsearch( const void * key, const void * base, size_t nmemb, size_t size, int (*compar)( const void *, const void * ) );
197
198 /* Do a quicksort on an array with a given base pointer, which consists of
199    nmemb elements that are of the given size each. To compare two elements from
200    the array, the given function compar is called, which should return a value
201    less than, equal to, or greater than 0 if the first argument is considered
202    to be less than, equal to, or greater than the second argument, respectively.
203    If two elements are compared equal, their order in the sorted array is not
204    specified.
205 */
206 void qsort( void * base, size_t nmemb, size_t size, int (*compar)( const void *, const void * ) );
207
208 /* Integer arithmetic functions */
209
210 /* Return the absolute value of the argument. Note that on machines using two-
211    complement's notation (most modern CPUs), the largest negative value cannot
212    be represented as positive value. In this case, behaviour is unspecified.
213 */
214 int abs( int j );
215 long int labs( long int j );
216 long long int llabs( long long int j );
217
218 /* These structures each have a member quot and a member rem, of type int (for
219    div_t), long int (for ldiv_t) and long long it (for lldiv_t) respectively.
220    The order of the members is platform-defined to allow the div() functions
221    below to be implemented efficiently.
222 */
223 typedef struct _PDCLIB_div_t     div_t;
224 typedef struct _PDCLIB_ldiv_t   ldiv_t;
225 typedef struct _PDCLIB_lldiv_t lldiv_t;
226
227 /* Return quotient (quot) and remainder (rem) of an integer division in one of
228    the structs above.
229 */
230 div_t div( int numer, int denom );
231 ldiv_t ldiv( long int numer, long int denom );
232 lldiv_t lldiv( long long int numer, long long int denom );
233
234 /* TODO: Multibyte / wide character conversion functions */
235
236 /* TODO: Macro MB_CUR_MAX */
237
238 /*
239 int mblen( const char * s, size_t n );
240 int mbtowc( wchar_t * _PDCLIB_restrict pwc, const char * _PDCLIB_restrict s, size_t n );
241 int wctomb( char * s, wchar_t wc );
242 size_t mbstowcs( wchar_t * _PDCLIB_restrict pwcs, const char * _PDCLIB_restrict s, size_t n );
243 size_t wcstombs( char * _PDCLIB_restrict s, const wchar_t * _PDCLIB_restrict pwcs, size_t n );
244 */
245
246 #endif