The OpenD Programming Language

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