Back to index

cell-binutils  2.17cvs20070401
strtoul.c
Go to the documentation of this file.
00001 /*
00002  * Copyright (c) 1990 Regents of the University of California.
00003  * All rights reserved.
00004  *
00005  * Redistribution and use in source and binary forms, with or without
00006  * modification, are permitted provided that the following conditions
00007  * are met:
00008  * 1. Redistributions of source code must retain the above copyright
00009  *    notice, this list of conditions and the following disclaimer.
00010  * 2. Redistributions in binary form must reproduce the above copyright
00011  *    notice, this list of conditions and the following disclaimer in the
00012  *    documentation and/or other materials provided with the distribution.
00013  * 3. [rescinded 22 July 1999]
00014  * 4. Neither the name of the University nor the names of its contributors
00015  *    may be used to endorse or promote products derived from this software
00016  *    without specific prior written permission.
00017  *
00018  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
00019  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
00020  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
00021  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
00022  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
00023  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
00024  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
00025  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
00026  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
00027  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
00028  * SUCH DAMAGE.
00029  */
00030 
00031 #ifdef HAVE_CONFIG_H
00032 #include "config.h"
00033 #endif
00034 #ifdef HAVE_LIMITS_H
00035 #include <limits.h>
00036 #endif
00037 #ifdef HAVE_SYS_PARAM_H
00038 #include <sys/param.h>
00039 #endif
00040 #include <errno.h>
00041 #ifdef NEED_DECLARATION_ERRNO
00042 extern int errno;
00043 #endif
00044 #if 0
00045 #include <stdlib.h>
00046 #endif
00047 #include "ansidecl.h"
00048 #include "safe-ctype.h"
00049 
00050 #ifndef ULONG_MAX
00051 #define       ULONG_MAX     ((unsigned long)(~0L))             /* 0xFFFFFFFF */
00052 #endif
00053 
00054 /*
00055  * Convert a string to an unsigned long integer.
00056  *
00057  * Ignores `locale' stuff.  Assumes that the upper and lower case
00058  * alphabets and digits are each contiguous.
00059  */
00060 unsigned long
00061 strtoul(const char *nptr, char **endptr, register int base)
00062 {
00063        register const char *s = nptr;
00064        register unsigned long acc;
00065        register int c;
00066        register unsigned long cutoff;
00067        register int neg = 0, any, cutlim;
00068 
00069        /*
00070         * See strtol for comments as to the logic used.
00071         */
00072        do {
00073               c = *s++;
00074        } while (ISSPACE(c));
00075        if (c == '-') {
00076               neg = 1;
00077               c = *s++;
00078        } else if (c == '+')
00079               c = *s++;
00080        if ((base == 0 || base == 16) &&
00081            c == '0' && (*s == 'x' || *s == 'X')) {
00082               c = s[1];
00083               s += 2;
00084               base = 16;
00085        }
00086        if (base == 0)
00087               base = c == '0' ? 8 : 10;
00088        cutoff = (unsigned long)ULONG_MAX / (unsigned long)base;
00089        cutlim = (unsigned long)ULONG_MAX % (unsigned long)base;
00090        for (acc = 0, any = 0;; c = *s++) {
00091               if (ISDIGIT(c))
00092                      c -= '0';
00093               else if (ISALPHA(c))
00094                      c -= ISUPPER(c) ? 'A' - 10 : 'a' - 10;
00095               else
00096                      break;
00097               if (c >= base)
00098                      break;
00099               if (any < 0 || acc > cutoff || (acc == cutoff && c > cutlim))
00100                      any = -1;
00101               else {
00102                      any = 1;
00103                      acc *= base;
00104                      acc += c;
00105               }
00106        }
00107        if (any < 0) {
00108               acc = ULONG_MAX;
00109               errno = ERANGE;
00110        } else if (neg)
00111               acc = -acc;
00112        if (endptr != 0)
00113               *endptr = (char *) (any ? s - 1 : nptr);
00114        return (acc);
00115 }