On Wed, 15 Oct 2025 15:46:12 +1100
David Gibson
On Wed, Oct 15, 2025 at 11:50:53AM +0800, Yumei Huang wrote:
On Wed, Oct 15, 2025 at 7:28 AM David Gibson
wrote: On Tue, Oct 14, 2025 at 03:38:34PM +0800, Yumei Huang wrote:
Signed-off-by: Yumei Huang
[snip] + * @path: Path to the sysctl file + * @fallback: Default value if file can't be read + * + * Return: Parameter value, fallback on failure +*/ +long read_file_long(const char *path, long fallback) +{ + char buf[32]; Rather than just using a semi-arbitrary 32 here, I'd suggest defining a new constant similar to UINT16_STRLEN. Except that's trickier for a type that doesn't have a known fixed width. Pity the C library doesn't have constants for these AFAICT.
I will just define a UINTMAX_STRLEN with (sizeof("2147483647")).
That's not quite right. - It should be INTMAX_STRLEN (signed), UINTMAX would be for the unsigned version - That assumes intmax_t is 32-bit which is probably not the case (it will be 64-bit, maybe even 128-bit on modern systems) - For signed cases, it's the minimum (negative) value that gives the longest possible string (for 32-bit, "-2147483648")
By the way, while it doesn't cover intmax_t explicitly, I think this is a pretty good resource as it covers most architectures supported by the Linux kernel (hence, most architectures we support): https://wiki.debian.org/ArchitectureSpecificsMemo#Summary and judging from intmax_t(3type) I'd say that the sizeof(long double) column tells you how big intmax_t is. Well, at least, that's the page I use to know which architectures I can use to check things when I suspect a type portability bug. That's because 'long double' should always be the biggest "native" data type, that is, excluding __int128 or vectorised / SIMD types such as __m256i. -- Stefano