0
  • 编译器:代码::块(GNU GCC)
  • 平台:Windows(x86)

更新:我通过在调用 opendir() 之前使用 chdir() 更改当前工作目录解决了这个问题。所以我假设 opendir() 只能打开当前工作目录中的目录。所以我的新问题是,我正确吗?

我目前正在编写对window 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 操作系统设计的。

4

1 回答 1

1

stat(directory_contents->d_name,

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

于 2013-01-17T17:45:56.700 回答