打印本文 打印本文  关闭窗口 关闭窗口  
怎么删除一个非空的目录?
作者:佚名  文章来源:不详  点击数  更新时间:2008/4/18 13:56:45  文章录入:杜斌  责任编辑:杜斌

  这是还没写好的源码:

  #include <stdio.h>
  #include <stdlib.h>
  #include <dirent.h>
  #include <unistd.h>
  #include <sys/types.h>
  int main( void )
  {
      DIR* dirp;
      struct dirent* direntp;
      struct stat    buf;
      pid_t          pid;
      dirp = opendir( "/share/tmp" );/*打开一个目录 */
      if( dirp == NULL ) {
               perror( "can't open /share/tmp" );
      } else {
          for( ; ; ) {
                direntp = readdir( dirp ); /*读目录里的内容*/
                if( direntp == NULL ) /*如果是个空目录就把它删除*/
                      rmdir(/share/tmp);
              for( ; ; ) {
                      if(lstat(directp->s_name,&buf)<0) { /*读出文件名*/
                             printf("lstat error\n");
                             exit(-1);
                      }else{
                             if( ! S_ISDIR(buf.st_mode)) < 0) { /*判断是否是一个目录*/
                                     printf("check file mode error\n");
                                     exit(1);
                             }else if
                                     unlink(directp->s_name) ; /*如果不是就删除它*/
                             else
                                     if((pid=fork())<0) {/*如果是就判断是否是一个空目录,是就删除它,这里考虑得不周到,fork一个进程,运行上面的算法,就是这里不懂了。要写个函数吧?*/
                                               printf("fork error\n");
                                               exit(1);
                                     else if(pie == 0 )
                                               chdir(directp->s_name);/*新的进程cd到新的目录*/
          }
          closedir( dirp );
      }
      return EXIT_SUCCESS;
  }

打印本文 打印本文  关闭窗口 关闭窗口