123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440
/**
 * D header file for POSIX.
 *
 * Copyright: Public Domain
 * License:   Public Domain
 * Authors:   Sean Kelly
 * Standards: The Open Group Base Specifications Issue 6, IEEE Std 1003.1, 2004 Edition
 */
module tango.stdc.posix.stdlib;

private import tango.stdc.posix.config;
public import tango.stdc.stdlib;
public import tango.stdc.posix.sys.wait;

extern (C):

//
// Required (defined in tango.stdc.stdlib)
//
/*
EXIT_FAILURE
EXIT_SUCCESS
NULL
RAND_MAX
MB_CUR_MAX
div_t
ldiv_t
lldiv_t
size_t
wchar_t

void    _Exit(int);
void    abort();
int     abs(int);
int     atexit(void function());
double  atof(in char*);
int     atoi(in char*);
c_long  atol(in char*);
long    atoll(in char*);
void*   bsearch(in void*, in void*, size_t, size_t, int function(in void*, in void*));
void*   calloc(size_t, size_t);
div_t   div(int, int);
void    exit(int);
void    free(void*);
char*   getenv(in char*);
c_long  labs(c_long);
ldiv_t  ldiv(c_long, c_long);
long    llabs(long);
lldiv_t lldiv(long, long);
void*   malloc(size_t);
int     mblen(in char*, size_t);
size_t  mbstowcs(wchar_t*, in char*, size_t);
int     mbtowc(wchar_t*, in char*, size_t);
void    qsort(void*, size_t, size_t, int function(in void*, in void*));
int     rand();
void*   realloc(void*, size_t);
void    srand(uint);
double  strtod(in char*, char**);
float   strtof(in char*, char**);
c_long  strtol(in char*, char**, int);
real    strtold(in char*, char**);
long    strtoll(in char*, char**, int);
c_ulong strtoul(in char*, char**, int);
ulong   strtoull(in char*, char**, int);
int     system(in char*);
size_t  wcstombs(char*, in wchar_t*, size_t);
int     wctomb(char*, wchar_t);
*/

//
// Advisory Information (ADV)
//
/*
int posix_memalign(void**, size_t, size_t);
*/

version( linux )
{
    int posix_memalign(void**, size_t, size_t);
}

//
// C Extension (CX)
//
/*
int setenv(in char*, in char*, int);
int unsetenv(in char*);
*/

version( linux )
{
    int setenv(in char*, in char*, int);
    int unsetenv(in char*);

    void* valloc(size_t); // LEGACY non-standard
}
else version( darwin )
{
    int setenv(in char*, in char*, int);
    int unsetenv(in char*);

    void* valloc(size_t); // LEGACY non-standard
}
else version( FreeBSD )
{
    int setenv(in char*, in char*, int);
    int unsetenv(in char*);

    void* valloc(size_t); // LEGACY non-standard
}
else version( solaris )
{
    int setenv(in char*, in char*, int);
    int unsetenv(in char*);

    void* valloc(size_t); // LEGACY non-standard
}

//
// Thread-Safe Functions (TSF)
//
/*
int rand_r(uint*);
*/

version( linux )
{
    int rand_r(uint*);
}
else version( darwin )
{
    int rand_r(uint*);
}
else version( FreeBSD )
{
    int rand_r(uint*);
}
else version( solaris )
{
    int rand_r(uint*);
}

//
// XOpen (XSI)
//
/*
WNOHANG     (defined in tango.stdc.posix.sys.wait)
WUNTRACED   (defined in tango.stdc.posix.sys.wait)
WEXITSTATUS (defined in tango.stdc.posix.sys.wait)
WIFEXITED   (defined in tango.stdc.posix.sys.wait)
WIFSIGNALED (defined in tango.stdc.posix.sys.wait)
WIFSTOPPED  (defined in tango.stdc.posix.sys.wait)
WSTOPSIG    (defined in tango.stdc.posix.sys.wait)
WTERMSIG    (defined in tango.stdc.posix.sys.wait)

c_long a64l(in char*);
double drand48();
char*  ecvt(double, int, int *, int *); // LEGACY
double erand48(ushort[3]);
char*  fcvt(double, int, int *, int *); // LEGACY
char*  gcvt(double, int, char*); // LEGACY
// per spec: int getsubopt(char** char* const*, char**);
int    getsubopt(char**, in char**, char**);
int    grantpt(int);
char*  initstate(uint, char*, size_t);
c_long jrand48(ushort[3]);
char*  l64a(c_long);
void   lcong48(ushort[7]);
c_long lrand48();
char*  mktemp(char*); // LEGACY
int    mkstemp(char*);
c_long mrand48();
c_long nrand48(ushort[3]);
int    posix_openpt(int);
char*  ptsname(int);
int    putenv(char*);
c_long random();
char*  realpath(in char*, char*);
ushort seed48(ushort[3]);
void   setkey(in char*);
char*  setstate(in char*);
void   srand48(c_long);
void   srandom(uint);
int    unlockpt(int);
*/

version( linux )
{
    //WNOHANG     (defined in tango.stdc.posix.sys.wait)
    //WUNTRACED   (defined in tango.stdc.posix.sys.wait)
    //WEXITSTATUS (defined in tango.stdc.posix.sys.wait)
    //WIFEXITED   (defined in tango.stdc.posix.sys.wait)
    //WIFSIGNALED (defined in tango.stdc.posix.sys.wait)
    //WIFSTOPPED  (defined in tango.stdc.posix.sys.wait)
    //WSTOPSIG    (defined in tango.stdc.posix.sys.wait)
    //WTERMSIG    (defined in tango.stdc.posix.sys.wait)

    c_long a64l(in char*);
    double drand48();
    char*  ecvt(double, int, int *, int *); // LEGACY
    double erand48(ushort[3]);
    char*  fcvt(double, int, int *, int *); // LEGACY
    char*  gcvt(double, int, char*); // LEGACY
    int    getsubopt(char**, in char**, char**);
    int    grantpt(int);
    char*  initstate(uint, char*, size_t);
    c_long jrand48(ushort[3]);
    char*  l64a(c_long);
    void   lcong48(ushort[7]);
    c_long lrand48();
    char*  mktemp(char*); // LEGACY
    //int    mkstemp(char*);
    c_long mrand48();
    c_long nrand48(ushort[3]);
    int    posix_openpt(int);
    char*  ptsname(int);
    int    putenv(char*);
    c_long random();
    char*  realpath(in char*, char*);
    ushort seed48(ushort[3]);
    void   setkey(in char*);
    char*  setstate(in char*);
    void   srand48(c_long);
    void   srandom(uint);
    int    unlockpt(int);

  static if( __USE_LARGEFILE64 )
  {
    int    mkstemp64(char*);
    alias  mkstemp64 mkstemp;
  }
  else
  {
    int    mkstemp(char*);
  }
}
else version( darwin )
{
    //WNOHANG     (defined in tango.stdc.posix.sys.wait)
    //WUNTRACED   (defined in tango.stdc.posix.sys.wait)
    //WEXITSTATUS (defined in tango.stdc.posix.sys.wait)
    //WIFEXITED   (defined in tango.stdc.posix.sys.wait)
    //WIFSIGNALED (defined in tango.stdc.posix.sys.wait)
    //WIFSTOPPED  (defined in tango.stdc.posix.sys.wait)
    //WSTOPSIG    (defined in tango.stdc.posix.sys.wait)
    //WTERMSIG    (defined in tango.stdc.posix.sys.wait)

    c_long a64l(in char*);
    double drand48();
    char*  ecvt(double, int, int *, int *); // LEGACY
    double erand48(ushort[3]);
    char*  fcvt(double, int, int *, int *); // LEGACY
    char*  gcvt(double, int, char*); // LEGACY
    int    getsubopt(char**, in char**, char**);
    int    grantpt(int);
    char*  initstate(uint, char*, size_t);
    c_long jrand48(ushort[3]);
    char*  l64a(c_long);
    void   lcong48(ushort[7]);
    c_long lrand48();
    char*  mktemp(char*); // LEGACY
    int    mkstemp(char*);
    c_long mrand48();
    c_long nrand48(ushort[3]);
    int    posix_openpt(int);
    char*  ptsname(int);
    int    putenv(char*);
    c_long random();
    char*  realpath(in char*, char*);
    ushort seed48(ushort[3]);
    void   setkey(in char*);
    char*  setstate(in char*);
    void   srand48(c_long);
    void   srandom(uint);
    int    unlockpt(int);
}
else version( FreeBSD )
{
    //WNOHANG     (defined in tango.stdc.posix.sys.wait)
    //WUNTRACED   (defined in tango.stdc.posix.sys.wait)
    //WEXITSTATUS (defined in tango.stdc.posix.sys.wait)
    //WIFEXITED   (defined in tango.stdc.posix.sys.wait)
    //WIFSIGNALED (defined in tango.stdc.posix.sys.wait)
    //WIFSTOPPED  (defined in tango.stdc.posix.sys.wait)
    //WSTOPSIG    (defined in tango.stdc.posix.sys.wait)
    //WTERMSIG    (defined in tango.stdc.posix.sys.wait)

    c_long a64l(in char*);
    double drand48();
    
    // Unimplemented on FreeBSD, but required by tango 
    import tango.stdc.math : modf; 
    char* ecvt(double arg, int ndigits, int* decpt, int* sign) 
    { 
        return(cvt(arg, ndigits, decpt, sign, true)); 
    } 
    char* fcvt(double arg, int ndigits, int* decpt, int* sign) 
    { 
        return(cvt(arg, ndigits, decpt, sign, false)); 
    } 
    private char* cvt(double arg, int ndigits, int* decpt, int* sign, bool eflag) 
    { 
        int r2; 
        double fi, fj; 
        char* p, p1; 
        char[] buf; 
    
        if (ndigits<0) 
            ndigits = 0; 
        buf = new char[ndigits]; 
    
        r2 = 0; 
        *sign = 0; 
        p = &buf[0]; 
        if (arg<0) { 
            *sign = 1; 
            arg = -arg; 
        } 
        arg = modf(arg, &fi); 
        p1 = &buf[$-1]; 
        /* 
         * Do integer part 
         */ 
        if (fi != 0) { 
            p1 = &buf[$-1]; 
            while (fi != 0) { 
                fj = modf(fi/10, &fi); 
                *--p1 = cast(char)((cast(char) (fj+.03)*10) + '0');
                r2++; 
            } 
            while (p1 < &buf[$-1]) 
                *p++ = *p1++; 
        } else if (arg > 0) { 
            while ((fj = arg*10) < 1) { 
                arg = fj; 
                r2--; 
            } 
        } 
        p1 = &buf[ndigits]; 
        if (!eflag) 
            p1 += r2; 
        *decpt = r2; 
        if (p1 < &buf[0]) { 
            buf[0] = '\0'; 
            return(buf.ptr); 
        } 
        while (p<=p1 && p<&buf[$-1]) { 
            arg *= 10; 
            arg = modf(arg, &fj); 
            *p++ = cast(char) (cast(char)(fj) + '0');
        } 
        if (p1 >= &buf[$-1]) { 
            buf[$-2] = '\0'; 
            return(buf.ptr); 
        } 
        p = p1; 
        *p1 += 5; 
        while (*p1 > '9') { 
            *p1 = '0'; 
            if (p1>buf.ptr) 
                ++*--p1; 
            else { 
                *p1 = '1'; 
                (*decpt)++; 
                if (!eflag) { 
                    if (p>buf.ptr) 
                        *p = '0'; 
                    p++; 
                } 
            } 
        } 
        *p = '\0'; 
        return(buf.ptr); 
    } 

    double erand48(ushort[3]);
    int    getsubopt(char**, in char**, char**);
    int    grantpt(int);
    char*  initstate(uint, char*, size_t);
    c_long jrand48(ushort[3]);
    char*  l64a(c_long);
    void   lcong48(ushort[7]);
    c_long lrand48();
    char*  mktemp(char*); // LEGACY
    int    mkstemp(char*);
    c_long mrand48();
    c_long nrand48(ushort[3]);
    int    posix_openpt(int);
    char*  ptsname(int);
    int    putenv(char*);
    c_long random();
    char*  realpath(in char*, char*);
    ushort seed48(ushort[3]);
    void   setkey(in char*);
    char*  setstate(in char*);
    void   srand48(c_long);
    void   srandom(uint);
    int    unlockpt(int);
}
else version( solaris )
{
    //WNOHANG     (defined in tango.stdc.posix.sys.wait)
    //WUNTRACED   (defined in tango.stdc.posix.sys.wait)
    //WEXITSTATUS (defined in tango.stdc.posix.sys.wait)
    //WIFEXITED   (defined in tango.stdc.posix.sys.wait)
    //WIFSIGNALED (defined in tango.stdc.posix.sys.wait)
    //WIFSTOPPED  (defined in tango.stdc.posix.sys.wait)
    //WSTOPSIG    (defined in tango.stdc.posix.sys.wait)
    //WTERMSIG    (defined in tango.stdc.posix.sys.wait)

    c_long a64l(in char*);
    double drand48();
    //char*  ecvt(double, int, int *, int *); // LEGACY
    double erand48(ushort[3]);
    //char*  fcvt(double, int, int *, int *); // LEGACY
    //char*  gcvt(double, int, char*); // LEGACY
    int    getsubopt(char**, in char**, char**);
    int    grantpt(int);
    char*  initstate(uint, char*, size_t);
    c_long jrand48(ushort[3]);
    char*  l64a(c_long);
    void   lcong48(ushort[7]);
    c_long lrand48();
    char*  mktemp(char*); // LEGACY
    int    mkstemp(char*);
    c_long mrand48();
    c_long nrand48(ushort[3]);
    int    posix_openpt(int);
    char*  ptsname(int);
    int    putenv(char*);
    c_long random();
    char*  realpath(in char*, char*);
    ushort seed48(ushort[3]);
    void   setkey(in char*);
    char*  setstate(in char*);
    void   srand48(c_long);
    void   srandom(uint);
    int    unlockpt(int);
}