summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorAlejandro Colomar <alx@kernel.org>2024-01-16 21:29:59 +0100
committerSerge Hallyn <serge@hallyn.com>2024-03-29 14:29:13 -0500
commitb085c3f612e61d06d7c452953c7e097430ba1b3c (patch)
tree74d097f5854a0b4b2ecb0503335cd613aec71662
parent27e236ca79b6fc2a4bedc871258d0518aa5c30f5 (diff)
lib/atoi/: Add a2[su]l() and reimplement get[u]long() in terms of them
Signed-off-by: Alejandro Colomar <alx@kernel.org>
-rw-r--r--lib/Makefile.am2
-rw-r--r--lib/atoi/a2i.c13
-rw-r--r--lib/atoi/a2i.h56
-rw-r--r--lib/atoi/str2i.h33
4 files changed, 78 insertions, 26 deletions
diff --git a/lib/Makefile.am b/lib/Makefile.am
index 9676c529..22abb978 100644
--- a/lib/Makefile.am
+++ b/lib/Makefile.am
@@ -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 \
diff --git a/lib/atoi/a2i.c b/lib/atoi/a2i.c
new file mode 100644
index 00000000..1b9037c3
--- /dev/null
+++ b/lib/atoi/a2i.c
@@ -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);
diff --git a/lib/atoi/a2i.h b/lib/atoi/a2i.h
new file mode 100644
index 00000000..2b65f4b3
--- /dev/null
+++ b/lib/atoi/a2i.h
@@ -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
diff --git a/lib/atoi/str2i.h b/lib/atoi/str2i.h
index 6bf79fc4..b72c509f 100644
--- a/lib/atoi/str2i.h
+++ b/lib/atoi/str2i.h
@@ -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);
}