scc

simple C compiler
git clone git://git.2f30.org/scc
Log | Files | Refs | README | LICENSE

commit 2fbb1d663cc2194fd23937459c5b598f2fab65ac
parent d21dc62bd4af1887ec743aa6f16550b2a474ad7f
Author: Roberto E. Vargas Caballero <k0ga@shike2.com>
Date:   Tue, 28 Nov 2017 09:01:30 +0000

[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.

Diffstat:
lib/c/src/Makefile | 2+-
lib/c/src/atoi.c | 4++--
lib/c/src/atol.c | 27+++++++++++++++++++++++++++
lib/c/src/atoll.c | 27+++++++++++++++++++++++++++
4 files changed, 57 insertions(+), 3 deletions(-)

diff --git a/lib/c/src/Makefile b/lib/c/src/Makefile @@ -9,7 +9,7 @@ OBJ = 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 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 diff --git a/lib/c/src/atoi.c b/lib/c/src/atoi.c @@ -7,10 +7,10 @@ atoi(const char *s) { int n, sign = -1; - while(isspace(*s)) + while (isspace(*s)) ++s; - switch(*s) { + switch (*s) { case '-': sign = 1; case '+': diff --git a/lib/c/src/atol.c b/lib/c/src/atol.c @@ -0,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; +} + diff --git a/lib/c/src/atoll.c b/lib/c/src/atoll.c @@ -0,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; +} +