home *** CD-ROM | disk | FTP | other *** search
/ GEMini Atari / GEMini_Atari_CD-ROM_Walnut_Creek_December_1993.iso / zip / gnu / gawk213s.lzh / GAWK213S / STRTOL.C < prev    next >
C/C++ Source or Header  |  1993-07-29  |  4KB  |  121 lines

  1. /*
  2. Article 4291 of comp.lang.c:
  3. From: chris@mimsy.umd.edu (Chris Torek)
  4. Newsgroups: comp.lang.c
  5. Subject: Re: error checking strtol
  6. Message-ID: <24445@mimsy.umd.edu>
  7. Date: 17 May 90 09:31:17 GMT
  8. Organization: U of Maryland, Dept. of Computer Science, Coll. Pk., MD 20742
  9.  
  10. The following is a working strtol.  It depends only on the existence of
  11. correct header files (including <limits.h>) and on ASCII (IBM programmers
  12. will have to use strchr()).  It does not support locales other than `C'.
  13. System V programmers should be able to replace their current strtol with
  14. this one.  (After writing this, I checked the SVR2 source; it did not
  15. handle several cases correctly.)
  16. */
  17.  
  18. #ifdef __STDC__
  19. #include <limits.h>
  20. #else
  21. #define    LONG_MIN    (-0x80000000)    /* for 32-bit 2s-complement at least */
  22. #define    LONG_MAX    0x7fffffff
  23. #endif
  24.  
  25. #if 0
  26. #include <limits.h>
  27. #include <ctype.h>
  28. #include <errno.h>
  29. #endif
  30.  
  31. #ifndef _MSC_VER
  32. int    errno;
  33. #endif
  34.  
  35. /*
  36.  * Convert a string to a long integer.
  37.  *
  38.  * Ignores `locale' stuff.  Assumes that the upper and lower case
  39.  * alphabets and digits are each contiguous.
  40.  */
  41. long
  42. strtol(nptr, endptr, base)
  43.     const char *nptr;
  44.     char **endptr;
  45.     register int base;
  46. {
  47.     register const char *s = nptr;
  48.         register unsigned long acc;
  49.         register int c;
  50.         register unsigned long cutoff;
  51.         register int neg = 0, any, cutlim;
  52.  
  53.         /*
  54.          * Skip white space and pick up leading +/- sign if any.
  55.          * If base is 0, allow 0x for hex and 0 for octal, else
  56.          * assume decimal; if base is already 16, allow 0x.
  57.          */
  58.         do {
  59.                 c = *s++;
  60.         } while (isspace(c));
  61.         if (c == '-') {
  62.                 neg = 1;
  63.                 c = *s++;
  64.         } else if (c == '+')
  65.                 c = *s++;
  66.         if ((base == 0 || base == 16) &&
  67.             c == '0' && (*s == 'x' || *s == 'X')) {
  68.                 c = s[1];
  69.                 s += 2;
  70.                 base = 16;
  71.         }
  72.         if (base == 0)
  73.                 base = c == '0' ? 8 : 10;
  74.  
  75.         /*
  76.          * Compute the cutoff value between legal numbers and illegal
  77.          * numbers.  That is the largest legal value, divided by the
  78.          * base.  An input number that is greater than this value, if
  79.          * followed by a legal input character, is too big.  One that
  80.          * is equal to this value may be valid or not; the limit
  81.          * between valid and invalid numbers is then based on the last
  82.          * digit.  For instance, if the range for longs is
  83.          * [-2147483648..2147483647] and the input base is 10,
  84.          * cutoff will be set to 214748364 and cutlim to either
  85.          * 7 (neg==0) or 8 (neg==1), meaning that if we have accumulated
  86.          * a value > 214748364, or equal but the next digit is > 7 (or 8),
  87.          * the number is too big, and we will return a range error.
  88.          *
  89.          * Set any if any `digits' consumed; make it negative to indicate
  90.          * overflow.
  91.          */
  92.         cutoff = neg ? -(unsigned long)LONG_MIN : LONG_MAX;
  93.         cutlim = cutoff % (unsigned long)base;
  94.         cutoff /= (unsigned long)base;
  95.         for (acc = 0, any = 0;; c = *s++) {
  96.                 if (isdigit(c))
  97.                         c -= '0';
  98.                 else if (isalpha(c))
  99.                         c -= isupper(c) ? 'A' - 10 : 'a' - 10;
  100.                 else
  101.                         break;
  102.                 if (c >= base)
  103.                         break;
  104.                 if (any < 0 || acc > cutoff || acc == cutoff && c > cutlim)
  105.                         any = -1;
  106.                 else {
  107.                         any = 1;
  108.                         acc *= base;
  109.                         acc += c;
  110.                 }
  111.         }
  112.         if (any < 0) {
  113.                 acc = neg ? LONG_MIN : LONG_MAX;
  114.                 errno = ERANGE;
  115.         } else if (neg)
  116.                 acc = -acc;
  117.         if (endptr != 0)
  118.         *endptr = (char *) (any ? s - 1 : nptr);
  119.         return (acc);
  120. }
  121.