home *** CD-ROM | disk | FTP | other *** search
/ Aminet 18 / aminetcdnumber181997.iso / Aminet / dev / gcc / ixemulsrc.lha / ixemul / stdlib / strtol.c < prev    next >
C/C++ Source or Header  |  1996-12-11  |  4KB  |  117 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. #define _KERNEL
  25. #include "ixemul.h"
  26. #include "kprintf.h"
  27.  
  28. #include <limits.h>
  29. #include <ctype.h>
  30. #include <stdlib.h>
  31.  
  32.  
  33. /*
  34.  * Convert a string to a long integer.
  35.  *
  36.  * Ignores `locale' stuff.  Assumes that the upper and lower case
  37.  * alphabets and digits are each contiguous.
  38.  */
  39. long
  40. strtol(const char *nptr, char **endptr, int base)
  41. {
  42.     register const char *s = nptr;
  43.     register unsigned long acc;
  44.     register int c;
  45.     register unsigned long cutoff;
  46.     register int neg = 0, any, cutlim;
  47.  
  48.     /*
  49.      * Skip white space and pick up leading +/- sign if any.
  50.      * If base is 0, allow 0x for hex and 0 for octal, else
  51.      * assume decimal; if base is already 16, allow 0x.
  52.      */
  53.     do {
  54.         c = *s++;
  55.     } while (isspace(c));
  56.     if (c == '-') {
  57.         neg = 1;
  58.         c = *s++;
  59.     } else if (c == '+')
  60.         c = *s++;
  61.     if ((base == 0 || base == 16) &&
  62.         c == '0' && (*s == 'x' || *s == 'X')) {
  63.         c = s[1];
  64.         s += 2;
  65.         base = 16;
  66.     }
  67.     if (base == 0)
  68.         base = c == '0' ? 8 : 10;
  69.  
  70.     /*
  71.      * Compute the cutoff value between legal numbers and illegal
  72.      * numbers.  That is the largest legal value, divided by the
  73.      * base.  An input number that is greater than this value, if
  74.      * followed by a legal input character, is too big.  One that
  75.      * is equal to this value may be valid or not; the limit
  76.      * between valid and invalid numbers is then based on the last
  77.      * digit.  For instance, if the range for longs is
  78.      * [-2147483648..2147483647] and the input base is 10,
  79.      * cutoff will be set to 214748364 and cutlim to either
  80.      * 7 (neg==0) or 8 (neg==1), meaning that if we have accumulated
  81.      * a value > 214748364, or equal but the next digit is > 7 (or 8),
  82.      * the number is too big, and we will return a range error.
  83.      *
  84.      * Set any if any `digits' consumed; make it negative to indicate
  85.      * overflow.
  86.      */
  87.     cutoff = neg ? -(unsigned long)LONG_MIN : LONG_MAX;
  88.     cutlim = cutoff % (unsigned long)base;
  89.     cutoff /= (unsigned long)base;
  90.     for (acc = 0, any = 0;; c = *s++) {
  91.         if (isdigit(c))
  92.             c -= '0';
  93.         else if (isalpha(c))
  94.             c -= isupper(c) ? 'A' - 10 : 'a' - 10;
  95.         else
  96.             break;
  97.         if (c >= base)
  98.             break;
  99.         if (any < 0 || acc > cutoff || (acc == cutoff && c > cutlim))
  100.             any = -1;
  101.         else {
  102.             any = 1;
  103.             acc *= base;
  104.             acc += c;
  105.         }
  106.     }
  107.     if (any < 0) {
  108.         acc = neg ? LONG_MIN : LONG_MAX;
  109.         errno = ERANGE;
  110.         KPRINTF (("&errno = %lx, errno = %ld\n", &errno, errno));
  111.     } else if (neg)
  112.         acc = -acc;
  113.     if (endptr != 0)
  114.         *endptr = (char *) (any ? s - 1 : nptr);
  115.     return (acc);
  116. }
  117.