Main Page | Alphabetical List | Class List | File List | Class Members | File Members

stdlib.h

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

Generated on Tue Jan 10 12:43:39 2006 for nlibc by doxygen 1.3.5