// Rowley C Compiler, runtime support. // // Copyright (c) 2001, 2002, 2009, 2010 Rowley Associates Limited. // // This file may be distributed under the terms of the License Agreement // provided with this software. // // THIS FILE IS PROVIDED AS IS WITH NO WARRANTY OF ANY KIND, INCLUDING THE // WARRANTY OF DESIGN, MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.#ifndef __stdlib_H #define __stdlib_H #include "__crossworks.h" #ifdef __cplusplusextern"C" { #endif #ifndef __SIZE_T_DEFINED #define __SIZE_T_DEFINEDtypedef__SIZE_Tsize_t; #endif #ifndef __WCHAR_T_DEFINED #define __WCHAR_T_DEFINEDtypedefunsignedwchar_t; #endif #ifndef NULL #define NULL 0 #endif/*!\briefEXIT_SUCCESS\ingroupMacros\synopsis\desc\b\thispass to\refexit on successful termination. */#define EXIT_SUCCESS 0/*!\briefEXIT_FAILURE\ingroupMacros\synopsis\desc\b\thispass to\refexit on unsuccessful termination. */#define EXIT_FAILURE 1/*!\briefRAND_MAX\ingroupMacros\synopsis\desc\b\thisexpands to an integer constant expression that is the maximum value returned by\refrand. */#define RAND_MAX 32767/*!\briefStructure containing quotient and remainder after division of an int\ingroupTypes\desc\b\thisstores the quotient and remainder returned by\refdiv. */typedefstruct{intquot;intrem; } div_t;/*!\briefStructure containing quotient and remainder after division of a long\ingroupTypes\desc\b\thisstores the quotient and remainder returned by\refldiv. */typedefstruct{longquot;longrem; } ldiv_t;/*!\briefStructure containing quotient and remainder after division of a long long\ingroupTypes\desc\b\thisstores the quotient and remainder returned by\reflldiv. */typedefstruct{longlongquot;longrem; } lldiv_t;/*!\briefReturn an integer absolute value\ingroupInteger arithmetic functions\synopsis\desc\b\thisreturns the absolute value of the integer argument\aj. */intabs(int__j);/*!\briefReturn a long integer absolute value\ingroupInteger arithmetic functions\synopsis\desc\b\thisreturns the absolute value of the long integer argument\aj. */longintlabs(longint__j);/*!\briefReturn a long long integer absolute value\ingroupInteger arithmetic functions\synopsis\desc\b\thisreturns the absolute value of the long long integer argument\aj. */longlongintllabs(longlongint__j);/*!\briefDivide two ints returning quotient and remainder\ingroupInteger arithmetic functions\synopsis\desc\b\thiscomputes\anumer /\adenom and\anumer %\adenom in a single operation.\b\thisreturns a structure of type\refdiv_t comprising both the quotient and the remainder. The structures contain the members\bquot (the quotient) and\brem (the remainder), each of which has the same type as the arguments\anumer and\adenom. If either part of the result cannot be represented, the behavior is undefined.\sa\refdiv_t */div_t div(int__numer,int__denom);/*!\briefDivide two longs returning quotient and remainder\ingroupInteger arithmetic functions\synopsis\desc\b\thiscomputes\anumer /\adenom and\anumer %\adenom in a single operation.\b\thisreturns a structure of type\refldiv_t comprising both the quotient and the remainder. The structures contain the members\bquot (the quotient) and\brem (the remainder), each of which has the same type as the arguments\bnumer and\bdenom. If either part of the result cannot be represented, the behavior is undefined.\sa\refldiv_t */ldiv_t ldiv(longint__numer,longint__denom);/*!\briefDivide two long longs returning quotient and remainder\ingroupInteger arithmetic functions\synopsis\b\thiscomputes\bnumer /\bdenom and\bnumer %\bdenom in a single operation.\b\thisreturns a structure of type\reflldiv_t comprising both the quotient and the remainder. The structures contain the members\bquot (the quotient) and\brem (the remainder), each of which has the same type as the arguments\bnumer and\bdenom. If either part of the result cannot be represented, the behavior is undefined.\sa\reflldiv_t */lldiv_t lldiv(longlongint__numer,longlongint__denom);/*!\briefAllocate space for a single object\ingroupMemory allocation functions\synopsis\desc\b\thisallocates space for an object whose size is specified by 'b size and whose value is indeterminate.\b\thisreturns a null pointer if the space for the object cannot be allocated from free memory; if space for the object can be allocated,\b\thisreturns a pointer to the start of the allocated space. */void*malloc(size_t__size);/*!\briefAllocate space for an array of objects and initialize them to zero\ingroupMemory allocation functions\synopsis\desc\b\thisallocates space for an array of\bnmemb objects, each of whose size is\bsize. The space is initialized to all zero bits.\b\thisreturns a null pointer if the space for the array of object cannot be allocated from free memory; if space for the array can be allocated,\bcalloc returns a pointer to the start of the allocated space. */void*calloc(size_t__nobj,size_t__size);/*!\briefResizes allocated memory space or allocates memory space\ingroupMemory allocation functions\synopsis\desc\b\thisdeallocates the old object pointed to by\bptr and returns a pointer to a new object that has the size specified by\bsize. The contents of the new object is identical to that of the old object prior to deallocation, up to the lesser of the new and old sizes. Any bytes in the new object beyond the size of the old object have indeterminate values. If\bptr is a null pointer,\b\thisbehaves like\thisfor the specified size. If memory for the new object cannot be allocated, the old object is not deallocated and its value is unchanged.\b\thisreturns a pointer to the new object (which may have the same value as a pointer to the old object), or a null pointer if the new object could not be allocated. If\bptr does not match a pointer earlier returned by\bcalloc,\bmalloc, or\b\this, or if the space has been deallocated by a call to\bfree or\b\this, the behavior is undefined. */void*realloc(void*p,size_t__size);/*!\briefFrees allocated memory for reuse\ingroupMemory allocation functions\synopsis\desc\b\thiscauses the space pointed to by\bptr to be deallocated, that is, made available for further allocation. If\bptr is a null pointer, no action occurs. If\bptr does not match a pointer earlier returned by\bcalloc,\bmalloc, or\brealloc, or if the space has been deallocated by a call to\b\thisor\brealloc, the behavior is undefined. */voidfree(void*__p);/*!\briefConvert string to double\ingroupString to number conversions\synopsis\desc\b\thisconverts the initial portion of the string pointed to by\bnptr to a\bdouble representation.\b\thisdoes not affect the value of\berrno on an error. If the value of the result cannot be represented, the behavior is undefined. Except for the behavior on error,\b\thisis equivalent to\tt{strtod(nptr, (char **)NULL)}.\b\thisreturns the converted value.\sa\refstrtod */doubleatof(constchar*__nptr);/*!\briefConvert string to double\ingroupString to number conversions\synopsis\desc\b\thisconverts the initial portion of the string pointed to by\bnptr to a\bdouble representation. First,\b\thisdecomposes the input string into three parts: an initial, possibly empty, sequence of white-space characters (as specified by\refisspace), a subject sequence resembling a floating-point constant, and a final string of one or more unrecognized characters, including the terminating null character of the input string.\b\thisthen attempts to convert the subject sequence to a floating-point number, and return the result. The subject sequence is defined as the longest initial subsequence of the input string, starting with the first non-white-space character, that is of the expected form. The subject sequence contains no characters if the input string is empty or consists entirely of white space, or if the first non-white-space character is other than a sign or a permissible letter or digit. The expected form of the subject sequence is an optional plus or minus sign followed by a nonempty sequence of decimal digits optionally containing a decimal-point character, then an optional exponent part. If the subject sequence begins with a minus sign, the value resulting from the conversion is negated. A pointer to the final string is stored in the object pointed to by\b\this, provided that\bendptr is not a null pointer. If the subject sequence is empty or does not have the expected form, no conversion is performed, the value of\bnptr is stored in the object pointed to by\bendptr, provided that\bendptr is not a null pointer.\b\thisreturns the converted value, if any. If no conversion could be performed, zero is returned. If the correct value is outside the range of representable values,\bHUGE_VAL is returned according to the sign of the value, if any, and the value of the macro\referrno is stored in\referrno. */doublestrtod(constchar*__nptr,char**__endptr);/*!\briefConvert string to float\ingroupString to number conversions\synopsis\desc\b\thisconverts the initial portion of the string pointed to by\bnptr to a\bdouble representation. First,\b\thisdecomposes the input string into three parts: an initial, possibly empty, sequence of white-space characters (as specified by\refisspace), a subject sequence resembling a floating-point constant, and a final string of one or more unrecognized characters, including the terminating null character of the input string.\b\thisthen attempts to convert the subject sequence to a floating-point number, and return the result. The subject sequence is defined as the longest initial subsequence of the input string, starting with the first non-white-space character, that is of the expected form. The subject sequence contains no characters if the input string is empty or consists entirely of white space, or if the first non-white-space character is other than a sign or a permissible letter or digit. The expected form of the subject sequence is an optional plus or minus sign followed by a nonempty sequence of decimal digits optionally containing a decimal-point character, then an optional exponent part. If the subject sequence begins with a minus sign, the value resulting from the conversion is negated. A pointer to the final string is stored in the object pointed to by\bendptr, provided that\bendptr is not a null pointer. If the subject sequence is empty or does not have the expected form, no conversion is performed, the value of\bnptr is stored in the object pointed to by\bendptr, provided that\bendptr is not a null pointer.\b\thisreturns the converted value, if any. If no conversion could be performed, zero is returned. If the correct value is outside the range of representable values,\bHUGE_VALF is returned according to the sign of the value, if any, and the value of the macro\referrno is stored in\referrno. */floatstrtof(constchar*__nptr,char**__endptr);/*!\briefConvert string to int\ingroupString to number conversions\synopsis\desc\b\thisconverts the initial portion of the string pointed to by\bnptr to an\bint representation.\b\thisdoes not affect the value of\berrno on an error. If the value of the result cannot be represented, the behavior is undefined. Except for the behavior on error,\b\thisis equivalent to\tt{(int)strtol(nptr, (char **)NULL, 10)}.\b\thisreturns the converted value.\sa\refstrtol */intatoi(constchar*__nptr);/*!\briefConvert string to long\ingroupString to number conversions\synopsis\desc\b\thisconverts the initial portion of the string pointed to by\bnptr\bto a\b{long int} representation.\batol does not affect the value of\berrno on an error. If the value of the result cannot be represented, the behavior is undefined. Except for the behavior on error,\b\thisis equivalent to\tt{strtol(nptr, (char **)NULL, 10)}.\b\thisreturns the converted value.\sa\refstrtol */longintatol(constchar*__nptr);/*!\briefConvert string to long long\ingroupString to number conversions\synopsis\desc\b\thisconverts the initial portion of the string pointed to by\bnptr to a\b{long long int} representation.\b\thisdoes not affect the value of\b\thisis equivalent to\tt{strtoll(nptr, (char **)NULL, 10)}.\b\thisreturns the converted value.\sa\refstrtoll */longlongintatoll(constchar*__nptr);/*!\briefConvert string to long\ingroupString to number conversions\synopsis\desc\b\thisconverts the initial portion of the string pointed to by\bnptr to a\b{long int} representation. First,\bstrtol decomposes the input string into three parts: an initial, possibly empty, sequence of white-space characters (as specified by\refisspace), a subject sequence resembling an integer represented in some radix determined by the value of\bbase, and a final string of one or more unrecognized characters, including the terminating null character of the input string.\b\thisthen attempts to convert the subject sequence to an integer, and return the result. When converting, no integer suffix (such as U, L, UL, LL, ULL) is allowed. If the value of\bbase is zero, the expected form of the subject sequence is an optional plus or minus sign followed by an integer constant. If the value of\bbase is between 2 and 36 (inclusive), the expected form of the subject sequence is an optional plus or minus sign followed by a sequence of letters and digits representing an integer with the radix specified by\bbase. The letters from a (or A) through z (or Z) represent the values 10 through 35; only letters and digits whose ascribed values are less than that of\bbase are permitted. If the value of\bbase is 16, the characters\q{0x} or\q{0X} may optionally precede the sequence of letters and digits, following the optional sign. The subject sequence is defined as the longest initial subsequence of the input string, starting with the first non-white-space character, that is of the expected form. The subject sequence contains no characters if the input string is empty or consists entirely of white space, or if the first non-white-space character is other than a sign or a permissible letter or digit. If the subject sequence has the expected form and the value of\bbase is zero, the sequence of characters starting with the first digit is interpreted as an integer constant. If the subject sequence has the expected form and the value of\bbase is between 2 and 36, it is used as the base for conversion. If the subject sequence begins with a minus sign, the value resulting from the conversion is negated. A pointer to the final string is stored in the object pointed to by\bendptr, provided that\bendptr is not a null pointer. If the subject sequence is empty or does not have the expected form, no conversion is performed, the value of\bnptr is stored in the object pointed to by\bendptr, provided that\bendptr is not a null pointer.\b\thisreturns the converted value, if any. If no conversion could be performed, zero is returned. If the correct value is outside the range of representable values,\refLONG_MIN or\refLONG_MAX is returned according to the sign of the value, if any, and the value of the macro\referrno is stored in\referrno. */longintstrtol(constchar*__nptr,char**__endptr,int__base);/*!\briefConvert string to long long\ingroupString to number conversions\synopsis\desc\b\thisconverts the initial portion of the string pointed to by\bnptr to a\b{long int} representation. First,\b\thisdecomposes the input string into three parts: an initial, possibly empty, sequence of white-space characters (as specified by\refisspace), a subject sequence resembling an integer represented in some radix determined by the value of\bbase, and a final string of one or more unrecognized characters, including the terminating null character of the input string.\b\thisthen attempts to convert the subject sequence to an integer, and return the result. When converting, no integer suffix (such as U, L, UL, LL, ULL) is allowed. If the value of\bbase is zero, the expected form of the subject sequence is an optional plus or minus sign followed by an integer constant. If the value of\bbase is between 2 and 36 (inclusive), the expected form of the subject sequence is an optional plus or minus sign followed by a sequence of letters and digits representing an integer with the radix specified by\bbase. The letters from a (or A) through z (or Z) represent the values 10 through 35; only letters and digits whose ascribed values are less than that of\bbase are permitted. If the value of\bbase is 16, the characters\q{0x} or\q{0X} may optionally precede the sequence of letters and digits, following the optional sign. The subject sequence is defined as the longest initial subsequence of the input string, starting with the first non-white-space character, that is of the expected form. The subject sequence contains no characters if the input string is empty or consists entirely of white space, or if the first non-white-space character is other than a sign or a permissible letter or digit. If the subject sequence has the expected form and the value of\bbase is zero, the sequence of characters starting with the first digit is interpreted as an integer constant. If the subject sequence has the expected form and the value of\bbase is between 2 and 36, it is used as the base for conversion. If the subject sequence begins with a minus sign, the value resulting from the conversion is negated. A pointer to the final string is stored in the object pointed to by\bendptr, provided that\bnptr is stored in the object pointed to by\bendptr, provided that\bendptr is not a null pointer.\b\thisLLONG_MIN or\refLLONG_MAX is returned according to the sign of the value, if any, and the value of the macro\bERANGE is stored in\referrno. */longlongintstrtoll(constchar*__nptr,char**__endptr,int__base);/*!\briefConvert string to unsigned long\ingroupString to number conversions\synopsis\desc\b\thisconverts the initial portion of the string pointed to by\bnptr to a\b{long int} representation. First,\b\thisisspace), a subject sequence resembling an integer represented in some radix determined by the value of\bbase, and a final string of one or more unrecognized characters, including the terminating null character of the input string.\bstrtoul then attempts to convert the subject sequence to an integer, and return the result. When converting, no integer suffix (such as U, L, UL, LL, ULL) is allowed. If the value of\bbase is zero, the expected form of the subject sequence is an optional plus or minus sign followed by an integer constant. If the value of\bbase is between 2 and 36 (inclusive), the expected form of the subject sequence is an optional plus or minus sign followed by a sequence of letters and digits representing an integer with the radix specified by\bbase. The letters from a (or A) through z (or Z) represent the values 10 through 35; only letters and digits whose ascribed values are less than that of\bbase are permitted. If the value of\bbase is 16, the characters\q{0x} or\q{0X} may optionally precede the sequence of letters and digits, following the optional sign. The subject sequence is defined as the longest initial subsequence of the input string, starting with the first non-white-space character, that is of the expected form. The subject sequence contains no characters if the input string is empty or consists entirely of white space, or if the first non-white-space character is other than a sign or a permissible letter or digit. If the subject sequence has the expected form and the value of\bbase is zero, the sequence of characters starting with the first digit is interpreted as an integer constant. If the subject sequence has the expected form and the value of\bbase is between 2 and 36, it is used as the base for conversion. If the subject sequence begins with a minus sign, the value resulting from the conversion is negated. A pointer to the final string is stored in the object pointed to by\bendptr, provided that\bnptr is stored in the object pointed to by\bendptr, provided that\bendptr is not a null pointer.\b\thisLONG_MAX or\refULONG_MAX is returned according to the sign of the value, if any, and the value of the macro\bERANGE is stored in\referrno. */unsignedlongintstrtoul(constchar*__nptr,char**__endptr,int__base);/*!\briefConvert string to unsigned long long\ingroupString to number conversions\synopsis\desc\b\thisconverts the initial portion of the string pointed to by\bnptr to a\b{long int} representation. First,\bstrtoull decomposes the input string into three parts: an initial, possibly empty, sequence of white-space characters (as specified by\ref\thisthen attempts to convert the subject sequence to an integer, and return the result. When converting, no integer suffix (such as U, L, UL, LL, ULL) is allowed. If the value of\bbase are permitted. If the value of\bbase is 16, the characters\q{0x} or\qendptr, provided that\bnptr is stored in the object pointed to by\bendptr, provided that\bendptr is not a null pointer.\b\thisLLONG_MAX or\refULLONG_MAX is returned according to the sign of the value, if any, and the value of the macro\bERANGE is stored in\referrno. */unsignedlonglongintstrtoull(constchar*__nptr,char**__endptr,int__base);/*!\briefReturn next random number in sequence\ingroupPseudo-random sequence generation functions\synopsis\desc\b\thiscomputes a sequence of pseudo-random integers in the range 0 to\bRAND_MAX.\b\thisreturns the computed pseudo-random integer. */intrand(void);/*!\briefSet seed of random number sequence\ingroupPseudo-random sequence generation functions\synopsis\desc\b\thisuses the argument\bseed as a seed for a new sequence of pseudo-random numbers to be returned by subsequent calls to\brand. If\b\thisis called with the same seed value, the same sequence of pseudo-random numbers is generated. If\brand is called before any calls to\b\thishave been made, a sequence is generated as if\b\thisis first called with a seed value of 1.\sa\refrand or 'ref rand_max */voidsrand(unsignedint__seed);/*!\briefSearch a sorted array\ingroupSearch and sort functions\synopsis\desc\b\thissearches the array\a{*base} for the specified\b{*key} and returns a pointer to the first entry that matches or null if no match. The array should have\bnum elements of\bsize bytes and be sorted by the same algorithm as the\bcompare function The\bcompare function should return a negative value if the first parameter is less than second parameter, zero if the parameters are equal, and a positive value if the first parameter is greater than the second parameter. */void*bsearch(constvoid*__key,constvoid*__buf,size_t__num,size_t__size,int(*__compare)(constvoid*,constvoid*));/*!\briefSort an array\ingroupSearch and sort functions\synopsis\bqsort sorts the array\b*base using the\bcompare algorithm. The array should have\bnum elements of\bsize bytes. The\bcompare function should return a negative value if the first parameter is less than second parameter, zero if the parameters are equal and a positive value if the first parameter is greater than the second parameter. */voidqsort(void*__buf,size_t__num,size_t__size,int(*__compare)(constvoid*,constvoid*));voidabort(void);/*!\briefTerminates the calling process\ingroupEnvironment\synopsis\desc\b\thisreturns to the startup code and performs the appropriate cleanup process. */voidexit(int__exit_code);/*!\briefSet function to be execute on exit\ingroupEnvironment\synopsis\desc\b\thisregisters\bfunction to be called when the application has exited. The functions registered with\b\thisare executed in reverse order of their registration.\b\thisreturns 0 on success and non-zero on failure. */intatexit(void(*__func)(void));char*getenv(constchar*__name);intsystem(constchar*__command);// Extensions /*!\briefConvert int to string\ingroupNumber to string conversions\synopsis\desc\b\thisconverts\bval to a string in base\bradix and places the result in\bbuf.\b\thisreturns\bbuf as the result. If\bradix is greater than 36, the result is undefined. If\bval is negative and\bradix is 10, the string has a leading minus sign (-); for all other values of\bradix,\bvalue is considered unsigned and never has a leading minus sign.\sa\refltoa,\reflltoa,\refultoa,\refulltoa,\refutoa */char*itoa(int__val,char*__buf,int__radix);/*!\briefConvert unsigned to string\ingroupNumber to string conversions\synopsis\desc\b\thisconverts\bval to a string in base\bradix and places the result in\bbuf.\b\thisreturns\bbuf as the result. If\bradix is greater than 36, the result is undefined.\sa\refitoa,\refltoa,\reflltoa,\refultoa,\refulltoa */char*utoa(unsignedval,char*buf,intradix);/*!\briefConvert long to string\ingroupNumber to string conversions\synopsis\desc\b\thisconverts\bval to a string in base\bradix and places the result in\bbuf.\b\thisreturns\bbuf as the result. If\bradix is greater than 36, the result is undefined. If\bval is negative and radix is 10, the string has a leading minus sign (-); for all other values of\bradix,\bvalue is considered unsigned and never has a leading minus sign.\sa\refitoa,\reflltoa,\refultoa,\refulltoa,\refutoa */char*ltoa(long__val,char*__buf,int__radix);/*!\briefConvert unsigned long to string\ingroupNumber to string conversions\synopsis\desc\b\thisconverts\bval to a string in base\bradix and places the result in\bbuf.\b\thisreturns\bbuf as the result. If\bradix is greater than 36, the result is undefined.\sa\refitoa,\refltoa,\reflltoa,\refulltoa,\refutoa */char*ultoa(unsignedlong__val,char*__buf,int__radix);/*!\briefConvert long long to string\ingroupNumber to string conversions\synopsis\desc\b\thisconverts\bval to a string in base\bradix and places the result in\bbuf.\b\thisreturns\bbuf as the result. If\bradix is greater than 36, the result is undefined. If\bval is negative and radix is 10, the string has a leading minus sign (-); for all other values of\bradix,\bvalue is considered unsigned and never has a leading minus sign.\sa\refitoa,\refltoa,\refultoa,\refulltoa,\refutoa */char*lltoa(longlong__val,char*__buf,int__radix);/*!\briefConvert unsigned long long to string\ingroupNumber to string conversions\synopsis\desc\b\thisconverts\bval to a string in base\bradix and places the result in\bbuf.\b\thisreturns\bbuf as the result. If\bradix is greater than 36, the result is undefined.\sa\refitoa,\refltoa,\reflltoa,\refultoa,\refutoa */char*ulltoa(unsignedlonglong__val,char*__buf,int__radix); #ifdef __cplusplus } #endif #endif