home *** CD-ROM | disk | FTP | other *** search
/ ftp.uv.es / 2014.11.ftp.uv.es.tar / ftp.uv.es / pub / unix / pine4.10.tar.gz / pine4.10.tar / pine4.10 / imap / src / ansilib / strtoul.c < prev   
Text File  |  1998-09-16  |  4KB  |  111 lines

  1. /*
  2.  * Program:    String to unsigned long
  3.  *
  4.  * Author:    Mark Crispin
  5.  *        Networks and Distributed Computing
  6.  *        Computing & Communications
  7.  *        University of Washington
  8.  *        Administration Building, AG-44
  9.  *        Seattle, WA  98195
  10.  *
  11.  * Date:    14 February 1995
  12.  * Last Edited:    23 March 1995
  13.  *
  14.  * Copyright 1995 by the University of Washington
  15.  *
  16.  *  Permission to use, copy, modify, and distribute this software and its
  17.  * documentation for any purpose and without fee is hereby granted, provided
  18.  * that the above copyright notice appears in all copies and that both the
  19.  * above copyright notice and this permission notice appear in supporting
  20.  * documentation, and that the name of the University of Washington not be
  21.  * used in advertising or publicity pertaining to distribution of the software
  22.  * without specific, written prior permission.  This software is made
  23.  * available "as is", and
  24.  * THE UNIVERSITY OF WASHINGTON DISCLAIMS ALL WARRANTIES, EXPRESS OR IMPLIED,
  25.  * WITH REGARD TO THIS SOFTWARE, INCLUDING WITHOUT LIMITATION ALL IMPLIED
  26.  * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE, AND IN
  27.  * NO EVENT SHALL THE UNIVERSITY OF WASHINGTON BE LIABLE FOR ANY SPECIAL,
  28.  * INDIRECT OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM
  29.  * LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, TORT
  30.  * (INCLUDING NEGLIGENCE) OR STRICT LIABILITY, ARISING OUT OF OR IN CONNECTION
  31.  * WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
  32.  *
  33.  */
  34.  
  35. /*
  36.  * Turn a string unsigned long into the real thing
  37.  * Accepts: source string
  38.  *        pointer to place to return end pointer
  39.  *        base
  40.  * Returns: parsed unsigned long integer, end pointer is updated
  41.  */
  42.  
  43. unsigned long strtoul (char *s,char **endp,int base)
  44. {
  45.   unsigned long value = 0;    /* the accumulated value */
  46.   /* Yes, you can feed it negative number strings */
  47.   int negative = 0;        /* this a negative number? */
  48.   int ok;            /* true while valid chars for number */
  49.   char c;
  50.   if (base && (base < 2 || base > 36)) {
  51.     errno = EINVAL;        /* insist upon valid base */
  52.     return NIL;
  53.   }
  54.   while (isspace (*s)) s++;    /* skip leading whitespace */
  55.   if (base) {            /* if have a base */
  56.     switch (*s) {        /* check for leading sign char */
  57.     case '-':
  58.       negative = 1;        /* yes, negative #.  fall into '+' */
  59.     case '+':
  60.       s++;            /* skip the sign character */
  61.     }
  62.                 /* allow hex prefix "0x" */
  63.     if (base == 16 && *s == '0' && toupper (*(s + 1)) == 'X') s += 2;
  64.     do {            /* convert to numeric form if digit*/
  65.       if (isdigit (*s)) c = toint (*s);
  66.                 /* alphabetic conversion */
  67.       else if (isalpha (*s)) c = *s - (isupper (*s) ? 'A' : 'a') + 10;
  68.       else break;        /* else no way it's valid */
  69.       if (c >= base) break;    /* digit out of range for base? */
  70.       value = value * base + c;    /* accumulate the digit */
  71.     } while (*++s);        /* loop until non-numeric character */
  72.   }
  73.  
  74.   else {            /* if base = 0, */
  75.     if (*s == '-') {        /* integer constants are allowed a */
  76.       negative = 1;        /* leading unary minus, but not */
  77.       s++;            /* unary plus. */
  78.     }
  79.     if (*s == '0') {        /* if it starts with 0, its either */
  80.       if (toupper (*++s)=='X') {/* 0X, which marks a hex constant, */
  81.     s++;            /* skip the X */
  82.            base = 16;        /* base is hex 16 */
  83.       }
  84.       else base = 8;        /* leading 0 means octal number */
  85.     }
  86.     else base = 10;        /* otherwise, decimal. */
  87.     do {
  88.       switch (base) {        /* char has to be valid for base */
  89.       case 8:            /* this digit ok? */
  90.     ok = isodigit (*s);
  91.     break;
  92.       case 10:
  93.     ok = isdigit (*s);
  94.     break;
  95.       case 16:
  96.     ok = isxdigit (*s);
  97.     break;
  98.       default:            /* it's good form you know */
  99.     return NIL;
  100.       }
  101.                 /* if valid char, accumulate */
  102.       if (ok) value = value * base + toint (*s++);
  103.     } while (ok);
  104.     if (toupper (*s) == 'L')s++;/* ignore 'l' or 'L' marker */
  105.   }
  106.  
  107.   if (endp) *endp = s;        /* save users endp to after number */
  108.                 /* negate number if needed */
  109.   return (negative) ? -value : value;
  110. }
  111.