home *** CD-ROM | disk | FTP | other *** search
/ rtsi.com / 2014.01.www.rtsi.com.tar / www.rtsi.com / OS9 / OSK / CMDS / file.lzh / strtoul.c < prev   
C/C++ Source or Header  |  1995-04-27  |  3KB  |  169 lines

  1. #include <stdio.h>        /* For NULL */
  2.  
  3.  
  4. /* strtol.c */
  5.  
  6. #include <ctype.h>
  7. #include <errno.h>
  8.  
  9. /* #include <limits.h> */
  10.  
  11. #define UNSIGNED 1
  12.  
  13. #ifndef ULONG_MAX
  14. #define    LONG_MAX (~(1 << (sizeof (long) * 8 - 1)))
  15. #define LONG_MIN (-LONG_MAX-1)
  16. #define ULONG_MAX ((unsigned long) ~(unsigned long) 0)
  17. #endif
  18.  
  19. #ifndef NULL
  20. #define NULL 0
  21. #endif
  22.  
  23. extern int errno;
  24.  
  25. #if !defined(__STDC__)
  26. #define const
  27. #endif
  28.  
  29. #ifndef    UNSIGNED
  30. #define    UNSIGNED    0
  31. #endif
  32.  
  33. /* Convert NPTR to an `unsigned long int' or `long int' in base BASE.
  34.    If BASE is 0 the base is determined by the presence of a leading
  35.    zero, indicating octal or a leading "0x" or "0X", indicating hexadecimal.
  36.    If BASE is < 2 or > 36, it is reset to 10.
  37.    If ENDPTR is not NULL, a pointer to the character after the last
  38.    one converted is stored in *ENDPTR.  */
  39. #if UNSIGNED
  40. unsigned long
  41. #define    strtol    strtoul
  42. #else
  43. long int
  44. #endif
  45. strtol (nptr, endptr, base)
  46.      const char *nptr;
  47.      char **endptr;
  48.      int base;
  49. {
  50.   int negative;
  51.   register unsigned long int cutoff;
  52.   register unsigned int cutlim;
  53.   register unsigned long int i;
  54.   register const char *s;
  55.   register unsigned char c;
  56.   const char *save;
  57.   int overflow;
  58.  
  59.   if (base < 0 || base == 1 || base > 36)
  60.     base = 10;
  61.  
  62.   s = nptr;
  63.  
  64.   /* Skip white space.  */
  65.   while (isspace (*s))
  66.     ++s;
  67.   if (*s == '\0')
  68.     goto noconv;
  69.  
  70.   /* Check for a sign.  */
  71.   if (*s == '-')
  72.     {
  73.       negative = 1;
  74.       ++s;
  75.     }
  76.   else if (*s == '+')
  77.     {
  78.       negative = 0;
  79.       ++s;
  80.     }
  81.   else
  82.     negative = 0;
  83.  
  84.   if (base == 16 && s[0] == '0' && toupper (s[1]) == 'X')
  85.     s += 2;
  86.  
  87.   /* If BASE is zero, figure it out ourselves.  */
  88.   if (base == 0)
  89.     {
  90.       if (*s == '0')
  91.     {
  92.       if (toupper (s[1]) == 'X')
  93.         {
  94.           s += 2;
  95.           base = 16;
  96.         }
  97.       else
  98.         base = 8;
  99.     }
  100.       else
  101.     base = 10;
  102.     }
  103.  
  104.   /* Save the pointer so we can check later if anything happened.  */
  105.   save = s;
  106.  
  107.   cutoff = ULONG_MAX / (unsigned long int) base;
  108.   cutlim = ULONG_MAX % (unsigned long int) base;
  109.  
  110.   overflow = 0;
  111.   i = 0;
  112.   for (c = *s; c != '\0'; c = *++s)
  113.     {
  114.       if (isdigit (c))
  115.     c -= '0';
  116.       else if (isalpha (c))
  117.     c = toupper (c) - 'A' + 10;
  118.       else
  119.     break;
  120.       if (c >= base)
  121.     break;
  122.       /* Check for overflow.  */
  123.       if (i > cutoff || (i == cutoff && c > cutlim))
  124.     overflow = 1;
  125.       else
  126.     {
  127.       i *= (unsigned long int) base;
  128.       i += c;
  129.     }
  130.     }
  131.  
  132.   /* Check if anything actually happened.  */
  133.   if (s == save)
  134.     goto noconv;
  135.  
  136.   /* Store in ENDPTR the address of one character
  137.      past the last character we converted.  */
  138.   if (endptr != NULL)
  139.     *endptr = (char *) s;
  140.  
  141. #if    !UNSIGNED
  142.   /* Check for a value that is within the range of
  143.      `unsigned long int', but outside the range of `long int'.  */
  144.   if (i > (negative ?
  145.        - (unsigned long int) LONG_MIN : (unsigned long int) LONG_MAX))
  146.     overflow = 1;
  147. #endif
  148.  
  149.   if (overflow)
  150.     {
  151.       errno = ERANGE;
  152. #if    UNSIGNED
  153.       return ULONG_MAX;
  154. #else
  155.       return negative ? LONG_MIN : LONG_MAX;
  156. #endif
  157.     }
  158.  
  159.   /* Return the result of the appropriate sign.  */
  160.   return (negative ? - i : i);
  161.  
  162. noconv:;
  163.   /* There was no number to convert.  */
  164.   if (endptr != NULL)
  165.     *endptr = (char *) nptr;
  166.   return 0L;
  167. }
  168.  
  169.