1 /*
2  *  Copyright (C) 1991, 1992  Linus Torvalds
3  */
4 
5 #include <xen/ctype.h>
6 #include <xen/lib.h>
7 
8 /**
9  * simple_strtoul - convert a string to an unsigned long
10  * @cp: The start of the string
11  * @endp: A pointer to the end of the parsed string will be placed here
12  * @base: The number base to use
13  */
simple_strtoul(const char * cp,const char ** endp,unsigned int base)14 unsigned long simple_strtoul(
15     const char *cp, const char **endp, unsigned int base)
16 {
17     unsigned long result = 0, value;
18 
19     if ( !base )
20     {
21         base = 10;
22         if ( *cp == '0' )
23         {
24             base = 8;
25             cp++;
26             if ( (toupper(*cp) == 'X') && isxdigit(cp[1]) )
27             {
28                 cp++;
29                 base = 16;
30             }
31         }
32     }
33     else if ( base == 16 )
34     {
35         if ( cp[0] == '0' && toupper(cp[1]) == 'X' )
36             cp += 2;
37     }
38 
39     while ( isxdigit(*cp) &&
40             (value = isdigit(*cp) ? *cp - '0'
41                                   : toupper(*cp) - 'A' + 10) < base )
42     {
43         result = result * base + value;
44         cp++;
45     }
46 
47     if ( endp )
48         *endp = cp;
49 
50     return result;
51 }
52 
53 /*
54  * Local variables:
55  * mode: C
56  * c-file-style: "BSD"
57  * c-basic-offset: 4
58  * tab-width: 4
59  * indent-tabs-mode: nil
60  * End:
61  */
62