Magellan Linux

Annotation of /trunk/mkinitrd-magellan/klibc/usr/klibc/strntoumax.c

Parent Directory Parent Directory | Revision Log Revision Log


Revision 815 - (hide annotations) (download)
Fri Apr 24 18:32:46 2009 UTC (15 years ago) by niro
File MIME type: text/plain
File size: 1253 byte(s)
-updated to klibc-1.5.15
1 niro 532 /*
2     * strntoumax.c
3     *
4     * The strntoumax() function and associated
5     */
6    
7     #include <stddef.h>
8     #include <stdint.h>
9     #include <ctype.h>
10 niro 815 #include <inttypes.h>
11 niro 532
12     static inline int digitval(int ch)
13     {
14     if (ch >= '0' && ch <= '9') {
15     return ch - '0';
16     } else if (ch >= 'A' && ch <= 'Z') {
17     return ch - 'A' + 10;
18     } else if (ch >= 'a' && ch <= 'z') {
19     return ch - 'a' + 10;
20     } else {
21     return -1;
22     }
23     }
24    
25     uintmax_t strntoumax(const char *nptr, char **endptr, int base, size_t n)
26     {
27     int minus = 0;
28     uintmax_t v = 0;
29     int d;
30    
31     while (n && isspace((unsigned char)*nptr)) {
32     nptr++;
33     n--;
34     }
35    
36     /* Single optional + or - */
37     if (n) {
38     char c = *nptr;
39     if (c == '-' || c == '+') {
40     minus = (c == '-');
41     nptr++;
42     n--;
43     }
44     }
45    
46     if (base == 0) {
47     if (n >= 2 && nptr[0] == '0' &&
48     (nptr[1] == 'x' || nptr[1] == 'X')) {
49     n -= 2;
50     nptr += 2;
51     base = 16;
52     } else if (n >= 1 && nptr[0] == '0') {
53     n--;
54     nptr++;
55     base = 8;
56     } else {
57     base = 10;
58     }
59     } else if (base == 16) {
60     if (n >= 2 && nptr[0] == '0' &&
61     (nptr[1] == 'x' || nptr[1] == 'X')) {
62     n -= 2;
63     nptr += 2;
64     }
65     }
66    
67     while (n && (d = digitval(*nptr)) >= 0 && d < base) {
68     v = v * base + d;
69     n--;
70     nptr++;
71     }
72    
73     if (endptr)
74     *endptr = (char *)nptr;
75    
76     return minus ? -v : v;
77     }