home *** CD-ROM | disk | FTP | other *** search
/ The CDPD Public Domain Collection for CDTV 3 / CDPDIII.bin / pd / programming / gnuc / stdlib / rcs / strtoul.c,v < prev    next >
Encoding:
Text File  |  1992-07-04  |  2.7 KB  |  118 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 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[] = "@@(#)strtoul.c    5.2 (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.  * Convert a string to an unsigned long integer.
  57.  *
  58.  * Ignores `locale' stuff.  Assumes that the upper and lower case
  59.  * alphabets and digits are each contiguous.
  60.  */
  61. unsigned long
  62. strtoul(const char *nptr, char **endptr, int base)
  63. {
  64.     register const char *s = nptr;
  65.     register unsigned long acc;
  66.     register int c;
  67.     register unsigned long cutoff;
  68.     register int neg = 0, any, cutlim;
  69.  
  70.     /*
  71.      * See strtol for comments as to the logic used.
  72.      */
  73.     do {
  74.         c = *s++;
  75.     } while (isspace(c));
  76.     if (c == '-') {
  77.         neg = 1;
  78.         c = *s++;
  79.     } else if (c == '+')
  80.         c = *s++;
  81.     if ((base == 0 || base == 16) &&
  82.         c == '0' && (*s == 'x' || *s == 'X')) {
  83.         c = s[1];
  84.         s += 2;
  85.         base = 16;
  86.     }
  87.     if (base == 0)
  88.         base = c == '0' ? 8 : 10;
  89.     cutoff = (unsigned long)ULONG_MAX / (unsigned long)base;
  90.     cutlim = (unsigned long)ULONG_MAX % (unsigned long)base;
  91.     for (acc = 0, any = 0;; c = *s++) {
  92.         if (isdigit(c))
  93.             c -= '0';
  94.         else if (isalpha(c))
  95.             c -= isupper(c) ? 'A' - 10 : 'a' - 10;
  96.         else
  97.             break;
  98.         if (c >= base)
  99.             break;
  100.         if (any < 0 || acc > cutoff || acc == cutoff && c > cutlim)
  101.             any = -1;
  102.         else {
  103.             any = 1;
  104.             acc *= base;
  105.             acc += c;
  106.         }
  107.     }
  108.     if (any < 0) {
  109.         acc = ULONG_MAX;
  110.         errno = ERANGE;
  111.     } else if (neg)
  112.         acc = -acc;
  113.     if (endptr != 0)
  114.         *endptr = any ? s - 1 : nptr;
  115.     return (acc);
  116. }
  117. @
  118.