在 CentOS 系统中,readdir 函数用于读取目录中的条目。默认情况下,readdir 会返回目录中的所有条目,包括符号链接。但是,如果你想要对符号链接进行特殊处理,例如不跟随符号链接,你可以使用 readdir 结合其他函数来实现。
以下是一个简单的示例,展示了如何在 CentOS 系统中使用 readdir 函数读取目录条目,并检查它们是否为符号链接:
#include <stdio.h> #include <stdlib.h> #include <dirent.h> #include <sys/stat.h> #include <unistd.h> int main(int argc, char *argv[]) { if (argc != 2) { fprintf(stderr, "Usage: %s <directory>\n", argv[0]); return 1; } DIR *dir = opendir(argv[1]); if (dir == NULL) { perror("opendir"); return 1; } struct dirent *entry; while ((entry = readdir(dir)) != NULL) { struct stat statbuf; char path[PATH_MAX]; snprintf(path, sizeof(path), "%s/%s", argv[1], entry->d_name); if (lstat(path, &statbuf) == -1) { perror("lstat"); continue; } if (S_ISLNK(statbuf.st_mode)) { printf("%s is a symbolic link\n", entry->d_name); } else { printf("%s is not a symbolic link\n", entry->d_name); } } closedir(dir); return 0; }  在这个示例中,我们首先使用 opendir 打开目录,然后使用 readdir 读取目录中的每个条目。对于每个条目,我们使用 lstat 函数获取文件的状态信息,而不是 stat 函数。lstat 函数会返回符号链接本身的信息,而不是跟随符号链接。通过检查 statbuf.st_mode 是否包含 S_IFLNK 标志,我们可以判断一个条目是否为符号链接。
编译并运行这个程序,它将输出目录中每个条目是否为符号链接。