imapext-2007

view src/ansilib/strtoul.c @ 0:ada5e610ab86

imap-2007e
author yuuji@gentei.org
date Mon, 14 Sep 2009 15:17:45 +0900
parents
children
line source
1 /* ========================================================================
2 * Copyright 1988-2006 University of Washington
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 *
11 * ========================================================================
12 */
14 /*
15 * Program: String to unsigned long
16 *
17 * Author: Mark Crispin
18 * Networks and Distributed Computing
19 * Computing & Communications
20 * University of Washington
21 * Administration Building, AG-44
22 * Seattle, WA 98195
23 *
24 * Date: 14 February 1995
25 * Last Edited: 30 August 2006
26 */
28 /*
29 * Turn a string unsigned long into the real thing
30 * Accepts: source string
31 * pointer to place to return end pointer
32 * base
33 * Returns: parsed unsigned long integer, end pointer is updated
34 */
36 unsigned long strtoul (char *t,char **endp,int base)
37 {
38 unsigned long value = 0; /* the accumulated value */
39 int negative = 0; /* this a negative number? */
40 unsigned char c,*s = t;
41 if (base && (base < 2 || base > 36)) {
42 errno = EINVAL; /* insist upon valid base */
43 return value;
44 }
45 while (isspace (*s)) s++; /* skip leading whitespace */
46 switch (*s) { /* check for leading sign char */
47 case '-':
48 negative = 1; /* yes, negative #. fall into '+' */
49 case '+':
50 s++; /* skip the sign character */
51 }
52 if (!base) { /* base not specified? */
53 if (*s != '0') base = 10; /* must be decimal if doesn't start with 0 */
54 /* starts with 0x? */
55 else if (tolower (*++s) == 'x') {
56 base = 16; /* yes, is hex */
57 s++; /* skip the x */
58 }
59 else base = 8; /* ...or octal */
60 }
61 do { /* convert to numeric form if digit */
62 if (isdigit (*s)) c = *s - '0';
63 /* alphabetic conversion */
64 else if (isalpha (*s)) c = *s - (isupper (*s) ? 'A' : 'a') + 10;
65 else break; /* else no way it's valid */
66 if (c >= base) break; /* digit out of range for base? */
67 value = value * base + c; /* accumulate the digit */
68 } while (*++s); /* loop until non-numeric character */
69 if (tolower (*s) == 'l') s++; /* ignore 'l' or 'L' marker */
70 if (endp) *endp = s; /* save users endp to after number */
71 /* negate number if needed */
72 return negative ? -value : value;
73 }

UW-IMAP'd extensions by yuuji