]> git.ipfire.org Git - thirdparty/glibc.git/blame - stdlib/stdlib.h
* misc/efgcvt_r.c (ecvt_r): Handle negative values.
[thirdparty/glibc.git] / stdlib / stdlib.h
CommitLineData
28f540f4
RM
1/* Copyright (C) 1991, 1992, 1993, 1994, 1995 Free Software Foundation, Inc.
2This file is part of the GNU C Library.
3
4The GNU C Library is free software; you can redistribute it and/or
5modify it under the terms of the GNU Library General Public License as
6published by the Free Software Foundation; either version 2 of the
7License, or (at your option) any later version.
8
9The GNU C Library is distributed in the hope that it will be useful,
10but WITHOUT ANY WARRANTY; without even the implied warranty of
11MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12Library General Public License for more details.
13
14You should have received a copy of the GNU Library General Public
15License along with the GNU C Library; see the file COPYING.LIB. If
16not, write to the, 1992 Free Software Foundation, Inc., 675 Mass Ave,
17Cambridge, MA 02139, USA. */
18
19/*
20 * ANSI Standard: 4.10 GENERAL UTILITIES <stdlib.h>
21 */
22
23#ifndef _STDLIB_H
24
25#define _STDLIB_H 1
26#include <features.h>
27
28/* Get size_t, wchar_t and NULL from <stddef.h>. */
29#define __need_size_t
30#define __need_wchar_t
31#define __need_NULL
32#include <stddef.h>
33
34#define __need_Emath
35#include <errno.h>
36
37__BEGIN_DECLS
38
39/* Returned by `div'. */
40typedef struct
41 {
42 int quot; /* Quotient. */
43 int rem; /* Remainder. */
44 } div_t;
45
46/* Returned by `ldiv'. */
47typedef struct
48 {
49 long int quot; /* Quotient. */
50 long int rem; /* Remainder. */
51 } ldiv_t;
52
53
54/* The largest number rand will return (same as INT_MAX). */
55#define RAND_MAX 2147483647
56
57
58/* We define these the same for all machines.
59 Changes from this to the outside world should be done in `_exit'. */
60#define EXIT_FAILURE 1 /* Failing exit status. */
61#define EXIT_SUCCESS 0 /* Successful exit status. */
62
63
64/* Maximum length of a multibyte character in the current locale.
65 This is just one until the fancy locale support is finished. */
66#define MB_CUR_MAX 1
67
68
69/* Convert a string to a floating-point number. */
70extern double atof __P ((__const char *__nptr));
71/* Convert a string to an integer. */
72extern int atoi __P ((__const char *__nptr));
73/* Convert a string to a long integer. */
74extern long int atol __P ((__const char *__nptr));
75
76/* Convert a string to a floating-point number. */
77extern double strtod __P ((__const char *__nptr, char **__endptr));
78
79#ifdef __USE_GNU
80/* Likewise for `float' and `long double' sizes of floating-point numbers. */
28f540f4 81extern float strtof __P ((__const char *__nptr, char **__endptr));
28f540f4
RM
82extern __long_double_t strtold __P ((__const char *__nptr, char **__endptr));
83#endif
84
85/* Convert a string to a long integer. */
86extern long int strtol __P ((__const char *__nptr, char **__endptr,
87 int __base));
88/* Convert a string to an unsigned long integer. */
89extern unsigned long int strtoul __P ((__const char *__nptr,
90 char **__endptr, int __base));
91
92#if defined (__GNUC__) && defined (__USE_BSD)
93/* Convert a string to a quadword integer. */
94extern long long int strtoq __P ((__const char *__nptr, char **__endptr,
95 int __base));
96/* Convert a string to an unsigned quadword integer. */
97extern unsigned long long int strtouq __P ((__const char *__nptr,
98 char **__endptr, int __base));
99#endif /* GCC and use BSD. */
100
f0bf9cb9
RM
101
102/* The internal entry points for `strtoX' take an extra flag argument
103 saying whether or not to parse locale-dependent number grouping. */
104
105extern double __strtod_internal (__const char *__nptr,
106 char **__endptr, int __group);
107extern float __strtof_internal (__const char *__nptr, char **__endptr,
108 int __group);
109extern __long_double_t __strtold_internal (__const char *__nptr,
110 char **__endptr, int __group);
111extern long int __strtol_internal (__const char *__nptr, char **__endptr,
112 int __base, int __group);
113extern unsigned long int __strtoul_internal (__const char *__nptr,
114 char **__endptr, int __base,
115 int __group);
116extern long long int __strtoq_internal (__const char *__nptr, char **__endptr,
117 int __base, int __group);
118extern unsigned long long int __strtouq_internal (__const char *__nptr,
119 char **__endptr, int __base,
120 int __group);
121
28f540f4 122#if defined (__OPTIMIZE__) && __GNUC__ >= 2
f0bf9cb9
RM
123/* Define inline functions which call the internal entry points. */
124
125extern __inline double strtod (__const char *__nptr, char **__endptr)
126{ return __strtod_internal (__nptr, __endptr, 0); }
127extern __inline long int strtol (__const char *__nptr,
128 char **__endptr, int __base)
129{ return __strtol_internal (__nptr, __endptr, __base, 0); }
130extern __inline unsigned long int strtoul (__const char *__nptr,
131 char **__endptr, int __base)
132{ return __strtoul_internal (__nptr, __endptr, __base, 0); }
133
134#ifdef __USE_GNU
135extern __inline float strtof (__const char *__nptr, char **__endptr)
136{ return __strtof_internal (__nptr, __endptr, 0); }
137extern __inline __long_double_t strtold (__const char *__nptr, char **__endptr)
138{ return __strtold_internal (__nptr, __endptr, 0); }
139#endif
140
141#ifdef __USE_BSD
142extern __inline long long int strtoq (__const char *__nptr, char **__endptr,
143 int __base)
144{ return __strtoq_internal (__nptr, __endptr, __base, 0); }
145extern __inline unsigned long long int strtouq (__const char *__nptr,
146 char **__endptr, int __base)
147{ return __strtouq_internal (__nptr, __endptr, __base, 0); }
148#endif
149
28f540f4 150extern __inline double atof (__const char *__nptr)
f0bf9cb9 151{ return strtod (__nptr, (char **) NULL); }
28f540f4
RM
152extern __inline int atoi (__const char *__nptr)
153{ return (int) strtol (__nptr, (char **) NULL, 10); }
154extern __inline long int atol (__const char *__nptr)
155{ return strtol (__nptr, (char **) NULL, 10); }
156#endif /* Optimizing GCC >=2. */
157
158
159/* Return a random integer between 0 and RAND_MAX inclusive. */
160extern int rand __P ((void));
161/* Seed the random number generator with the given number. */
162extern void srand __P ((unsigned int __seed));
163
164/* These are the functions that actually do things. The `random', `srandom',
165 `initstate' and `setstate' functions are those from BSD Unices.
166 The `rand' and `srand' functions are required by the ANSI standard.
167 We provide both interfaces to the same random number generator. */
168/* Return a random long integer between 0 and RAND_MAX inclusive. */
169extern long int __random __P ((void));
170/* Seed the random number generator with the given number. */
171extern void __srandom __P ((unsigned int __seed));
172
173/* Initialize the random number generator to use state buffer STATEBUF,
174 of length STATELEN, and seed it with SEED. Optimal lengths are 8, 16,
175 32, 64, 128 and 256, the bigger the better; values less than 8 will
176 cause an error and values greater than 256 will be rounded down. */
177extern __ptr_t __initstate __P ((unsigned int __seed, __ptr_t __statebuf,
178 size_t __statelen));
179/* Switch the random number generator to state buffer STATEBUF,
180 which should have been previously initialized by `initstate'. */
181extern __ptr_t __setstate __P ((__ptr_t __statebuf));
182
183#ifdef __USE_BSD
184extern long int random __P ((void));
185extern void srandom __P ((unsigned int __seed));
186extern __ptr_t initstate __P ((unsigned int __seed, __ptr_t __statebuf,
187 size_t __statelen));
188extern __ptr_t setstate __P ((__ptr_t __statebuf));
189
190#if defined (__OPTIMIZE__) && __GNUC__ >= 2
191extern __inline long int random (void)
192{ return __random(); }
193extern __inline void srandom (unsigned int __seed)
194{ __srandom(__seed); }
195extern __inline __ptr_t initstate (unsigned int __seed,
196 __ptr_t __statebuf, size_t __statelen)
197{ return __initstate (__seed, __statebuf, __statelen); }
198extern __inline __ptr_t setstate (__ptr_t __statebuf)
199{ return __setstate (__statebuf); }
200#endif /* Optimizing GCC >=2. */
60478656
RM
201
202#ifdef __USE_REENTRANT
203/* Reentrant versions of the `random' family of functions.
204 These functions all use the following data structure to contain
205 state, rather than global state variables. */
206
207struct random_data
208 {
209 long int *fptr; /* Front pointer. */
210 long int *rptr; /* Rear pointer. */
211 long int *state; /* Array of state values. */
212 int rand_type; /* Type of random number generator. */
213 int rand_deg; /* Degree of random number generator. */
214 int rand_sep; /* Distance between front and rear. */
215 long int *end_ptr; /* Pointer behind state table. */
216 };
217
218extern int __random_r __P ((struct random_data *__buf, long int *__result));
219extern int __srandom_r __P ((unsigned int __seed, struct random_data *__buf));
220extern int __initstate_r __P ((unsigned int __seed, __ptr_t __statebuf,
221 size_t __statelen, struct random_data *__buf));
222extern int __setstate_r __P ((__ptr_t __statebuf, struct random_data *__buf));
223
224extern int random_r __P ((struct random_data *__buf, long int *__result));
225extern int srandom_r __P ((unsigned int __seed, struct random_data *__buf));
226extern int initstate_r __P ((unsigned int __seed, __ptr_t __statebuf,
227 size_t __statelen, struct random_data *__buf));
228extern int setstate_r __P ((__ptr_t __statebuf, struct random_data *__buf));
229#endif /* __USE_REENTRANT. */
230#endif /* Use BSD. */
231
232
233#ifdef __USE_SVID
234/* System V style 48-bit random number generator functions. */
235
236/* Data structure for communication with thread safe versions. */
237struct drand48_data
238 {
239 unsigned short int X[3]; /* Current state. */
240 unsigned short int a[3]; /* Factor in congruential formula. */
241 unsigned short int c; /* Additive const. in congruential formula. */
242 unsigned short int old_X[3]; /* Old state. */
243 int init; /* Flag for initializing. */
244 };
245
246/* Return non-negative, double-precision floating-point value in [0.0,1.0). */
247extern double drand48 __P ((void));
248extern int drand48_r __P ((struct drand48_data *__buffer, double *__result));
249extern double erand48 __P ((unsigned short int __xsubi[3]));
250extern int erand48_r __P ((unsigned short int __xsubi[3],
251 struct drand48_data *__buffer, double *__result));
252/* Return non-negative, long integer in [0,2^31). */
253extern long lrand48 __P ((void));
254extern int lrand48_r __P ((struct drand48_data *__buffer, long *__result));
255extern long nrand48 __P ((unsigned short int __xsubi[3]));
256extern int nrand48_r __P ((unsigned short int __xsubi[3],
257 struct drand48_data *__buffer, long *__result));
258/* Return signed, long integers in [-2^31,2^31). */
259extern long mrand48 __P ((void));
260extern int mrand48_r __P ((struct drand48_data *__buffer, long *__result));
261extern long jrand48 __P ((unsigned short int __xsubi[3]));
262extern int jrand48_r __P ((unsigned short int __xsubi[3],
263 struct drand48_data *__buffer, long *__result));
264/* Seed random number generator. */
265extern void srand48 __P ((long __seedval));
266extern int srand48_r __P ((long __seedval, struct drand48_data *__buffer));
267extern unsigned short int *seed48 __P ((unsigned short int __seed16v[3]));
268extern int seed48_r __P ((unsigned short int __seed16v[3],
269 struct drand48_data *__buffer));
270extern void lcong48 __P ((unsigned short int __param[7]));
271extern int lcong48_r __P ((unsigned short int __param[7],
272 struct drand48_data *__buffer));
273
274/* Internal function to compute next state of the generator. */
275extern int __drand48_iterate __P ((unsigned short int __xsubi[3],
276 struct drand48_data *__buffer));
277#endif /* __USE_SVID. */
28f540f4
RM
278
279
280/* Allocate SIZE bytes of memory. */
281extern __ptr_t malloc __P ((size_t __size));
282/* Re-allocate the previously allocated block
283 in __ptr_t, making the new block SIZE bytes long. */
284extern __ptr_t realloc __P ((__ptr_t __ptr, size_t __size));
285/* Allocate NMEMB elements of SIZE bytes each, all initialized to 0. */
286extern __ptr_t calloc __P ((size_t __nmemb, size_t __size));
287/* Free a block allocated by `malloc', `realloc' or `calloc'. */
288extern void free __P ((__ptr_t __ptr));
289
290#ifdef __USE_MISC
291/* Free a block. An alias for `free'. (Sun Unices). */
292extern void cfree __P ((__ptr_t __ptr));
293#endif /* Use misc. */
294
295#if defined(__USE_GNU) || defined(__USE_BSD) || defined(__USE_MISC)
296#include <alloca.h>
297#endif /* Use GNU, BSD, or misc. */
298
299#ifdef __USE_BSD
300/* Allocate SIZE bytes on a page boundary. The storage cannot be freed. */
301extern __ptr_t valloc __P ((size_t __size));
302#endif
303
304
305/* Abort execution and generate a core-dump. */
306extern void abort __P ((void)) __attribute__ ((__noreturn__));
307
308
309/* Register a function to be called when `exit' is called. */
310extern int atexit __P ((void (*__func) (void)));
311
312#ifdef __USE_MISC
313/* Register a function to be called with the status
314 given to `exit' and the given argument. */
315extern int on_exit __P ((void (*__func) (int __status, __ptr_t __arg),
316 __ptr_t __arg));
317#endif
318
319/* Call all functions registered with `atexit' and `on_exit',
320 in the reverse of the order in which they were registered
321 perform stdio cleanup, and terminate program execution with STATUS. */
322extern void exit __P ((int __status)) __attribute__ ((__noreturn__));
323
324
325/* Return the value of envariable NAME, or NULL if it doesn't exist. */
326extern char *getenv __P ((__const char *__name));
327
328#ifdef __USE_SVID
329/* The SVID says this is in <stdio.h>, but this seems a better place. */
330/* Put STRING, which is of the form "NAME=VALUE", in the environment.
331 If there is no `=', remove NAME from the environment. */
332extern int putenv __P ((__const char *__string));
333#endif
334
335#ifdef __USE_BSD
336/* Set NAME to VALUE in the environment.
337 If REPLACE is nonzero, overwrite an existing value. */
338extern int setenv __P ((__const char *__name, __const char *__value,
339 int __replace));
196980f5
RM
340
341/* Remove the variable NAME from the environment. */
342extern void unsetenv __P ((__const char *__name));
28f540f4
RM
343#endif
344
345/* Execute the given line as a shell command. */
346extern int system __P ((__const char *__command));
347
348
349/* Shorthand for type of comparison functions. */
60478656
RM
350#ifndef __COMPAR_FN_T
351#define __COMPAR_FN_T
28f540f4 352typedef int (*__compar_fn_t) __P ((__const __ptr_t, __const __ptr_t));
60478656 353#endif
28f540f4
RM
354
355#ifdef __USE_GNU
356typedef __compar_fn_t comparison_fn_t;
357#endif
358
359/* Do a binary search for KEY in BASE, which consists of NMEMB elements
360 of SIZE bytes each, using COMPAR to perform the comparisons. */
361extern __ptr_t bsearch __P ((__const __ptr_t __key, __const __ptr_t __base,
362 size_t __nmemb, size_t __size,
363 __compar_fn_t __compar));
364
365/* Sort NMEMB elements of BASE, of SIZE bytes each,
366 using COMPAR to perform the comparisons. */
367extern void qsort __P ((__ptr_t __base, size_t __nmemb, size_t __size,
368 __compar_fn_t __compar));
369
370
28f540f4 371/* Return the absolute value of X. */
7176f4e4
RM
372extern int abs __P ((int __x)) __attribute__ ((__const__));
373extern long int labs __P ((long int __x)) __attribute__ ((__const__));
28f540f4
RM
374
375
376/* Return the `div_t' or `ldiv_t' representation
377 of the value of NUMER over DENOM. */
378/* GCC may have built-ins for these someday. */
7176f4e4
RM
379extern div_t div __P ((int __numer, int __denom)) __attribute__ ((__const__));
380extern ldiv_t ldiv __P ((long int __numer, long int __denom)) __attribute__ ((__const__));
28f540f4
RM
381
382
60478656
RM
383#ifdef __USE_SVID
384/* Convert floating point numbers to strings. The returned values are
385 valid only until another call to the same function. */
386
387/* Convert VALUE to a string with NDIGIT digits and return a pointer to
388 this. Set *DECPT with the position of the decimal character and *SIGN
389 with the sign of the number. */
390char *ecvt __P ((double __value, int __ndigit, int *__decpt, int *sign));
391
392/* Convert VALUE to a string rounded to NDIGIT decimal digits. Set *DECPT
393 with the position of the decimal character and *SIGN with the sign of
394 the number. */
395char *fcvt __P ((double __value, int __ndigit, int *__decpt, int *sign));
396
397/* If possible convert VALUE to a string with NDIGIT significant digits.
398 Otherwise use exponential representation. The resulting string will
399 be written to BUF. */
400char *gcvt __P ((double __value, int __ndigit, char *__buf));
401
402/* Reentrant version of the functions above which provide their own
403 buffers. */
404int ecvt_r __P ((double __value, int __ndigit, int *__decpt, int *sign,
c2216480 405 char *__buf, size_t __len));
60478656 406int fcvt_r __P ((double __value, int __ndigit, int *__decpt, int *sign,
c2216480 407 char *__buf, size_t __len));
60478656
RM
408#endif
409
410
28f540f4
RM
411/* Return the length of the multibyte character
412 in S, which is no longer than N. */
413extern int mblen __P ((__const char *__s, size_t __n));
414/* Return the length of the given multibyte character,
415 putting its `wchar_t' representation in *PWC. */
416extern int mbtowc __P ((wchar_t * __pwc, __const char *__s, size_t __n));
417/* Put the multibyte character represented
418 by WCHAR in S, returning its length. */
419extern int wctomb __P ((char *__s, wchar_t __wchar));
420
421#if defined (__OPTIMIZE__) && __GNUC__ >= 2
422extern __inline int mblen (__const char *__s, size_t __n)
423{ return mbtowc ((wchar_t *) NULL, __s, __n); }
424#endif /* Optimizing GCC >=2. */
425
426
427/* Convert a multibyte string to a wide char string. */
428extern size_t mbstowcs __P ((wchar_t * __pwcs, __const char *__s, size_t __n));
429/* Convert a wide char string to multibyte string. */
430extern size_t wcstombs __P ((char *__s, __const wchar_t * __pwcs, size_t __n));
431
432
433__END_DECLS
434
435#endif /* stdlib.h */