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