From a0d4ea3bece0e781fb9e0b5facb074744a9a0479 Mon Sep 17 00:00:00 2001 From: Alejandro Colomar Date: Sat, 16 Nov 2024 15:15:32 +0100 Subject: [PATCH] lib/string/strspn/, lib/, src/: stprspn(), strrspn_(): Split API into function and macro This provides a safer and more consistent API. We had the strrspn(3) function as it was for compatibility with Oracle Solaris, but let's not repeat their mistake. Nevertheless, name our function strrspn_() with a trailing underscore, to differentiate it from the one in Solaris, since it's slightly different. Signed-off-by: Alejandro Colomar --- lib/Makefile.am | 2 ++ lib/fields.c | 4 ++-- lib/getdef.c | 4 ++-- lib/string/strspn/stprspn.c | 7 +++++++ lib/string/strspn/stprspn.h | 26 ++++++++++++++++++++++++++ lib/string/strspn/strrspn.c | 4 +++- lib/string/strspn/strrspn.h | 14 +++++++------- src/login_nopam.c | 4 ++-- src/suauth.c | 4 ++-- 9 files changed, 53 insertions(+), 16 deletions(-) create mode 100644 lib/string/strspn/stprspn.c create mode 100644 lib/string/strspn/stprspn.h diff --git a/lib/Makefile.am b/lib/Makefile.am index 4bf598295..fc39e0179 100644 --- a/lib/Makefile.am +++ b/lib/Makefile.am @@ -224,6 +224,8 @@ libshadow_la_SOURCES = \ string/strspn/stpspn.h \ string/strspn/stprcspn.c \ string/strspn/stprcspn.h \ + string/strspn/stprspn.c \ + string/strspn/stprspn.h \ string/strspn/strrcspn.c \ string/strspn/strrcspn.h \ string/strspn/strrspn.c \ diff --git a/lib/fields.c b/lib/fields.c index 37e9af5f4..9e08adadf 100644 --- a/lib/fields.c +++ b/lib/fields.c @@ -18,7 +18,7 @@ #include "prototypes.h" #include "string/strcmp/streq.h" #include "string/strspn/stpspn.h" -#include "string/strspn/strrspn.h" +#include "string/strspn/stprspn.h" #include "string/strtok/stpsep.h" @@ -93,7 +93,7 @@ change_field(char *buf, size_t maxsize, const char *prompt) * makes it possible to change the field to empty, by * entering a space. --marekm */ - stpcpy(strrspn(newf, " \t"), ""); + stpcpy(stprspn(newf, " \t"), ""); cp = stpspn(newf, " \t"); strcpy (buf, cp); } diff --git a/lib/getdef.c b/lib/getdef.c index 5f4708611..e354d8976 100644 --- a/lib/getdef.c +++ b/lib/getdef.c @@ -33,7 +33,7 @@ #include "string/strcmp/strcaseeq.h" #include "string/strcmp/streq.h" #include "string/strspn/stpspn.h" -#include "string/strspn/strrspn.h" +#include "string/strspn/stprspn.h" #include "string/strtok/stpsep.h" @@ -562,7 +562,7 @@ static void def_load (void) /* * Trim trailing whitespace. */ - stpcpy(strrspn(buf, " \t\n"), ""); + stpcpy(stprspn(buf, " \t\n"), ""); /* * Break the line into two fields. diff --git a/lib/string/strspn/stprspn.c b/lib/string/strspn/stprspn.c new file mode 100644 index 000000000..c7450e113 --- /dev/null +++ b/lib/string/strspn/stprspn.c @@ -0,0 +1,7 @@ +// SPDX-FileCopyrightText: 2024, Alejandro Colomar +// SPDX-License-Identifier: BSD-3-Clause + + +#include + +#include "string/strspn/stprspn.h" diff --git a/lib/string/strspn/stprspn.h b/lib/string/strspn/stprspn.h new file mode 100644 index 000000000..0cdce632f --- /dev/null +++ b/lib/string/strspn/stprspn.h @@ -0,0 +1,26 @@ +// SPDX-FileCopyrightText: 2024, Alejandro Colomar +// SPDX-License-Identifier: BSD-3-Clause + + +#ifndef SHADOW_INCLUDE_LIB_STRING_STRSPN_STPRSPN_H_ +#define SHADOW_INCLUDE_LIB_STRING_STRSPN_STPRSPN_H_ + + +#include + +#include + +#include "string/strspn/strrspn.h" + + +// Available in Oracle Solaris as strrspn(3GEN). +// +#define stprspn(s, accept) \ +({ \ + __auto_type s_ = (s); \ + \ + s_ + strrspn_(s_, accept); \ +}) + + +#endif // include guard diff --git a/lib/string/strspn/strrspn.c b/lib/string/strspn/strrspn.c index e7cfca002..a8fbeff1c 100644 --- a/lib/string/strspn/strrspn.c +++ b/lib/string/strspn/strrspn.c @@ -6,5 +6,7 @@ #include "string/strspn/strrspn.h" +#include -extern inline char *strrspn(char *restrict s, const char *restrict accept); + +extern inline size_t strrspn_(const char *s, const char *accept); diff --git a/lib/string/strspn/strrspn.h b/lib/string/strspn/strrspn.h index b75818b02..ea703ff44 100644 --- a/lib/string/strspn/strrspn.h +++ b/lib/string/strspn/strrspn.h @@ -8,20 +8,20 @@ #include +#include #include #include "attr.h" #include "string/strchr/strnul.h" +ATTR_STRING(1) ATTR_STRING(2) -inline char *strrspn(char *restrict s, const char *restrict accept); +inline size_t strrspn_(const char *s, const char *accept); -// Available in Oracle Solaris: strrspn(3GEN). -// -inline char * -strrspn(char *restrict s, const char *restrict accept) +inline size_t +strrspn_(const char *s, const char *accept) { char *p; @@ -29,9 +29,9 @@ strrspn(char *restrict s, const char *restrict accept) while (p > s) { p--; if (strchr(accept, *p) == NULL) - return p + 1; + return p + 1 - s; } - return s; + return 0; } diff --git a/src/login_nopam.c b/src/login_nopam.c index bc77525d9..b4f637baf 100644 --- a/src/login_nopam.c +++ b/src/login_nopam.c @@ -61,7 +61,7 @@ #include "sizeof.h" #include "string/strcmp/strcaseeq.h" #include "string/strcmp/streq.h" -#include "string/strspn/strrspn.h" +#include "string/strspn/stprspn.h" #include "string/strtok/stpsep.h" @@ -112,7 +112,7 @@ login_access(const char *user, const char *from) if (line[0] == '#') { continue; /* comment line */ } - stpcpy(strrspn(line, " \t"), ""); + stpcpy(stprspn(line, " \t"), ""); if (streq(line, "")) { /* skip blank lines */ continue; } diff --git a/src/suauth.c b/src/suauth.c index 9b539ceb1..cd2f87b65 100644 --- a/src/suauth.c +++ b/src/suauth.c @@ -21,7 +21,7 @@ #include "prototypes.h" #include "string/strcmp/streq.h" #include "string/strspn/stpspn.h" -#include "string/strspn/strrspn.h" +#include "string/strspn/stprspn.h" #include "string/strtok/stpsep.h" @@ -84,7 +84,7 @@ check_su_auth(const char *actual_id, const char *wanted_id, bool su_to_root) continue; } - stpcpy(strrspn(temp, " \t"), ""); + stpcpy(stprspn(temp, " \t"), ""); p = stpspn(temp, " \t"); if (*p == '#' || streq(p, "")) -- 2.47.2