lib/atoi/: Add a2[su]l() and reimplement get[u]long() in terms of them

Signed-off-by: Alejandro Colomar <alx@kernel.org>
This commit is contained in:
Alejandro Colomar
2024-01-16 21:29:59 +01:00
committed by Serge Hallyn
parent 27e236ca79
commit b085c3f612
4 changed files with 78 additions and 26 deletions
+2
View File
@@ -31,6 +31,8 @@ libshadow_la_SOURCES = \
agetpass.h \
alloc.c \
alloc.h \
atoi/a2i.c \
atoi/a2i.h \
atoi/str2i.c \
atoi/str2i.h \
atoi/strtoi.c \
+13
View File
@@ -0,0 +1,13 @@
// SPDX-FileCopyrightText: 2023-2024, Alejandro Colomar <alx@kernel.org>
// SPDX-License-Identifier: BSD-3-Clause
#include <config.h>
#include "atoi/a2i.h"
extern inline int a2sl(long *restrict n, const char *s,
char **restrict endp, int base, long min, long max);
extern inline int a2ul(unsigned long *restrict n, const char *s,
char **restrict endp, int base, unsigned long min, unsigned long max);
+56
View File
@@ -0,0 +1,56 @@
// SPDX-FileCopyrightText: 2023-2024, Alejandro Colomar <alx@kernel.org>
// SPDX-License-Identifier: BSD-3-Clause
#ifndef SHADOW_INCLUDE_LIB_ATOI_A2I_H_
#define SHADOW_INCLUDE_LIB_ATOI_A2I_H_
#include <config.h>
#include <errno.h>
#include "atoi/strtoi.h"
#include "atoi/strtou_noneg.h"
#include "attr.h"
ATTR_STRING(2) ATTR_ACCESS(write_only, 1) ATTR_ACCESS(write_only, 3)
inline int a2sl(long *restrict n, const char *s,
char **restrict endp, int base, long min, long max);
ATTR_STRING(2) ATTR_ACCESS(write_only, 1) ATTR_ACCESS(write_only, 3)
inline int a2ul(unsigned long *restrict n, const char *s,
char **restrict endp, int base, unsigned long min, unsigned long max);
inline int
a2sl(long *restrict n, const char *s, char **restrict endp,
int base, long min, long max)
{
int status;
*n = strtoi_(s, endp, base, min, max, &status);
if (status != 0) {
errno = status;
return -1;
}
return 0;
}
inline int
a2ul(unsigned long *restrict n, const char *s, char **restrict endp,
int base, unsigned long min, unsigned long max)
{
int status;
*n = strtou_noneg(s, endp, base, min, max, &status);
if (status != 0) {
errno = status;
return -1;
}
return 0;
}
#endif // include guard
+7 -26
View File
@@ -9,49 +9,30 @@
#include <config.h>
#include <stdlib.h>
#include <errno.h>
#include <limits.h>
#include <stddef.h>
#include "atoi/str2i.h"
#include "atoi/strtou_noneg.h"
#include "atoi/a2i.h"
#include "attr.h"
ATTR_ACCESS(write_only, 2)
ATTR_STRING(1) ATTR_ACCESS(write_only, 2)
inline int getlong(const char *restrict s, long *restrict n);
ATTR_ACCESS(write_only, 2)
ATTR_STRING(1) ATTR_ACCESS(write_only, 2)
inline int getulong(const char *restrict s, unsigned long *restrict n);
inline int
getlong(const char *restrict s, long *restrict n)
{
char *endp;
long val;
errno = 0;
val = strtol(s, &endp, 0);
if (('\0' == *s) || ('\0' != *endp) || (0 != errno))
return -1;
*n = val;
return 0;
return a2sl(n, s, NULL, 0, LONG_MIN, LONG_MAX);
}
inline int
getulong(const char *restrict s, unsigned long *restrict n)
{
char *endp;
unsigned long val;
errno = 0;
val = strtoul_noneg(s, &endp, 0);
if (('\0' == *s) || ('\0' != *endp) || (0 != errno))
return -1;
*n = val;
return 0;
return a2ul(n, s, NULL, 0, 0, ULONG_MAX);
}