ref: dcdd9662fcdefba9fd4384714a327cad99786b8e
parent: 3b342595ee8598daefeb5cdf52e59aa29ddad468
author: Roberto E. Vargas Caballero <k0ga@shike2.com>
date: Tue Nov 28 04:01:30 EST 2017
[lib/c] Add atol() and atoll() These functions could be integrated in only one, atoll, but I prefer to keep the 3 versions to make the code simpler and avoid the dependencies between different functions of the library.
--- a/lib/c/src/Makefile
+++ b/lib/c/src/Makefile
@@ -9,7 +9,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 atoi.o atexit.o exit.o \
+ localeconv.o atoi.o atol.o atoll.o atexit.o exit.o \
printf.o fprintf.o vfprintf.o \
realloc.o calloc.o malloc.o
--- a/lib/c/src/atoi.c
+++ b/lib/c/src/atoi.c
@@ -7,10 +7,10 @@
{
int n, sign = -1;
- while(isspace(*s))
+ while (isspace(*s))
++s;
- switch(*s) {
+ switch (*s) {
case '-':
sign = 1;
case '+':
--- /dev/null
+++ b/lib/c/src/atol.c
@@ -1,0 +1,27 @@
+#include <ctype.h>
+#include <stdlib.h>
+#undef atol
+
+long
+atol(const char *s)
+{
+ int sign = -1;
+ long n;
+
+ while (isspace(*s))
+ ++s;
+
+ switch (*s) {
+ case '-':
+ sign = 1;
+ case '+':
+ ++s;
+ }
+
+ /* Compute n as a negative number to avoid overflow on LONG_MIN */
+ for (n = 0; isdigit(*s); ++s)
+ n = 10*n - (*s - '0');
+
+ return sign * n;
+}
+
--- /dev/null
+++ b/lib/c/src/atoll.c
@@ -1,0 +1,27 @@
+#include <ctype.h>
+#include <stdlib.h>
+#undef atoll
+
+long long
+atoll(const char *s)
+{
+ int sign = -1;
+ long long n;
+
+ while (isspace(*s))
+ ++s;
+
+ switch (*s) {
+ case '-':
+ sign = 1;
+ case '+':
+ ++s;
+ }
+
+ /* Compute n as a negative number to avoid overflow on LLONG_MIN */
+ for (n = 0; isdigit(*s); ++s)
+ n = 10*n - (*s - '0');
+
+ return sign * n;
+}
+