/* General utilities <stdlib.h>12This file is part of the Public Domain C Library (PDCLib).3Permission is granted to use, modify, and / or redistribute at will.4*/56#ifndef _PDCLIB_STDLIB_H7#define _PDCLIB_STDLIB_H _PDCLIB_STDLIB_H8#include "_PDCLIB_int.h"910#ifdef __cplusplus11extern "C" {12#endif1314#ifndef _PDCLIB_SIZE_T_DEFINED15#define _PDCLIB_SIZE_T_DEFINED _PDCLIB_SIZE_T_DEFINED16typedef _PDCLIB_size_t size_t;17#endif1819#ifndef _PDCLIB_NULL_DEFINED20#define _PDCLIB_NULL_DEFINED _PDCLIB_NULL_DEFINED21#define NULL _PDCLIB_NULL22#endif2324#ifndef __cplusplus2526#ifndef _PDCLIB_WCHAR_T_DEFINED27#define _PDCLIB_WCHAR_T_DEFINED _PDCLIB_WCHAR_T_DEFINED28typedef _PDCLIB_wchar_t wchar_t;29#endif3031#endif3233#ifndef _PDCLIB_MB_CUR_MAX_DEFINED34#define _PDCLIB_MB_CUR_MAX_DEFINED35#define MB_CUR_MAX (_PDCLIB_mb_cur_max())36#endif3738/* Numeric conversion functions */3940/* TODO: atof(), strtof(), strtod(), strtold() */4142double atof( const char * nptr ) _PDCLIB_nothrow;43double strtod( const char * _PDCLIB_restrict nptr, char * * _PDCLIB_restrict endptr ) _PDCLIB_nothrow;44float strtof( const char * _PDCLIB_restrict nptr, char * * _PDCLIB_restrict endptr ) _PDCLIB_nothrow;45long double strtold( const char * _PDCLIB_restrict nptr, char * * _PDCLIB_restrict endptr ) _PDCLIB_nothrow;4647/* Seperate the character array nptr into three parts: A (possibly empty)48sequence of whitespace characters, a character representation of an integer49to the given base, and trailing invalid characters (including the terminating50null character). If base is 0, assume it to be 10, unless the integer51representation starts with 0x / 0X (setting base to 16) or 0 (setting base to528). If given, base can be anything from 0 to 36, using the 26 letters of the53base alphabet (both lowercase and uppercase) as digits 10 through 35.54The integer representation is then converted into the return type of the55function. It can start with a '+' or '-' sign. If the sign is '-', the result56of the conversion is negated.57If the conversion is successful, the converted value is returned. If endptr58is not a NULL pointer, a pointer to the first trailing invalid character is59returned in *endptr.60If no conversion could be performed, zero is returned (and nptr in *endptr,61if endptr is not a NULL pointer). If the converted value does not fit into62the return type, the functions return LONG_MIN, LONG_MAX, ULONG_MAX,63LLONG_MIN, LLONG_MAX, or ULLONG_MAX respectively, depending on the sign of64the integer representation and the return type, and errno is set to ERANGE.65*/66/* There is strtoimax() and strtoumax() in <inttypes.h> operating on intmax_t /67uintmax_t, if the long long versions do not suit your needs.68*/69long int strtol( const char * _PDCLIB_restrict nptr, char * * _PDCLIB_restrict endptr, int base ) _PDCLIB_nothrow;70long long int strtoll( const char * _PDCLIB_restrict nptr, char * * _PDCLIB_restrict endptr, int base ) _PDCLIB_nothrow;71unsigned long int strtoul( const char * _PDCLIB_restrict nptr, char * * _PDCLIB_restrict endptr, int base ) _PDCLIB_nothrow;72unsigned long long int strtoull( const char * _PDCLIB_restrict nptr, char * * _PDCLIB_restrict endptr, int base ) _PDCLIB_nothrow;7374/* These functions are the equivalent of (int)strtol( nptr, NULL, 10 ),75strtol( nptr, NULL, 10 ) and strtoll(nptr, NULL, 10 ) respectively, with the76exception that they do not have to handle overflow situations in any defined77way.78(PDCLib does not simply forward these to their strtox() equivalents, but79provides a simpler atox() function that saves a couple of tests and simply80continues with the conversion in case of overflow.)81*/82int atoi( const char * nptr ) _PDCLIB_nothrow;83long int atol( const char * nptr ) _PDCLIB_nothrow;84long long int atoll( const char * nptr ) _PDCLIB_nothrow;8586/* Pseudo-random sequence generation functions */8788extern unsigned long int _PDCLIB_seed;8990#define RAND_MAX 327679192/* Returns the next number in a pseudo-random sequence, which is between 0 and93RAND_MAX.94(PDCLib uses the implementation suggested by the standard document, which is95next = next * 1103515245 + 12345; return (unsigned int)(next/65536) % 32768;)96*/97int rand( void ) _PDCLIB_nothrow;9899/* Initialize a new pseudo-random sequence with the starting seed. Same seeds100result in the same pseudo-random sequence. The default seed is 1.101*/102void srand( unsigned int seed ) _PDCLIB_nothrow;103104/* Memory management functions */105106/* Allocate a chunk of memory of given size. If request could not be107satisfied, return NULL. Otherwise, return a pointer to the allocated108memory. Memory contents are undefined.109*/110void * malloc( size_t size ) _PDCLIB_nothrow;111112/* Allocate a chunk of memory that is large enough to hold nmemb elements of113the given size, and zero-initialize that memory. If request could not be114satisfied, return NULL. Otherwise, return a pointer to the allocated115memory.116*/117void * calloc( size_t nmemb, size_t size ) _PDCLIB_nothrow;118119/* Allocate a chunk of memory of given size, with specified alignment (which120must be a power of two; if it is not, the next greater power of two is121used). If request could not be satisfied, return NULL. Otherwise, return122a pointer to the allocated memory.123*/124void * aligned_alloc( size_t alignment, size_t size ) _PDCLIB_nothrow;125126/* De-allocate a chunk of heap memory previously allocated using malloc(),127calloc(), or realloc(), and pointed to by ptr. If ptr does not match a128pointer previously returned by the mentioned allocation functions, or129free() has already been called for this ptr, behaviour is undefined.130*/131void free( void * ptr ) _PDCLIB_nothrow;132133/* Resize a chunk of memory previously allocated with malloc() and pointed to134by ptr to the given size (which might be larger or smaller than the original135size). Returns a pointer to the reallocated memory, or NULL if the request136could not be satisfied. Note that the resizing might include a memcpy()137from the original location to a different one, so the return value might or138might not equal ptr. If size is larger than the original size, the value of139memory beyond the original size is undefined. If ptr is NULL, realloc()140behaves like malloc().141*/142void * realloc( void * ptr, size_t size ) _PDCLIB_nothrow;143144/* Communication with the environment */145146/* These two can be passed to exit() or _Exit() as status values, to signal147successful and unsuccessful program termination, respectively. EXIT_SUCCESS148can be replaced by 0. How successful or unsuccessful program termination are149signaled to the environment, and what happens if exit() or _Exit() are being150called with a value that is neither of the three, is defined by the hosting151OS and its glue function.152*/153#define EXIT_SUCCESS _PDCLIB_SUCCESS154#define EXIT_FAILURE _PDCLIB_FAILURE155156/* Initiate abnormal process termination, unless programm catches SIGABRT and157does not return from the signal handler.158This implementantion flushes all streams, closes all files, and removes any159temporary files before exiting with EXIT_FAILURE.160abort() does not return.161*/162_PDCLIB_noreturn void abort( void ) _PDCLIB_nothrow;163164/* Register a function that will be called on exit(), or when main() returns.165At least 32 functions can be registered this way, and will be called in166reverse order of registration (last-in, first-out).167Returns zero if registration is successfull, nonzero if it failed.168*/169int atexit( void (*func)( void ) ) _PDCLIB_nothrow;170171/* Register a function that will be called on quick_exit(), or when main() returns.172At least 32 functions can be registered this way, and will be called in173reverse order of registration (last-in, first-out).174Returns zero if registration is successfull, nonzero if it failed.175*/176int at_quick_exit( void (*func)( void ) ) _PDCLIB_nothrow;177178/* Normal process termination. Functions registered by atexit() (see above) are179called, streams flushed, files closed and temporary files removed before the180program is terminated with the given status. (See comment for EXIT_SUCCESS181and EXIT_FAILURE above.)182exit() does not return.183*/184_PDCLIB_noreturn void exit( int status ) _PDCLIB_nothrow;185186/* Normal process termination. Functions registered by atexit() (see above) are187NOT CALLED. This implementation DOES flush streams, close files and removes188temporary files before the program is teminated with the given status. (See189comment for EXIT_SUCCESS and EXIT_FAILURE above.)190_Exit() does not return.191*/192_PDCLIB_noreturn void _Exit( int status ) _PDCLIB_nothrow;193194/* Quick process termination. Functions registered by at_quick_exit() (see195above) are called, and the process terminated. No functions registered196with atexit() (see above) or signal handlers are called.197quick_exit() does not return.198*/199_PDCLIB_noreturn void quick_exit( int status );200201/* Search an environment-provided key-value map for the given key name, and202return a pointer to the associated value string (or NULL if key name cannot203be found). The value string pointed to might be overwritten by a subsequent204call to getenv(). The library never calls getenv() itself.205Details on the provided keys and how to set / change them are determined by206the hosting OS and its glue function.207*/208char * getenv( const char * name ) _PDCLIB_nothrow;209210/* If string is a NULL pointer, system() returns nonzero if a command processor211is available, and zero otherwise. If string is not a NULL pointer, it is212passed to the command processor. If system() returns, it does so with a213value that is determined by the hosting OS and its glue function.214*/215int system( const char * string ) _PDCLIB_nothrow;216217/* Searching and sorting */218219/* Do a binary search for a given key in the array with a given base pointer,220which consists of nmemb elements that are of the given size each. To compare221the given key with an element from the array, the given function compar is222called (with key as first parameter and a pointer to the array member as223second parameter); the function should return a value less than, equal to,224or greater than 0 if the key is considered to be less than, equal to, or225greater than the array element, respectively.226The function returns a pointer to the first matching element found, or NULL227if no match is found.228229** May throw **230*/231void * bsearch( const void * key, const void * base, size_t nmemb, size_t size, int (*compar)( const void *, const void * ) );232233/* Do a quicksort on an array with a given base pointer, which consists of234nmemb elements that are of the given size each. To compare two elements from235the array, the given function compar is called, which should return a value236less than, equal to, or greater than 0 if the first argument is considered237to be less than, equal to, or greater than the second argument, respectively.238If two elements are compared equal, their order in the sorted array is not239specified.240241** May throw **242*/243void qsort( void * base, size_t nmemb, size_t size, int (*compar)( const void *, const void * ) );244245/* Integer arithmetic functions */246247/* Return the absolute value of the argument. Note that on machines using two-248complement's notation (most modern CPUs), the largest negative value cannot249be represented as positive value. In this case, behaviour is unspecified.250*/251int abs( int j ) _PDCLIB_nothrow;252long int labs( long int j ) _PDCLIB_nothrow;253long long int llabs( long long int j ) _PDCLIB_nothrow;254255/* These structures each have a member quot and a member rem, of type int (for256div_t), long int (for ldiv_t) and long long it (for lldiv_t) respectively.257The order of the members is platform-defined to allow the div() functions258below to be implemented efficiently.259*/260typedef struct _PDCLIB_div_t div_t;261typedef struct _PDCLIB_ldiv_t ldiv_t;262typedef struct _PDCLIB_lldiv_t lldiv_t;263264/* Return quotient (quot) and remainder (rem) of an integer division in one of265the structs above.266*/267div_t div( int numer, int denom ) _PDCLIB_nothrow;268ldiv_t ldiv( long int numer, long int denom ) _PDCLIB_nothrow;269lldiv_t lldiv( long long int numer, long long int denom ) _PDCLIB_nothrow;270271/* Multibyte / wide character conversion functions */272273/* Affected by LC_CTYPE of the current locale. For state-dependent encoding,274each function is placed into its initial conversion state at program275startup, and can be returned to that state by a call with its character276pointer argument s being a null pointer.277Changing LC_CTYPE causes the conversion state to become indeterminate.278*/279280/* If s is not a null pointer, returns the number of bytes contained in the281multibyte character pointed to by s (if the next n or fewer bytes form a282valid multibyte character); -1, if they don't; or 0, if s points to the283null character.284If s is a null pointer, returns nonzero if multibyte encodings in the285current locale are stateful, and zero otherwise.286*/287int mblen( const char * s, size_t n );288289/* If s is not a null pointer, and the next n bytes (maximum) form a valid290multibyte character sequence (possibly including shift sequences), the291corresponding wide character is stored in pwc (unless that is a null292pointer). If the wide character is the null character, the function is293left in the initial conversion state.294Returns the number of bytes in the consumed multibyte character sequence;295or 0, if the resulting wide character is the null character. If the next296n bytes do not form a valid sequence, returns -1.297In no case will the returned value be greater than n or MB_CUR_MAX.298If s is a null pointer, returns nonzero if multibyte encodings in the299current locale are stateful, and zero otherwise.300*/301int mbtowc( wchar_t * _PDCLIB_restrict pwc, const char * _PDCLIB_restrict s, size_t n );302303/* Converts the wide character wc into the corresponding multibyte character304sequence (including shift sequences). If s is not a null pointer, the305multibyte sequence (at most MB_CUR_MAX characters) is stored at that306location. If wc is a null character, a null byte is stored, preceded by307any shift sequence needed to restore the initial shift state, and the308function is left in the initial conversion state.309Returns the number of bytes in the generated multibyte character sequence.310If wc does not correspond to a valid multibyte character, returns -1.311In no case will the returned value be greater than MB_CUR_MAX.312If s is a null pointer, returns nonzero if multibyte encodings in the313current locale are stateful, and zero otherwise.314*/315int wctomb( char * s, wchar_t wc );316317/* Convert a sequence of multibyte characters beginning in the initial shift318state from the array pointed to by s into the corresponding wide character319sequence, storing no more than n wide characters into pwcs. A null320character is converted into a null wide character, and marks the end of321the multibyte character sequence.322If copying takes place between objects that overlap, behaviour is323undefined.324Returns (size_t)-1 if an invalid multibyte sequence is encountered.325Otherwise, returns the number of array elements modified, not including326a terminating null wide character, if any. (Target string will not be327null terminated if the return value equals n.)328*/329size_t mbstowcs( wchar_t * _PDCLIB_restrict pwcs, const char * _PDCLIB_restrict s, size_t n );330331/* Convert a sequence of wide characters from the array pointed to by pwcs332into a sequence of corresponding multibyte characters, beginning in the333initial shift state, storing them in the array pointed to by s, stopping334if the next multibyte character would exceed the limit of n total bytes335or a null character is stored.336If copying takes place between objects that overlap, behaviour is337undefined.338Returns (size_t)-1 if a wide character is encountered that does not339correspond to a valid multibyte character. Otherwise, returns the number340of array elements modified, not including a terminating null character,341if any. (Target string will not be null terminated if the return value342equals n.)343*/344size_t wcstombs( char * _PDCLIB_restrict s, const wchar_t * _PDCLIB_restrict pwcs, size_t n );345346#ifdef __cplusplus347}348#endif349350#endif351352353