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);
36 long atol(const char *str)
42 int neg = 0, any, cutlim;
45 * Skip white space and pick up leading +/- sign if any.
59 * Compute the cutoff value between legal numbers and illegal
60 * numbers. That is the largest legal value, divided by the
61 * base. An input number that is greater than this value, if
62 * followed by a legal input character, is too big. One that
63 * is equal to this value may be valid or not; the limit
64 * between valid and invalid numbers is then based on the last
65 * digit. For instance, if the range for longs is
66 * [-2147483648..2147483647] and the input base is 10,
67 * cutoff will be set to 214748364 and cutlim to either
68 * 7 (neg==0) or 8 (neg==1), meaning that if we have accumulated
69 * a value > 214748364, or equal but the next digit is > 7 (or 8),
70 * the number is too big, and we will return a range error.
72 * Set any if any `digits' consumed; make it negative to indicate
75 cutoff = neg ? -(unsigned long)LONG_MIN : LONG_MAX;
76 cutlim = cutoff % (unsigned long)10;
77 cutoff /= (unsigned long)10;
78 for (acc = 0, any = 0;; c = *s++)
86 if (any < 0 || acc > cutoff || (acc == cutoff && c > cutlim))
97 acc = neg ? LONG_MIN : LONG_MAX;
107 * NOTE: no radix range check (valid range: 2 - 36)
110 char *_itoa (int value, char *string, int radix)
122 sign = (radix == 10 && value < 0);
128 while (v || tp == tmp)
135 *tp++ = i + 'a' - 10;
151 next = next * 0x5deece66dLL + 11;
152 return (int)((next >> 16) & RAND_MAX);
156 void srand(unsigned seed)
162 int mbtowc (wchar_t *wchar, const char *mbchar, size_t count)
170 Status = RtlMultiByteToUnicodeN (wchar,
175 if (!NT_SUCCESS(Status))
182 size_t mbstowcs (wchar_t *wcstr, const char *mbstr, size_t count)
188 Length = strlen (mbstr);
192 RtlMultiByteToUnicodeSize (&Size,
199 Status = RtlMultiByteToUnicodeN (wcstr,
204 if (!NT_SUCCESS(Status))
211 int wctomb (char *mbchar, wchar_t wchar)
219 Status = RtlUnicodeToMultiByteN (mbchar,
224 if (!NT_SUCCESS(Status))
231 size_t wcstombs (char *mbstr, const wchar_t *wcstr, size_t count)
237 Length = wcslen (wcstr);
241 RtlUnicodeToMultiByteSize (&Size,
243 Length * sizeof(WCHAR));
248 Status = RtlUnicodeToMultiByteN (mbstr,
252 Length * sizeof(WCHAR));
253 if (!NT_SUCCESS(Status))