代码之家  ›  专栏  ›  技术社区  ›  Alopex

扫描目录以查找软链接指向的文件

  •  0
  • Alopex  · 技术社区  · 7 年前

    我正在为我的操作系统类做这个练习:我应该通过命令行传递一个特定的目录,以查找其中由软链接指向的任何文件。

    这就是我所做的:

    #include <stdio.h>
    #include <stdlib.h>
    #include <string.h>
    #include <unistd.h>
    #include <fcntl.h>
    #include <errno.h>
    #include <sys/types.h>
    #include <sys/stat.h>
    #include <dirent.h>
    
    int main(int argc, char *argv[])
    {
    
     DIR *dir_ptr;
     struct dirent *dir_str;
     struct stat buf;
     struct stat buf2;
    
     if(argc!=2)
     {
      printf("Error! I need a directory.\n");
      exit(-1);
     }
    
    
     if((dir_ptr=opendir(argv[1]))==NULL)       
     {
      printf("Opendir error: %s\n", strerror(errno));
      exit(-1);
     }
    
     while((dir_str=readdir(dir_ptr))!=NULL)
     {
      lstat(dir_str->d_name, &buf);     
      if(S_ISLNK(buf.st_mode))
      {
       stat(dir_str->d_name, &buf2);
       printf("'%s' points to a file of %ld bytes.\n", dir_str->d_name, buf2.st_size);
      }
    
     }
    
     closedir(dir_ptr);
     exit(0);
    
    }
    

    现在我的问题是:这个程序只将指向某个文件大小的所有软链接写入标准输出。相反,我需要它打印一个软链接指向的所有文件。第二,奇怪的是,这个程序似乎只在不需要目录的情况下工作,我的意思是,用 getcwd() 并将返回的路径名传递给 opendir() .这一个错误甚至不打印传递目录中的所有软链接。

    事先谢谢!任何帮助都将不胜感激。

    编辑:假设我们有一个名为“我的目录”的目录,其中包含以下文件:

    justatext.txt
    softlink1 (it points to justatext.txt)
    justanothertext.txt
    softlink2(it points to justanothertext)
    

    1 回复  |  直到 7 年前
        1
  •  0
  •   Alopex    7 年前

    好吧,我想我已经解决了我的问题。总结一下评论中的所有建议,这就是我所做的,而且似乎是有效的:

    #include <stdio.h>
    #include <stdlib.h>
    #include <string.h>
    #include <unistd.h>
    #include <fcntl.h>
    #include <errno.h>
    #include <limits.h>     
    #include <sys/types.h>
    #include <sys/stat.h>
    #include <dirent.h>
    
    int main(int argc, char *argv[])
    {
    
     DIR *dir_ptr;
     struct dirent *dir_str;
     struct stat buf;
     struct stat buf2;
     char *buf3;            
     ssize_t nbyte, bufsize;
     int flag=0;
    
     if(argc!=2)
     {
      printf("Error! I need a directory.\n");
      exit(EXIT_FAILURE);
     }
    
     if((dir_ptr=opendir(argv[1]))==NULL)
     {
      printf("Opendir error: %s\n", strerror(errno));
      exit(EXIT_FAILURE);
     }
    
     chdir(argv[1]);    
    
     while((dir_str=readdir(dir_ptr))!=NULL)    
     {
      lstat(dir_str->d_name, &buf);     
      bufsize=buf.st_size+1;    
      if(buf.st_size==0)        
       bufsize=PATH_MAX;        
    
      buf3=malloc(bufsize); 
      if(buf3==NULL)
      {
       perror("malloc");
       exit(EXIT_FAILURE);
      }
      nbyte=readlink(dir_str->d_name, buf3, bufsize);   
    
      if(S_ISLNK(buf.st_mode))  
      {
       stat(dir_str->d_name, &buf2);
       printf("%s is a file of %ld bytes pointed by a symbolic link (%s).\n", buf3, buf2.st_size, dir_str->d_name);
       flag+=1;
      }
    
     }
    
     if(flag==0)
      printf("No files pointed by a syslink found in this directory!\n");
    
     free(buf3);
     closedir(dir_ptr);
     exit(0);
    
    }
    

    首先,我用 chdir() 为了通过命令行更改当前目录,然后我实现了 readlink() 获取名为其参数的符号链接中的路径名。我也用过 exit(EXIT_FAILURE) 如果发生错误,关闭程序。

    我不知道这是否完全正确。如果不是,请告诉我!