From: Alejandro Colomar Date: Sun, 30 Jun 2024 00:18:55 +0000 (+0200) Subject: lib/atoi/, *: Split files X-Git-Tag: 4.17.0-rc1~113 X-Git-Url: http://git.ipfire.org/?a=commitdiff_plain;h=745281f295cc3fde16455ffaf355e8fcc4375be2;p=thirdparty%2Fshadow.git lib/atoi/, *: Split files --- diff --git a/lib/Makefile.am b/lib/Makefile.am index 184b3d453..4b02764d3 100644 --- a/lib/Makefile.am +++ b/lib/Makefile.am @@ -43,16 +43,34 @@ libshadow_la_SOURCES = \ alloc/x/xmalloc.h \ alloc/x/xrealloc.c \ alloc/x/xrealloc.h \ - atoi/a2i.c \ - atoi/a2i.h \ + atoi/a2i/a2i.c \ + atoi/a2i/a2i.h \ + atoi/a2i/a2s.c \ + atoi/a2i/a2s.h \ + atoi/a2i/a2s_c.c \ + atoi/a2i/a2s_c.h \ + atoi/a2i/a2s_nc.c \ + atoi/a2i/a2s_nc.h \ + atoi/a2i/a2u.c \ + atoi/a2i/a2u.h \ + atoi/a2i/a2u_c.c \ + atoi/a2i/a2u_c.h \ + atoi/a2i/a2u_nc.c \ + atoi/a2i/a2u_nc.h \ atoi/getnum.c \ atoi/getnum.h \ - atoi/str2i.c \ - atoi/str2i.h \ - atoi/strtoi.c \ - atoi/strtoi.h \ - atoi/strtou_noneg.c \ - atoi/strtou_noneg.h \ + atoi/str2i/str2i.c \ + atoi/str2i/str2i.h \ + atoi/str2i/str2s.c \ + atoi/str2i/str2s.h \ + atoi/str2i/str2u.c \ + atoi/str2i/str2u.h \ + atoi/strtoi/strtoi.c \ + atoi/strtoi/strtoi.h \ + atoi/strtoi/strtou.c \ + atoi/strtoi/strtou.h \ + atoi/strtoi/strtou_noneg.c \ + atoi/strtoi/strtou_noneg.h \ attr.h \ audit_help.c \ basename.c \ diff --git a/lib/atoi/a2i.c b/lib/atoi/a2i.c deleted file mode 100644 index a2cf8723d..000000000 --- a/lib/atoi/a2i.c +++ /dev/null @@ -1,46 +0,0 @@ -// SPDX-FileCopyrightText: 2023-2024, Alejandro Colomar -// SPDX-License-Identifier: BSD-3-Clause - - -#include - -#include "atoi/a2i.h" - - -extern inline int a2sh_c(short *restrict n, const char *s, - const char **restrict endp, int base, short min, short max); -extern inline int a2si_c(int *restrict n, const char *s, - const char **restrict endp, int base, int min, int max); -extern inline int a2sl_c(long *restrict n, const char *s, - const char **restrict endp, int base, long min, long max); -extern inline int a2sll_c(long long *restrict n, const char *s, - const char **restrict endp, int base, long long min, long long max); -extern inline int a2uh_c(unsigned short *restrict n, const char *s, - const char **restrict endp, int base, unsigned short min, - unsigned short max); -extern inline int a2ui_c(unsigned int *restrict n, const char *s, - const char **restrict endp, int base, unsigned int min, unsigned int max); -extern inline int a2ul_c(unsigned long *restrict n, const char *s, - const char **restrict endp, int base, unsigned long min, unsigned long max); -extern inline int a2ull_c(unsigned long long *restrict n, const char *s, - const char **restrict endp, int base, unsigned long long min, - unsigned long long max); - - -extern inline int a2sh_nc(short *restrict n, char *s, - char **restrict endp, int base, short min, short max); -extern inline int a2si_nc(int *restrict n, char *s, - char **restrict endp, int base, int min, int max); -extern inline int a2sl_nc(long *restrict n, char *s, - char **restrict endp, int base, long min, long max); -extern inline int a2sll_nc(long long *restrict n, char *s, - char **restrict endp, int base, long long min, long long max); -extern inline int a2uh_nc(unsigned short *restrict n, char *s, - char **restrict endp, int base, unsigned short min, unsigned short max); -extern inline int a2ui_nc(unsigned int *restrict n, char *s, - char **restrict endp, int base, unsigned int min, unsigned int max); -extern inline int a2ul_nc(unsigned long *restrict n, char *s, - char **restrict endp, int base, unsigned long min, unsigned long max); -extern inline int a2ull_nc(unsigned long long *restrict n, char *s, - char **restrict endp, int base, unsigned long long min, - unsigned long long max); diff --git a/lib/atoi/a2i.h b/lib/atoi/a2i.h deleted file mode 100644 index 64f775a94..000000000 --- a/lib/atoi/a2i.h +++ /dev/null @@ -1,386 +0,0 @@ -// SPDX-FileCopyrightText: 2023-2024, Alejandro Colomar -// SPDX-License-Identifier: BSD-3-Clause - - -#ifndef SHADOW_INCLUDE_LIB_ATOI_A2I_H_ -#define SHADOW_INCLUDE_LIB_ATOI_A2I_H_ - - -#include - -#include - -#include "atoi/strtoi.h" -#include "atoi/strtou_noneg.h" -#include "attr.h" - - -/* - * See the manual of these macros in liba2i's documentation: - * - */ - - -#define a2i(TYPE, n, s, ...) \ -( \ - _Generic((void (*)(TYPE, typeof(s))) 0, \ - void (*)(short, const char *): a2sh_c, \ - void (*)(short, const void *): a2sh_c, \ - void (*)(short, char *): a2sh_nc, \ - void (*)(short, void *): a2sh_nc, \ - void (*)(int, const char *): a2si_c, \ - void (*)(int, const void *): a2si_c, \ - void (*)(int, char *): a2si_nc, \ - void (*)(int, void *): a2si_nc, \ - void (*)(long, const char *): a2sl_c, \ - void (*)(long, const void *): a2sl_c, \ - void (*)(long, char *): a2sl_nc, \ - void (*)(long, void *): a2sl_nc, \ - void (*)(long long, const char *): a2sll_c, \ - void (*)(long long, const void *): a2sll_c, \ - void (*)(long long, char *): a2sll_nc, \ - void (*)(long long, void *): a2sll_nc, \ - void (*)(unsigned short, const char *): a2uh_c, \ - void (*)(unsigned short, const void *): a2uh_c, \ - void (*)(unsigned short, char *): a2uh_nc, \ - void (*)(unsigned short, void *): a2uh_nc, \ - void (*)(unsigned int, const char *): a2ui_c, \ - void (*)(unsigned int, const void *): a2ui_c, \ - void (*)(unsigned int, char *): a2ui_nc, \ - void (*)(unsigned int, void *): a2ui_nc, \ - void (*)(unsigned long, const char *): a2ul_c, \ - void (*)(unsigned long, const void *): a2ul_c, \ - void (*)(unsigned long, char *): a2ul_nc, \ - void (*)(unsigned long, void *): a2ul_nc, \ - void (*)(unsigned long long, const char *): a2ull_c, \ - void (*)(unsigned long long, const void *): a2ull_c, \ - void (*)(unsigned long long, char *): a2ull_nc, \ - void (*)(unsigned long long, void *): a2ull_nc \ - )(n, s, __VA_ARGS__) \ -) - - -#define a2sh(n, s, ...) \ -( \ - _Generic(s, \ - const char *: a2sh_c, \ - const void *: a2sh_c, \ - char *: a2sh_nc, \ - void *: a2sh_nc \ - )(n, s, __VA_ARGS__) \ -) - -#define a2si(n, s, ...) \ -( \ - _Generic(s, \ - const char *: a2si_c, \ - const void *: a2si_c, \ - char *: a2si_nc, \ - void *: a2si_nc \ - )(n, s, __VA_ARGS__) \ -) - -#define a2sl(n, s, ...) \ -( \ - _Generic(s, \ - const char *: a2sl_c, \ - const void *: a2sl_c, \ - char *: a2sl_nc, \ - void *: a2sl_nc \ - )(n, s, __VA_ARGS__) \ -) - -#define a2sll(n, s, ...) \ -( \ - _Generic(s, \ - const char *: a2sll_c, \ - const void *: a2sll_c, \ - char *: a2sll_nc, \ - void *: a2sll_nc \ - )(n, s, __VA_ARGS__) \ -) - -#define a2uh(n, s, ...) \ -( \ - _Generic(s, \ - const char *: a2uh_c, \ - const void *: a2uh_c, \ - char *: a2uh_nc, \ - void *: a2uh_nc \ - )(n, s, __VA_ARGS__) \ -) - -#define a2ui(n, s, ...) \ -( \ - _Generic(s, \ - const char *: a2ui_c, \ - const void *: a2ui_c, \ - char *: a2ui_nc, \ - void *: a2ui_nc \ - )(n, s, __VA_ARGS__) \ -) - -#define a2ul(n, s, ...) \ -( \ - _Generic(s, \ - const char *: a2ul_c, \ - const void *: a2ul_c, \ - char *: a2ul_nc, \ - void *: a2ul_nc \ - )(n, s, __VA_ARGS__) \ -) - -#define a2ull(n, s, ...) \ -( \ - _Generic(s, \ - const char *: a2ull_c, \ - const void *: a2ull_c, \ - char *: a2ull_nc, \ - void *: a2ull_nc \ - )(n, s, __VA_ARGS__) \ -) - - -ATTR_STRING(2) ATTR_ACCESS(write_only, 1) ATTR_ACCESS(write_only, 3) -inline int a2sh_c(short *restrict n, const char *s, - const 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_c(int *restrict n, const char *s, - const 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_c(long *restrict n, const char *s, - const 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_c(long long *restrict n, const char *s, - const 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_c(unsigned short *restrict n, const char *s, - const 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_c(unsigned int *restrict n, const char *s, - const 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_c(unsigned long *restrict n, const char *s, - const 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_c(unsigned long long *restrict n, const char *s, - const char **restrict endp, int base, unsigned long long min, - unsigned long long max); - -ATTR_STRING(2) ATTR_ACCESS(write_only, 1) ATTR_ACCESS(write_only, 3) -inline int a2sh_nc(short *restrict n, 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_nc(int *restrict n, 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_nc(long *restrict n, 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_nc(long long *restrict n, 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_nc(unsigned short *restrict n, 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_nc(unsigned int *restrict n, 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_nc(unsigned long *restrict n, 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_nc(unsigned long long *restrict n, char *s, - char **restrict endp, int base, unsigned long long min, - unsigned long long max); - - -inline int -a2sh_c(short *restrict n, const char *s, - const char **restrict endp, int base, short min, short max) -{ - return a2sh(n, (char *) s, (char **) endp, base, min, max); -} - - -inline int -a2si_c(int *restrict n, const char *s, - const char **restrict endp, int base, int min, int max) -{ - return a2si(n, (char *) s, (char **) endp, base, min, max); -} - - -inline int -a2sl_c(long *restrict n, const char *s, - const char **restrict endp, int base, long min, long max) -{ - return a2sl(n, (char *) s, (char **) endp, base, min, max); -} - - -inline int -a2sll_c(long long *restrict n, const char *s, - const char **restrict endp, int base, long long min, long long max) -{ - return a2sll(n, (char *) s, (char **) endp, base, min, max); -} - - -inline int -a2uh_c(unsigned short *restrict n, const char *s, - const char **restrict endp, int base, unsigned short min, - unsigned short max) -{ - return a2uh(n, (char *) s, (char **) endp, base, min, max); -} - - -inline int -a2ui_c(unsigned int *restrict n, const char *s, - const char **restrict endp, int base, unsigned int min, unsigned int max) -{ - return a2ui(n, (char *) s, (char **) endp, base, min, max); -} - - -inline int -a2ul_c(unsigned long *restrict n, const char *s, - const char **restrict endp, int base, unsigned long min, unsigned long max) -{ - return a2ul(n, (char *) s, (char **) endp, base, min, max); -} - - -inline int -a2ull_c(unsigned long long *restrict n, const char *s, - const char **restrict endp, int base, unsigned long long min, - unsigned long long max) -{ - return a2ull(n, (char *) s, (char **) endp, base, min, max); -} - - -inline int -a2sh_nc(short *restrict n, 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_nc(int *restrict n, 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 -a2sl_nc(long *restrict n, 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 -a2sll_nc(long long *restrict n, 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_nc(unsigned short *restrict n, 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_nc(unsigned int *restrict n, 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_nc(unsigned long *restrict n, 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; -} - - -inline int -a2ull_nc(unsigned long long *restrict n, 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 diff --git a/lib/atoi/a2i/a2i.c b/lib/atoi/a2i/a2i.c new file mode 100644 index 000000000..ec4c976de --- /dev/null +++ b/lib/atoi/a2i/a2i.c @@ -0,0 +1,7 @@ +// SPDX-FileCopyrightText: 2023-2024, Alejandro Colomar +// SPDX-License-Identifier: BSD-3-Clause + + +#include + +#include "atoi/a2i/a2i.h" diff --git a/lib/atoi/a2i/a2i.h b/lib/atoi/a2i/a2i.h new file mode 100644 index 000000000..b1a47242b --- /dev/null +++ b/lib/atoi/a2i/a2i.h @@ -0,0 +1,62 @@ +// SPDX-FileCopyrightText: 2023-2024, Alejandro Colomar +// SPDX-License-Identifier: BSD-3-Clause + + +#ifndef SHADOW_INCLUDE_LIB_ATOI_A2I_A2I_H_ +#define SHADOW_INCLUDE_LIB_ATOI_A2I_A2I_H_ + + +#include + +#include "atoi/a2i/a2s_c.h" +#include "atoi/a2i/a2s_nc.h" +#include "atoi/a2i/a2u_c.h" +#include "atoi/a2i/a2u_nc.h" + + +/* + * See the manual of these macros in liba2i's documentation: + * + */ + + +#define a2i(TYPE, n, s, ...) \ +( \ + _Generic((void (*)(TYPE, typeof(s))) 0, \ + void (*)(short, const char *): a2sh_c, \ + void (*)(short, const void *): a2sh_c, \ + void (*)(short, char *): a2sh_nc, \ + void (*)(short, void *): a2sh_nc, \ + void (*)(int, const char *): a2si_c, \ + void (*)(int, const void *): a2si_c, \ + void (*)(int, char *): a2si_nc, \ + void (*)(int, void *): a2si_nc, \ + void (*)(long, const char *): a2sl_c, \ + void (*)(long, const void *): a2sl_c, \ + void (*)(long, char *): a2sl_nc, \ + void (*)(long, void *): a2sl_nc, \ + void (*)(long long, const char *): a2sll_c, \ + void (*)(long long, const void *): a2sll_c, \ + void (*)(long long, char *): a2sll_nc, \ + void (*)(long long, void *): a2sll_nc, \ + void (*)(unsigned short, const char *): a2uh_c, \ + void (*)(unsigned short, const void *): a2uh_c, \ + void (*)(unsigned short, char *): a2uh_nc, \ + void (*)(unsigned short, void *): a2uh_nc, \ + void (*)(unsigned int, const char *): a2ui_c, \ + void (*)(unsigned int, const void *): a2ui_c, \ + void (*)(unsigned int, char *): a2ui_nc, \ + void (*)(unsigned int, void *): a2ui_nc, \ + void (*)(unsigned long, const char *): a2ul_c, \ + void (*)(unsigned long, const void *): a2ul_c, \ + void (*)(unsigned long, char *): a2ul_nc, \ + void (*)(unsigned long, void *): a2ul_nc, \ + void (*)(unsigned long long, const char *): a2ull_c, \ + void (*)(unsigned long long, const void *): a2ull_c, \ + void (*)(unsigned long long, char *): a2ull_nc, \ + void (*)(unsigned long long, void *): a2ull_nc \ + )(n, s, __VA_ARGS__) \ +) + + +#endif // include guard diff --git a/lib/atoi/a2i/a2s.c b/lib/atoi/a2i/a2s.c new file mode 100644 index 000000000..a60c97bc9 --- /dev/null +++ b/lib/atoi/a2i/a2s.c @@ -0,0 +1,7 @@ +// SPDX-FileCopyrightText: 2023-2024, Alejandro Colomar +// SPDX-License-Identifier: BSD-3-Clause + + +#include + +#include "atoi/a2i/a2s.h" diff --git a/lib/atoi/a2i/a2s.h b/lib/atoi/a2i/a2s.h new file mode 100644 index 000000000..935e109a9 --- /dev/null +++ b/lib/atoi/a2i/a2s.h @@ -0,0 +1,56 @@ +// SPDX-FileCopyrightText: 2023-2024, Alejandro Colomar +// SPDX-License-Identifier: BSD-3-Clause + + +#ifndef SHADOW_INCLUDE_LIB_ATOI_A2I_A2S_H_ +#define SHADOW_INCLUDE_LIB_ATOI_A2I_A2S_H_ + + +#include + +#include "atoi/a2i/a2s_c.h" +#include "atoi/a2i/a2s_nc.h" + + +#define a2sh(n, s, ...) \ +( \ + _Generic(s, \ + const char *: a2sh_c, \ + const void *: a2sh_c, \ + char *: a2sh_nc, \ + void *: a2sh_nc \ + )(n, s, __VA_ARGS__) \ +) + +#define a2si(n, s, ...) \ +( \ + _Generic(s, \ + const char *: a2si_c, \ + const void *: a2si_c, \ + char *: a2si_nc, \ + void *: a2si_nc \ + )(n, s, __VA_ARGS__) \ +) + +#define a2sl(n, s, ...) \ +( \ + _Generic(s, \ + const char *: a2sl_c, \ + const void *: a2sl_c, \ + char *: a2sl_nc, \ + void *: a2sl_nc \ + )(n, s, __VA_ARGS__) \ +) + +#define a2sll(n, s, ...) \ +( \ + _Generic(s, \ + const char *: a2sll_c, \ + const void *: a2sll_c, \ + char *: a2sll_nc, \ + void *: a2sll_nc \ + )(n, s, __VA_ARGS__) \ +) + + +#endif // include guard diff --git a/lib/atoi/a2i/a2s_c.c b/lib/atoi/a2i/a2s_c.c new file mode 100644 index 000000000..257b8d4e1 --- /dev/null +++ b/lib/atoi/a2i/a2s_c.c @@ -0,0 +1,17 @@ +// SPDX-FileCopyrightText: 2023-2024, Alejandro Colomar +// SPDX-License-Identifier: BSD-3-Clause + + +#include + +#include "atoi/a2i/a2s_c.h" + + +extern inline int a2sh_c(short *restrict n, const char *s, + const char **restrict endp, int base, short min, short max); +extern inline int a2si_c(int *restrict n, const char *s, + const char **restrict endp, int base, int min, int max); +extern inline int a2sl_c(long *restrict n, const char *s, + const char **restrict endp, int base, long min, long max); +extern inline int a2sll_c(long long *restrict n, const char *s, + const char **restrict endp, int base, long long min, long long max); diff --git a/lib/atoi/a2i/a2s_c.h b/lib/atoi/a2i/a2s_c.h new file mode 100644 index 000000000..5d0024d11 --- /dev/null +++ b/lib/atoi/a2i/a2s_c.h @@ -0,0 +1,64 @@ +// SPDX-FileCopyrightText: 2023-2024, Alejandro Colomar +// SPDX-License-Identifier: BSD-3-Clause + + +#ifndef SHADOW_INCLUDE_LIB_ATOI_A2I_A2S_C_H_ +#define SHADOW_INCLUDE_LIB_ATOI_A2I_A2S_C_H_ + + +#include + +#include +#include + +#include "atoi/a2i/a2s_nc.h" +#include "attr.h" + + +ATTR_STRING(2) ATTR_ACCESS(write_only, 1) ATTR_ACCESS(write_only, 3) +inline int a2sh_c(short *restrict n, const char *s, + const 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_c(int *restrict n, const char *s, + const 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_c(long *restrict n, const char *s, + const 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_c(long long *restrict n, const char *s, + const char **restrict endp, int base, long long min, long long max); + + +inline int +a2sh_c(short *restrict n, const char *s, + const char **restrict endp, int base, short min, short max) +{ + return a2sh_nc(n, (char *) s, (char **) endp, base, min, max); +} + + +inline int +a2si_c(int *restrict n, const char *s, + const char **restrict endp, int base, int min, int max) +{ + return a2si_nc(n, (char *) s, (char **) endp, base, min, max); +} + + +inline int +a2sl_c(long *restrict n, const char *s, + const char **restrict endp, int base, long min, long max) +{ + return a2sl_nc(n, (char *) s, (char **) endp, base, min, max); +} + + +inline int +a2sll_c(long long *restrict n, const char *s, + const char **restrict endp, int base, long long min, long long max) +{ + return a2sll_nc(n, (char *) s, (char **) endp, base, min, max); +} + + +#endif // include guard diff --git a/lib/atoi/a2i/a2s_nc.c b/lib/atoi/a2i/a2s_nc.c new file mode 100644 index 000000000..7ef8be6c1 --- /dev/null +++ b/lib/atoi/a2i/a2s_nc.c @@ -0,0 +1,17 @@ +// SPDX-FileCopyrightText: 2023-2024, Alejandro Colomar +// SPDX-License-Identifier: BSD-3-Clause + + +#include + +#include "atoi/a2i/a2s_nc.h" + + +extern inline int a2sh_nc(short *restrict n, char *s, + char **restrict endp, int base, short min, short max); +extern inline int a2si_nc(int *restrict n, char *s, + char **restrict endp, int base, int min, int max); +extern inline int a2sl_nc(long *restrict n, char *s, + char **restrict endp, int base, long min, long max); +extern inline int a2sll_nc(long long *restrict n, char *s, + char **restrict endp, int base, long long min, long long max); diff --git a/lib/atoi/a2i/a2s_nc.h b/lib/atoi/a2i/a2s_nc.h new file mode 100644 index 000000000..8ca16f27a --- /dev/null +++ b/lib/atoi/a2i/a2s_nc.h @@ -0,0 +1,91 @@ +// SPDX-FileCopyrightText: 2023-2024, Alejandro Colomar +// SPDX-License-Identifier: BSD-3-Clause + + +#ifndef SHADOW_INCLUDE_LIB_ATOI_A2I_A2S_NC_H_ +#define SHADOW_INCLUDE_LIB_ATOI_A2I_A2S_NC_H_ + + +#include + +#include + +#include "atoi/strtoi/strtoi.h" +#include "attr.h" + + +ATTR_STRING(2) ATTR_ACCESS(write_only, 1) ATTR_ACCESS(write_only, 3) +inline int a2sh_nc(short *restrict n, 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_nc(int *restrict n, 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_nc(long *restrict n, 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_nc(long long *restrict n, char *s, + char **restrict endp, int base, long long min, long long max); + + +inline int +a2sh_nc(short *restrict n, 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_nc(int *restrict n, 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 +a2sl_nc(long *restrict n, 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 +a2sll_nc(long long *restrict n, 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; +} + + +#endif // include guard diff --git a/lib/atoi/a2i/a2u.c b/lib/atoi/a2i/a2u.c new file mode 100644 index 000000000..8523e8bf7 --- /dev/null +++ b/lib/atoi/a2i/a2u.c @@ -0,0 +1,7 @@ +// SPDX-FileCopyrightText: 2023-2024, Alejandro Colomar +// SPDX-License-Identifier: BSD-3-Clause + + +#include + +#include "atoi/a2i/a2u.h" diff --git a/lib/atoi/a2i/a2u.h b/lib/atoi/a2i/a2u.h new file mode 100644 index 000000000..2a3852e45 --- /dev/null +++ b/lib/atoi/a2i/a2u.h @@ -0,0 +1,56 @@ +// SPDX-FileCopyrightText: 2023-2024, Alejandro Colomar +// SPDX-License-Identifier: BSD-3-Clause + + +#ifndef SHADOW_INCLUDE_LIB_ATOI_A2I_A2U_H_ +#define SHADOW_INCLUDE_LIB_ATOI_A2I_A2U_H_ + + +#include + +#include "atoi/a2i/a2u_c.h" +#include "atoi/a2i/a2u_nc.h" + + +#define a2uh(n, s, ...) \ +( \ + _Generic(s, \ + const char *: a2uh_c, \ + const void *: a2uh_c, \ + char *: a2uh_nc, \ + void *: a2uh_nc \ + )(n, s, __VA_ARGS__) \ +) + +#define a2ui(n, s, ...) \ +( \ + _Generic(s, \ + const char *: a2ui_c, \ + const void *: a2ui_c, \ + char *: a2ui_nc, \ + void *: a2ui_nc \ + )(n, s, __VA_ARGS__) \ +) + +#define a2ul(n, s, ...) \ +( \ + _Generic(s, \ + const char *: a2ul_c, \ + const void *: a2ul_c, \ + char *: a2ul_nc, \ + void *: a2ul_nc \ + )(n, s, __VA_ARGS__) \ +) + +#define a2ull(n, s, ...) \ +( \ + _Generic(s, \ + const char *: a2ull_c, \ + const void *: a2ull_c, \ + char *: a2ull_nc, \ + void *: a2ull_nc \ + )(n, s, __VA_ARGS__) \ +) + + +#endif // include guard diff --git a/lib/atoi/a2i/a2u_c.c b/lib/atoi/a2i/a2u_c.c new file mode 100644 index 000000000..e1d04b850 --- /dev/null +++ b/lib/atoi/a2i/a2u_c.c @@ -0,0 +1,19 @@ +// SPDX-FileCopyrightText: 2023-2024, Alejandro Colomar +// SPDX-License-Identifier: BSD-3-Clause + + +#include + +#include "atoi/a2i/a2u_c.h" + + +extern inline int a2uh_c(unsigned short *restrict n, const char *s, + const char **restrict endp, int base, unsigned short min, + unsigned short max); +extern inline int a2ui_c(unsigned int *restrict n, const char *s, + const char **restrict endp, int base, unsigned int min, unsigned int max); +extern inline int a2ul_c(unsigned long *restrict n, const char *s, + const char **restrict endp, int base, unsigned long min, unsigned long max); +extern inline int a2ull_c(unsigned long long *restrict n, const char *s, + const char **restrict endp, int base, unsigned long long min, + unsigned long long max); diff --git a/lib/atoi/a2i/a2u_c.h b/lib/atoi/a2i/a2u_c.h new file mode 100644 index 000000000..420e78529 --- /dev/null +++ b/lib/atoi/a2i/a2u_c.h @@ -0,0 +1,65 @@ +// SPDX-FileCopyrightText: 2023-2024, Alejandro Colomar +// SPDX-License-Identifier: BSD-3-Clause + + +#ifndef SHADOW_INCLUDE_LIB_ATOI_A2I_A2U_C_H_ +#define SHADOW_INCLUDE_LIB_ATOI_A2I_A2U_C_H_ + + +#include + +#include "atoi/a2i/a2u_nc.h" +#include "attr.h" + + +ATTR_STRING(2) ATTR_ACCESS(write_only, 1) ATTR_ACCESS(write_only, 3) +inline int a2uh_c(unsigned short *restrict n, const char *s, + const 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_c(unsigned int *restrict n, const char *s, + const 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_c(unsigned long *restrict n, const char *s, + const 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_c(unsigned long long *restrict n, const char *s, + const char **restrict endp, int base, unsigned long long min, + unsigned long long max); + + +inline int +a2uh_c(unsigned short *restrict n, const char *s, + const char **restrict endp, int base, unsigned short min, + unsigned short max) +{ + return a2uh_nc(n, (char *) s, (char **) endp, base, min, max); +} + + +inline int +a2ui_c(unsigned int *restrict n, const char *s, + const char **restrict endp, int base, unsigned int min, unsigned int max) +{ + return a2ui_nc(n, (char *) s, (char **) endp, base, min, max); +} + + +inline int +a2ul_c(unsigned long *restrict n, const char *s, + const char **restrict endp, int base, unsigned long min, unsigned long max) +{ + return a2ul_nc(n, (char *) s, (char **) endp, base, min, max); +} + + +inline int +a2ull_c(unsigned long long *restrict n, const char *s, + const char **restrict endp, int base, unsigned long long min, + unsigned long long max) +{ + return a2ull_nc(n, (char *) s, (char **) endp, base, min, max); +} + + +#endif // include guard diff --git a/lib/atoi/a2i/a2u_nc.c b/lib/atoi/a2i/a2u_nc.c new file mode 100644 index 000000000..4ca282bdb --- /dev/null +++ b/lib/atoi/a2i/a2u_nc.c @@ -0,0 +1,18 @@ +// SPDX-FileCopyrightText: 2023-2024, Alejandro Colomar +// SPDX-License-Identifier: BSD-3-Clause + + +#include + +#include "atoi/a2i/a2u_nc.h" + + +extern inline int a2uh_nc(unsigned short *restrict n, char *s, + char **restrict endp, int base, unsigned short min, unsigned short max); +extern inline int a2ui_nc(unsigned int *restrict n, char *s, + char **restrict endp, int base, unsigned int min, unsigned int max); +extern inline int a2ul_nc(unsigned long *restrict n, char *s, + char **restrict endp, int base, unsigned long min, unsigned long max); +extern inline int a2ull_nc(unsigned long long *restrict n, char *s, + char **restrict endp, int base, unsigned long long min, + unsigned long long max); diff --git a/lib/atoi/a2i/a2u_nc.h b/lib/atoi/a2i/a2u_nc.h new file mode 100644 index 000000000..c8d993e7f --- /dev/null +++ b/lib/atoi/a2i/a2u_nc.h @@ -0,0 +1,94 @@ +// SPDX-FileCopyrightText: 2023-2024, Alejandro Colomar +// SPDX-License-Identifier: BSD-3-Clause + + +#ifndef SHADOW_INCLUDE_LIB_ATOI_A2I_A2U_NC_H_ +#define SHADOW_INCLUDE_LIB_ATOI_A2I_A2U_NC_H_ + + +#include + +#include + +#include "atoi/strtoi/strtou_noneg.h" +#include "attr.h" + + +ATTR_STRING(2) ATTR_ACCESS(write_only, 1) ATTR_ACCESS(write_only, 3) +inline int a2uh_nc(unsigned short *restrict n, 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_nc(unsigned int *restrict n, 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_nc(unsigned long *restrict n, 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_nc(unsigned long long *restrict n, char *s, + char **restrict endp, int base, unsigned long long min, + unsigned long long max); + + +inline int +a2uh_nc(unsigned short *restrict n, 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_nc(unsigned int *restrict n, 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_nc(unsigned long *restrict n, 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; +} + + +inline int +a2ull_nc(unsigned long long *restrict n, 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 diff --git a/lib/atoi/getnum.h b/lib/atoi/getnum.h index efb8bcb46..33d4876a4 100644 --- a/lib/atoi/getnum.h +++ b/lib/atoi/getnum.h @@ -13,7 +13,8 @@ #include #include -#include "atoi/a2i.h" +#include "atoi/a2i/a2i.h" +#include "atoi/a2i/a2s.h" #include "attr.h" #include "typetraits.h" diff --git a/lib/atoi/str2i.h b/lib/atoi/str2i.h deleted file mode 100644 index b3ded0310..000000000 --- a/lib/atoi/str2i.h +++ /dev/null @@ -1,108 +0,0 @@ -// SPDX-FileCopyrightText: 2007-2009, Nicolas François -// SPDX-FileCopyrightText: 2023-2024, Alejandro Colomar -// SPDX-License-Identifier: BSD-3-Clause - - -#ifndef SHADOW_INCLUDE_LIB_ATOI_STR2I_H_ -#define SHADOW_INCLUDE_LIB_ATOI_STR2I_H_ - - -#include - -#include -#include - -#include "atoi/a2i.h" -#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 -str2sl(long *restrict n, const char *restrict s) -{ - return a2sl(n, s, NULL, 0, LONG_MIN, LONG_MAX); -} - - -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) -{ - return a2ul(n, s, NULL, 0, 0, ULONG_MAX); -} - - -inline int -str2ull(unsigned long long *restrict n, const char *restrict s) -{ - return a2ull(n, s, NULL, 0, 0, ULLONG_MAX); -} - - -#endif // include guard diff --git a/lib/atoi/str2i/str2i.c b/lib/atoi/str2i/str2i.c new file mode 100644 index 000000000..ff5ad8f02 --- /dev/null +++ b/lib/atoi/str2i/str2i.c @@ -0,0 +1,8 @@ +// SPDX-FileCopyrightText: 2007-2009, Nicolas François +// SPDX-FileCopyrightText: 2023-2024, Alejandro Colomar +// SPDX-License-Identifier: BSD-3-Clause + + +#include + +#include "atoi/str2i/str2i.h" diff --git a/lib/atoi/str2i/str2i.h b/lib/atoi/str2i/str2i.h new file mode 100644 index 000000000..e2cecc753 --- /dev/null +++ b/lib/atoi/str2i/str2i.h @@ -0,0 +1,31 @@ +// SPDX-FileCopyrightText: 2007-2009, Nicolas François +// SPDX-FileCopyrightText: 2023-2024, Alejandro Colomar +// SPDX-License-Identifier: BSD-3-Clause + + +#ifndef SHADOW_INCLUDE_LIB_ATOI_STR2I_STR2I_H_ +#define SHADOW_INCLUDE_LIB_ATOI_STR2I_STR2I_H_ + + +#include + +#include "atoi/str2i/str2s.h" +#include "atoi/str2i/str2u.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__) \ +) + + +#endif // include guard diff --git a/lib/atoi/str2i/str2s.c b/lib/atoi/str2i/str2s.c new file mode 100644 index 000000000..f77c594e5 --- /dev/null +++ b/lib/atoi/str2i/str2s.c @@ -0,0 +1,14 @@ +// SPDX-FileCopyrightText: 2007-2009, Nicolas François +// SPDX-FileCopyrightText: 2023-2024, Alejandro Colomar +// SPDX-License-Identifier: BSD-3-Clause + + +#include + +#include "atoi/str2i/str2s.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); diff --git a/lib/atoi/str2i/str2s.h b/lib/atoi/str2i/str2s.h new file mode 100644 index 000000000..dc20faa19 --- /dev/null +++ b/lib/atoi/str2i/str2s.h @@ -0,0 +1,57 @@ +// SPDX-FileCopyrightText: 2007-2009, Nicolas François +// SPDX-FileCopyrightText: 2023-2024, Alejandro Colomar +// SPDX-License-Identifier: BSD-3-Clause + + +#ifndef SHADOW_INCLUDE_LIB_ATOI_STR2I_STR2S_H_ +#define SHADOW_INCLUDE_LIB_ATOI_STR2I_STR2S_H_ + + +#include + +#include +#include + +#include "atoi/a2i/a2s.h" +#include "attr.h" + + +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); + + +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 +str2sl(long *restrict n, const char *restrict s) +{ + return a2sl(n, s, NULL, 0, LONG_MIN, LONG_MAX); +} + + +inline int +str2sll(long long *restrict n, const char *restrict s) +{ + return a2sll(n, s, NULL, 0, LLONG_MIN, LLONG_MAX); +} + + +#endif // include guard diff --git a/lib/atoi/str2i.c b/lib/atoi/str2i/str2u.c similarity index 62% rename from lib/atoi/str2i.c rename to lib/atoi/str2i/str2u.c index 25ce3609b..25fffda9c 100644 --- a/lib/atoi/str2i.c +++ b/lib/atoi/str2i/str2u.c @@ -5,13 +5,9 @@ #include -#include "atoi/str2i.h" +#include "atoi/str2i/str2u.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); diff --git a/lib/atoi/str2i/str2u.h b/lib/atoi/str2i/str2u.h new file mode 100644 index 000000000..c25941ce9 --- /dev/null +++ b/lib/atoi/str2i/str2u.h @@ -0,0 +1,57 @@ +// SPDX-FileCopyrightText: 2007-2009, Nicolas François +// SPDX-FileCopyrightText: 2023-2024, Alejandro Colomar +// SPDX-License-Identifier: BSD-3-Clause + + +#ifndef SHADOW_INCLUDE_LIB_ATOI_STR2I_STR2U_H_ +#define SHADOW_INCLUDE_LIB_ATOI_STR2I_STR2U_H_ + + +#include + +#include +#include + +#include "atoi/a2i/a2u.h" +#include "attr.h" + + +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 +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) +{ + return a2ul(n, s, NULL, 0, 0, ULONG_MAX); +} + + +inline int +str2ull(unsigned long long *restrict n, const char *restrict s) +{ + return a2ull(n, s, NULL, 0, 0, ULLONG_MAX); +} + + +#endif // include guard diff --git a/lib/atoi/strtoi.c b/lib/atoi/strtoi.c deleted file mode 100644 index 197707b10..000000000 --- a/lib/atoi/strtoi.c +++ /dev/null @@ -1,15 +0,0 @@ -// SPDX-FileCopyrightText: 2023, Alejandro Colomar -// SPDX-License-Identifier: BSD-3-Clause - - -#include - -#include "atoi/strtoi.h" - -#include - - -extern inline intmax_t strtoi_(const char *s, char **restrict endp, int base, - intmax_t min, intmax_t max, int *restrict status); -extern inline uintmax_t strtou_(const char *s, char **restrict endp, int base, - uintmax_t min, uintmax_t max, int *restrict status); diff --git a/lib/atoi/strtoi.h b/lib/atoi/strtoi.h deleted file mode 100644 index 1f061fc0c..000000000 --- a/lib/atoi/strtoi.h +++ /dev/null @@ -1,96 +0,0 @@ -// SPDX-FileCopyrightText: 2023, Alejandro Colomar -// SPDX-License-Identifier: BSD-3-Clause - - -#ifndef SHADOW_INCLUDE_LIB_ATOI_STRTOI_H_ -#define SHADOW_INCLUDE_LIB_ATOI_STRTOI_H_ - - -#include - -#include -#include -#include -#include -#include -#include - -#include "attr.h" - - -#define strtoNmax(TYPE, ...) \ -( \ - _Generic((TYPE) 0, \ - intmax_t: strtoimax, \ - uintmax_t: strtoumax \ - )(__VA_ARGS__) \ -) - - -#define strtoN(s, endp, base, min, max, status, TYPE) \ -({ \ - const char *s_ = s; \ - char **endp_ = endp; \ - int base_ = base; \ - TYPE min_ = min; \ - TYPE max_ = max; \ - int *status_ = status; \ - \ - int e_, st_; \ - char *end_; \ - TYPE n_; \ - \ - if (endp_ == NULL) \ - endp_ = &end_; \ - if (status_ == NULL) \ - status_ = &st_; \ - \ - if (base_ != 0 && (base_ < 0 || base_ > 36)) { \ - *status_ = EINVAL; \ - n_ = 0; \ - \ - } else { \ - e_ = errno; \ - errno = 0; \ - n_ = strtoNmax(TYPE, s_, endp_, base_); \ - \ - if (*endp_ == s_) \ - *status_ = ECANCELED; \ - else if (errno == ERANGE || n_ < min_ || n_ > max_) \ - *status_ = ERANGE; \ - else if (**endp_ != '\0') \ - *status_ = ENOTSUP; \ - else \ - *status_ = 0; \ - \ - errno = e_; \ - } \ - MAX(min_, MIN(max_, n_)); \ -}) - - -ATTR_STRING(1) ATTR_ACCESS(write_only, 2) ATTR_ACCESS(write_only, 6) -inline intmax_t strtoi_(const char *s, char **restrict endp, int base, - intmax_t min, intmax_t max, int *restrict status); -ATTR_STRING(1) ATTR_ACCESS(write_only, 2) ATTR_ACCESS(write_only, 6) -inline uintmax_t strtou_(const char *s, char **restrict endp, int base, - uintmax_t min, uintmax_t max, int *restrict status); - - -inline intmax_t -strtoi_(const char *s, char **restrict endp, int base, - intmax_t min, intmax_t max, int *restrict status) -{ - return strtoN(s, endp, base, min, max, status, intmax_t); -} - - -inline uintmax_t -strtou_(const char *s, char **restrict endp, int base, - uintmax_t min, uintmax_t max, int *restrict status) -{ - return strtoN(s, endp, base, min, max, status, uintmax_t); -} - - -#endif // include guard diff --git a/lib/atoi/strtoi/strtoi.c b/lib/atoi/strtoi/strtoi.c new file mode 100644 index 000000000..c478c2a1d --- /dev/null +++ b/lib/atoi/strtoi/strtoi.c @@ -0,0 +1,13 @@ +// SPDX-FileCopyrightText: 2023-2024, Alejandro Colomar +// SPDX-License-Identifier: BSD-3-Clause + + +#include + +#include "atoi/strtoi/strtoi.h" + +#include + + +extern inline intmax_t strtoi_(const char *s, char **restrict endp, int base, + intmax_t min, intmax_t max, int *restrict status); diff --git a/lib/atoi/strtoi/strtoi.h b/lib/atoi/strtoi/strtoi.h new file mode 100644 index 000000000..a8f6f5408 --- /dev/null +++ b/lib/atoi/strtoi/strtoi.h @@ -0,0 +1,64 @@ +// SPDX-FileCopyrightText: 2023-2024, Alejandro Colomar +// SPDX-License-Identifier: BSD-3-Clause + + +#ifndef SHADOW_INCLUDE_LIB_ATOI_STRTOI_STRTOI_H_ +#define SHADOW_INCLUDE_LIB_ATOI_STRTOI_STRTOI_H_ + + +#include + +#include +#include +#include +#include +#include +#include + +#include "attr.h" + + +ATTR_STRING(1) ATTR_ACCESS(write_only, 2) ATTR_ACCESS(write_only, 6) +inline intmax_t strtoi_(const char *s, char **restrict endp, int base, + intmax_t min, intmax_t max, int *restrict status); + + +inline intmax_t +strtoi_(const char *s, char **restrict endp, int base, + intmax_t min, intmax_t max, int *restrict status) +{ + int e, st; + char *end; + intmax_t n; + + if (endp == NULL) + endp = &end; + if (status == NULL) + status = &st; + + if (base != 0 && (base < 0 || base > 36)) { + *status = EINVAL; + return MAX(min, MIN(max, 0)); + } + + e = errno; + errno = 0; + + n = strtoimax(s, endp, base); + + if (*endp == s) + *status = ECANCELED; + else if (errno == ERANGE || n < min || n > max) + *status = ERANGE; + else if (**endp != '\0') + *status = ENOTSUP; + else + *status = 0; + + errno = e; + + return MAX(min, MIN(max, n)); +} + + +#endif // include guard diff --git a/lib/atoi/strtoi/strtou.c b/lib/atoi/strtoi/strtou.c new file mode 100644 index 000000000..c70ca4e85 --- /dev/null +++ b/lib/atoi/strtoi/strtou.c @@ -0,0 +1,13 @@ +// SPDX-FileCopyrightText: 2023-2024, Alejandro Colomar +// SPDX-License-Identifier: BSD-3-Clause + + +#include + +#include "atoi/strtoi/strtou.h" + +#include + + +extern inline uintmax_t strtou_(const char *s, char **restrict endp, int base, + uintmax_t min, uintmax_t max, int *restrict status); diff --git a/lib/atoi/strtoi/strtou.h b/lib/atoi/strtoi/strtou.h new file mode 100644 index 000000000..5ce7bd6a1 --- /dev/null +++ b/lib/atoi/strtoi/strtou.h @@ -0,0 +1,64 @@ +// SPDX-FileCopyrightText: 2023-2024, Alejandro Colomar +// SPDX-License-Identifier: BSD-3-Clause + + +#ifndef SHADOW_INCLUDE_LIB_ATOI_STRTOI_STRTOU_H_ +#define SHADOW_INCLUDE_LIB_ATOI_STRTOI_STRTOU_H_ + + +#include + +#include +#include +#include +#include +#include +#include + +#include "attr.h" + + +ATTR_STRING(1) ATTR_ACCESS(write_only, 2) ATTR_ACCESS(write_only, 6) +inline uintmax_t strtou_(const char *s, char **restrict endp, int base, + uintmax_t min, uintmax_t max, int *restrict status); + + +inline uintmax_t +strtou_(const char *s, char **restrict endp, int base, + uintmax_t min, uintmax_t max, int *restrict status) +{ + int e, st; + char *end; + uintmax_t n; + + if (endp == NULL) + endp = &end; + if (status == NULL) + status = &st; + + if (base != 0 && (base < 0 || base > 36)) { + *status = EINVAL; + return MAX(min, 0); + } + + e = errno; + errno = 0; + + n = strtoumax(s, endp, base); + + if (*endp == s) + *status = ECANCELED; + else if (errno == ERANGE || n < min || n > max) + *status = ERANGE; + else if (**endp != '\0') + *status = ENOTSUP; + else + *status = 0; + + errno = e; + + return MAX(min, MIN(max, n)); +} + + +#endif // include guard diff --git a/lib/atoi/strtou_noneg.c b/lib/atoi/strtoi/strtou_noneg.c similarity index 67% rename from lib/atoi/strtou_noneg.c rename to lib/atoi/strtoi/strtou_noneg.c index 71cacbd1e..51d60c930 100644 --- a/lib/atoi/strtou_noneg.c +++ b/lib/atoi/strtoi/strtou_noneg.c @@ -1,10 +1,10 @@ -// SPDX-FileCopyrightText: 2023, Alejandro Colomar +// SPDX-FileCopyrightText: 2023-2024, Alejandro Colomar // SPDX-License-Identifier: BSD-3-Clause #include -#include "atoi/strtou_noneg.h" +#include "atoi/strtoi/strtou_noneg.h" #include diff --git a/lib/atoi/strtou_noneg.h b/lib/atoi/strtoi/strtou_noneg.h similarity index 73% rename from lib/atoi/strtou_noneg.h rename to lib/atoi/strtoi/strtou_noneg.h index 6d77adf5e..52fa9034a 100644 --- a/lib/atoi/strtou_noneg.h +++ b/lib/atoi/strtoi/strtou_noneg.h @@ -1,9 +1,9 @@ -// SPDX-FileCopyrightText: 2023, Alejandro Colomar +// SPDX-FileCopyrightText: 2023-2024, Alejandro Colomar // SPDX-License-Identifier: BSD-3-Clause -#ifndef SHADOW_INCLUDE_LIB_ATOI_STRTOU_NONEG_H_ -#define SHADOW_INCLUDE_LIB_ATOI_STRTOU_NONEG_H_ +#ifndef SHADOW_INCLUDE_LIB_ATOI_STRTOI_STRTOU_NONEG_H_ +#define SHADOW_INCLUDE_LIB_ATOI_STRTOI_STRTOU_NONEG_H_ #include @@ -12,7 +12,8 @@ #include #include -#include "atoi/strtoi.h" +#include "atoi/strtoi/strtoi.h" +#include "atoi/strtoi/strtou.h" #include "attr.h" diff --git a/lib/getdef.c b/lib/getdef.c index ce1f98dd7..335c723e1 100644 --- a/lib/getdef.c +++ b/lib/getdef.c @@ -22,8 +22,9 @@ #include #endif -#include "atoi/a2i.h" -#include "atoi/str2i.h" +#include "atoi/a2i/a2s.h" +#include "atoi/a2i/a2u.h" +#include "atoi/str2i/str2u.h" #include "defines.h" #include "getdef.h" #include "prototypes.h" diff --git a/lib/getrange.c b/lib/getrange.c index 466e90892..603152e68 100644 --- a/lib/getrange.c +++ b/lib/getrange.c @@ -10,7 +10,7 @@ #include #include -#include "atoi/a2i.h" +#include "atoi/a2i/a2u.h" #include "defines.h" #include "prototypes.h" diff --git a/lib/gettime.c b/lib/gettime.c index c61c88c39..69285e543 100644 --- a/lib/gettime.c +++ b/lib/gettime.c @@ -11,7 +11,7 @@ #include #include -#include "atoi/a2i.h" +#include "atoi/a2i/a2i.h" #include "defines.h" #include "prototypes.h" #include "shadowlog.h" diff --git a/lib/idmapping.c b/lib/idmapping.c index adc61af88..248269c84 100644 --- a/lib/idmapping.c +++ b/lib/idmapping.c @@ -15,7 +15,7 @@ #include "alloc/calloc.h" #include "alloc/x/xmalloc.h" -#include "atoi/a2i.h" +#include "atoi/a2i/a2u.h" #include "prototypes.h" #include "string/sprintf/stpeprintf.h" #include "idmapping.h" diff --git a/lib/limits.c b/lib/limits.c index fe0e24db7..9713a305b 100644 --- a/lib/limits.c +++ b/lib/limits.c @@ -30,8 +30,11 @@ #include "shadowlog.h" #include -#include "atoi/a2i.h" -#include "atoi/str2i.h" +#include "atoi/a2i/a2i.h" +#include "atoi/a2i/a2s.h" +#include "atoi/str2i/str2i.h" +#include "atoi/str2i/str2s.h" +#include "atoi/str2i/str2u.h" #include "memzero.h" #include "typetraits.h" diff --git a/lib/sgetspent.c b/lib/sgetspent.c index 3ebe427c8..97f64edd5 100644 --- a/lib/sgetspent.c +++ b/lib/sgetspent.c @@ -19,8 +19,8 @@ #include #include -#include "atoi/a2i.h" -#include "atoi/str2i.h" +#include "atoi/a2i/a2s.h" +#include "atoi/str2i/str2u.h" #include "defines.h" #include "prototypes.h" #include "shadowlog_internal.h" diff --git a/lib/shadow.c b/lib/shadow.c index e243ac5a5..8c08a8349 100644 --- a/lib/shadow.c +++ b/lib/shadow.c @@ -18,8 +18,8 @@ #include #include -#include "atoi/a2i.h" -#include "atoi/str2i.h" +#include "atoi/a2i/a2s.h" +#include "atoi/str2i/str2u.h" #include "defines.h" #include "prototypes.h" #include "string/strtok/stpsep.h" diff --git a/lib/strtoday.c b/lib/strtoday.c index dabd5b529..2d69e74b1 100644 --- a/lib/strtoday.c +++ b/lib/strtoday.c @@ -13,7 +13,7 @@ #ident "$Id$" -#include "atoi/str2i.h" +#include "atoi/str2i/str2s.h" #include "prototypes.h" #include "getdate.h" diff --git a/lib/subordinateio.c b/lib/subordinateio.c index c417e7197..295df2bd4 100644 --- a/lib/subordinateio.c +++ b/lib/subordinateio.c @@ -21,7 +21,7 @@ #include "alloc/malloc.h" #include "alloc/realloc.h" #include "alloc/reallocf.h" -#include "atoi/str2i.h" +#include "atoi/str2i/str2u.h" #include "string/sprintf/snprintf.h" diff --git a/src/chage.c b/src/chage.c index 4b482be7a..1e296fca5 100644 --- a/src/chage.c +++ b/src/chage.c @@ -26,7 +26,7 @@ #endif /* ACCT_TOOLS_SETUID */ #include -#include "atoi/a2i.h" +#include "atoi/a2i/a2s.h" #include "defines.h" #include "memzero.h" #include "prototypes.h" diff --git a/src/check_subid_range.c b/src/check_subid_range.c index 35ecf12a8..5f5b75918 100644 --- a/src/check_subid_range.c +++ b/src/check_subid_range.c @@ -14,7 +14,7 @@ #include #include "atoi/getnum.h" -#include "atoi/str2i.h" +#include "atoi/str2i/str2u.h" #include "defines.h" #include "prototypes.h" #include "subordinateio.h" diff --git a/src/chgpasswd.c b/src/chgpasswd.c index 4f87b824f..50a299aed 100644 --- a/src/chgpasswd.c +++ b/src/chgpasswd.c @@ -22,7 +22,7 @@ #include "pam_defs.h" #endif /* USE_PAM */ #endif /* ACCT_TOOLS_SETUID */ -#include "atoi/str2i.h" +#include "atoi/str2i/str2s.h" #include "defines.h" #include "nscd.h" #include "sssd.h" diff --git a/src/chpasswd.c b/src/chpasswd.c index 9e12c6eec..55817ddf4 100644 --- a/src/chpasswd.c +++ b/src/chpasswd.c @@ -20,7 +20,7 @@ #ifdef USE_PAM #include "pam_defs.h" #endif /* USE_PAM */ -#include "atoi/str2i.h" +#include "atoi/str2i/str2s.h" #include "defines.h" #include "nscd.h" #include "sssd.h" diff --git a/src/faillog.c b/src/faillog.c index 23a9c6c53..cf0292499 100644 --- a/src/faillog.c +++ b/src/faillog.c @@ -19,7 +19,7 @@ #include #include -#include "atoi/str2i.h" +#include "atoi/str2i/str2s.h" #include "defines.h" #include "faillog.h" #include "memzero.h" diff --git a/src/free_subid_range.c b/src/free_subid_range.c index 4bdacfb7d..bff6f7b2b 100644 --- a/src/free_subid_range.c +++ b/src/free_subid_range.c @@ -4,7 +4,7 @@ #include #include -#include "atoi/str2i.h" +#include "atoi/str2i/str2u.h" #include "subid.h" #include "stdlib.h" #include "prototypes.h" diff --git a/src/lastlog.c b/src/lastlog.c index 3914b72bb..085e70cd0 100644 --- a/src/lastlog.c +++ b/src/lastlog.c @@ -23,7 +23,7 @@ #include #endif -#include "atoi/str2i.h" +#include "atoi/str2i/str2u.h" #include "defines.h" #include "prototypes.h" #include "getdef.h" diff --git a/src/new_subid_range.c b/src/new_subid_range.c index 6be957409..6c62cc08b 100644 --- a/src/new_subid_range.c +++ b/src/new_subid_range.c @@ -3,7 +3,7 @@ #include #include -#include "atoi/str2i.h" +#include "atoi/str2i/str2u.h" #include "subid.h" #include "stdlib.h" #include "prototypes.h" diff --git a/src/newusers.c b/src/newusers.c index 74c8925af..bd24f7c72 100644 --- a/src/newusers.c +++ b/src/newusers.c @@ -32,7 +32,7 @@ #include "alloc/reallocf.h" #include "atoi/getnum.h" -#include "atoi/str2i.h" +#include "atoi/str2i/str2s.h" #ifdef ACCT_TOOLS_SETUID #ifdef USE_PAM #include "pam_defs.h" diff --git a/src/passwd.c b/src/passwd.c index b7bf925db..7ec70fe12 100644 --- a/src/passwd.c +++ b/src/passwd.c @@ -21,7 +21,7 @@ #include #include "agetpass.h" -#include "atoi/a2i.h" +#include "atoi/a2i/a2s.h" #include "defines.h" #include "getdef.h" #include "memzero.h" diff --git a/src/useradd.c b/src/useradd.c index 82bf22395..2134ce9e1 100644 --- a/src/useradd.c +++ b/src/useradd.c @@ -37,7 +37,7 @@ #include #include "alloc/x/xmalloc.h" -#include "atoi/a2i.h" +#include "atoi/a2i/a2s.h" #include "atoi/getnum.h" #include "chkname.h" #include "defines.h" diff --git a/src/usermod.c b/src/usermod.c index 702675a9e..7181d47f8 100644 --- a/src/usermod.c +++ b/src/usermod.c @@ -34,7 +34,8 @@ #include "alloc/malloc.h" #include "alloc/x/xmalloc.h" -#include "atoi/a2i.h" +#include "atoi/a2i/a2i.h" +#include "atoi/a2i/a2s.h" #include "atoi/getnum.h" #include "chkname.h" #include "defines.h" diff --git a/tests/unit/Makefile.am b/tests/unit/Makefile.am index c854a15d0..6a83973d7 100644 --- a/tests/unit/Makefile.am +++ b/tests/unit/Makefile.am @@ -35,7 +35,8 @@ test_adds_LDADD = \ $(NULL) test_atoi_strtoi_SOURCES = \ - ../../lib/atoi/strtoi.c \ + ../../lib/atoi/strtoi/strtoi.c \ + ../../lib/atoi/strtoi/strtou_noneg.c \ test_atoi_strtoi.c \ $(NULL) test_atoi_strtoi_CFLAGS = \ diff --git a/tests/unit/test_atoi_strtoi.c b/tests/unit/test_atoi_strtoi.c index 535b6ab74..02d62cf3d 100644 --- a/tests/unit/test_atoi_strtoi.c +++ b/tests/unit/test_atoi_strtoi.c @@ -14,8 +14,8 @@ #include // Required by #include -#include "atoi/strtoi.h" -#include "atoi/strtou_noneg.h" +#include "atoi/strtoi/strtoi.h" +#include "atoi/strtoi/strtou_noneg.h" static void test_strtoi(void **state);