[PATCH v2 11/24] stat.2: Cast to 'unsigned long' rather than 'long' when printing with "%lx"

Alejandro Colomar
2020-09-11T09:16:18Z

Hi Michael,

On 2020-09-11 09:25, Michael Kerrisk (man-pages) wrote:
> See my reply to patch 10/24.

As with 10/24, here's the new version.

Cheers,

Alex

--------------------------------------------------------
From 911c791f0168851cdfdb30a65b6935011e4a161c Mon Sep 17 00:00:00 2001
From: Alejandro Colomar <colomar.6.4.3@gmail.com>
Date: Fri, 11 Sep 2020 10:52:14 +0200
Subject: [PATCH v2 11/24] stat.2: Cast to 'unsigned long' rather than 'long'
when printing with "%lx"

From the email conversation:

On 2020-09-11 09:24, Michael Kerrisk (man-pages) wrote:
> Hi Alex,
>
> On 9/10/20 11:13 PM, Alejandro Colomar wrote:
>> Both major(3) and minor(3) return an 'unsigned int',
>> so there is no need to use a 'long' for printing.
>> Moreover, it should have been 'unsigned long',
>> as "%lx" expects an unsigned type.
>
> This may be true on Linux, but is not true on other systems.
> For example, on HP-UX, according to one header file I'm
> looking at, the return value is 'long'.
>
> These kinds of casts are intended to improve code portability
> across UNIX implementations, so I think they should stay
> (although, I do wonder if they would be even better as casts
> to 'unsigned long')
>
> Thanks,
>
> Michael

Signed-off-by: Alejandro Colomar <colomar.6.4.3@gmail.com>
---
man2/stat.2 | 3 ++-
1 file changed, 2 insertions(+), 1 deletion(-)

diff --git a/man2/stat.2 b/man2/stat.2
index b35e3c615..08af24c56 100644
--- a/man2/stat.2
+++ b/man2/stat.2
@@ -665,7 +665,8 @@ main(int argc, char *argv[])
}

printf("ID of containing device: [%lx,%lx]\en",
- (long) major(sb.st_dev), (long) minor(sb.st_dev));
+ (unsigned long) major(sb.st_dev),
+ (unsigned long) minor(sb.st_dev));

printf("File type: ");

--
2.28.0