72 Commits

Author SHA1 Message Date
Tobias Stoeckmann
876e874684 Fix documentation style
Always start a sentence with lowercase letter after 'Note:', 'Warning:',
etc. This unifies all occurrences.

No functional change.

Signed-off-by: Tobias Stoeckmann <tobias@stoeckmann.org>
2025-12-19 16:48:08 +01:00
Tobias Stoeckmann
5369ad7566 lib/, src/: Fix formatting
Fix places where spaces were used instead of tabs.

No functional change.

Signed-off-by: Tobias Stoeckmann <tobias@stoeckmann.org>
2025-12-16 23:20:32 +01:00
Alejandro Colomar
0afe216953 lib/: Rename MALLOC() => malloc_T()
The 'T' in the name notes that this API is a type-safe variant of the
API it wraps.  This makes the names more explicative.

Signed-off-by: Alejandro Colomar <alx@kernel.org>
2025-12-05 21:22:45 -06:00
Alejandro Colomar
68346aa406 lib/string/strspn/: Add missing const
Closes: <https://github.com/shadow-maint/shadow/issues/1218>
Reported-by: Chris Hofstaedtler <zeha@debian.org>
Signed-off-by: Alejandro Colomar <alx@kernel.org>
2025-12-04 20:35:51 -06:00
Alejandro Colomar
910f54c083 lib/string/: strerrno(): Use statement expression to perform lvalue conversion
Compound literals are lvalues.  This means it's possible to take their
address.  That is, it would be possible (albeit nonsensical) to do

	&strerrno();

It is also possible to assign to them (albeit also nonsensical):

	strerrno() = NULL;

The statement expression performs lvalue conversion, which turns the
lvalue into an "rvalue", as expected, and disallows all those issues.

Signed-off-by: Alejandro Colomar <alx@kernel.org>
2025-11-27 21:26:49 -06:00
Alejandro Colomar
4f523497df lib/string/README: Document *_a() macros
Signed-off-by: Alejandro Colomar <alx@kernel.org>
2025-11-27 20:50:48 -06:00
Alejandro Colomar
0b75eb75e5 */: s/STRNEQ/strneq_a/
Signed-off-by: Alejandro Colomar <alx@kernel.org>
2025-11-27 20:50:48 -06:00
Alejandro Colomar
af3f3202d0 */: s/STRSEP2ARR/strsep2arr_a/
This name better reflects that it handles arrays, and doesn't shout.

This case is slightly different, as this macro does a little bit more
than just enforcing arrays.  It changes the return value too.  However,
that is related-enough to the handling of arrays that I'm inclined to
accept it as a minor inconsistency.

Signed-off-by: Alejandro Colomar <alx@kernel.org>
2025-11-27 20:50:48 -06:00
Alejandro Colomar
a2207ea60a */: s/STRSEP2LS/strsep2ls_a/
This name better reflects that it handles arrays, and doesn't shout.

Signed-off-by: Alejandro Colomar <alx@kernel.org>
2025-11-27 20:50:48 -06:00
Alejandro Colomar
c1c685b5ff */: s/STRFTIME/strftime_a/
This name better reflects that it handles arrays, and doesn't shout.

Signed-off-by: Alejandro Colomar <alx@kernel.org>
2025-11-27 20:50:48 -06:00
Alejandro Colomar
cd3a5077b0 */: s/MEMZERO/memzero_a/
This name better reflects that it handles arrays, and doesn't shout.

Signed-off-by: Alejandro Colomar <alx@kernel.org>
2025-11-27 20:50:48 -06:00
Alejandro Colomar
fb9f7f51a8 */: s/STRTCPY/strtcpy_a/
This name better reflects that it handles arrays, and doesn't shout.

Signed-off-by: Alejandro Colomar <alx@kernel.org>
2025-11-27 20:50:48 -06:00
Alejandro Colomar
d13bb54184 */: s/STRNCPY/strncpy_a/
This name better reflects that it handles arrays, and doesn't shout.

Signed-off-by: Alejandro Colomar <alx@kernel.org>
2025-11-27 20:50:48 -06:00
Alejandro Colomar
4ddc73dbaf */: s/STRNCAT/strncat_a/
This name better reflects that it handles arrays, and doesn't shout.

Signed-off-by: Alejandro Colomar <alx@kernel.org>
2025-11-27 20:50:48 -06:00
Alejandro Colomar
ce044d1791 */: s/SNPRINTF/stprintf_a/
This name better reflects that it handles arrays, and doesn't shout.

Signed-off-by: Alejandro Colomar <alx@kernel.org>
2025-11-27 20:50:48 -06:00
Alejandro Colomar
56df586d1e */: s/STRNDUP/strndup_a/ s/XSTRNDUP/xstrndup_a/
This name better reflects that it handles arrays, and doesn't shout.

Signed-off-by: Alejandro Colomar <alx@kernel.org>
2025-11-27 20:50:48 -06:00
Alejandro Colomar
1c57e03090 */: s/STRNDUPA/strndupa_a/
This name better reflects that it handles arrays, and doesn't shout.

Signed-off-by: Alejandro Colomar <alx@kernel.org>
2025-11-27 20:50:48 -06:00
Alejandro Colomar
b5c5791fe7 */: s/SIZEOF_ARRAY/sizeof_a/
This name better reflects that it handles arrays, and doesn't shout.

Signed-off-by: Alejandro Colomar <alx@kernel.org>
2025-11-27 20:50:48 -06:00
Alejandro Colomar
8f58cc428c lib/string/: strerrno(): Add macro
Signed-off-by: Alejandro Colomar <alx@kernel.org>
2025-11-04 10:11:16 +01:00
Alejandro Colomar
655ffcbe1b lib/, src/, tests/: Move x*() definitions to non-x* header files
Now that all of these are one-liners, they don't need a separate header
file.  Compact stuff.

Signed-off-by: Alejandro Colomar <alx@kernel.org>
2025-10-27 14:32:06 +01:00
Alejandro Colomar
e5b6968406 lib/string/strtok/: xastrsep2ls() Reimplement in terms of exit_if_null()
Signed-off-by: Alejandro Colomar <alx@kernel.org>
2025-10-27 14:32:06 +01:00
Alejandro Colomar
77fd7f66eb lib/string/strdup/xstrndup.h: Add STRNDUP()
Signed-off-by: Alejandro Colomar <alx@kernel.org>
2025-10-27 14:32:06 +01:00
Alejandro Colomar
b14d14a405 lib/string/strdup/: XSTRNDUP(): Reimplement in terms of exit_if_null()
This is much simpler.

Signed-off-by: Alejandro Colomar <alx@kernel.org>
2025-10-27 14:32:06 +01:00
Alejandro Colomar
e645cc0d28 lib/string/sprintf/, tests/: xaprintf(): Reimplement in terms of exit_if_null()
Signed-off-by: Alejandro Colomar <alx@kernel.org>
2025-10-27 14:32:06 +01:00
Alejandro Colomar
64a2a85fb9 lib/string/strdup/: xstrdup(): Reimplement xstrdup() in terms of exit_if_null()
Signed-off-by: Alejandro Colomar <alx@kernel.org>
2025-10-27 14:32:06 +01:00
Alejandro Colomar
7f9f3fa0b1 lib/string/strcmp/: strneq(), STRNEQ(): Add APIs
Signed-off-by: Alejandro Colomar <alx@kernel.org>
2025-10-18 14:08:47 -05:00
Alejandro Colomar
0c9c46a518 lib/string/README: Add guidelines for using strings
Suggested-by: Iker Pedrosa <ipedrosa@redhat.com>
Suggested-by: Serge Hallyn <serge@hallyn.com>
Suggested-by: Evgeny Grin (Karlson2k) <k2k@drgrin.dev>
Suggested-by: Lukas Slebodnik <lslebodn@fedoraproject.org>
Signed-off-by: Alejandro Colomar <alx@kernel.org>
2025-09-22 09:48:39 +02:00
Evgeny Grin (Karlson2k)
eb71706b1c */: Fix including <config.h> as system header
"config.h" is a locally generated header. It must be included as
'#include "config.h"'.
It is already included correctly in some sources files. This commit
unifies the way how "config.h" is included.

Signed-off-by: Evgeny Grin (Karlson2k) <k2k@drgrin.dev>
2025-07-16 14:12:40 +02:00
Alejandro Colomar
422a5e9975 lib/string/strtok/: xastrsep2ls(): Add function
Signed-off-by: Alejandro Colomar <alx@kernel.org>
2025-06-07 16:52:03 +02:00
Alejandro Colomar
e168508b00 lib/string/strtok/: astrsep2ls(): Add function
Signed-off-by: Alejandro Colomar <alx@kernel.org>
2025-06-07 16:52:03 +02:00
Alejandro Colomar
2af9f9d012 lib/string/strtok/: strsep2ls(), STRSEP2LS(): Add APIs
This API set implements another usual loop around strsep(3).

This one implements a loop where we are interested in an arbitrary
number of fields.  For that, a NULL terminator is added at the end.
That is commonly referred to as "list".

Signed-off-by: Alejandro Colomar <alx@kernel.org>
2025-06-07 16:52:03 +02:00
Alejandro Colomar
4e64ef6993 lib/string/strtok/: strsep2arr(), STRSEP2ARR(): Add APIs
This API set implements the usual loop around strsep(3).

This one implements a loop where we are interested in an exact number of
fields.  I'll add another API set, strsep2ls() and STRSEP2LS(), which
will add a NULL terminator, for arbitrary numbers of fields.

Signed-off-by: Alejandro Colomar <alx@kernel.org>
2025-06-07 16:52:03 +02:00
Alejandro Colomar
423fd652b5 lib/string/sprintf/, tests/unit/: Transform x[v]asprintf() into x[v]aprintf()
Wrap [v]aprintf() instead of [v]asprintf(3).

Repurpose x[v]asprintf()'s tests to test x[v]aprintf().

Signed-off-by: Alejandro Colomar <alx@kernel.org>
2025-06-03 23:25:02 -05:00
Alejandro Colomar
5949132308 lib/string/sprintf/: [v]aprintf(): Add functions
These functions are just like [v]asprintf(3), but simpler.

They return the newly allocated memory, which allows us to use the
[[gnu::malloc(free)]] attribute, which enhances static analysis.
They also omit the length, which we don't care about at all.

As a curiosity, Plan9 seems to provide this same API, under the name
smprint(3).

Link: <https://9fans.github.io/plan9port/man/man3/print.html>
Signed-off-by: Alejandro Colomar <alx@kernel.org>
2025-06-03 23:25:02 -05:00
Alejandro Colomar
ba3456c9fd lib/, src/, tests/: Use the standard countof() instead of our NITEMS()
countof() is the name blessed by the C Committee for ISO C2y.
Use it if available, and define it if not.

countof() will be provided by GCC 16 and Clang 21.

This is mostly a scripted change:

	$ grep -rl NITEMS | xargs sed -i s/NITEMS/countof/;

Apart from the scripted changes, I've adjusted white-space alignment,
and of course the definition at "lib/sizeof.h".

Link: <https://www.open-std.org/jtc1/sc22/wg14/www/docs/n3550.pdf#subsubsection.0.6.5.4.5>
Link: <https://www.open-std.org/jtc1/sc22/wg14/www/docs/n3550.pdf#section.0.7.21>
Signed-off-by: Alejandro Colomar <alx@kernel.org>
2025-06-03 09:34:55 -05:00
Alejandro Colomar
a0c8876b9e lib/string/ctype/strchrisascii/: strchriscntrl(): Add function
Signed-off-by: Alejandro Colomar <alx@kernel.org>
2025-06-03 09:04:01 -05:00
Alejandro Colomar
d7031cecd2 lib/string/ctype/strisascii/: strisprint(): Add function
Signed-off-by: Alejandro Colomar <alx@kernel.org>
2025-06-03 09:04:01 -05:00
Alejandro Colomar
93493077a1 lib/string/strcmp/: strcaseprefix(): Add API
Signed-off-by: Alejandro Colomar <alx@kernel.org>
2025-06-02 20:08:57 -05:00
Alejandro Colomar
abafa63be1 lib/string/ctype/strtoascii/: strtolower(): Add API
Signed-off-by: Alejandro Colomar <alx@kernel.org>
2025-05-30 15:32:09 -05:00
Alejandro Colomar
0e7162772d lib/string/strdup/strndupa.h: STRNDUPA(): Simplify implementation
Signed-off-by: Alejandro Colomar <alx@kernel.org>
2025-05-26 20:21:08 -05:00
Alejandro Colomar
ab14486633 lib/string/strdup/strndupa.h: strndupa(3): Add macro
musl doesn't provide strndupa(3).

Signed-off-by: Alejandro Colomar <alx@kernel.org>
2025-05-26 20:21:08 -05:00
Alejandro Colomar
34e9e5a8f5 lib/string/strcmp/: strprefix(): Add API
Signed-off-by: Alejandro Colomar <alx@kernel.org>
2025-05-26 11:29:26 -05:00
Alejandro Colomar
79142cd281 lib/string/ctype/strisascii/: strisdigit(): Add function
Reviewed-by: Serge Hallyn <serge@hallyn.com>
Signed-off-by: Alejandro Colomar <alx@kernel.org>
2025-02-16 16:12:16 -06:00
Alejandro Colomar
3cba5e2e1c lib/string/: Add comments expanding the letter-soup API names
Suggested-by: Serge Hallyn <serge@hallyn.com>
Cc: Chris Bazley <Chris.Bazley@arm.com>
Signed-off-by: Alejandro Colomar <alx@kernel.org>
2025-02-16 13:22:51 -06:00
Alejandro Colomar
a0d4ea3bec 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 <alx@kernel.org>
2025-02-16 13:22:51 -06:00
Alejandro Colomar
872de49dcf lib/string/strspn/, lib/, src/: Move *spn() APIs to separate subdir
This provides a better organization.

Signed-off-by: Alejandro Colomar <alx@kernel.org>
2025-02-16 13:22:51 -06:00
Alejandro Colomar
c2a634efa7 lib/string/strchr/: strrcspn(), stprcspn(): Add function and macro
These APIs are to strrspn(), like strcspn() is to strspn().
They are like strcspn(3), but search from the end of the string.

The function is meant for internal use, and consistency with libc.
The macro is meant for normal use, since it returns a pointer,
which is what algorithms using this need.

See also strspn(3) and strcspn(3).

Signed-off-by: Alejandro Colomar <alx@kernel.org>
2025-02-16 13:22:51 -06:00
Alejandro Colomar
224466f603 lib/string/strcmp/: strcaseeq(): Add function
Signed-off-by: Alejandro Colomar <alx@kernel.org>
2025-02-15 10:26:50 -06:00
Alejandro Colomar
8d42b04c80 lib/string/strchr/: strchrscnt(): Add function
Reviewed-by: Serge Hallyn <serge@hallyn.com>
Signed-off-by: Alejandro Colomar <alx@kernel.org>
2025-01-24 07:58:13 -06:00
Alejandro Colomar
9efce1ac85 lib/string/strchr/: strchrcnt(): Add function
Signed-off-by: Alejandro Colomar <alx@kernel.org>
2024-11-10 23:07:19 -06:00