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