home *** CD-ROM | disk | FTP | other *** search
/ Aminet 18 / aminetcdnumber181997.iso / Aminet / dev / gcc / ixemulsrc.lha / ixemul / stdlib / strtoul.c < prev    next >
C/C++ Source or Header  |  1996-12-11  |  3KB  |  95 lines

  1. /*
  2.  * Copyright (c) 1990 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[] = "@(#)strtoul.c    5.2 (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.  * Convert a string to an unsigned long integer.
  34.  *
  35.  * Ignores `locale' stuff.  Assumes that the upper and lower case
  36.  * alphabets and digits are each contiguous.
  37.  */
  38. unsigned long
  39. strtoul(const char *nptr, char **endptr, int base)
  40. {
  41.     register const char *s = nptr;
  42.     register unsigned long acc;
  43.     register int c;
  44.     register unsigned long cutoff;
  45.     register int neg = 0, any, cutlim;
  46.  
  47.     /*
  48.      * See strtol for comments as to the logic used.
  49.      */
  50.     do {
  51.         c = *s++;
  52.     } while (isspace(c));
  53.     if (c == '-') {
  54.         neg = 1;
  55.         c = *s++;
  56.     } else if (c == '+')
  57.         c = *s++;
  58.     if ((base == 0 || base == 16) &&
  59.         c == '0' && (*s == 'x' || *s == 'X')) {
  60.         c = s[1];
  61.         s += 2;
  62.         base = 16;
  63.     }
  64.     if (base == 0)
  65.         base = c == '0' ? 8 : 10;
  66.     cutoff = (unsigned long)ULONG_MAX / (unsigned long)base;
  67.     cutlim = (unsigned long)ULONG_MAX % (unsigned long)base;
  68.     for (acc = 0, any = 0;; c = *s++) {
  69.         if (isdigit(c))
  70.             c -= '0';
  71.         else if (isalpha(c))
  72.             c -= isupper(c) ? 'A' - 10 : 'a' - 10;
  73.         else
  74.             break;
  75.         if (c >= base)
  76.             break;
  77.         if (any < 0 || acc > cutoff || (acc == cutoff && c > cutlim))
  78.             any = -1;
  79.         else {
  80.             any = 1;
  81.             acc *= base;
  82.             acc += c;
  83.         }
  84.     }
  85.     if (any < 0) {
  86.         acc = ULONG_MAX;
  87.         errno = ERANGE;
  88.         KPRINTF (("&errno = %lx, errno = %ld\n", &errno, errno));
  89.     } else if (neg)
  90.         acc = -acc;
  91.     if (endptr != 0)
  92.         *endptr = (char *) (any ? s - 1 : nptr);
  93.     return (acc);
  94. }
  95.