commit e167a1b4e19b2911a3fd1b0e97538034b421e6b6 parent d9565cfbb3e18d78b683bae3891f50cf320c6fb0 Author: Christopher M. Graff <cm0graff@gmail.com> Date: Fri, 24 Feb 2017 05:53:17 -0600 [libc] Add atoi Diffstat:
M | libc/src/Makefile | | | 2 | +- |
A | libc/src/atoi.c | | | 28 | ++++++++++++++++++++++++++++ |
2 files changed, 29 insertions(+), 1 deletion(-)
diff --git a/libc/src/Makefile b/libc/src/Makefile @@ -8,7 +8,7 @@ LIBCOBJ = assert.o strcpy.o strcmp.o strlen.o strchr.o \ isalnum.o isalpha.o isascii.o isblank.o iscntrl.o isdigit.o \ isgraph.o islower.o isprint.o ispunct.o isspace.o isupper.o \ isxdigit.o toupper.o tolower.o ctype.o setlocale.o \ - localeconv.o + localeconv.o atoi.o all: libc.a diff --git a/libc/src/atoi.c b/libc/src/atoi.c @@ -0,0 +1,28 @@ +/* See LICENSE file for copyright and license details. */ + +#include <ctype.h> +#include <stdlib.h> + +int +atoi(const char *s) +{ + int n, sign = 1; + + while(isspace(*s)) + ++s; + + switch(*s) { + case '-': + sign = -1; + case '+': + ++s; + default: + break; + } + + for (n = 0; isdigit(*s); ++s) + n = 10 * n + (*s - '0'); + + return sign * n; +} +