5 /* General utilities <stdlib.h>
7 This file is part of the Public Domain C Library (PDCLib).
8 Permission is granted to use, modify, and / or redistribute at will.
11 #ifndef _PDCLIB_STDLIB_H
12 #define _PDCLIB_STDLIB_H _PDCLIB_STDLIB_H
15 #define _PDCLIB_INT_H _PDCLIB_INT_H
16 #include <_PDCLIB_int.h>
19 #ifndef _PDCLIB_SIZE_T_DEFINED
20 #define _PDCLIB_SIZE_T_DEFINED _PDCLIB_SIZE_T_DEFINED
21 typedef _PDCLIB_size_t size_t;
24 #define NULL _PDCLIB_NULL
25 #define EXIT_SUCCESS _PDCLIB_SUCCESS
26 #define EXIT_FAILURE _PDCLIB_FAILURE
28 /* Numeric conversion functions */
30 /* TODO: atof(), strtof(), strtod(), strtold() */
32 /* Seperate the character array nptr into three parts: A (possibly empty)
33 sequence of whitespace characters, a character representation of an integer
34 to the given base, and trailing invalid characters (including the terminating
35 null character). If base is 0, assume it to be 10, unless the integer
36 representation starts with 0x / 0X (setting base to 16) or 0 (setting base to
37 8). If given, base can be anything from 0 to 36, using the 26 letters of the
38 base alphabet (both lowercase and uppercase) as digits 10 through 35.
39 The integer representation is then converted into the return type of the
40 function. It can start with a '+' or '-' sign. If the sign is '-', the result
41 of the conversion is negated.
42 If the conversion is successful, the converted value is returned. If endptr
43 is not a NULL pointer, a pointer to the first trailing invalid character is
45 If no conversion could be performed, zero is returned (and nptr in *endptr,
46 if endptr is not a NULL pointer). If the converted value does not fit into
47 the return type, the functions return LONG_MIN, LONG_MAX, ULONG_MAX,
48 LLONG_MIN, LLONG_MAX, or ULLONG_MAX respectively, depending on the sign of
49 the integer representation and the return type, and errno is set to ERANGE.
51 long int strtol( const char * _PDCLIB_restrict nptr, char * * _PDCLIB_restrict endptr, int base );
52 long long int strtoll( const char * _PDCLIB_restrict nptr, char * * _PDCLIB_restrict endptr, int base );
53 unsigned long int strtoul( const char * _PDCLIB_restrict nptr, char * * _PDCLIB_restrict endptr, int base );
54 unsigned long long int strtoull( const char * _PDCLIB_restrict nptr, char * * _PDCLIB_restrict endptr, int base );
56 /* These functions are the equivalent of (int)strtol( nptr, NULL, 10 ),
57 strtol( nptr, NULL, 10 ) and strtoll(nptr, NULL, 10 ) respectively, with the
58 exception that they do not have to handle overflow situations in any defined
60 (PDCLib does not simply forward these to their strtox() equivalents, but
61 provides a simpler atox() function that saves a couple of tests and simply
62 continues with the conversion in case of overflow.)
64 int atoi( const char * nptr );
65 long int atol( const char * nptr );
66 long long int atoll( const char * nptr );
68 /* Pseudo-random sequence generation functions */
70 extern unsigned long int _PDCLIB_seed;
72 #define RAND_MAX 32767
74 /* Returns the next number in a pseudo-random sequence, which is between 0 and
76 (PDCLib uses the implementation suggested by the standard document, which is
77 next = next * 1103515245 + 12345; return (unsigned int)(next/65536) % 32768;)
81 /* Initialize a new pseudo-random sequence with the starting seed. Same seeds
82 result in the same pseudo-random sequence. The default seed is 1.
84 void srand( unsigned int seed );
86 /* Memory management functions */
88 void * malloc( size_t size );
89 void free( void * ptr );
91 /* Communication with the environment */
94 void exit( int status );
95 void _Exit( int status );
97 /* Searching and sorting */
99 void * bsearch( const void * key, const void * base, size_t nmemb, size_t size, int (*compar)( const void *, const void * ) );
100 void qsort( void * base, size_t nmemb, size_t size, int (*compar)( const void *, const void * ) );
102 /* Integer arithmetic functions */
105 long int labs( long int j );
106 long long int llabs( long long int j );
108 typedef struct _PDCLIB_div_t div_t;
109 typedef struct _PDCLIB_ldiv_t ldiv_t;
110 typedef struct _PDCLIB_lldiv_t lldiv_t;
112 div_t div( int numer, int denom );
113 ldiv_t ldiv( long int numer, long int denom );
114 lldiv_t lldiv( long long int numer, long long int denom );
116 /* Multibyte / wide character conversion functions */
118 /* TODO: Macro MB_CUR_MAX */
120 /* Multibyte / wide string conversion functions */