這是還沒寫好的源碼:
#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; }
}
closedir( dirp );
}
return EXIT_SUCCESS;
}