c - 在C中检测文件系统

标签 c linux

有没有办法判断一个文件是在本地磁盘还是在 C 中的 NFS?代码应该可以跨各种 Linux 发行版移植,并且不应依赖于系统调用(例如 stat -f)。

最佳答案

您想使用 statfs 来自 <sys/vfs.h> .

int statfs(const char *path, struct statfs *buf);

struct statfs {
    __SWORD_TYPE f_type;    /* type of file system (see below) */

使用方法如下:

struct statfs s;
if (statfs("/etc", &s))
    perror("statfs");

switch (s->f_type) {
case EXT2_SUPER_MAGIC:
    break;
case EXT3_SUPER_MAGIC:
    break;
default:
    break;
}

还有:

  • 你混淆了“外部命令”和“系统调用”。它们是非常不同的东西
  • stat(1) 命令在 Linux 发行版之间非常可移植。

关于c - 在C中检测文件系统,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8389482/

相关文章:

c++ - 没有分号的程序在 C 中编译得很好,而不是在 C++ 中为什么?

c++ - 展开 C 或 C++ 源文件中的单个宏

linux - 我有几个 EMF 文件。我如何在 Linux 上将它们转换为 ps/pdf/tiff?

python - 在集群上使用 Python、Cython 和 GSL

mysql - 在c中调用MySQL存储过程,传递变量参数

c++ - 在 `return 0` 末尾设置 `main` 可选的理由是什么?

linux - 打开文件过多错误,但 99.5% 的 inode 是免费的

linux - 时差bash脚本

c - printf 的意外输出

linux - 用于过滤掉日志中不相邻的重复项的 Bash 脚本