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

  1. /*-
  2.  * Copyright (c) 1990 The 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[] = "@@(#)strtol.c    5.3 (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 a long integer.
  31.  *
  32.  * Ignores `locale' stuff.  Assumes that the upper and lower case
  33.  * alphabets and digits are each contiguous.
  34.  */
  35. long
  36. strtol(const char *nptr, char **endptr, int base)
  37. {
  38.     register 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.      * Skip white space and pick up leading +/- sign if any.
  46.      * If base is 0, allow 0x for hex and 0 for octal, else
  47.      * assume decimal; if base is already 16, allow 0x.
  48.      */
  49.     do {
  50.         c = *s++;
  51.     } while (isspace(c));
  52.     if (c == '-') {
  53.         neg = 1;
  54.         c = *s++;
  55.     } else if (c == '+')
  56.         c = *s++;
  57.     if ((base == 0 || base == 16) &&
  58.         c == '0' && (*s == 'x' || *s == 'X')) {
  59.         c = s[1];
  60.         s += 2;
  61.         base = 16;
  62.     }
  63.     if (base == 0)
  64.         base = c == '0' ? 8 : 10;
  65.  
  66.     /*
  67.      * Compute the cutoff value between legal numbers and illegal
  68.      * numbers.  That is the largest legal value, divided by the
  69.      * base.  An input number that is greater than this value, if
  70.      * followed by a legal input character, is too big.  One that
  71.      * is equal to this value may be valid or not; the limit
  72.      * between valid and invalid numbers is then based on the last
  73.      * digit.  For instance, if the range for longs is
  74.      * [-2147483648..2147483647] and the input base is 10,
  75.      * cutoff will be set to 214748364 and cutlim to either
  76.      * 7 (neg==0) or 8 (neg==1), meaning that if we have accumulated
  77.      * a value > 214748364, or equal but the next digit is > 7 (or 8),
  78.      * the number is too big, and we will return a range error.
  79.      *
  80.      * Set any if any `digits' consumed; make it negative to indicate
  81.      * overflow.
  82.      */
  83.     cutoff = neg ? -(unsigned long)LONG_MIN : LONG_MAX;
  84.     cutlim = cutoff % (unsigned long)base;
  85.     cutoff /= (unsigned long)base;
  86.     for (acc = 0, any = 0;; c = *s++) {
  87.         if (isdigit(c))
  88.             c -= '0';
  89.         else if (isalpha(c))
  90.             c -= isupper(c) ? 'A' - 10 : 'a' - 10;
  91.         else
  92.             break;
  93.         if (c >= base)
  94.             break;
  95.         if (any < 0 || acc > cutoff || acc == cutoff && c > cutlim)
  96.             any = -1;
  97.         else {
  98.             any = 1;
  99.             acc *= base;
  100.             acc += c;
  101.         }
  102.     }
  103.     if (any < 0) {
  104.         acc = neg ? LONG_MIN : LONG_MAX;
  105.         errno = ERANGE;
  106.     } else if (neg)
  107.         acc = -acc;
  108.     if (endptr != 0)
  109.         *endptr = any ? s - 1 : nptr;
  110.     return (acc);
  111. }