问题
The documentation for glibc stays they are integer types (no narrower than unsigned int), but I'm not finding a standards reference that says they have to be an integer type (see also time_t).
So in the end, the question becomes: Is
#include <stdio.h>
#include <stdint.h>
struct stat st;
if (stat("somefile", &st) == 0) {
printf("%ju %ju\n", (uintmax_t)st.st_dev, (uintmax_t)st.st_ino);
}
portable.
回答1:
POSIX standard requires dev_t
to be an integer type and ino_t
to be an unsigned integer.
dev_t shall be an integer type.
fsblkcnt_t, fsfilcnt_t, and ino_t shall be defined as unsigned integer types.
Since intmax_t
and uintmax_t
are supposed to be the "greatest width" integers, your code is safe.
Just to be sure in case st_dev
happens to be negative, you could write it as:
printf("%jd %ju\n", (intmax_t)st.st_dev, (uintmax_t)st.st_ino);
Otherwise, your code is safe.
回答2:
From the current POSIX specifications:
dev_t shall be an integer type.
[...]
ino_t shall be defined as unsigned integer types
来源:https://stackoverflow.com/questions/48169603/are-dev-t-and-ino-t-required-to-be-integer-types