gpt4 book ai didi

c - 如何使用 S_ISREG() 和 S_ISDIR() POSIX 宏?

转载 作者:太空狗 更新时间:2023-10-29 16:43:30 27 4
gpt4 key购买 nike

这是我编写的一个 C 程序,用于递归导航和输出目录和常规文件。它在我的 Linux 机器上编译和运行良好。但是在 Solaris 上,dit->d_type == 8 检查和其他类似的检查不起作用,因为没有 d_type 字段。我读到的这个问题的答案是使用 S_ISREG()S_ISDIR() 宏,但它们根本无法按照我的方式工作我目前的代码。我注释掉了适用于我的 Linux 机器的行。

#include <sys/types.h>
#include <sys/stat.h>
#include <stdlib.h>
#include <dirent.h>
#include <stdio.h>
#include <unistd.h>
#include <errno.h>
#include <string.h>

void helper(DIR *, struct dirent *, struct stat, char *, int, char **);
void dircheck(DIR *, struct dirent *, struct stat, char *, int, char **);

int main(int argc, char *argv[]){

DIR *dip;
struct dirent *dit;
struct stat statbuf;
char currentPath[FILENAME_MAX];
int depth = 0; /*Used to correctly space output*/

/*Open Current Directory*/
if((dip = opendir(".")) == NULL)
return errno;

/*Store Current Working Directory in currentPath*/
if((getcwd(currentPath, FILENAME_MAX)) == NULL)
return errno;

/*Read all items in directory*/
while((dit = readdir(dip)) != NULL){
/*Skips . and ..*/
if(strcmp(dit->d_name, ".") == 0 || strcmp(dit->d_name, "..") == 0)
continue;

if(stat(currentPath, &statbuf) == -1){
perror("stat");
return errno;
}

/*Checks if current item is of the type file (type 8) and no command line arguments
if(dit->d_type == 8 && argv[1] == NULL)*/
if(S_ISREG(statbuf.st_mode) && argv[1] == NULL)
printf("%s (%d bytes)\n", dit->d_name, (int)statbuf.st_size);

/*If a command line argument is given, checks for filename match
if(dit->d_type == 8 && argv[1] != NULL)*/
if(S_ISREG(statbuf.st_mode) && argv[1] != NULL)
if(strcmp(dit->d_name, argv[1]) == 0)
printf("%s (%d bytes)\n", dit->d_name, (int)statbuf.st_size);

/*Checks if current item is of the type directory (type 4)
if(dit->d_type == 4)*/
if(S_ISDIR(statbuf.st_mode))
dircheck(dip, dit, statbuf, currentPath, depth, argv);

}
closedir(dip);
return 0;
}

/*Recursively called helper function*/
void helper(DIR *dip, struct dirent *dit, struct stat statbuf,
char currentPath[FILENAME_MAX], int depth, char *argv[]){
int i = 0;

if((dip = opendir(currentPath)) == NULL)
printf("Error: Failed to open Directory ==> %s\n", currentPath);

while((dit = readdir(dip)) != NULL){

if(strcmp(dit->d_name, ".") == 0 || strcmp(dit->d_name, "..") == 0)
continue;

stat(currentPath, &statbuf);

/*if(dit->d_type == 8 && argv[1] == NULL){*/
if(S_ISREG(statbuf.st_mode) && argv[1] == NULL){
for(i = 0; i < depth; i++)
printf(" ");
printf("%s (%d bytes)\n", dit->d_name, (int)statbuf.st_size);
}

/*if(dit->d_type == 8 && argv[1] != NULL){*/
if(S_ISREG(statbuf.st_mode) && argv[1] != NULL){
if(strcmp(dit->d_name, argv[1]) == 0){
for(i = 0; i < depth; i++)
printf(" ");
printf("%s (%d bytes)\n", dit->d_name, (int)statbuf.st_size);
}
}

/*if(dit->d_type == 4)*/
if(S_ISDIR(statbuf.st_mode))
dircheck(dip, dit, statbuf, currentPath, depth, argv);

}
}

void dircheck(DIR *dip, struct dirent *dit, struct stat statbuf,
char currentPath[FILENAME_MAX], int depth, char *argv[]){
int i = 0;

strcat(currentPath, "/");
strcat(currentPath, dit->d_name);

/*If two directories exist at the same level the path
is built wrong and needs to be corrected*/
if((chdir(currentPath)) == -1){
chdir("..");
getcwd(currentPath, FILENAME_MAX);
strcat(currentPath, "/");
strcat(currentPath, dit->d_name);

for(i = 0; i < depth; i++)
printf (" ");
printf("%s (subdirectory)\n", dit->d_name);
depth++;
helper(dip, dit, statbuf, currentPath, depth, argv);
}

else{
for(i =0; i < depth; i++)
printf(" ");
printf("%s (subdirectory)\n", dit->d_name);
chdir(currentPath);
depth++;
helper(dip, dit, statbuf, currentPath, depth, argv);
}

}

最佳答案

您正在使用 S_ISREG()S_ISDIR()正确,你只是在错误的事情上使用它们。

在你的while((dit = readdir(dip)) != NULL)循环 main , 你调用 statcurrentPath一遍遍不改currentPath :

if(stat(currentPath, &statbuf) == -1) {
perror("stat");
return errno;
}

你不应该附加一个斜线和dit->d_name吗?至 currentPath获取您想要的文件的完整路径 stat ?我认为与您的其他人有类似的变化 stat也需要调用。

关于c - 如何使用 S_ISREG() 和 S_ISDIR() POSIX 宏?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4989431/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com