| 1 |
/** |
| 2 |
* D header file for C99. |
| 3 |
* |
| 4 |
* $(C_HEADER_DESCRIPTION pubs.opengroup.org/onlinepubs/009695399/basedefs/_stdlib.h.html, _stdlib.h) |
| 5 |
* |
| 6 |
* Copyright: Copyright Sean Kelly 2005 - 2014. |
| 7 |
* License: Distributed under the |
| 8 |
* $(LINK2 http://www.boost.org/LICENSE_1_0.txt, Boost Software License 1.0). |
| 9 |
* (See accompanying file LICENSE) |
| 10 |
* Authors: Sean Kelly |
| 11 |
* Standards: ISO/IEC 9899:1999 (E) |
| 12 |
* Source: $(DRUNTIMESRC core/stdc/_stdlib.d) |
| 13 |
*/ |
| 14 |
|
| 15 |
module core.stdc.stdlib; |
| 16 |
|
| 17 |
import core.stdc.config; |
| 18 |
public import core.stdc.stddef; // for wchar_t |
| 19 |
|
| 20 |
version (OSX) |
| 21 |
version = Darwin; |
| 22 |
else version (iOS) |
| 23 |
version = Darwin; |
| 24 |
else version (TVOS) |
| 25 |
version = Darwin; |
| 26 |
else version (WatchOS) |
| 27 |
version = Darwin; |
| 28 |
|
| 29 |
version (CRuntime_Glibc) |
| 30 |
version = AlignedAllocSupported; |
| 31 |
else version (CRuntime_Newlib) |
| 32 |
version = AlignedAllocSupported; |
| 33 |
else {} |
| 34 |
|
| 35 |
extern (C): |
| 36 |
|
| 37 |
/* Placed outside `nothrow` and `@nogc` in order to not constrain what the callback does. |
| 38 |
*/ |
| 39 |
/// |
| 40 |
alias _compare_fp_t = int function(const void*, const void*); |
| 41 |
|
| 42 |
nothrow: |
| 43 |
@nogc: |
| 44 |
|
| 45 |
/// |
| 46 |
inout(void)* bsearch(const void* key, inout(void)* base, size_t nmemb, size_t size, _compare_fp_t compar); |
| 47 |
/// |
| 48 |
void qsort(void* base, size_t nmemb, size_t size, _compare_fp_t compar); |
| 49 |
|
| 50 |
// https://issues.dlang.org/show_bug.cgi?id=17188 |
| 51 |
@system unittest |
| 52 |
{ |
| 53 |
struct S |
| 54 |
{ |
| 55 |
extern(C) static int cmp(const void*, const void*) { return 0; } |
| 56 |
} |
| 57 |
int[4] arr; |
| 58 |
qsort(arr.ptr, arr[0].sizeof, arr.length, &S.cmp); |
| 59 |
int key; |
| 60 |
bsearch(&key, arr.ptr, arr[0].sizeof, arr.length, &S.cmp); |
| 61 |
} |
| 62 |
|
| 63 |
/// |
| 64 |
struct div_t |
| 65 |
{ |
| 66 |
int quot, |
| 67 |
rem; |
| 68 |
} |
| 69 |
|
| 70 |
/// |
| 71 |
struct ldiv_t |
| 72 |
{ |
| 73 |
c_long quot, |
| 74 |
rem; |
| 75 |
} |
| 76 |
|
| 77 |
/// |
| 78 |
struct lldiv_t |
| 79 |
{ |
| 80 |
long quot, |
| 81 |
rem; |
| 82 |
} |
| 83 |
|
| 84 |
/// |
| 85 |
enum EXIT_SUCCESS = 0; |
| 86 |
/// |
| 87 |
enum EXIT_FAILURE = 1; |
| 88 |
/// |
| 89 |
enum MB_CUR_MAX = 1; |
| 90 |
|
| 91 |
/// |
| 92 |
version (Windows) enum RAND_MAX = 0x7fff; |
| 93 |
else version (CRuntime_Glibc) enum RAND_MAX = 0x7fffffff; |
| 94 |
else version (Darwin) enum RAND_MAX = 0x7fffffff; |
| 95 |
else version (FreeBSD) enum RAND_MAX = 0x7ffffffd; |
| 96 |
else version (NetBSD) enum RAND_MAX = 0x7fffffff; |
| 97 |
else version (OpenBSD) enum RAND_MAX = 0x7fffffff; |
| 98 |
else version (DragonFlyBSD) enum RAND_MAX = 0x7fffffff; |
| 99 |
else version (Solaris) enum RAND_MAX = 0x7fff; |
| 100 |
else version (CRuntime_Bionic) enum RAND_MAX = 0x7fffffff; |
| 101 |
else version (CRuntime_Musl) enum RAND_MAX = 0x7fffffff; |
| 102 |
else version (CRuntime_Newlib) enum RAND_MAX = 0x7fffffff; |
| 103 |
else version (CRuntime_UClibc) enum RAND_MAX = 0x7fffffff; |
| 104 |
else version (WASI) enum RAND_MAX = 0x7fffffff; |
| 105 |
else static assert( false, "Unsupported platform" ); |
| 106 |
|
| 107 |
/// |
| 108 |
double atof(scope const char* nptr); |
| 109 |
/// |
| 110 |
int atoi(scope const char* nptr); |
| 111 |
/// |
| 112 |
c_long atol(scope const char* nptr); |
| 113 |
/// |
| 114 |
long atoll(scope const char* nptr); |
| 115 |
|
| 116 |
/// |
| 117 |
double strtod(scope inout(char)* nptr, scope inout(char)** endptr); |
| 118 |
/// |
| 119 |
float strtof(scope inout(char)* nptr, scope inout(char)** endptr); |
| 120 |
/// |
| 121 |
c_long strtol(scope inout(char)* nptr, scope inout(char)** endptr, int base); |
| 122 |
/// |
| 123 |
long strtoll(scope inout(char)* nptr, scope inout(char)** endptr, int base); |
| 124 |
/// |
| 125 |
c_ulong strtoul(scope inout(char)* nptr, scope inout(char)** endptr, int base); |
| 126 |
/// |
| 127 |
ulong strtoull(scope inout(char)* nptr, scope inout(char)** endptr, int base); |
| 128 |
|
| 129 |
version (CRuntime_Microsoft) |
| 130 |
{ |
| 131 |
version (MinGW) |
| 132 |
{ |
| 133 |
/// |
| 134 |
real __mingw_strtold(scope inout(char)* nptr, scope inout(char)** endptr); |
| 135 |
/// |
| 136 |
alias __mingw_strtold strtold; |
| 137 |
} |
| 138 |
else |
| 139 |
{ |
| 140 |
// strtold exists starting from VS2013, so we give it D linkage to avoid link errors |
| 141 |
/// |
| 142 |
extern (D) real strtold(scope inout(char)* nptr, inout(char)** endptr) |
| 143 |
{ // Fake it 'till we make it |
| 144 |
return strtod(nptr, endptr); |
| 145 |
} |
| 146 |
} |
| 147 |
} |
| 148 |
else |
| 149 |
{ |
| 150 |
static if (PPCUseIEEE128) |
| 151 |
{ |
| 152 |
real __strtoieee128(scope inout(char)* nptr, scope inout(char)** endptr); |
| 153 |
alias strtold = __strtoieee128; |
| 154 |
} |
| 155 |
else |
| 156 |
{ |
| 157 |
/// Added to Bionic since Lollipop. |
| 158 |
real strtold(scope inout(char)* nptr, scope inout(char)** endptr); |
| 159 |
} |
| 160 |
} |
| 161 |
|
| 162 |
// No unsafe pointer manipulation. |
| 163 |
@trusted |
| 164 |
{ |
| 165 |
/// These two were added to Bionic in Lollipop. |
| 166 |
int rand(); |
| 167 |
/// |
| 168 |
void srand(uint seed); |
| 169 |
} |
| 170 |
|
| 171 |
// We don't mark these @trusted. Given that they return a void*, one has |
| 172 |
// to do a pointer cast to do anything sensible with the result. Thus, |
| 173 |
// functions using these already have to be @trusted, allowing them to |
| 174 |
// call @system stuff anyway. |
| 175 |
/// |
| 176 |
void* malloc(size_t size); |
| 177 |
/// |
| 178 |
void* calloc(size_t nmemb, size_t size); |
| 179 |
/// |
| 180 |
void* realloc(void* ptr, size_t size); |
| 181 |
/// |
| 182 |
void free(void* ptr); |
| 183 |
|
| 184 |
/// since C11 |
| 185 |
version (AlignedAllocSupported) |
| 186 |
{ |
| 187 |
void* aligned_alloc(size_t alignment, size_t size); |
| 188 |
} |
| 189 |
|
| 190 |
/// |
| 191 |
noreturn abort() @safe; |
| 192 |
/// |
| 193 |
noreturn exit(int status); |
| 194 |
/// |
| 195 |
int atexit(void function() func); |
| 196 |
/// |
| 197 |
noreturn _Exit(int status); |
| 198 |
|
| 199 |
/// |
| 200 |
char* getenv(scope const char* name); |
| 201 |
/// |
| 202 |
int system(scope const char* string); |
| 203 |
|
| 204 |
// These only operate on integer values. |
| 205 |
@trusted |
| 206 |
{ |
| 207 |
/// |
| 208 |
pure int abs(int j); |
| 209 |
/// |
| 210 |
pure c_long labs(c_long j); |
| 211 |
/// |
| 212 |
pure long llabs(long j); |
| 213 |
|
| 214 |
/// |
| 215 |
div_t div(int numer, int denom); |
| 216 |
/// |
| 217 |
ldiv_t ldiv(c_long numer, c_long denom); |
| 218 |
/// |
| 219 |
lldiv_t lldiv(long numer, long denom); |
| 220 |
} |
| 221 |
|
| 222 |
/// |
| 223 |
int mblen(scope const char* s, size_t n); |
| 224 |
/// |
| 225 |
int mbtowc(scope wchar_t* pwc, scope const char* s, size_t n); |
| 226 |
/// |
| 227 |
int wctomb(scope char* s, wchar_t wc); |
| 228 |
/// |
| 229 |
size_t mbstowcs(scope wchar_t* pwcs, scope const char* s, size_t n); |
| 230 |
/// |
| 231 |
size_t wcstombs(scope char* s, scope const wchar_t* pwcs, size_t n); |
| 232 |
|
| 233 |
/// |
| 234 |
version (DigitalMars) |
| 235 |
{ |
| 236 |
// See malloc comment about @trusted. |
| 237 |
void* alloca(size_t size) pure; // non-standard |
| 238 |
} |
| 239 |
else version (GNU) |
| 240 |
{ |
| 241 |
void* alloca(size_t size) pure; // compiler intrinsic |
| 242 |
} |
| 243 |
else version (LDC) |
| 244 |
{ |
| 245 |
pragma(LDC_alloca) |
| 246 |
void* alloca(size_t size) pure; |
| 247 |
} |
| 248 |
|
| 249 |
version (CRuntime_Microsoft) |
| 250 |
{ |
| 251 |
/// |
| 252 |
ulong _strtoui64(scope inout(char)*, scope inout(char)**,int); |
| 253 |
/// |
| 254 |
ulong _wcstoui64(scope inout(wchar)*, scope inout(wchar)**,int); |
| 255 |
|
| 256 |
/// |
| 257 |
long _strtoi64(scope inout(char)*, scope inout(char)**,int); |
| 258 |
/// |
| 259 |
long _wcstoi64(scope inout(wchar)*, scope inout(wchar)**,int); |
| 260 |
} |