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

C 扫描目录以查找软链接指向的文件,c,directory,operating-system,posix,symlink,C,Directory,Operating System,Posix,Symlink,我试图为我的操作系统类做这个练习:我应该通过命令行传递一个特定的目录,以查找其中由软链接指向的任何文件 这就是我所做的: #include <stdio.h> #include <stdlib.h> #include <string.h> #include <unistd.h> #include <fcntl.h> #include <errno.h> #include <sys/types.h> #includ

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

这就是我所做的:

#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);

}
当我通过命令行(./a.out my_directory)传递“my_directory”时,我希望程序写入标准输出“justatext.txt”和“justanothertext.txt”,因为目录中的这些文件由软链接指向。
如果我将此目录传递给我的程序,则不会打印任何输出。

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

#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);

}
#包括
#包括
#包括
#包括
#包括
#包括
#包括
#包括
#包括
#包括
int main(int argc,char*argv[])
{
DIR*DIR_ptr;
结构方向*dir_str;
结构统计buf;
结构统计buf2;
char*buf3;
小男孩,小男孩;
int标志=0;
如果(argc!=2)
{
printf(“错误!我需要一个目录。\n”);
退出(退出失败);
}
if((dir_ptr=opendir(argv[1]))==NULL)
{
printf(“Opendir错误:%s\n”,strerror(errno));
退出(退出失败);
}
chdir(argv[1]);
while((dir_str=readdir(dir_ptr))!=NULL)
{
lstat(dir_str->d_name和buf);
bufsize=buf.标准尺寸+1;
如果(基本单位大小==0)
bufsize=路径_最大值;
buf3=malloc(bufsize);
如果(buf3==NULL)
{
佩罗尔(“马洛克”);
退出(退出失败);
}
nbyte=readlink(dir\u str->d\u name,buf3,bufsize);
if(S_ISLNK(buf.st_模式))
{
统计(dir_str->d_name和buf2);
printf(“%s是由符号链接(%s)指向的%ld字节的文件。\n”,buf3,buf2.st\u size,dir\u str->d\u name);
flag+=1;
}
}
如果(标志==0)
printf(“在此目录中未找到系统链接指向的文件!\n”);
免费(buf3);
closedir(dir_ptr);
出口(0);
}
首先,我使用了
chdir()
,以便使用通过命令行传递的目录更改当前目录;然后我实现了
readlink()
,以获取作为其参数命名的符号链接中的路径名。我还使用了
exit(exit\u FAILURE)
在出现错误时关闭程序


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

请向我们展示一些“输入”(参数)示例以及预期和实际输出。我还建议您。使用readlink()获取存储在符号链接中的路径名,并将其命名为其参数(请注意,它不会以null结尾字符串)。是否显示软链接的目标文件?即使链接位于另一个目录中?@Gerhardh是的,我知道。我想显示软链接指向的所有文件,最好是在同一目录中,但这不是必需的。您希望
exit(-1)
做什么?在Linux中,退出代码只能是0到127。通常的做法是使用
退出(1)
#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);

}