LinuxSir.cn,穿越时空的Linuxsir!

 找回密码
 注册
搜索
热搜: shell linux mysql
查看: 756|回复: 1

怎么删除一个非空的目录?

[复制链接]
发表于 2004-2-22 20:28:20 | 显示全部楼层 |阅读模式
这是还没写好的源码:

  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <dirent.h>
  4. #include <unistd.h>
  5. #include <sys/types.h>
  6. int main( void )
  7. {
  8.     DIR* dirp;
  9.     struct dirent* direntp;
  10.     struct stat    buf;
  11.     pid_t          pid;
  12.     dirp = opendir( "/share/tmp" );/*打开一个目录 */
  13.     if( dirp == NULL ) {
  14.              perror( "can't open /share/tmp" );
  15.     } else {
  16.         for( ; ; ) {
  17.               direntp = readdir( dirp ); /*读目录里的内容*/
  18.               if( direntp == NULL ) /*如果是个空目录就把它删除*/
  19.                       rmdir(/share/tmp);
  20.               for( ; ; ) {
  21.                       if(lstat(directp->s_name,&buf)<0) { /*读出文件名*/
  22.                              printf("lstat error\n");
  23.                              exit(-1);
  24.                       }else{
  25.                              if( ! S_ISDIR(buf.st_mode)) < 0) { /*判断是否是一个目录*/
  26.                                      printf("check file mode error\n");
  27.                                      exit(1);
  28.                              }else if
  29.                                      unlink(directp->s_name) ; /*如果不是就删除它*/
  30.                              else
  31.                                      if((pid=fork())<0) {/*如果是就判断是否是一个空目录,是就删除它,这里考虑得不周到,fork一个进程,运行上面的算法,就是这里不懂了。要写个函数吧?*/
  32.                                                printf("fork error\n");
  33.                                                exit(1);
  34.                                      else if(pie == 0 )
  35.                                                chdir(directp->s_name);/*新的进程cd到新的目录*/

  36.                            


  37.         }
  38.         closedir( dirp );
  39.     }
  40.     return EXIT_SUCCESS;
  41. }
复制代码
发表于 2004-2-22 21:57:32 | 显示全部楼层
你没有考虑'.'和'..'
您需要登录后才可以回帖 登录 | 注册

本版积分规则

快速回复 返回顶部 返回列表