]> git.ipfire.org Git - thirdparty/shadow.git/commitdiff
lib/atoi/: a2*(), str2*(): Add variants for other types
authorAlejandro Colomar <alx@kernel.org>
Tue, 16 Jan 2024 20:48:22 +0000 (21:48 +0100)
committerSerge Hallyn <serge@hallyn.com>
Fri, 29 Mar 2024 19:29:13 +0000 (14:29 -0500)
And type-generic macros that wrap them: a2i(), str2i()

Signed-off-by: Alejandro Colomar <alx@kernel.org>
lib/atoi/a2i.c
lib/atoi/a2i.h
lib/atoi/str2i.c
lib/atoi/str2i.h

index 1b9037c3bd07489a4c1616c36ab53e660bb334f1..6dca7e89a29997468f5e5e229b7c230e0f21312c 100644 (file)
@@ -7,7 +7,20 @@
 #include "atoi/a2i.h"
 
 
+extern inline int a2sh(short *restrict n, const char *s,
+    char **restrict endp, int base, short min, short max);
+extern inline int a2si(int *restrict n, const char *s,
+    char **restrict endp, int base, int min, int max);
 extern inline int a2sl(long *restrict n, const char *s,
     char **restrict endp, int base, long min, long max);
+extern inline int a2sll(long long *restrict n, const char *s,
+    char **restrict endp, int base, long long min, long long max);
+extern inline int a2uh(unsigned short *restrict n, const char *s,
+    char **restrict endp, int base, unsigned short min, unsigned short max);
+extern inline int a2ui(unsigned int *restrict n, const char *s,
+    char **restrict endp, int base, unsigned int min, unsigned int max);
 extern inline int a2ul(unsigned long *restrict n, const char *s,
     char **restrict endp, int base, unsigned long min, unsigned long max);
+extern inline int a2ull(unsigned long long *restrict n, const char *s,
+    char **restrict endp, int base, unsigned long long min,
+    unsigned long long max);
index 2b65f4b3ea65bbbfb54f2a9113c9128457bb0ca7..3935f42d132935067a526e41757c7c0edc4a4c8c 100644 (file)
 #include "attr.h"
 
 
+#define a2i(TYPE, ...)                                                        \
+(                                                                             \
+       _Generic((TYPE) 0,                                                    \
+               short:              a2sh,                                     \
+               int:                a2si,                                     \
+               long:               a2sl,                                     \
+               long long:          a2sll,                                    \
+               unsigned short:     a2uh,                                     \
+               unsigned int:       a2ui,                                     \
+               unsigned long:      a2ul,                                     \
+               unsigned long long: a2ull                                     \
+       )(__VA_ARGS__)                                                        \
+)
+
+
+ATTR_STRING(2) ATTR_ACCESS(write_only, 1) ATTR_ACCESS(write_only, 3)
+inline int a2sh(short *restrict n, const char *s,
+    char **restrict endp, int base, short min, short max);
+ATTR_STRING(2) ATTR_ACCESS(write_only, 1) ATTR_ACCESS(write_only, 3)
+inline int a2si(int *restrict n, const char *s,
+    char **restrict endp, int base, int min, int max);
 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 a2sll(long long *restrict n, const char *s,
+    char **restrict endp, int base, long long min, long long max);
+ATTR_STRING(2) ATTR_ACCESS(write_only, 1) ATTR_ACCESS(write_only, 3)
+inline int a2uh(unsigned short *restrict n, const char *s,
+    char **restrict endp, int base, unsigned short min, unsigned short max);
+ATTR_STRING(2) ATTR_ACCESS(write_only, 1) ATTR_ACCESS(write_only, 3)
+inline int a2ui(unsigned int *restrict n, const char *s,
+    char **restrict endp, int base, unsigned int min, unsigned int 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);
+ATTR_STRING(2) ATTR_ACCESS(write_only, 1) ATTR_ACCESS(write_only, 3)
+inline int a2ull(unsigned long long *restrict n, const char *s,
+    char **restrict endp, int base, unsigned long long min,
+    unsigned long long max);
+
+
+inline int
+a2sh(short *restrict n, const char *s, char **restrict endp,
+    int base, short min, short max)
+{
+       int  status;
+
+       *n = strtoi_(s, endp, base, min, max, &status);
+       if (status != 0) {
+               errno = status;
+               return -1;
+       }
+       return 0;
+}
+
+
+inline int
+a2si(int *restrict n, const char *s, char **restrict endp,
+    int base, int min, int max)
+{
+       int  status;
+
+       *n = strtoi_(s, endp, base, min, max, &status);
+       if (status != 0) {
+               errno = status;
+               return -1;
+       }
+       return 0;
+}
 
 
 inline int
@@ -38,6 +102,51 @@ a2sl(long *restrict n, const char *s, char **restrict endp,
 }
 
 
+inline int
+a2sll(long long *restrict n, const char *s, char **restrict endp,
+    int base, long long min, long long max)
+{
+       int  status;
+
+       *n = strtoi_(s, endp, base, min, max, &status);
+       if (status != 0) {
+               errno = status;
+               return -1;
+       }
+       return 0;
+}
+
+
+inline int
+a2uh(unsigned short *restrict n, const char *s, char **restrict endp,
+    int base, unsigned short min, unsigned short max)
+{
+       int  status;
+
+       *n = strtou_noneg(s, endp, base, min, max, &status);
+       if (status != 0) {
+               errno = status;
+               return -1;
+       }
+       return 0;
+}
+
+
+inline int
+a2ui(unsigned int *restrict n, const char *s, char **restrict endp,
+    int base, unsigned int min, unsigned int max)
+{
+       int  status;
+
+       *n = strtou_noneg(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)
@@ -53,4 +162,19 @@ a2ul(unsigned long *restrict n, const char *s, char **restrict endp,
 }
 
 
+inline int
+a2ull(unsigned long long *restrict n, const char *s, char **restrict endp,
+    int base, unsigned long long min, unsigned long 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 127ac82798d81615aa3268deda5f82f9a1ed0cea..25ce3609b052cd0773b4e0d3bffd821a2ed64427 100644 (file)
@@ -8,5 +8,11 @@
 #include "atoi/str2i.h"
 
 
+extern inline int str2sh(short *restrict n, const char *restrict s);
+extern inline int str2si(int *restrict n, const char *restrict s);
 extern inline int str2sl(long *restrict n, const char *restrict s);
+extern inline int str2sll(long long *restrict n, const char *restrict s);
+extern inline int str2uh(unsigned short *restrict n, const char *restrict s);
+extern inline int str2ui(unsigned int *restrict n, const char *restrict s);
 extern inline int str2ul(unsigned long *restrict n, const char *restrict s);
+extern inline int str2ull(unsigned long long *restrict n, const char *restrict s);
index ee3cece17af9ecbef5775b1f82ab55cbc40febcd..b3ded031005436c35e72f99d6c27650c8dd552d3 100644 (file)
 #include "attr.h"
 
 
+#define str2i(TYPE, ...)                                                      \
+(                                                                             \
+       _Generic((TYPE) 0,                                                    \
+               short:              str2sh,                                   \
+               int:                str2si,                                   \
+               long:               str2sl,                                   \
+               long long:          str2sll,                                  \
+               unsigned short:     str2uh,                                   \
+               unsigned int:       str2ui,                                   \
+               unsigned long:      str2ul,                                   \
+               unsigned long long: str2ull                                   \
+       )(__VA_ARGS__)                                                        \
+)
+
+
+ATTR_STRING(2) ATTR_ACCESS(write_only, 1)
+inline int str2sh(short *restrict n, const char *restrict s);
+ATTR_STRING(2) ATTR_ACCESS(write_only, 1)
+inline int str2si(int *restrict n, const char *restrict s);
 ATTR_STRING(2) ATTR_ACCESS(write_only, 1)
 inline int str2sl(long *restrict n, const char *restrict s);
 ATTR_STRING(2) ATTR_ACCESS(write_only, 1)
+inline int str2sll(long long *restrict n, const char *restrict s);
+ATTR_STRING(2) ATTR_ACCESS(write_only, 1)
+inline int str2uh(unsigned short *restrict n, const char *restrict s);
+ATTR_STRING(2) ATTR_ACCESS(write_only, 1)
+inline int str2ui(unsigned int *restrict n, const char *restrict s);
+ATTR_STRING(2) ATTR_ACCESS(write_only, 1)
 inline int str2ul(unsigned long *restrict n, const char *restrict s);
+ATTR_STRING(2) ATTR_ACCESS(write_only, 1)
+inline int str2ull(unsigned long long *restrict n, const char *restrict s);
+
+
+inline int
+str2sh(short *restrict n, const char *restrict s)
+{
+       return a2sh(n, s, NULL, 0, SHRT_MIN, SHRT_MAX);
+}
+
+
+inline int
+str2si(int *restrict n, const char *restrict s)
+{
+       return a2si(n, s, NULL, 0, INT_MIN, INT_MAX);
+}
 
 
 inline int
@@ -29,6 +70,27 @@ str2sl(long *restrict n, const char *restrict s)
 }
 
 
+inline int
+str2sll(long long *restrict n, const char *restrict s)
+{
+       return a2sll(n, s, NULL, 0, LLONG_MIN, LLONG_MAX);
+}
+
+
+inline int
+str2uh(unsigned short *restrict n, const char *restrict s)
+{
+       return a2uh(n, s, NULL, 0, 0, USHRT_MAX);
+}
+
+
+inline int
+str2ui(unsigned int *restrict n, const char *restrict s)
+{
+       return a2ui(n, s, NULL, 0, 0, UINT_MAX);
+}
+
+
 inline int
 str2ul(unsigned long *restrict n, const char *restrict s)
 {
@@ -36,4 +98,11 @@ str2ul(unsigned long *restrict n, const char *restrict s)
 }
 
 
+inline int
+str2ull(unsigned long long *restrict n, const char *restrict s)
+{
+       return a2ull(n, s, NULL, 0, 0, ULLONG_MAX);
+}
+
+
 #endif  // include guard