Initial xloong code
[xloong.git] / lib / libc / atoi.c
blobbf5d5039cf8d17fd1294d77f091eac5ec37dfd53
1 /* $Id: atoi.c,v 1.1.1.1 2006/09/14 01:59:06 root Exp $ */
3 /*
4 * Copyright (c) 2000-2002 Opsycon AB (www.opsycon.se)
5 *
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions
8 * are met:
9 * 1. Redistributions of source code must retain the above copyright
10 * notice, this list of conditions and the following disclaimer.
11 * 2. Redistributions in binary form must reproduce the above copyright
12 * notice, this list of conditions and the following disclaimer in the
13 * documentation and/or other materials provided with the distribution.
14 * 3. All advertising materials mentioning features or use of this software
15 * must display the following acknowledgement:
16 * This product includes software developed by Opsycon AB.
17 * 4. The name of the author may not be used to endorse or promote products
18 * derived from this software without specific prior written permission.
20 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS
21 * OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
22 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
23 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY
24 * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
25 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
26 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
27 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
28 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
29 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
30 * SUCH DAMAGE.
34 #include <string.h>
35 #include <stdlib.h>
36 #include <ctype.h>
38 /** atoi(p) converts p to int */
40 int32_t
41 atoi(const char *p)
43 int32_t digit, isneg;
44 int32_t value;
46 isneg = 0;
47 value = 0;
48 for (; isspace (*p); p++); /* gobble up leading whitespace */
50 /* do I have a sign? */
51 if (*p == '-') {
52 isneg = 1;
53 p++;
55 else if (*p == '+')
56 p++;
58 for (; *p; p++) {
59 if (*p >= '0' && *p <= '9')
60 digit = *p - '0';
61 else
62 break;
63 value *= 10;
64 value += digit;
67 if (isneg)
68 value = 0 - value;
69 return (value);