shithub: scc

Download patch

ref: b44a95b8fbe1a524074dfcb96ee5efb89c9a1c9a
parent: cb31a80ef0c5eafd2a6e338397134c503b3f8b19
author: Christopher M. Graff <cm0graff@gmail.com>
date: Fri Feb 24 00:53:17 EST 2017

[libc] Add atoi

--- a/libc/src/Makefile
+++ b/libc/src/Makefile
@@ -8,7 +8,7 @@
           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
 
--- /dev/null
+++ b/libc/src/atoi.c
@@ -1,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;
+}
+