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