C程序列出隐藏文件和只读文件

标签 c linux

我正在创建一个列出目录内容的简单 C 程序。有谁知道如何只列出隐藏的?以下代码从目录中提取每个文件并且工作完美,但我只需要隐藏文件。谢谢。

最佳答案

在 GNU/Linux 上,隐藏文件以点开头。

#include <string.h>

int is_hidden(const char *name)
{
  return name[0] == '.' &&
         strcmp(name, ".") != 0 &&
         strcmp(name, "..") != 0);
}

要检查文件是否为只读,使用 stat 函数可能是个好主意。

#include <sys/types.h>
#include <sys/stat.h>
#include <unistd.h>

int is_readonly(const char *name)
{
  struct stat buf;

  stat(name, &buf);

  return buf->st_mode & /* ... */;
}

关于C程序列出隐藏文件和只读文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14663548/

相关文章:

c - 使用 glib 和 VS C++ 无法解析的外部符号

c++ - 使用 GDI 创建透明位图?

linux - 用于连接 SSH Ubuntu 18.04 的多个 key 。如何使用?

php - 我怎样才能在 linux 中进行非对称 cron 作业?

linux - 如何从命令行计算程序或脚本的执行时间?

c - realloc 如何处理使用 calloc 分配的内存?

c - 父子进程计时

c - 用于求平方根的最快汇编代码。需要解释

linux - linux中python的可执行路径错误

我可以将 GCC 作为守护进程运行(或将其用作库)吗?