tech-misc archive

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index][Old Index]

Re: alx-0008 - Standardize strtoi(3) and strtou(3) from NetBSD



Hi Thorsten,

On Thu, Mar 20, 2025 at 12:52:07AM +0100, Thorsten Glaser wrote:
> On Thu, 20 Mar 2025, Alejandro Colomar wrote:
> 
> >> At the same time, disallowing a leading '-' sign
> >> is a benefit as well. I consider it a misfeature that strtoul() parses
> >> "-3" successfully and returns ULONG_MAX-2, which was most certainly
> >> not intended by the user.
> >
> >Agree; it is a misfeature.
> 
> What?
> 
> From a user’s PoV, this is hugely useful, and many other
> read-unsigned-integer-value routines handle this similarily
> (of course using whatever range they have) and C also defines
> this, so from an implementor’s PoV this is no trouble.

Can you clarify how this is useful as a programmer?  I have replaced
*all* calls to strtoul(3) et al. in shadow-utils by my strtou_noneg(),
and never ever saw a valid use case of that feature.

> The other user’s PoV thing would be to allow 0x prefixing,
> but that needs an entire duplication of the inner loop, so
> I can see why people would want to exclude that.

I don't understand what you mean.  strtou(3) supports 0x strings as long
as you specify 0 or 16 as the base.

	alx@devuan:~/tmp$ cat hex.c 
	#include <bsd/inttypes.h>
	#include <stdio.h>

	int
	main(void)
	{
		int   status, n;
		char  *end;

		n = strtou("0xF", &end, 0, 0, 1000, &status);

		printf("%d\n", n);
		printf("%s\n", end);
		printf("%d\n", status);

		n = strtou("0xF", &end, 16, 0, 1000, &status);

		printf("%d\n", n);
		printf("%s\n", end);
		printf("%d\n", status);
	}
	alx@devuan:~/tmp$ gcc -Wall -Wextra hex.c -lbsd
	alx@devuan:~/tmp$ ./a.out 
	15

	0
	15

	0


Have a lovely night!
Alex

-- 
<https://www.alejandro-colomar.es/>

Attachment: signature.asc
Description: PGP signature



Home | Main Index | Thread Index | Old Index