Nut/OS  4.10.3
API Reference
strtol.c
Go to the documentation of this file.
00001 /*
00002  * Copyright (C) 2004 by egnite Software GmbH. All rights reserved.
00003  *
00004  * Redistribution and use in source and binary forms, with or without
00005  * modification, are permitted provided that the following conditions
00006  * are met:
00007  *
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. Neither the name of the copyright holders nor the names of
00014  *    contributors may be used to endorse or promote products derived
00015  *    from this software without specific prior written permission.
00016  *
00017  * THIS SOFTWARE IS PROVIDED BY EGNITE SOFTWARE GMBH AND CONTRIBUTORS
00018  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
00019  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
00020  * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL EGNITE
00021  * SOFTWARE GMBH OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
00022  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
00023  * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
00024  * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
00025  * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
00026  * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF
00027  * THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
00028  * SUCH DAMAGE.
00029  *
00030  * For additional information see http://www.ethernut.de/
00031  *
00032  *-
00033  * Copyright (c) 1990 The Regents of the University of California.
00034  * All rights reserved.
00035  *
00036  * Redistribution and use in source and binary forms, with or without
00037  * modification, are permitted provided that the following conditions
00038  * are met:
00039  * 1. Redistributions of source code must retain the above copyright
00040  *    notice, this list of conditions and the following disclaimer.
00041  * 2. Redistributions in binary form must reproduce the above copyright
00042  *    notice, this list of conditions and the following disclaimer in the
00043  *    documentation and/or other materials provided with the distribution.
00044  * 3. Neither the name of the University nor the names of its contributors
00045  *    may be used to endorse or promote products derived from this software
00046  *    without specific prior written permission.
00047  *
00048  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
00049  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
00050  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
00051  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
00052  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
00053  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
00054  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
00055  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
00056  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
00057  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
00058  * SUCH DAMAGE.
00059  */
00060 
00061 /*
00062  * $Log$
00063  * Revision 1.4  2008/02/15 17:13:01  haraldkipp
00064  * Use configurable constant attribute.
00065  *
00066  * Revision 1.3  2006/10/08 16:48:08  haraldkipp
00067  * Documentation fixed
00068  *
00069  * Revision 1.2  2005/08/02 17:46:47  haraldkipp
00070  * Major API documentation update.
00071  *
00072  * Revision 1.1  2004/09/08 10:23:35  haraldkipp
00073  * Generic C stdlib added
00074  *
00075  */
00076 
00077 #include <compiler.h>
00078 #include <ctype.h>
00079 #include <errno.h>
00080 #include <limits.h>
00081 #include <stdlib.h>
00082 
00087 
00099 long strtol(CONST char *nptr, char **endptr, int base)
00100 {
00101     register CONST char *s;
00102     register long acc, cutoff;
00103     register int c;
00104     register int neg, any, cutlim;
00105 
00106     /*
00107      * Skip white space and pick up leading +/- sign if any.
00108      * If base is 0, allow 0x for hex and 0 for octal, else
00109      * assume decimal; if base is already 16, allow 0x.
00110      */
00111     s = nptr;
00112     do {
00113         c = (unsigned char) *s++;
00114     } while (isspace(c));
00115     if (c == '-') {
00116         neg = 1;
00117         c = *s++;
00118     } else {
00119         neg = 0;
00120         if (c == '+')
00121             c = *s++;
00122     }
00123     if ((base == 0 || base == 16) && c == '0' && (*s == 'x' || *s == 'X')) {
00124         c = s[1];
00125         s += 2;
00126         base = 16;
00127     }
00128     if (base == 0)
00129         base = c == '0' ? 8 : 10;
00130 
00131     /*
00132      * Compute the cutoff value between legal numbers and illegal
00133      * numbers.  That is the largest legal value, divided by the
00134      * base.  An input number that is greater than this value, if
00135      * followed by a legal input character, is too big.  One that
00136      * is equal to this value may be valid or not; the limit
00137      * between valid and invalid numbers is then based on the last
00138      * digit.  For instance, if the range for longs is
00139      * [-2147483648..2147483647] and the input base is 10,
00140      * cutoff will be set to 214748364 and cutlim to either
00141      * 7 (neg==0) or 8 (neg==1), meaning that if we have accumulated
00142      * a value > 214748364, or equal but the next digit is > 7 (or 8),
00143      * the number is too big, and we will return a range error.
00144      *
00145      * Set any if any `digits' consumed; make it negative to indicate
00146      * overflow.
00147      */
00148     cutoff = neg ? LONG_MIN : LONG_MAX;
00149     cutlim = cutoff % base;
00150     cutoff /= base;
00151     if (neg) {
00152         if (cutlim > 0) {
00153             cutlim -= base;
00154             cutoff += 1;
00155         }
00156         cutlim = -cutlim;
00157     }
00158     for (acc = 0, any = 0;; c = (unsigned char) *s++) {
00159         if (isdigit(c))
00160             c -= '0';
00161         else if (isalpha(c))
00162             c -= isupper(c) ? 'A' - 10 : 'a' - 10;
00163         else
00164             break;
00165         if (c >= base)
00166             break;
00167         if (any < 0)
00168             continue;
00169         if (neg) {
00170             if ((acc < cutoff || acc == cutoff) && c > cutlim) {
00171                 any = -1;
00172                 acc = LONG_MIN;
00173                 errno = ERANGE;
00174             } else {
00175                 any = 1;
00176                 acc *= base;
00177                 acc -= c;
00178             }
00179         } else {
00180             if ((acc > cutoff || acc == cutoff) && c > cutlim) {
00181                 any = -1;
00182                 acc = LONG_MAX;
00183                 errno = ERANGE;
00184             } else {
00185                 any = 1;
00186                 acc *= base;
00187                 acc += c;
00188             }
00189         }
00190     }
00191     if (endptr != 0)
00192         *endptr = (char *) (any ? s - 1 : nptr);
00193     return (acc);
00194 }
00195