3 * COPYRIGHT: See COPYING in the top level directory
4 * PROJECT: ReactOS kernel
5 * FILE: ntoskrnl/rtl/stdlib.c
6 * PURPOSE: Standard library functions
7 * PROGRAMMER: Eric Kohl (ekohl@abo.rhein-zeitung.de)
9 * 1999/07/29 ekohl Created
12 /* INCLUDES *****************************************************************/
15 #include <internal/ctype.h>
17 /* GLOBALS ****************************************************************/
20 static unsigned long long next
= 0;
22 static unsigned __int64 next
= 0;
25 /* FUNCTIONS ****************************************************************/
27 int atoi(const char *str
)
29 return (int)atol (str
);
38 long atol(const char *str
)
44 int neg
= 0, any
, cutlim
;
47 * Skip white space and pick up leading +/- sign if any.
61 * Compute the cutoff value between legal numbers and illegal
62 * numbers. That is the largest legal value, divided by the
63 * base. An input number that is greater than this value, if
64 * followed by a legal input character, is too big. One that
65 * is equal to this value may be valid or not; the limit
66 * between valid and invalid numbers is then based on the last
67 * digit. For instance, if the range for longs is
68 * [-2147483648..2147483647] and the input base is 10,
69 * cutoff will be set to 214748364 and cutlim to either
70 * 7 (neg==0) or 8 (neg==1), meaning that if we have accumulated
71 * a value > 214748364, or equal but the next digit is > 7 (or 8),
72 * the number is too big, and we will return a range error.
74 * Set any if any `digits' consumed; make it negative to indicate
77 cutoff
= neg
? -(unsigned long)LONG_MIN
: LONG_MAX
;
78 cutlim
= cutoff
% (unsigned long)10;
79 cutoff
/= (unsigned long)10;
80 for (acc
= 0, any
= 0;; c
= *s
++)
88 if (any
< 0 || acc
> cutoff
|| (acc
== cutoff
&& c
> cutlim
))
99 acc
= neg
? LONG_MIN
: LONG_MAX
;
109 * NOTE: no radix range check (valid range: 2 - 36)
114 char *_itoa (int value
, char *string
, int radix
)
126 sign
= (radix
== 10 && value
< 0);
132 while (v
|| tp
== tmp
)
139 *tp
++ = i
+ 'a' - 10;
153 * NOTE: no radix range check (valid range: 2 - 36)
158 wchar_t *_itow (int value
, wchar_t *string
, int radix
)
170 sign
= (radix
== 10 && value
< 0);
176 while (v
|| tp
== tmp
)
183 *tp
++ = i
+ L
'a' - 10;
202 #if defined(__GNUC__)
203 next
= next
* 0x5deece66dLL
+ 11;
205 next
= next
* 0x5deece66di
64 + 11;
207 return (int)((next
>> 16) & RAND_MAX
);
214 void srand(unsigned seed
)
223 int mbtowc (wchar_t *wchar
, const char *mbchar
, size_t count
)
231 Status
= RtlMultiByteToUnicodeN (wchar
,
236 if (!NT_SUCCESS(Status
))
246 size_t mbstowcs (wchar_t *wcstr
, const char *mbstr
, size_t count
)
252 Length
= strlen (mbstr
);
256 RtlMultiByteToUnicodeSize (&Size
,
263 Status
= RtlMultiByteToUnicodeN (wcstr
,
268 if (!NT_SUCCESS(Status
))
278 int wctomb (char *mbchar
, wchar_t wchar
)
286 Status
= RtlUnicodeToMultiByteN (mbchar
,
291 if (!NT_SUCCESS(Status
))
301 size_t wcstombs (char *mbstr
, const wchar_t *wcstr
, size_t count
)
307 Length
= wcslen (wcstr
);
311 RtlUnicodeToMultiByteSize (&Size
,
313 Length
* sizeof(WCHAR
));
318 Status
= RtlUnicodeToMultiByteN (mbstr
,
322 Length
* sizeof(WCHAR
));
323 if (!NT_SUCCESS(Status
))