00001 /*--------------------------------------------------------------------------------------------- 00002 * / 00003 * / apeNEXT stdlib functions for nlcc 00004 * / 00005 * / $Id: stdlib.h,v 1.19 2006/05/06 08:16:27 pleiter Exp $ 00006 * / 00007 * / 00008 * / ISO/IEC 9899:1999 (E) Standard 00009 * / General utilities <stdlib.h> 00010 * / 00011 * / 00012 * / 00013 * / IMPLEMENTATION for APEnext: partly possible (stdlib.h) 00014 * /-------------------------------------------------------------------------------------------*/ 00015 #ifndef _STDLIB_H 00016 #define _STDLIB_H 00017 00018 #include <nlibc.h> 00019 #include <stddef.h> 00020 #include <float.h> 00021 00022 typedef struct 00023 { 00024 int quot; 00025 int rem; 00026 } div_t; 00027 00028 #ifndef __ldiv_t_defined 00029 typedef struct 00030 { 00031 long int quot; 00032 long int rem; 00033 } ldiv_t; 00034 # define __ldiv_t_defined 1 00035 #endif 00036 00037 #ifndef __lldiv_t_defined 00038 typedef struct 00039 { 00040 long long int quot; 00041 long long int rem; 00042 } lldiv_t; 00043 # define __lldiv_t_defined 1 00044 #endif 00045 00046 #define EXIT_FAILURE 1 /* Failing exit status. */ 00047 #define EXIT_SUCCESS 0 /* Successful exit status. */ 00048 00049 #define RAND_MAX INT_MAX 00050 00051 /*--------------------------------------------------------------------------------------------- 00052 * / 00053 * / NAME 00054 * / abort - cause abnormal program termination 00055 * / 00056 * / SYNOPSIS 00057 * / #include <stdlib.h> 00058 * / 00059 * / void abort(void); 00060 * / 00061 * / DESCRIPTION 00062 * / The abort() function causes abnormal program termination 00063 * / unless the signal SIGABRT is caught and the signal handler 00064 * / does not return. If the abort() function causes program 00065 * / termination, all open streams are closed and flushed. 00066 * / 00067 * / If the SIGABRT signal is blocked or ignored, the abort() 00068 * / function will still override it. 00069 * / 00070 * / RETURN VALUE 00071 * / The abort() function never returns. 00072 * / 00073 * / IMPLEMENTATION for APEnext: 00074 * / done, except for signal handling 00075 * / The function now simply unconditionedly issues a 00076 * / software exception. 00077 * /-------------------------------------------------------------------------------------------*/ 00078 inline void abort(void){ 00079 register int maskval; 00080 // fetch current mem exception mask 00081 asm("\tctr %0 0x21\n" : "=r" (maskval)); 00082 // allow SEX and store back 00083 asm("\trtc 0x21 %0\n" : : "r" (maskval & ~0x2)); 00084 // issue a SEX 00085 asm("\tsex\t!! via abort()"); 00086 } 00087 00088 00089 /*--------------------------------------------------------------------------------------------- 00090 * Extensions implemented by Norbert: 00091 *-------------------------------------------------------------------------------------------*/ 00092 00093 #include <alloca.h> 00094 00095 #undef __roundup 00096 #define __roundup(x,n) (((x)+((n)-1))&(~((n)-1))) 00097 00101 #define sizeof_aligned(TY) __roundup(sizeof(TY),__alignof__(TY)) 00102 00103 00104 00105 //--------------------------------------------------------------------------------------------- 00130 #define atof(STR) strtod((STR), (char **)NULL) 00131 //double atof(const char *nptr){} 00132 00133 //--------------------------------------------------------------------------------------------- 00167 #define atoi(nptr) strtol(nptr, (char **)NULL, 10) 00168 #define atol(nptr) strtol(nptr, (char **)NULL, 10) 00169 #define atoll(nptr) strtoll(nptr, (char **)NULL, 10) 00170 #define atoq(nptr) atoll(nptr); 00171 00172 00173 /*-------------------------------------------------------------------------------------------- 00174 * 00175 * include strtol() and strtoll() 00176 */ 00177 #include <stdlib/strtol.h> 00178 00179 /*-------------------------------------------------------------------------------------------- 00180 * 00181 * include strtoul() and strtoull() 00182 */ 00183 #include <stdlib/strtoul.h> 00184 00185 /*-------------------------------------------------------------------------------------------- 00186 * 00187 * include strtod(), strtof() and strtold() 00188 */ 00189 #include <stdlib/strtod.h> 00190 00191 /*-------------------------------------------------------------------------------------------- 00192 * 00193 * include rand() and srand() 00194 */ 00195 #include <stdlib/rand.h> 00196 00197 /*--------------------------------------------------------------------------------------------- 00198 * / 00199 * / NAME 00200 * / calloc, malloc, free, realloc - Allocate and free dynamic 00201 * / memory 00202 * / 00203 * / SYNOPSIS 00204 * / #include <stdlib.h> 00205 * / 00206 * / void *calloc(size_t nmemb, size_t size); 00207 * / void *malloc(size_t size); 00208 * / void free(void *ptr); 00209 * / void *realloc(void *ptr, size_t size); 00210 * / 00211 * / DESCRIPTION 00212 * / calloc() allocates memory for an array of nmemb elements 00213 * / of size bytes each and returns a pointer to the allocated 00214 * / memory. The memory is set to zero. 00215 * / 00216 * / malloc() allocates size bytes and returns a pointer to the 00217 * / allocated memory. The memory is not cleared. 00218 * / 00219 * / free() frees the memory space pointed to by ptr, which 00220 * / must have been returned by a previous call to malloc(), 00221 * / calloc() or realloc(). Otherwise, or if free(ptr) has 00222 * / already been called before, undefined behaviour occurs. 00223 * / If ptr is NULL, no operation is performed. 00224 * / 00225 * / realloc() changes the size of the memory block pointed to 00226 * / by ptr to size bytes. The contents will be unchanged to 00227 * / the minimum of the old and new sizes; newly allocated mem 00228 * / ory will be uninitialized. If ptr is NULL, the call is 00229 * / equivalent to malloc(size); if size is equal to zero, the 00230 * / call is equivalent to free(ptr). Unless ptr is NULL, it 00231 * / must have been returned by an earlier call to malloc(), 00232 * / calloc() or realloc(). 00233 * / 00234 * / RETURN VALUE 00235 * / For calloc() and malloc(), the value returned is a pointer 00236 * / to the allocated memory, which is suitably aligned for any 00237 * / kind of variable, or NULL if the request fails. 00238 * / 00239 * / free() returns no value. 00240 * / 00241 * / realloc() returns a pointer to the newly allocated memory, 00242 * / which is suitably aligned for any kind of variable and may 00243 * / be different from ptr, or NULL if the request fails or if 00244 * / size was equal to 0. If realloc() fails the original 00245 * / block is left untouched - it is not freed or moved. 00246 * / 00247 * / IMPLEMENTATION for APEnext: 00248 * / 00249 * /-------------------------------------------------------------------------------------------*/ 00250 #include <stdlib/bsdmalloc.h> 00251 00252 //--------------------------------------------------------------------------------------------- 00277 // int atexit(void (*func)(void)){} 00278 00279 /*--------------------------------------------------------------------------------------------- 00280 * / 00281 * / NAME 00282 * / exit - cause normal program termination 00283 * / 00284 * / SYNOPSIS 00285 * / #include <stdlib.h> 00286 * / 00287 * / void exit(int status); 00288 * / 00289 * / DESCRIPTION 00290 * / The exit() function causes normal program termination and 00291 * / the value of status is returned to the parent. All func 00292 * / tions registered with atexit() and on_exit() are called in 00293 * / the reverse order of their registration, and all open 00294 * / streams are flushed and closed. 00295 * / 00296 * / RETURN VALUE 00297 * / The exit() function does not return. 00298 * / 00299 * / IMPLEMENTATION for APEnext: possible (exit, _Exit) 00300 * /-------------------------------------------------------------------------------------------*/ 00301 // void exit(int status){} 00302 // void _Exit(int status){} 00303 00304 /*--------------------------------------------------------------------------------------------- 00305 * / 00306 * / NAME 00307 * / getenv - get an environment variable 00308 * / 00309 * / SYNOPSIS 00310 * / #include <stdlib.h> 00311 * / 00312 * / char *getenv(const char *name); 00313 * / 00314 * / DESCRIPTION 00315 * / The getenv() function searches the environment list for a 00316 * / string that matches the string pointed to by name. The 00317 * / strings are of the form name = value. 00318 * / 00319 * / RETURN VALUE 00320 * / The getenv() function returns a pointer to the value in 00321 * / the environment, or NULL if there is no match. 00322 * / 00323 * / IMPLEMENTATION for APEnext: not possible (getenv) 00324 * /-------------------------------------------------------------------------------------------*/ 00325 // char *getenv(const char *name){} 00326 00327 //------------------------------------------------------------------------------ 00360 #ifndef __HAS_MAIN 00361 extern int system(const char *s); 00362 #else 00363 #if !defined(__cflow_processed) || defined(_uses_system_stdlib_h) 00364 int system(const char *s) 00365 { 00366 register vector int retval=0; 00367 00368 if (s==NULL) 00369 retval=1; 00370 else 00371 if( strlen(s)!=0 ) 00372 { 00373 asm("\t$io_start\n"); 00374 asm("\t$io_packet($SYS_IOCMD_SYSTEM, $SYS_IOFMT_VARSTR, $SYS_IORDFLG_NOP, $SYS_IODEV_MEM, 0 , $SYS_IOBS_V, 0.%0)\n" : : "r" (s) ); 00375 asm("\t$io_end\n"); 00376 asm("\tlmtr %0 $MEM_SYS_RVAL\n" : "=r" (retval)); 00377 } 00378 00379 return retval.hi; 00380 } 00381 #endif // system() 00382 #endif // Has Main 00383 00384 //------------------------------------------------------------------------------ 00419 // void *bsearch(const void *key, const void *base, size_t nmemb, \ 00420 // size_t size, int (*compar)(const void *, const void *)){} 00421 00422 00423 //------------------------------------------------------------------------------ 00456 // void qsort(void *base, size_t nmemb, size_t size, int (*compar)(const void *, const void *)){} 00457 00458 //------------------------------------------------------------------------------ 00488 #if !defined(__cflow_processed) || defined(_uses_abs_stdlib_h) 00489 inline int abs( int i ) { 00490 register int a; 00491 where( i < 0 ) { 00492 a = -i; 00493 } else { 00494 a = i; 00495 } 00496 return a; 00497 } 00498 #endif // abs() 00499 00500 #define labs(VAL) abs(VAL) 00501 #define llabs(VAL) abs(VAL) 00502 00503 00504 //------------------------------------------------------------------------------ 00527 #if !defined(__cflow_processed) || defined(_uses_div_stdlib_h) 00528 inline div_t div(int numer, int denom) 00529 { 00530 register div_t a; 00531 00532 asm("\tidiv %0 %1 %2 %3" : "=r" (a.quot), "=r" (a.rem) : "r" (numer), "r" (denom)); 00533 00534 return(a); 00535 } 00536 #endif // _uses_div_stdlib_h 00537 00538 #if !defined(__cflow_processed) || defined(_uses_ldiv_stdlib_h) 00539 inline ldiv_t ldiv(long int numer, long int denom) 00540 { 00541 register ldiv_t a; 00542 00543 asm("\tidiv %0 %1 %2 %3" : "=r" (a.quot), "=r" (a.rem) : "r" (numer), "r" (denom)); 00544 00545 return(a); 00546 } 00547 #endif // _uses_ldiv_stdlib_h 00548 00549 #if !defined(__cflow_processed) || defined(_uses_lldiv_stdlib_h) 00550 inline lldiv_t lldiv(long long int numer, long long int denom) 00551 { 00552 register lldiv_t a; 00553 00554 asm("\tidiv %0 %1 %2 %3" : "=r" (a.quot), "=r" (a.rem) : "r" (numer), "r" (denom)); 00555 00556 return(a); 00557 } 00558 #endif // _uses_lldiv_stdlib_h 00559 00560 /*--------------------------------------------------------------------------------------------- 00561 * / 00562 * / NAME 00563 * / mblen - determine number of bytes in next multibyte char 00564 * / acter 00565 * / 00566 * / SYNOPSIS 00567 * / #include <stdlib.h> 00568 * / 00569 * / int mblen (const char *s, size_t n); 00570 * / 00571 * / DESCRIPTION 00572 * / If s is not a NULL pointer, the mblen function inspects at 00573 * / most n bytes of the multibyte string starting at s and 00574 * / extracts the next complete multibyte character. It uses a 00575 * / static anonymous shift state only known to the mblen func 00576 * / tion. If the multibyte character is not the null wide 00577 * / character, it returns the number of bytes that were con 00578 * / sumed from s. If the multibyte character is the null wide 00579 * / character, it returns 0. 00580 * / 00581 * / If the n bytes starting at s do not contain a complete 00582 * / multibyte character, mblen returns -1. This can happen 00583 * / even if n >= MB_CUR_MAX, if the multibyte string contains 00584 * / redundant shift sequences. 00585 * / 00586 * / If the multibyte string starting at s contains an invalid 00587 * / multibyte sequence before the next complete character, 00588 * / mblen also returns -1. 00589 * / 00590 * / If s is a NULL pointer, the mblen function resets the 00591 * / shift state, only known to this function, to the initial 00592 * / state, and returns non-zero if the encoding has non-triv 00593 * / ial shift state, or zero if the encoding is stateless. 00594 * / 00595 * / RETURN VALUE 00596 * / The mblen function returns the number of bytes parsed from 00597 * / the multibyte sequence starting at s, if a non-null wide 00598 * / character was recognized. It returns 0, if a null wide 00599 * / character was recognized. It returns -1, if an invalid 00600 * / multibyte sequence was encountered or if it couldn't parse 00601 * / a complete multibyte character. 00602 * / 00603 * / IMPLEMENTATION for APEnext: possible if strings are implemented (mblen) 00604 * /-------------------------------------------------------------------------------------------*/ 00605 // int mblen(const char *s, size_t n){} 00606 00607 00608 /*--------------------------------------------------------------------------------------------- 00609 * / 00610 * / NAME 00611 * / mbtowc - convert a multibyte sequence to a wide character 00612 * / 00613 * / SYNOPSIS 00614 * / #include <stdlib.h> 00615 * / 00616 * / int mbtowc (wchar_t *pwc, const char *s, size_t n); 00617 * / 00618 * / DESCRIPTION 00619 * / The main case for this function is when s is not NULL and 00620 * / pwc is not NULL. In this case, the mbtowc function 00621 * / inspects at most n bytes of the multibyte string starting 00622 * / at s, extracts the next complete multibyte character, con 00623 * / verts it to a wide character and stores it at *pwc. It 00624 * / updates an internal shift state only known to the mbtowc 00625 * / function. If s does not point to a '\0' byte, it returns 00626 * / the number of bytes that were consumed from s, otherwise 00627 * / it returns 0. 00628 * / 00629 * / If the n bytes starting at s do not contain a complete 00630 * / multibyte character, or if they contain an invalid multi 00631 * / byte sequence, mbtowc returns -1. This can happen even if 00632 * / n >= MB_CUR_MAX, if the multibyte string contains redun 00633 * / dant shift sequences. 00634 * / 00635 * / A different case is when s is not NULL but pwc is NULL. In 00636 * / this case the mbtowc function behaves as above, excepts 00637 * / that it does not store the converted wide character in 00638 * / memory. 00639 * / 00640 * / A third case is when s is NULL. In this case, pwc and n 00641 * / are ignored. The mbtowc function resets the shift state, 00642 * / only known to this function, to the initial state, and 00643 * / returns non-zero if the encoding has non-trivial shift 00644 * / state, or zero if the encoding is stateless. 00645 * / 00646 * / RETURN VALUE 00647 * / If s is not NULL, the mbtowc function returns the number 00648 * / of consumed bytes starting at s, or 0 if s points to a 00649 * / null byte, or -1 upon failure. 00650 * / 00651 * / If s is NULL, the mbtowc function returns non-zero if the 00652 * / encoding has non-trivial shift state, or zero if the 00653 * / encoding is stateless. 00654 * / 00655 * / IMPLEMENTATION for APEnext: possible if strings are implemented (mbtowc) 00656 * /-------------------------------------------------------------------------------------------*/ 00657 // int mbtowc(wchar_t * /* restrict */ pwc, const char * /* restrict */ s, size_t n){} 00658 00659 /*--------------------------------------------------------------------------------------------- 00660 * / 00661 * / NAME 00662 * / wctomb - convert a wide character to a multibyte sequence 00663 * / 00664 * / SYNOPSIS 00665 * / #include <stdlib.h> 00666 * / 00667 * / int wctomb (char *s, wchar_t wc); 00668 * / 00669 * / DESCRIPTION 00670 * / If s is not NULL, the wctomb function converts the wide 00671 * / character wc to its multibyte representation and stores it 00672 * / at the beginning of the character array pointed to by s. 00673 * / It updates the shift state, which is stored in a static 00674 * / anonymous variable only known to the wctomb function, and 00675 * / returns the length of said multibyte representation, i.e. 00676 * / the number of bytes written at s. 00677 * / 00678 * / The programmer must ensure that there is room for at least 00679 * / MB_CUR_MAX bytes at s. 00680 * / 00681 * / If s is NULL, the wctomb function resets the shift state, 00682 * / only known to this function, to the initial state, and 00683 * / returns non-zero if the encoding has non-trivial shift 00684 * / state, or zero if the encoding is stateless. 00685 * / 00686 * / RETURN VALUE 00687 * / If s is not NULL, the wctomb function returns the number 00688 * / of bytes that have been written to the byte array at s. If 00689 * / wc can not be represented as a multibyte sequence (accord 00690 * / ing to the current locale), -1 is returned. 00691 * / 00692 * / If s is NULL, the wctomb function returns non-zero if the 00693 * / encoding has non-trivial shift state, or zero if the 00694 * / encoding is stateless. 00695 * / 00696 * / IMPLEMENTATION for APEnext: possible if strings are implemented (wctomb) 00697 * /-------------------------------------------------------------------------------------------*/ 00698 // int wctomb(char *s, wchar_t wchar){} 00699 00700 /*--------------------------------------------------------------------------------------------- 00701 * / 00702 * / NAME 00703 * / mbstowcs - convert a multibyte string to a wide character 00704 * / string 00705 * / 00706 * / SYNOPSIS 00707 * / #include <stdlib.h> 00708 * / 00709 * / size_t mbstowcs (wchar_t *dest, const char *src, size_t n); 00710 * / 00711 * / DESCRIPTION 00712 * / If dest is not a NULL pointer, the mbstowcs function con 00713 * / verts the multibyte string src to a wide-character string 00714 * / starting at dest. At most n wide characters are written 00715 * / to dest. The conversion starts in the initial state. The 00716 * / conversion can stop for three reasons: 00717 * / 00718 * / 1. An invalid multibyte sequence has been encountered. In 00719 * / this case (size_t)(-1) is returned. 00720 * / 00721 * / 2. n non-L'\0' wide characters have been stored at dest. 00722 * / In this case the number of wide characters written to dest 00723 * / is returned, but the shift state at this point is lost. 00724 * / 00725 * / 3. The multibyte string has been completely converted, 00726 * / including the terminating '\0'. In this case the number of 00727 * / wide characters written to dest, excluding the terminating 00728 * / L'\0' character, is returned. 00729 * / 00730 * / The programmer must ensure that there is room for at least 00731 * / n wide characters at dest. 00732 * / 00733 * / If dest is NULL, n is ignored, and the conversion proceeds 00734 * / as above, except that the converted wide characters are 00735 * / not written out to memory, and that no length limit 00736 * / exists. 00737 * / 00738 * / In order to avoid the case 2 above, the programmer should 00739 * / make sure n is greater or equal to mbstowcs(NULL,src,0)+1. 00740 * / 00741 * / RETURN VALUE 00742 * / The mbstowcs function returns the number of wide charac 00743 * / ters that make up the converted part of the wide character 00744 * / string, not including the terminating null wide character. 00745 * / If an invalid multibyte sequence was encountered, 00746 * / (size_t)(-1) is returned. 00747 * / 00748 * / IMPLEMENTATION for APEnext: possible if strings are implemented (mbstowcs) 00749 * /-------------------------------------------------------------------------------------------*/ 00750 // size_t mbstowcs(wchar_t * /* restrict */ pwcs, const char * /* restrict */ s, size_t n){} 00751 00752 /*--------------------------------------------------------------------------------------------- 00753 * / 00754 * / NAME 00755 * / wcstombs - convert a wide character string to a multibyte 00756 * / string 00757 * / 00758 * / SYNOPSIS 00759 * / #include <stdlib.h> 00760 * / 00761 * / size_t wcstombs (char *dest, const wchar_t *src, size_t n); 00762 * / 00763 * / DESCRIPTION 00764 * / If dest is not a NULL pointer, the wcstombs function con 00765 * / verts the wide-character string src to a multibyte string 00766 * / starting at dest. At most n bytes are written to dest. The 00767 * / conversion starts in the initial state. The conversion can 00768 * / stop for three reasons: 00769 * / 00770 * / 1. A wide character has been encountered that can not be 00771 * / represented as a multibyte sequence (according to the cur 00772 * / rent locale). In this case (size_t)(-1) is returned. 00773 * / 00774 * / 2. The length limit forces a stop. In this case the number 00775 * / of bytes written to dest is returned, but the shift state 00776 * / at this point is lost. 00777 * / 00778 * / 3. The wide-character string has been completely con 00779 * / verted, including the terminating L'\0'. In this case the 00780 * / conversion ends in the initial state. The number of bytes 00781 * / written to dest, excluding the terminating '\0' byte, is 00782 * / returned. 00783 * / 00784 * / The programmer must ensure that there is room for at least 00785 * / n bytes at dest. 00786 * / 00787 * / If dest is NULL, n is ignored, and the conversion proceeds 00788 * / as above, except that the converted bytes are not written 00789 * / out to memory, and that no length limit exists. 00790 * / 00791 * / In order to avoid the case 2 above, the programmer should 00792 * / make sure n is greater or equal to wcstombs(NULL,src,0)+1. 00793 * / 00794 * / RETURN VALUE 00795 * / The wcstombs function returns the number of bytes that 00796 * / make up the converted part of multibyte sequence, not 00797 * / including the terminating null byte. If a wide character 00798 * / was encountered which could not be converted, (size_t)(-1) 00799 * / is returned. 00800 * / 00801 * / IMPLEMENTATION for APEnext: possible if strings are implemented (wcstombs) 00802 * /-------------------------------------------------------------------------------------------*/ 00803 // size_t wcstombs(char * /* restrict */ s, const wchar_t * /* restrict */ pwcs, size_t n){} 00804 00805 00806 00807 #endif /* ifndef _STDLIB_H */