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

时间:2022-09-01 22:47:52

Hello and thanks for reading.

您好,感谢您的阅读。

I am trying to build a program in C which is run in the Linux Shell, that accepts an argument (directory) and lists all the files in that directory, as well as whether they are a regular file, link file or directory. I need to use OpenDir()/ReadDir() and stat. Here is my code so far: (I'm a bit stuck) at the moment it only reads a file and outputs the type. How do I allow my program to read ALL files in a directory, and output their name AND what type of file they are?

我正在尝试在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);
   }

So I rewrote everything and now I am receiving some output on whether it's a link reg or dir file, however I am only ever receiving that a file is a Directory, even when it is clearly not (a text file or a .c file etc). here is the new code:

所以我重写了所有内容,现在我收到一些关于它是否是链接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;    
}

1 个解决方案

#1


1  

Your code is calling stat with argv[1] which always '.' - hence all items are identified incorrectly as directories.

你的代码用argv [1]调用stat,它总是'。' - 因此所有项目都被错误地识别为目录。

dent->d_name (the file name) is the correct parameter here.

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

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

#1


1  

Your code is calling stat with argv[1] which always '.' - hence all items are identified incorrectly as directories.

你的代码用argv [1]调用stat,它总是'。' - 因此所有项目都被错误地识别为目录。

dent->d_name (the file name) is the correct parameter here.

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

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