首页 文章

我是否需要为Dirent结构分配内存

提问于
浏览
0
  • 平台:Windows XP Service Pack 3

  • 编译器:Code :: Blocks版本12.11

我目前正在编写一个程序,它将使用POSIX目录函数递归删除给定目录 . 但是我遇到了readdir()及其对应的dirent结构的问题 . 我在readdir的documentation中读到,对函数的多次调用将覆盖函数返回的结构中保存的数据 . 所以我认为readdir()必须为结构本身创建memmory,然后简单地将指针地址重新分配给捕获它的结构's return value. I tested this theory and I was correct readdir() allocated memmory for it'成员d_name . 我遇到的问题是当目录流为空时readdir返回一个NULL指针,所以我使用带有条件(dirent_ptr!= NULL)的while循环来迭代整个目录 . 但是因为readdir()将处理结构的memmory分配,所以我只需声明一个dirent结构,让readdir()完成它的工作 . 由于某种原因unforrtatnly dirent结构初始化为NULL(或者它可能是我的编译器)所以我的循环永远不会启动,因为它的条件语句不是initialy true . 所以我想我的问题是我在这里做错了什么?

这是重要的变量声明和包含的库 . 请注意,所有这些变量都是全局声明的 .

#include <stdio.h>
#include <stdlib.h>
#include <dirent.h>
#include <sys/stat.h>
#include <string.h>

int recursive_delete(const char *path);
int file_delete(const char *path, int file_size);

struct dirent *direntp;
struct stat *statp;

struct switches
{
    int verbose;
    int no_prompt;
    int continue_if_error;
    int files_only;
}; struct switches switches;

我不是解析相对路径而是简单地cd到作为参数给出的路径,然后我使用 . 和..移动的通配符抛出directorys,使相对路径(d_names)有效 . 此外,交换机结构只包含命令行开关,应该被忽略,我知道以下代码中的错误,但我无法解决它们,因为我无法解决上述问题 .

int recursive_delete(const char *path)
{
    DIR *dirp;
    int return_value = 0;
    int recursive_return_value = 0;

    if((chdir(path)) == -1)
    {
        perror("ERROR(3)");
        return 1;
    }
    printf("CDED to \"%s\"\n", path);

    dirp = opendir(".");
    if(dirp == NULL)
    {
        perror("ERROR(4)");
        return 1;
    }
    printf("OPENED \"%s\"\n", path);

    while(direntp != NULL)
    {
        direntp = readdir(dirp);
        if( (direntp == NULL) && (errno != 0) )
        {
            perror("ERROR(5)");
            return 1;
        }
        printf("READ \"%s\" FROM \"%s\"\n", direntp->d_name, path);

        if( (strcmp(direntp->d_name, ".")!=0) && (strcmp(direntp->d_name, "..")!=0) )
        {
            if((stat(direntp->d_name, statp)) == -1)
            {
                perror("ERROR(6)");
                return 1;
            }
            printf("STATED \"%s\"\n", direntp->d_name);

            if(S_ISREG(statp->st_mode))
            {
                printf("DELETING \"...\\%s\\%s\"\n", path, direntp->d_name);
                return_value += file_delete(direntp->d_name, statp->st_size);
                if( (!switches.continue_if_error) && (return_value != 0) )
                {
                    break;
                }
            }
            else if(S_ISDIR(statp->st_mode))
            {
                printf("\n\n\nCALLING RECURSIVE DELETE with \"%s\"\n", direntp->d_name);
                recursive_return_value = recursive_delete(direntp->d_name);
                return_value += recursive_return_value;

                if( (!switches.continue_if_error) && (recursive_return_value != 0) )
                {
                    break;
                }

                if( (!switches.files_only) && (recursive_return_value == 0) )
                {
                    if((chdir("..")) == -1)
                    {
                        perror("ERROR(6)");
                        return 1;
                    }
                    printf("CDED BACK TO \"%s\" FROM \"%s\"\n", path, direntp->d_name);

                    if((rmdir(direntp->d_name)) == -1)
                    {
                        perror("ERROR(7)");
                        return 1;
                    }

                    if(switches.verbose)
                    {
                        printf("DELETED DIRECTORY \"...\\%s\\\"\n\n\n", direntp->d_name);
                    }
                }
            }
        }
    }

    return return_value;
}

2 回答

  • 1

    从附加的代码中,不清楚direntp的初始化位置(在 while 循环之前) . 可能尝试类似的东西:

    direntp = readdir(dirp);
    while(direntp != NULL)
    {
        // all your work here
    
        direntp = readdir(dirp);
    }
    

    此模式可确保为while循环初始化和更新 direntp . 但是,在你的代码的第二眼看来,我并不完全确定while循环应该首先做什么 . 你的循环中 direntpdirp 如何变化?

    您可以通过 if 测试(而不是 while )逃脱,只需让递归调用处理"looping"效果......

  • 1

    您的代码结构应该类似于此(为清晰起见,省略了大多数错误检查):

    int recursive_delete(const char *path)
    {
      DIR* dirp = NULL;
      int return_value = 0;
      char* initial_cur_dir = malloc(1000);
    
      getcwd(initial_cur_dir, 1000);
      chdir(path);
      dirp = opendir(".");
    
      while (dirp != NULL)
      {
        struct dirent* direntp;
        struct stat stat;
    
        direntp = readdir(dirp);
    
        if (direntp == NULL)
          break;
    
        stat(direntp->d_name, &stat);
    
        if (S_ISDIR(statp->st_mode))
        {
          if (strcmp(direntp->d_name, ".") && strcmp(direntp->d_name, ".."))
          {
            return_value += recursive_delete(direntp->d_name);
          }
        }
        else if (S_ISREG(statp->st_mode))
        {
          unlink(direntp->d_name);
        }
      }
    
      if (initial_cur_dir != NULL)
      {
        chdir(initial_cur_dir);
        rmdir(path);
      }
    
    ErrorLabel: // you should goto here when an error is detected
    
      if (dirp != NULL)
      {
        closedir(dirp);
      }
    
      if (initial_cur_dir != NULL)
      {
        chdir(initial_cur_dir);
        free(initial_cur_dir);
      }
    
      return return_value;
    }
    

相关问题