Re: [PATCH v2 1/2] tools/nolibc: Add dirfd()
From: David Laight
Date: Thu Oct 08 2026 - 03:59:39 EST
On Wed, 7 Oct 2026 14:29:12 +0900
Daniel Palmer <daniel@xxxxxxxxx> wrote:
> This is useful for things like fstatat() on children of a directory
> opened with opendir().
>
> At the same time use the new internal helper to replace the two places
> that are converting the DIR pointer back into an fd.
>
> Signed-off-by: Daniel Palmer <daniel@xxxxxxxxx>
> ---
> tools/include/nolibc/dirent.h | 38 +++++++++++++++++++++++++++--------
> 1 file changed, 30 insertions(+), 8 deletions(-)
>
> diff --git a/tools/include/nolibc/dirent.h b/tools/include/nolibc/dirent.h
> index 4e02ef25e72d..d282ea8eb5d3 100644
> --- a/tools/include/nolibc/dirent.h
> +++ b/tools/include/nolibc/dirent.h
> @@ -27,6 +27,18 @@ typedef struct {
> char dummy[1];
> } DIR;
>
> +/* Internal dirfd() that does not set errno */
> +static __attribute__((unused))
> +int __nolibc_dirfd(DIR *dirp)
> +{
> + intptr_t i = (intptr_t)dirp;
> +
> + if (i >= 0)
> + return -1;
> +
> + return ~i;
Might be better to use a different name for the variable.
The two return statements look the same at a first glance.
David