c - 使用 Stat 和 OpenDir()/ReadDir() 在 C 语言中列出当前目录和文件类型(Dir、Reg、Lnk)

标签 c linux stat opendir

您好,感谢阅读。

我正在尝试用 C 构建一个在 Linux Shell 中运行的程序,它接受一个参数(目录)并列出该目录中的所有文件,以及它们是常规文件、链接文件还是目录.我需要使用 OpenDir()/ReadDir() 和 stat。到目前为止,这是我的代码:(我有点卡住了)目前它只读取一个文件并输出类型。我如何允许我的程序读取目录中的所有文件,并输出它们的名称以及它们是什么类型的文件?

 #define _BSD_SOURCE
 #include <sys/types.h>
 #include <sys/stat.h>
 #include <time.h>
 #include <stdio.h>
 #include <stdlib.h>
 #include <stdio.h>
 #include <stdlib.h>
 #include <string.h>
 #include <sys/types.h>
 #include <sys/stat.h>
 #include <unistd.h>
 #include <dirent.h>
 #include <time.h>

   int
   main(int argc, char *argv[])
   {
       struct stat sb;

    /*
       if (argc != 2) {
           fprintf(stderr, "Usage: %s <pathname>\n", argv[0]);
           exit(EXIT_FAILURE);
       }*/

       if (stat(argv[1], &sb) == -1) {
           perror("stat");
           exit(EXIT_FAILURE);
       }



       switch (sb.st_mode & S_IFMT) {
       case S_IFBLK:  printf("block device\n");            break;
       case S_IFCHR:  printf("character device\n");        break;
       case S_IFDIR:  printf("dir ");                      break;
       case S_IFIFO:  printf("FIFO/pipe\n");               break;
       case S_IFLNK:  printf("lnk ");                      break;
       case S_IFREG:  printf("reg ");                      break;
       case S_IFSOCK: printf("socket\n");                  break;
       default:       printf("unknown?\n");                break;
       }

       printf("%s\n", argv[1]);

       exit(EXIT_SUCCESS);
   }

所以我重写了所有内容,现在我收到一些关于它是链接 reg 还是 dir 文件的输出,但是我只收到一个文件是目录,即使它显然不是(文本文件或 . c 文件等)。这是新代码:

#define _BSD_SOURCE
#include <stdio.h>
#include <dirent.h>
#include <sys/stat.h>
  #include <sys/types.h>
       #include <time.h>
       #include <stdio.h>
       #include <stdlib.h>


void typeOfFile(struct stat info){

    if (S_ISREG(info.st_mode)) {
        printf("reg");
    }
    if (S_ISDIR(info.st_mode)) {
            printf("dir");
    }
    if (S_ISLNK(info.st_mode)) {
                printf("lnk");
    }

}

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

    DIR *dirp;
    struct dirent* dent;
    struct stat info;







typeOfFile(info);

//If no args
    if(argc == 1){

        argv[1] = ".";  
            dirp=opendir(argv[1]); // specify directory here: "." is the "current directory"
            do {
                dent = readdir(dirp);
                if (dent)
                {
//////////////////////////////////////////////////////////////////////////
                    if (stat(argv[1], &info) == -1) {
               perror("stat");
               exit(EXIT_FAILURE);
           }



           switch (info.st_mode & S_IFMT) {
           case S_IFBLK:  printf("block device\n");            break;
           case S_IFCHR:  printf("character device\n");        break;
           case S_IFDIR:  printf("dir ");                      break;
           case S_IFIFO:  printf("FIFO/pipe\n");               break;
           case S_IFLNK:  printf("lnk ");                      break;
           case S_IFREG:  printf("reg ");                      break;
           case S_IFSOCK: printf("socket\n");                  break;
           default:       printf("unknown?\n");                break;
           }


//////////////////////////////////////////////////////////////////////////
                    printf("%s \n", dent->d_name);

                }
            } while (dent);
            closedir(dirp);

       }



////////////////////////////////////////////////////////////////////////////////////////////////
//If specified directory    
    if(argc > 1){
        dirp=opendir(argv[1]); // specify directory here: "." is the "current directory"
        do {
            dent = readdir(dirp);
            if (dent)
            {

/////////////////////////////////////////////////////////////////////////////////////           
                if (stat(argv[1], &info) == -1) {
               perror("stat");
               exit(EXIT_FAILURE);
           }



           switch (info.st_mode & S_IFMT) {
           case S_IFBLK:  printf("block device\n");            break;
           case S_IFCHR:  printf("character device\n");        break;
           case S_IFDIR:  printf("dir ");                      break;
           case S_IFIFO:  printf("FIFO/pipe\n");               break;
           case S_IFLNK:  printf("lnk ");                      break;
           case S_IFREG:  printf("reg ");                      break;
           case S_IFSOCK: printf("socket\n");                  break;
           default:       printf("unknown?\n");                break;
           }
//////////////////////////////////////////////////////////////////////////////////////
//           printf("%s\n", argv[1]);


                printf("%s \n", dent->d_name);

            }
        } while (dent);
        closedir(dirp);

   } 
return 0;    
}

最佳答案

您的代码正在使用始终为“.”的 argv[1] 调用 stat - 因此所有项目都被错误地识别为目录。

dent->d_name(文件名)是这里的正确参数。

  if (stat(dent->d_name, &info) == -1) {

关于c - 使用 Stat 和 OpenDir()/ReadDir() 在 C 语言中列出当前目录和文件类型(Dir、Reg、Lnk),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23969645/

相关文章:

linux - 启用 VNC 的 Amazon EC2 镜像?

linux - do_package : split_and_strip_files (arm-poky-linux-gnueabi-objcopy) fails 时出现问题

linux - 传递给 STAT 系统调用的结构的精确定义是什么?

c - C语言中使用stat()获取文件的长度

c - C 中的文件处理

c - 交换字符指针

python - PySCIPOpt 经常被段错误 (SIGSEGV) 中断

c - 声明 C 数组

linux - 如何连接到在端口中运行的 UDP 守护进程

c - stat 函数返回空结构