2013-01-15 37 views
0
  • 编译:代码::块(GNU GCC)
  • 平台:视窗(86)

更新:我已经使用CHDIR解决了这个问题()改变在我调用opendir()之前的当前工作目录。所以我假设opendir()只能打开当前工作目录下的一个目录。所以我的新问题是,我是对的吗?不正确的价值观ST_MODE - Visual C

我目前正在编写一个窗口的dir命令的基本模仿。我的程序正常工作时,“。”通配符用作opendir()的参数。但是,当我不使用通配符,而是指定一个目录。我的程序不会打开指定给它的目录。例如,如果我键入c:\ windows,它将打开c:\,而每个文件的st_mode将相同。至少我假设它们都是一样的,因为所有的文件类型(DIR,FILE,OTHER)都是相同的。

#include <stdio.h> 
#include <string.h> 

#include <sys/stat.h> 
#include <dirent.h> 

int main(int argc, char* argv[]) 
{ 
//'directory' points to the directory | 'directory_contents' is used with readdir() to read the directory's('directory') contents. 
DIR *directory; 
struct dirent *directory_contents; 
struct stat file_info; 

//IF no argument is present display the contents of the current directory | IF there is an arugment display the contents of that argument | ELSE Too many arguments 
if (argc == 1) 
{ 
    directory = opendir("."); 
} 
else if (argc == 2) 
{ 
    //New Code 
    chdir(argv[1]); directory = opendir("."); 

    //Old Code 
    directory = opendir(argv[1]); 
} 
else 
{ 
    printf("ERROR: Extra arguments\n"); 
} 

//Checks to see if the directory opened above was actually opened. 
if (directory == NULL) 
{ 
    printf("ERROR: Failed to open '%s'.\n", argv[1]); 
    return 2; 
} 
else 
{ 
    //WHILE there are file names to be read THEN read the file names 
    while (directory_contents = readdir(directory)) 
    { 
     stat(directory_contents->d_name, &file_info); 

     //Test for directory 
     if(S_ISDIR(file_info.st_mode)) 
     { 
      //File type 
      printf("<DIR> "); 

      //File name 
      if(strlen(directory_contents->d_name) <= 15) 
      { 
       printf("%-15s", directory_contents->d_name); 
      } 
      else if(strlen(directory_contents->d_name) > 15) 
      { 
       printf("%.12s.. ", directory_contents->d_name); 
      } 

      //File premissions 
      printf("<%c%c%c>\n", ((file_info.st_mode & S_IRUSR)==0) ? '-' : 'r', ((file_info.st_mode & S_IWUSR)==0) ? '-' : 'w', ((file_info.st_mode & S_IXUSR)==0) ? '-' : 'x'); 
     } 
     //Test for a regular file. 
     else if(S_ISREG(file_info.st_mode)) 
     { 
      //File type 
      printf("<FILE> "); 

      //File name 
      if(strlen(directory_contents->d_name) <= 15) 
      { 
       printf("%-15s", directory_contents->d_name); 
      } 
      else if(strlen(directory_contents->d_name) > 15) 
      { 
       printf("%.12s.. ", directory_contents->d_name); 
      } 

      //File premissions 
      printf("<%c%c%c> ", ((file_info.st_mode & S_IRUSR)==0) ? '-' : 'r', ((file_info.st_mode & S_IWUSR)==0) ? '-' : 'w', ((file_info.st_mode & S_IXUSR)==0) ? '-' : 'x'); 

      //File size 
      if (file_info.st_size < 1000) 
      { 
       printf("<%-3i B>\n", file_info.st_size); 
      } 
      else if ((file_info.st_size > 1000) && (file_info.st_size < 1000000)) 
      { 
       printf("<%-3i KB>\n", file_info.st_size/1000); 
      } 
      else if ((file_info.st_size > 1000000) && (file_info.st_size < 1000000000)) 
      { 
       printf("<%-3i MB>\n", file_info.st_size/1000000); 
      } 
      else 
      { 
       printf("<%-3i GB>\n", file_info.st_size/1000000000); 
      } 
     } 
     //Symbolic Link etc. 
     else 
     { 
      //File type 
      printf("<OTHER> "); 

      //File name 
      if(strlen(directory_contents->d_name) <= 15) 
      { 
       printf("%-15s", directory_contents->d_name); 
      } 
      else if(strlen(directory_contents->d_name) > 15) 
      { 
       printf("%.12s.. ", directory_contents->d_name); 
      } 

      //File premissions 
      printf("<%c%c%c>\n", ((file_info.st_mode & S_IRUSR)==0) ? '-' : 'r', ((file_info.st_mode & S_IWUSR)==0) ? '-' : 'w', ((file_info.st_mode & S_IXUSR)==0) ? '-' : 'x'); 
     } 
    } 
} 
} 

是的,我知道我输出的权限完全不相关,因为Window使用ACL。我只是在windows上编写这个程序,因为我现在没有选择,但它是针对Linux操作系统的。

+0

我不确定它是否仍然相关,但是我有一个头文件'sysstat.h',用于处理Windows上的'stat()'。基本上,当我写它的时候(c 1995),即使有些Unix系统不支持POSIX,情况也很糟糕。自那时以来,情况普遍有所改善,特别是在Unix系统上。我现在不太确定Windows。它可能取决于您使用的Code :: Blocks的编译器。 (我现在通常不会使用它,但是我的一些旧代码仍然存在,以防万一 - 更多'仅仅因为',意思是'仅仅因为我还没有修复它'。) –

+1

尝试使用转义字符像c:\\ windwos –

+0

正如你在windows上工作并面对Windows上的问题,你应该标记这个问题'windows',而不是'POSIX'。如果在POSIX系统上标记'POSIX'。 – alk

回答

1

stat(directory_contents->d_name,

这条线是问题所在。 d_name字段只是没有任何目录的文件的名称。因此,除非该目录恰好是当前目录,否则调用stat()将找不到该文件。

+0

感谢您的回复。这就解释了为什么我的程序工作时,我chdir()到它正在阅读的目录。 –

相关问题