Are dev_t and ino_t required to be integer types?

老子叫甜甜 提交于 2020-12-08 06:39:50

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!