什么是POSIX?我已阅读维基百科的文章,每次遇到这个词时我都会阅读它.事实是,我从来没有真正理解它是什么.
任何人都可以通过解释"对POSIX的需求"向我解释一下吗?
我想使用nftw遍历C中的目录结构.
但是,考虑到我想要做的事情,我没有看到使用全局变量的方法.
使用(n)ftw的教科书示例都涉及执行类似打印文件名的操作.相反,我希望获取路径名和文件校验和并将它们放在数据结构中.但考虑到可以传递给nftw的限制,我没有看到一个很好的方法.
我正在使用的解决方案涉及一个全局变量.然后,nftw调用的函数可以访问该变量并添加所需的数据.
没有使用全局变量有没有合理的方法呢?
以下C代码将列出文件和目录的数量,并且比linux find命令快4倍。我只需要文件夹的数量,对文件数量甚至列出它们都不感兴趣。有没有一种方法可以优化以下代码并使之更高效?
#include <errno.h>
#include <stdio.h>
#include <string.h>
#include <sys/types.h>
#include <unistd.h>
#include <dirent.h>
void listdir(char *path, size_t size) {
DIR *dir;
struct dirent *entry;
size_t len = strlen(path);
if (!(dir = opendir(path))) {
fprintf(stderr, "path not found: %s: %s\n",
path, strerror(errno));
return;
}
puts(path);
while ((entry = readdir(dir)) != NULL) {
char *name = entry->d_name;
if (entry->d_type == DT_DIR) {
if (!strcmp(name, ".") || !strcmp(name, ".."))
continue;
if (len + strlen(name) + 2 > size) {
fprintf(stderr, "path too …Run Code Online (Sandbox Code Playgroud)