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