xref: /dragonfly/include/stdlib.h (revision faed59899d101054a6cc8d506336755d9fd1821d)
1 /*-
2  * Copyright (c) 1990, 1993
3  *        The Regents of the University of California.  All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions
7  * are met:
8  * 1. Redistributions of source code must retain the above copyright
9  *    notice, this list of conditions and the following disclaimer.
10  * 2. Redistributions in binary form must reproduce the above copyright
11  *    notice, this list of conditions and the following disclaimer in the
12  *    documentation and/or other materials provided with the distribution.
13  * 3. Neither the name of the University nor the names of its contributors
14  *    may be used to endorse or promote products derived from this software
15  *    without specific prior written permission.
16  *
17  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
18  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
21  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
23  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
24  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
25  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
26  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
27  * SUCH DAMAGE.
28  *
29  *        @(#)stdlib.h        8.5 (Berkeley) 5/19/95
30  * $FreeBSD: src/include/stdlib.h,v 1.67 2008/07/22 11:40:42 ache Exp $
31  */
32 
33 #ifndef _STDLIB_H_
34 #define   _STDLIB_H_
35 
36 #include <sys/cdefs.h>
37 #include <sys/_null.h>
38 #include <sys/types.h>
39 #ifndef __cplusplus
40 #include <machine/wchar.h>              /* for ___wchar_t */
41 #endif
42 #if __BSD_VISIBLE
43 #include <malloc_np.h>                            /* for malloc_usable_size */
44 #endif
45 
46 #ifndef _SIZE_T_DECLARED
47 typedef   __size_t  size_t;             /* _GCC_SIZE_T OK */
48 #define   _SIZE_T_DECLARED
49 #endif
50 
51 #ifndef   __cplusplus
52 #ifndef _WCHAR_T_DECLARED
53 typedef   ___wchar_t          wchar_t;  /* _GCC_WCHAR_T OK */
54 #define   _WCHAR_T_DECLARED
55 #endif
56 #endif
57 
58 #if __EXT1_VISIBLE
59 #ifndef _ERRNO_T_DECLARED
60 typedef   int                 errno_t;
61 #define   _ERRNO_T_DECLARED
62 #endif
63 #endif
64 
65 typedef struct {
66           int       quot;               /* quotient */
67           int       rem;                /* remainder */
68 } div_t;
69 
70 typedef struct {
71           long      quot;
72           long      rem;
73 } ldiv_t;
74 
75 #define   EXIT_FAILURE        1
76 #define   EXIT_SUCCESS        0
77 
78 #define   RAND_MAX  0x7fffffff
79 
80 __BEGIN_DECLS
81 #ifdef _XLOCALE_H_
82 #include <xlocale/_stdlib.h>
83 #endif
84 extern int __mb_cur_max;
85 extern int ___mb_cur_max(void);
86 #define   MB_CUR_MAX          ((size_t)___mb_cur_max())
87 
88 void       abort(void) __dead2;
89 /* void    abort2(const char *, int, void **) __dead2; */
90 #if !defined(_KERNEL_VIRTUAL)
91 int        abs(int) __pure2;
92 #endif
93 int        atexit(void (*)(void));
94 double     atof(const char *);
95 int        atoi(const char *);
96 long       atol(const char *);
97 void      *bsearch(const void *, const void *, size_t,
98                      size_t, int (*)(const void *, const void *));
99 void      *calloc(size_t, size_t) __alloc_size2(1, 2) __malloclike __heedresult;
100 div_t      div(int, int) __pure2;
101 void       exit(int) __dead2;
102 void       free(void *);
103 char      *getenv(const char *);
104 #if !defined(_KERNEL_VIRTUAL)
105 long       labs(long) __pure2;
106 #endif
107 ldiv_t     ldiv(long, long) __pure2;
108 void      *malloc(size_t) __malloclike __heedresult __alloc_size(1);
109 int        mblen(const char *, size_t);
110 size_t     mbstowcs(wchar_t * __restrict , const char * __restrict, size_t);
111 int        mbtowc(wchar_t * __restrict, const char * __restrict, size_t);
112 void       qsort(void *, size_t, size_t, int (*)(const void *, const void *));
113 int        rand(void);
114 void      *realloc(void *, size_t) __heedresult __alloc_size(2);
115 void       srand(unsigned);
116 double     strtod(const char * __restrict, char ** __restrict);
117 float      strtof(const char * __restrict, char ** __restrict);
118 #if !defined(_KERNEL_VIRTUAL)
119 long       strtol(const char * __restrict, char ** __restrict, int);
120 #endif
121 long double
122            strtold(const char * __restrict, char ** __restrict);
123 #if !defined(_KERNEL_VIRTUAL)
124 unsigned long
125            strtoul(const char * __restrict, char ** __restrict, int);
126 #endif
127 int        system(const char *);
128 int        wctomb(char *, wchar_t);
129 size_t     wcstombs(char * __restrict, const wchar_t * __restrict, size_t);
130 
131 /*
132  * Functions added in C99 which we make conditionally available in the
133  * BSD^C89 namespace if the compiler supports `long long'.
134  * The #if test is more complicated than it ought to be because
135  * __BSD_VISIBLE implies __ISO_C_VISIBLE == 1999 *even if* `long long'
136  * is not supported in the compilation environment (which therefore means
137  * that it can't really be ISO C99).
138  *
139  * (The only other extension made by C99 in this header is _Exit().)
140  */
141 #if __ISO_C_VISIBLE >= 1999 || defined(__cplusplus)
142 #ifdef __LONG_LONG_SUPPORTED
143 /* LONGLONG */
144 typedef struct {
145           long long quot;
146           long long rem;
147 } lldiv_t;
148 
149 /* LONGLONG */
150 long long
151            atoll(const char *);
152 /* LONGLONG */
153 long long
154            llabs(long long) __pure2;
155 /* LONGLONG */
156 lldiv_t    lldiv(long long, long long) __pure2;
157 /* LONGLONG */
158 long long
159            strtoll(const char * __restrict, char ** __restrict, int);
160 /* LONGLONG */
161 unsigned long long
162            strtoull(const char * __restrict, char ** __restrict, int);
163 #endif /* __LONG_LONG_SUPPORTED */
164 
165 void       _Exit(int) __dead2;
166 #endif /* __ISO_C_VISIBLE >= 1999 */
167 
168 /*
169  * C11 functions.
170  */
171 #if __ISO_C_VISIBLE >= 2011 || (defined(__cplusplus) && __cplusplus >= 201103L)
172 void      *aligned_alloc(size_t, size_t) __malloclike __heedresult
173               __alloc_align(1) __alloc_size(2);
174 int       at_quick_exit(void (*)(void));          /* extra extern case for __cplusplus? */
175 void      quick_exit(int) __dead2;
176 #endif /* __ISO_C_VISIBLE >= 2011 */
177 
178 /*
179  * Extensions made by POSIX relative to C.
180  */
181 #if __POSIX_VISIBLE >= 199506
182 int        rand_r(unsigned *);                              /* (TSF) */
183 #endif
184 #if __POSIX_VISIBLE >= 200112
185 int        posix_memalign(void **, size_t, size_t) __nonnull(1); /* (ADV) */
186 int        setenv(const char *, const char *, int);
187 int        unsetenv(const char *);
188 #endif
189 
190 #if __POSIX_VISIBLE >= 200809 || __XSI_VISIBLE
191 int        getsubopt(char **, char *const *, char **);
192 #ifndef _MKDTEMP_DECLARED
193 char      *mkdtemp(char *);
194 #define   _MKDTEMP_DECLARED
195 #endif
196 #ifndef _MKSTEMP_DECLARED
197 int        mkstemp(char *);
198 #define   _MKSTEMP_DECLARED
199 #endif
200 #endif /* __POSIX_VISIBLE >= 200809 || __XSI_VISIBLE */
201 
202 /*
203  * The only changes to the XSI namespace in revision 6 were the deletion
204  * of the ttyslot() and valloc() functions, which we never declared
205  * in this header.  For revision 7, ecvt(), fcvt(), and gcvt(), which
206  * we also do not have, and mktemp(), are to be deleted.
207  */
208 #if __XSI_VISIBLE
209 /* XXX XSI requires pollution from <sys/wait.h> here.  We'd rather not. */
210 long       a64l(const char *);
211 double     drand48(void);
212 double     erand48(unsigned short[3]);
213 #if 0
214 #if __BSD_VISIBLE || (__XSI_VISIBLE && __XSI_VISIBLE < 700)
215 char      *ecvt(double, int, int * __restrict, int * __restrict);     /* LEGACY */
216 char      *fcvt(double, int, int * __restrict, int * __restrict);     /* LEGACY */
217 char      *gcvt(double, int, int * __restrict, int * __restrict);     /* LEGACY */
218 #endif
219 #endif
220 int        grantpt(int);
221 char      *initstate(unsigned long /* XSI requires u_int */, char *, long);
222 long       jrand48(unsigned short[3]);
223 char      *l64a(long);
224 void       lcong48(unsigned short[7]);
225 long       lrand48(void);
226 #if __BSD_VISIBLE || (__XSI_VISIBLE && __XSI_VISIBLE < 700)
227 #if !defined(_MKTEMP_DECLARED)
228 char      *mktemp(char *);                                            /* LEGACY */
229 #define   _MKTEMP_DECLARED
230 #endif
231 #endif
232 long       mrand48(void);
233 long       nrand48(unsigned short[3]);
234 int        posix_openpt(int);
235 char      *ptsname(int);
236 int        putenv(char *);
237 long       random(void);
238 char      *realpath(const char * __restrict, char * __restrict);
239 unsigned short
240           *seed48(unsigned short[3]);
241 int        setkey(const char *);
242 char      *setstate(/* const */ char *);
243 void       srand48(long);
244 void       srandom(unsigned long);
245 int        unlockpt(int);
246 #endif /* __XSI_VISIBLE */
247 
248 #if __BSD_VISIBLE
249 /* extern const char *_malloc_options;
250 extern void (*_malloc_message)(const char *, const char *, const char *,
251               const char *); */
252 
253 /*
254  * The alloca() function can't be implemented in C, and on some
255  * platforms it can't be implemented at all as a callable function.
256  * The GNU C compiler provides a built-in alloca() which we can use.
257  * On platforms where alloca() is not in libc, programs which use it
258  * will fail to link when compiled with non-GNU compilers.
259  */
260 #if __GNUC__ >= 2
261 #undef  alloca      /* some GNU bits try to get cute and define this on their own */
262 #define alloca(sz) __builtin_alloca(sz)
263 #endif
264 
265 __uint32_t
266            arc4random(void);
267 void       arc4random_addrandom(__uint8_t *, size_t);
268 void       arc4random_buf(void *, size_t);
269 void       arc4random_stir(void);
270 __uint32_t
271            arc4random_uniform(__uint32_t);
272 char      *getbsize(int *, long *);
273 
274 /* getcap(3) functions */
275 char      *cgetcap(char *, const char *, int);
276 int        cgetclose(void);
277 int        cgetent(char **, char **, const char *);
278 int        cgetfirst(char **, char **);
279 int        cgetmatch(const char *, const char *);
280 int        cgetnext(char **, char **);
281 int        cgetnum(char *, const char *, long *);
282 int        cgetset(const char *);
283 int        cgetstr(char *, const char *, char **);
284 int        cgetustr(char *, const char *, char **);
285 
286 int        clearenv(void);
287 int        daemon(int, int);
288 char      *devname(dev_t, mode_t);
289 char      *devname_r(dev_t, mode_t, char *, size_t);
290 char      *fdevname(int);
291 int        fdevname_r(int, char *, size_t);
292 void       freezero(void *, size_t);
293 int        getloadavg(double [], int);
294 const char *
295            getprogname(void);
296 
297 int        heapsort(void *, size_t, size_t, int (*)(const void *, const void *));
298 int        l64a_r(long, char *, int);
299 int        mergesort(void *, size_t, size_t, int (*)(const void *, const void *));
300 int        mkostemp(char *, int);
301 int        mkostemps(char *, int, int);
302 void       qsort_r(void *, size_t, size_t, void *,
303                      int (*)(void *, const void *, const void *));
304 int        radixsort(const unsigned char **, int, const unsigned char *,
305                        unsigned int);
306 void      *reallocarray(void *, size_t, size_t) __heedresult __alloc_size2(2, 3);
307 void      *recallocarray(void *, size_t, size_t, size_t) __heedresult
308               __alloc_size2(3, 4);
309 void      *reallocf(void *, size_t) __heedresult __alloc_size(2);
310 int        rpmatch(const char *);
311 void       setprogname(const char *);
312 int        sradixsort(const unsigned char **, int, const unsigned char *,
313                         unsigned int);
314 void       sranddev(void);
315 void       srandomdev(void);
316 long long
317            strsuftoll(const char *, const char *, long long, long long);
318 long long
319            strsuftollx(const char *, const char *, long long, long long, char *,
320               size_t);
321 long long
322            strtonum(const char *, long long, long long, const char **);
323 
324 /* Deprecated interfaces. */
325 #if !defined(_KERNEL_VIRTUAL)
326 __int64_t
327            strtoq(const char *, char **, int);
328 __uint64_t
329            strtouq(const char *, char **, int);
330 #endif
331 
332 extern char *suboptarg;                           /* getsubopt(3) external variable */
333 #endif /* __BSD_VISIBLE */
334 
335 #if __EXT1_VISIBLE
336 /* K.3.6 */
337 typedef   void (*constraint_handler_t)(const char * __restrict,
338     void * __restrict, errno_t);
339 /* K.3.6.1.1 */
340 constraint_handler_t set_constraint_handler_s(constraint_handler_t handler);
341 /* K.3.6.1.2 */
342 _Noreturn void abort_handler_s(const char * __restrict, void * __restrict,
343     errno_t);
344 /* K3.6.1.3 */
345 void ignore_handler_s(const char * __restrict, void * __restrict, errno_t);
346 #endif /* __EXT1_VISIBLE */
347 __END_DECLS
348 
349 #endif /* !_STDLIB_H_ */
350