home *** CD-ROM | disk | FTP | other *** search
/ The CDPD Public Domain Collection for CDTV 3 / CDPDIII.bin / pd / programming / gnuc / stdlib / rcs / strtol.c,v < prev    next >
Encoding:
Text File  |  1992-07-04  |  3.6 KB  |  140 lines

  1. head    1.1;
  2. access;
  3. symbols
  4.     version39-41:1.1;
  5. locks;
  6. comment    @ * @;
  7.  
  8.  
  9. 1.1
  10. date    92.06.08.17.01.06;    author mwild;    state Exp;
  11. branches;
  12. next    ;
  13.  
  14.  
  15. desc
  16. @initial checkin
  17. @
  18.  
  19.  
  20. 1.1
  21. log
  22. @Initial revision
  23. @
  24. text
  25. @/*-
  26.  * Copyright (c) 1990 The Regents of the University of California.
  27.  * All rights reserved.
  28.  *
  29.  * Redistribution and use in source and binary forms are permitted
  30.  * provided that: (1) source distributions retain this entire copyright
  31.  * notice and comment, and (2) distributions including binaries display
  32.  * the following acknowledgement:  ``This product includes software
  33.  * developed by the University of California, Berkeley and its contributors''
  34.  * in the documentation or other materials provided with the distribution
  35.  * and in all advertising materials mentioning features or use of this
  36.  * software. Neither the name of the University nor the names of its
  37.  * contributors may be used to endorse or promote products derived
  38.  * from this software without specific prior written permission.
  39.  * THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR
  40.  * IMPLIED WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED
  41.  * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
  42.  */
  43.  
  44. #if defined(LIBC_SCCS) && !defined(lint)
  45. static char sccsid[] = "@@(#)strtol.c    5.3 (Berkeley) 5/17/90";
  46. #endif /* LIBC_SCCS and not lint */
  47.  
  48. #define KERNEL
  49. #include "ixemul.h"
  50.  
  51. #include <limits.h>
  52. #include <ctype.h>
  53. #include <stdlib.h>
  54.  
  55.  
  56. /*
  57.  * Convert a string to a long integer.
  58.  *
  59.  * Ignores `locale' stuff.  Assumes that the upper and lower case
  60.  * alphabets and digits are each contiguous.
  61.  */
  62. long
  63. strtol(const char *nptr, char **endptr, int base)
  64. {
  65.     register char *s = nptr;
  66.     register unsigned long acc;
  67.     register int c;
  68.     register unsigned long cutoff;
  69.     register int neg = 0, any, cutlim;
  70.  
  71.     /*
  72.      * Skip white space and pick up leading +/- sign if any.
  73.      * If base is 0, allow 0x for hex and 0 for octal, else
  74.      * assume decimal; if base is already 16, allow 0x.
  75.      */
  76.     do {
  77.         c = *s++;
  78.     } while (isspace(c));
  79.     if (c == '-') {
  80.         neg = 1;
  81.         c = *s++;
  82.     } else if (c == '+')
  83.         c = *s++;
  84.     if ((base == 0 || base == 16) &&
  85.         c == '0' && (*s == 'x' || *s == 'X')) {
  86.         c = s[1];
  87.         s += 2;
  88.         base = 16;
  89.     }
  90.     if (base == 0)
  91.         base = c == '0' ? 8 : 10;
  92.  
  93.     /*
  94.      * Compute the cutoff value between legal numbers and illegal
  95.      * numbers.  That is the largest legal value, divided by the
  96.      * base.  An input number that is greater than this value, if
  97.      * followed by a legal input character, is too big.  One that
  98.      * is equal to this value may be valid or not; the limit
  99.      * between valid and invalid numbers is then based on the last
  100.      * digit.  For instance, if the range for longs is
  101.      * [-2147483648..2147483647] and the input base is 10,
  102.      * cutoff will be set to 214748364 and cutlim to either
  103.      * 7 (neg==0) or 8 (neg==1), meaning that if we have accumulated
  104.      * a value > 214748364, or equal but the next digit is > 7 (or 8),
  105.      * the number is too big, and we will return a range error.
  106.      *
  107.      * Set any if any `digits' consumed; make it negative to indicate
  108.      * overflow.
  109.      */
  110.     cutoff = neg ? -(unsigned long)LONG_MIN : LONG_MAX;
  111.     cutlim = cutoff % (unsigned long)base;
  112.     cutoff /= (unsigned long)base;
  113.     for (acc = 0, any = 0;; c = *s++) {
  114.         if (isdigit(c))
  115.             c -= '0';
  116.         else if (isalpha(c))
  117.             c -= isupper(c) ? 'A' - 10 : 'a' - 10;
  118.         else
  119.             break;
  120.         if (c >= base)
  121.             break;
  122.         if (any < 0 || acc > cutoff || acc == cutoff && c > cutlim)
  123.             any = -1;
  124.         else {
  125.             any = 1;
  126.             acc *= base;
  127.             acc += c;
  128.         }
  129.     }
  130.     if (any < 0) {
  131.         acc = neg ? LONG_MIN : LONG_MAX;
  132.         errno = ERANGE;
  133.     } else if (neg)
  134.         acc = -acc;
  135.     if (endptr != 0)
  136.         *endptr = any ? s - 1 : nptr;
  137.     return (acc);
  138. }
  139. @
  140.