]> git.ipfire.org Git - thirdparty/shadow.git/commit
lib/atoi/: a2i(): Re-implement with a statement expression
authorAlejandro Colomar <alx@kernel.org>
Thu, 16 Oct 2025 23:36:03 +0000 (01:36 +0200)
committerSerge Hallyn <serge@hallyn.com>
Fri, 28 Nov 2025 02:23:43 +0000 (20:23 -0600)
commitee575de9aecfc5775a800c98b9ed40f65ae39633
treee6b842ade39d0ecddd0df7238eb73b4241138804
parentf382a7cfb36417acfeb783664ef7bd828d503599
lib/atoi/: a2i(): Re-implement with a statement expression

Synopsis
int a2i(typename T, T *restrict n, QChar *s,
        QChar **_Nullable restrict endp, int base,
        T min, T max);

Description
This macro converts the initial portion of the string pointed to
by 's' to an integer of base 'base', ensure that the number is
in the range [min, max], and store it in *n.

It is similar to NetBSD's strtoi(3) and strtou(3), which
themselves are similar to strtol(3) and strtoul(3).

Arguments
T
The integer type used for the number.
n
A pointer to an integer.  The parsed number will be
stored there.
s
See strtol(3).
endp
See strtol(3).  A difference with strtol(3) is that this
macro is const-correct.  If 's' has type 'const char *',
then 'endp' must have type 'const char **', whereas if
's' has type 'char *', 'endp' must have type 'char **'.
base
See strtol(3).
min
max
See strtoi(3) and strtou(3).

An important difference with NetBSD's strtou(3) is that
a2i() (with an unsigned type T) doesn't intepret any
negative numbers as if they were large positive numbers.
a2i() respects the limits [min, max] as one would
intuitively expect.

Return value
On success, 0 is returned.
On error, -1 is returned and errno is set to indicate the error.

Errors
See strtoi(3) and strtou(3).

Examples
if (a2i(pid_t, &pid, s, &s, 10, 1, _Maxof(pid_t)) == -1)
goto err;

Signed-off-by: Alejandro Colomar <alx@kernel.org>
lib/atoi/a2i/a2i.h
lib/atoi/a2i/a2s_c.c [deleted file]
lib/atoi/a2i/a2s_c.h [deleted file]
lib/atoi/a2i/a2s_nc.c [deleted file]
lib/atoi/a2i/a2s_nc.h [deleted file]
lib/atoi/a2i/a2u_c.c [deleted file]
lib/atoi/a2i/a2u_c.h [deleted file]
lib/atoi/a2i/a2u_nc.c [deleted file]
lib/atoi/a2i/a2u_nc.h [deleted file]