home *** CD-ROM | disk | FTP | other *** search
/ Power-Programmierung / CD2.mdf / gnu / djgpp / libsrc / c / lib / strtoul.c < prev    next >
Encoding:
C/C++ Source or Header  |  1993-10-16  |  2.8 KB  |  99 lines

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