string: Add atol

Signed-off-by: Hector Martin <marcan@marcan.st>
This commit is contained in:
Hector Martin 2022-03-27 17:23:34 +09:00
parent ebf1cf3aa3
commit 550e39913f
2 changed files with 23 additions and 0 deletions

View file

@ -1,5 +1,7 @@
/* SPDX-License-Identifier: MIT */ /* SPDX-License-Identifier: MIT */
#include <stdbool.h>
#include "string.h" #include "string.h"
// Routines based on The Public Domain C Library // Routines based on The Public Domain C Library
@ -185,3 +187,23 @@ char *strrchr(const char *s, int c)
return NULL; return NULL;
} }
/* Very naive, no attempt to check for errors */
long atol(const char *s)
{
long val = 0;
bool neg = false;
if (*s == '-') {
neg = true;
s++;
}
while (*s >= '0' && *s <= '9')
val = (val * 10) + (*s++ - '0');
if (neg)
val = -val;
return val;
}

View file

@ -18,6 +18,7 @@ size_t strlen(const char *s);
size_t strnlen(const char *s, size_t n); size_t strnlen(const char *s, size_t n);
char *strchr(const char *s, int c); char *strchr(const char *s, int c);
char *strrchr(const char *s, int c); char *strrchr(const char *s, int c);
long atol(const char *s);
static inline int tolower(int c) static inline int tolower(int c)
{ {