Chameleon

Chameleon Svn Source Tree

Root/branches/Chimera/i386/modules/klibc/strntoumax.c

1/*
2 * strntoumax.c
3 *
4 * The strntoumax() function and associated
5 */
6#include "libsaio.h"
7
8static inline int digitval(int ch)
9{
10if (ch >= '0' && ch <= '9') {
11return ch - '0';
12} else if (ch >= 'A' && ch <= 'Z') {
13return ch - 'A' + 10;
14} else if (ch >= 'a' && ch <= 'z') {
15return ch - 'a' + 10;
16} else {
17return -1;
18}
19}
20
21uintmax_t strntoumax(const char *nptr, char **endptr, int base, size_t n)
22{
23int minus = 0;
24uintmax_t v = 0;
25int d;
26
27while (n && isspace((unsigned char)*nptr)) {
28nptr++;
29n--;
30}
31
32/* Single optional + or - */
33if (n) {
34char c = *nptr;
35if (c == '-' || c == '+') {
36minus = (c == '-');
37nptr++;
38n--;
39}
40}
41
42if (base == 0) {
43if (n >= 2 && nptr[0] == '0' &&
44 (nptr[1] == 'x' || nptr[1] == 'X')) {
45n -= 2;
46nptr += 2;
47base = 16;
48} else if (n >= 1 && nptr[0] == '0') {
49n--;
50nptr++;
51base = 8;
52} else {
53base = 10;
54}
55} else if (base == 16) {
56if (n >= 2 && nptr[0] == '0' &&
57 (nptr[1] == 'x' || nptr[1] == 'X')) {
58n -= 2;
59nptr += 2;
60}
61}
62
63while (n && (d = digitval(*nptr)) >= 0 && d < base) {
64v = v * base + d;
65n--;
66nptr++;
67}
68
69if (endptr)
70*endptr = (char *)nptr;
71
72return minus ? -v : v;
73}
74

Archive Download this file

Revision: 2225