home *** CD-ROM | disk | FTP | other *** search
/ Amiga ISO Collection / AmigaUtilCD2.iso / Programming / GCC / GERLIB_DEV08B.LHA / gerlib / Bonus / normal / strtoul.c < prev    next >
Encoding:
C/C++ Source or Header  |  1993-12-12  |  2.5 KB  |  90 lines

  1. /*
  2.  * Copyright (c) 1990 Regents of the University of California.
  3.  * All rights reserved.
  4.  *
  5.  * Redistribution and use in source and binary forms are permitted
  6.  * provided that: (1) source distributions retain this entire copyright
  7.  * notice and comment, and (2) distributions including binaries display
  8.  * the following acknowledgement:  ``This product includes software
  9.  * developed by the University of California, Berkeley and its contributors''
  10.  * in the documentation or other materials provided with the distribution
  11.  * and in all advertising materials mentioning features or use of this
  12.  * software. Neither the name of the University nor the names of its
  13.  * contributors may be used to endorse or promote products derived
  14.  * from this software without specific prior written permission.
  15.  * THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR
  16.  * IMPLIED WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED
  17.  * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
  18.  */
  19.  
  20. #if defined(LIBC_SCCS) && !defined(lint)
  21. static char sccsid[] = "@@(#)strtoul.c    5.2 (Berkeley) 5/17/90";
  22. #endif /* LIBC_SCCS and not lint */
  23.  
  24. #include <limits.h>
  25. #include <ctype.h>
  26. #include <stdlib.h>
  27. #include <errno.h>
  28.  
  29. /*
  30.  * Convert a string to an unsigned long integer.
  31.  *
  32.  * Ignores `locale' stuff.  Assumes that the upper and lower case
  33.  * alphabets and digits are each contiguous.
  34.  */
  35. unsigned long
  36. strtoul(const char *nptr, char **endptr, int base)
  37. {
  38.     register const char *s = nptr;
  39.     register unsigned long acc;
  40.     register int c;
  41.     register unsigned long cutoff;
  42.     register int neg = 0, any, cutlim;
  43.  
  44.     /*
  45.      * See strtol for comments as to the logic used.
  46.      */
  47.     do {
  48.         c = *s++;
  49.     } while (isspace(c));
  50.     if (c == '-') {
  51.         neg = 1;
  52.         c = *s++;
  53.     } else if (c == '+')
  54.         c = *s++;
  55.     if ((base == 0 || base == 16) &&
  56.         c == '0' && (*s == 'x' || *s == 'X')) {
  57.         c = s[1];
  58.         s += 2;
  59.         base = 16;
  60.     }
  61.     if (base == 0)
  62.         base = c == '0' ? 8 : 10;
  63.     cutoff = (unsigned long)ULONG_MAX / (unsigned long)base;
  64.     cutlim = (unsigned long)ULONG_MAX % (unsigned long)base;
  65.     for (acc = 0, any = 0;; c = *s++) {
  66.         if (isdigit(c))
  67.             c -= '0';
  68.         else if (isalpha(c))
  69.             c -= isupper(c) ? 'A' - 10 : 'a' - 10;
  70.         else
  71.             break;
  72.         if (c >= base)
  73.             break;
  74.         if (any < 0 || acc > cutoff || acc == cutoff && c > cutlim)
  75.             any = -1;
  76.         else {
  77.             any = 1;
  78.             acc *= base;
  79.             acc += c;
  80.         }
  81.     }
  82.     if (any < 0) {
  83.         acc = ULONG_MAX;
  84.         errno = ERANGE;
  85.     } else if (neg)
  86.         acc = -acc;
  87.     if (endptr != 0)
  88.         *endptr = any ? s - 1 : nptr;
  89.     return (acc);
  90. }