1 /* $Id: stdlib.c,v 1.8 2003/07/11 01:23:16 royce Exp $
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 *****************************************************************/
14 #include <ddk/ntddk.h>
20 /* GLOBALS ****************************************************************/
22 static unsigned long long next
= 0;
24 /* FUNCTIONS ****************************************************************/
26 int atoi(const char *str
)
28 return (int)atol (str
);
37 long atol(const char *str
)
43 int neg
= 0, any
, cutlim
;
46 * Skip white space and pick up leading +/- sign if any.
60 * Compute the cutoff value between legal numbers and illegal
61 * numbers. That is the largest legal value, divided by the
62 * base. An input number that is greater than this value, if
63 * followed by a legal input character, is too big. One that
64 * is equal to this value may be valid or not; the limit
65 * between valid and invalid numbers is then based on the last
66 * digit. For instance, if the range for longs is
67 * [-2147483648..2147483647] and the input base is 10,
68 * cutoff will be set to 214748364 and cutlim to either
69 * 7 (neg==0) or 8 (neg==1), meaning that if we have accumulated
70 * a value > 214748364, or equal but the next digit is > 7 (or 8),
71 * the number is too big, and we will return a range error.
73 * Set any if any `digits' consumed; make it negative to indicate
76 cutoff
= neg
? -(unsigned long)LONG_MIN
: LONG_MAX
;
77 cutlim
= cutoff
% (unsigned long)10;
78 cutoff
/= (unsigned long)10;
79 for (acc
= 0, any
= 0;; c
= *s
++)
87 if (any
< 0 || acc
> cutoff
|| (acc
== cutoff
&& c
> cutlim
))
98 acc
= neg
? LONG_MIN
: LONG_MAX
;
108 * NOTE: no radix range check (valid range: 2 - 36)
113 char *_itoa (int value
, char *string
, int radix
)
125 sign
= (radix
== 10 && value
< 0);
131 while (v
|| tp
== tmp
)
138 *tp
++ = i
+ 'a' - 10;
157 next
= next
* 0x5deece66dLL
+ 11;
158 return (int)((next
>> 16) & RAND_MAX
);
165 void srand(unsigned seed
)
174 int mbtowc (wchar_t *wchar
, const char *mbchar
, size_t count
)
182 Status
= RtlMultiByteToUnicodeN (wchar
,
187 if (!NT_SUCCESS(Status
))
197 size_t mbstowcs (wchar_t *wcstr
, const char *mbstr
, size_t count
)
203 Length
= strlen (mbstr
);
207 RtlMultiByteToUnicodeSize (&Size
,
214 Status
= RtlMultiByteToUnicodeN (wcstr
,
219 if (!NT_SUCCESS(Status
))
229 int wctomb (char *mbchar
, wchar_t wchar
)
237 Status
= RtlUnicodeToMultiByteN (mbchar
,
242 if (!NT_SUCCESS(Status
))
252 size_t wcstombs (char *mbstr
, const wchar_t *wcstr
, size_t count
)
258 Length
= wcslen (wcstr
);
262 RtlUnicodeToMultiByteSize (&Size
,
264 Length
* sizeof(WCHAR
));
269 Status
= RtlUnicodeToMultiByteN (mbstr
,
273 Length
* sizeof(WCHAR
));
274 if (!NT_SUCCESS(Status
))