]> git.ipfire.org Git - thirdparty/shadow.git/commitdiff
lib/atoi/: Add a2[su]l() and reimplement get[u]long() in terms of them
authorAlejandro Colomar <alx@kernel.org>
Tue, 16 Jan 2024 20:29:59 +0000 (21:29 +0100)
committerSerge Hallyn <serge@hallyn.com>
Fri, 29 Mar 2024 19:29:13 +0000 (14:29 -0500)
Signed-off-by: Alejandro Colomar <alx@kernel.org>
lib/Makefile.am
lib/atoi/a2i.c [new file with mode: 0644]
lib/atoi/a2i.h [new file with mode: 0644]
lib/atoi/str2i.h

index 9676c5297c2496f1a9b6f7e36660de95dfe502d1..22abb97840445c4b990856ba9eaba9e34d444026 100644 (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 \
diff --git a/lib/atoi/a2i.c b/lib/atoi/a2i.c
new file mode 100644 (file)
index 0000000..1b9037c
--- /dev/null
@@ -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 (file)
index 0000000..2b65f4b
--- /dev/null
@@ -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
index 6bf79fc481fd732b98e9af86643094e8e944253d..b72c509ff66e26dd62ddac7279765a01977ca030 100644 (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);
 }