dev_t 和 ino_t 必须是整数类型吗?

Are dev_t and ino_t required to be integer types?

glibc 的文档保持它们是整数类型(不比 unsigned int 窄),但我没有找到说它们必须是整数类型的标准参考(另请参见 time_t)。

所以最后问题就变成了:是

#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);
}

便携。

来自current POSIX specifications

dev_t shall be an integer type.

[...]

ino_t shall be defined as unsigned integer types

POSIX 标准要求 dev_t 是整数类型,ino_t 是无符号整数。

dev_t shall be an integer type.

fsblkcnt_t, fsfilcnt_t, and ino_t shall be defined as unsigned integer types.

由于 intmax_tuintmax_t 应该是 "greatest width" 整数,您的代码是安全的。 为了确保万一 st_dev 恰好为负数,您可以将其写为:

    printf("%jd %ju\n", (intmax_t)st.st_dev, (uintmax_t)st.st_ino);

否则,您的代码是安全的。