[hackers] [scc] [libc] Add atoi || Christopher M. Graff

From: <git_AT_suckless.org>
Date: Fri, 24 Feb 2017 16:08:48 +0100 (CET)

commit e167a1b4e19b2911a3fd1b0e97538034b421e6b6
Author: Christopher M. Graff <cm0graff_AT_gmail.com>
AuthorDate: Fri Feb 24 05:53:17 2017 -0600
Commit: Quentin Rameau <quinq_AT_fifth.space>
CommitDate: Fri Feb 24 16:07:40 2017 +0100

    [libc] Add atoi

diff --git a/libc/src/Makefile b/libc/src/Makefile
index f83e4d2..c80618f 100644
--- a/libc/src/Makefile
+++ b/libc/src/Makefile
_AT_@ -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
new file mode 100644
index 0000000..6619cf1
--- /dev/null
+++ b/libc/src/atoi.c
_AT_@ -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;
+}
+
Received on Fri Feb 24 2017 - 16:08:48 CET

This archive was generated by hypermail 2.3.0 : Fri Feb 24 2017 - 16:12:18 CET